Skip to content

Commit b308dea

Browse files
authored
feat(domain): add option to set controller domain (#52)
* Add new command line flag for a controller domain * Update docs
1 parent 5366d89 commit b308dea

13 files changed

+102
-58
lines changed

cmd/main.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ func main() {
5757
var metricsAddr string
5858
var enableLeaderElection bool
5959
var probeAddr string
60+
var controllerDomain string
6061
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6162
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6263
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6364
"Enable leader election for controller manager. "+
6465
"Enabling this will ensure there is only one active controller manager.")
66+
flag.StringVar(&controllerDomain, "controller-domain", "k8s.checklyhq.com", "Domain to use for annotations and finalizers.")
6567
opts := zap.Options{
6668
Development: true,
6769
}
@@ -70,6 +72,8 @@ func main() {
7072

7173
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
7274

75+
setupLog.Info("Controller domain setup", "value", controllerDomain)
76+
7377
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7478
Scheme: scheme,
7579
Metrics: metricsserver.Options{
@@ -107,32 +111,36 @@ func main() {
107111
client.SetAccountId(accountId)
108112

109113
if err = (&networkingcontrollers.IngressReconciler{
110-
Client: mgr.GetClient(),
111-
Scheme: mgr.GetScheme(),
114+
Client: mgr.GetClient(),
115+
Scheme: mgr.GetScheme(),
116+
ControllerDomain: controllerDomain,
112117
}).SetupWithManager(mgr); err != nil {
113118
setupLog.Error(err, "unable to create controller", "controller", "Ingress")
114119
os.Exit(1)
115120
}
116121
if err = (&checklycontrollers.ApiCheckReconciler{
117-
Client: mgr.GetClient(),
118-
Scheme: mgr.GetScheme(),
119-
ApiClient: client,
122+
Client: mgr.GetClient(),
123+
Scheme: mgr.GetScheme(),
124+
ApiClient: client,
125+
ControllerDomain: controllerDomain,
120126
}).SetupWithManager(mgr); err != nil {
121127
setupLog.Error(err, "unable to create controller", "controller", "ApiCheck")
122128
os.Exit(1)
123129
}
124130
if err = (&checklycontrollers.GroupReconciler{
125-
Client: mgr.GetClient(),
126-
Scheme: mgr.GetScheme(),
127-
ApiClient: client,
131+
Client: mgr.GetClient(),
132+
Scheme: mgr.GetScheme(),
133+
ApiClient: client,
134+
ControllerDomain: controllerDomain,
128135
}).SetupWithManager(mgr); err != nil {
129136
setupLog.Error(err, "unable to create controller", "controller", "Group")
130137
os.Exit(1)
131138
}
132139
if err = (&checklycontrollers.AlertChannelReconciler{
133-
Client: mgr.GetClient(),
134-
Scheme: mgr.GetScheme(),
135-
ApiClient: client,
140+
Client: mgr.GetClient(),
141+
Scheme: mgr.GetScheme(),
142+
ApiClient: client,
143+
ControllerDomain: controllerDomain,
136144
}).SetupWithManager(mgr); err != nil {
137145
setupLog.Error(err, "unable to create controller", "controller", "AlertChannel")
138146
os.Exit(1)

config/manager/manager.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ spec:
3232
- /manager
3333
args:
3434
- --leader-elect
35+
- --controller-domain=k8s.checklyhq.com
3536
image: controller:latest
3637
name: manager
3738
env:

docs/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ If you just want to try out the checkly-operator, you need a local kubernetes in
3939
First we'll download the provided `install.yaml` files, please change the version number accordingly, we might have newer [releases](https://github.com/checkly/checkly-operator/releases) since we've written these docs.
4040

4141
```bash
42-
export CHECKLY_OPERATOR_RELEASE=v1.4.1
42+
export CHECKLY_OPERATOR_RELEASE=v1.7.0
4343
wget "https://github.com/checkly/checkly-operator/releases/download/$CHECKLY_OPERATOR_RELEASE/install-$CHECKLY_OPERATOR_RELEASE.yaml" -O install.yaml
4444
unset CHECKLY_OPERATOR_RELEASE
4545
```
@@ -53,6 +53,12 @@ You can apply the `install.yaml`, this will create the namespace, we need this t
5353
kubectl apply -f install.yaml
5454
```
5555

56+
#### Controller Domain
57+
58+
We're using a domain name for finalizers and annotations, the default value is `k8s.checklyhq.com`, but it can be changed by supplying the `--controller-domain=other.domain.tld` runtime option.
59+
60+
This option allows you to run multiple independent deployments of the operator and each would handle different resources based on the controller domain configuration.
61+
5662
### Create secret
5763

5864
Grab your [checklyhq.com](checklyhq.com) API key and Account ID, [the official docs](https://www.checklyhq.com/docs/integrations/pulumi/#define-your-checkly-account-id-and-api-key) can help you get this information. Substitute the values into the below command:

internal/controller/checkly/alertchannel_controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package checkly
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
corev1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/api/errors"
@@ -36,8 +37,9 @@ import (
3637
// AlertChannelReconciler reconciles a AlertChannel object
3738
type AlertChannelReconciler struct {
3839
client.Client
39-
Scheme *runtime.Scheme
40-
ApiClient checkly.Client
40+
Scheme *runtime.Scheme
41+
ApiClient checkly.Client
42+
ControllerDomain string
4143
}
4244

4345
//+kubebuilder:rbac:groups=k8s.checklyhq.com,resources=alertchannels,verbs=get;list;watch;create;update;patch;delete
@@ -55,7 +57,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
5557

5658
logger.Info("Reconciler started")
5759

58-
acFinalizer := "k8s.checklyhq.com/finalizer"
60+
acFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain)
5961

6062
ac := &checklyv1alpha1.AlertChannel{}
6163

internal/controller/checkly/alertchannel_controller_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,15 @@ var _ = Describe("ApiCheck Controller", func() {
115115
Eventually(func() bool {
116116
f := &checklyv1alpha1.AlertChannel{}
117117
err := k8sClient.Get(context.Background(), acKey, f)
118-
if len(f.Finalizers) == 1 && err == nil {
119-
return true
120-
} else {
118+
if err != nil {
121119
return false
122120
}
121+
122+
for _, finalizer := range f.Finalizers {
123+
Expect(finalizer).To(Equal("testing.domain.tld/finalizer"), "Finalizer should match")
124+
}
125+
126+
return true
123127
}, timeout, interval).Should(BeTrue())
124128

125129
// Update

internal/controller/checkly/apicheck_controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package checkly
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"k8s.io/apimachinery/pkg/api/errors"
2324
"k8s.io/apimachinery/pkg/runtime"
@@ -35,8 +36,9 @@ import (
3536
// ApiCheckReconciler reconciles a ApiCheck object
3637
type ApiCheckReconciler struct {
3738
client.Client
38-
Scheme *runtime.Scheme
39-
ApiClient checkly.Client
39+
Scheme *runtime.Scheme
40+
ApiClient checkly.Client
41+
ControllerDomain string
4042
}
4143

4244
//+kubebuilder:rbac:groups=k8s.checklyhq.com,resources=apichecks,verbs=get;list;watch;create;update;patch;delete
@@ -56,7 +58,7 @@ type ApiCheckReconciler struct {
5658
func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5759
logger := log.FromContext(ctx)
5860

59-
apiCheckFinalizer := "k8s.checklyhq.com/finalizer"
61+
apiCheckFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain)
6062

6163
apiCheck := &checklyv1alpha1.ApiCheck{}
6264

internal/controller/checkly/apicheck_controller_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,15 @@ var _ = Describe("ApiCheck Controller", func() {
117117
Eventually(func() bool {
118118
f := &checklyv1alpha1.ApiCheck{}
119119
err := k8sClient.Get(context.Background(), key, f)
120-
if len(f.Finalizers) == 1 && err == nil {
121-
return true
122-
} else {
120+
if err != nil {
123121
return false
124122
}
123+
124+
for _, finalizer := range f.Finalizers {
125+
Expect(finalizer).To(Equal("testing.domain.tld/finalizer"), "Finalizer should match")
126+
}
127+
128+
return true
125129
}, timeout, interval).Should(BeTrue())
126130

127131
// Delete

internal/controller/checkly/group_controller.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package checkly
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"k8s.io/apimachinery/pkg/api/errors"
2324
"k8s.io/apimachinery/pkg/runtime"
@@ -35,8 +36,9 @@ import (
3536
// GroupReconciler reconciles a Group object
3637
type GroupReconciler struct {
3738
client.Client
38-
Scheme *runtime.Scheme
39-
ApiClient checkly.Client
39+
Scheme *runtime.Scheme
40+
ApiClient checkly.Client
41+
ControllerDomain string
4042
}
4143

4244
//+kubebuilder:rbac:groups=k8s.checklyhq.com,resources=groups,verbs=get;list;watch;create;update;patch;delete
@@ -53,7 +55,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
5355

5456
logger.Info("Reconciler started")
5557

56-
groupFinalizer := "k8s.checklyhq.com/finalizer"
58+
groupFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain)
5759

5860
group := &checklyv1alpha1.Group{}
5961

internal/controller/checkly/group_controller_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ var _ = Describe("ApiCheck Controller", func() {
111111
Eventually(func() bool {
112112
f := &checklyv1alpha1.Group{}
113113
err := k8sClient.Get(context.Background(), groupKey, f)
114-
if len(f.Finalizers) == 1 && err == nil {
115-
return true
116-
} else {
114+
if err != nil {
117115
return false
118116
}
117+
118+
for _, finalizer := range f.Finalizers {
119+
Expect(finalizer).To(Equal("testing.domain.tld/finalizer"), "Finalizer should match")
120+
}
121+
122+
return true
119123
}, timeout, interval).Should(BeTrue())
120124

121125
// Update

internal/controller/checkly/suite_test.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,29 @@ var _ = BeforeSuite(func() {
168168
http.ListenAndServe(":5555", nil)
169169
}()
170170

171+
testControllerDomain := "testing.domain.tld"
172+
171173
err = (&ApiCheckReconciler{
172-
Client: k8sManager.GetClient(),
173-
Scheme: k8sManager.GetScheme(),
174-
ApiClient: testClient,
174+
Client: k8sManager.GetClient(),
175+
Scheme: k8sManager.GetScheme(),
176+
ApiClient: testClient,
177+
ControllerDomain: testControllerDomain,
175178
}).SetupWithManager(k8sManager)
176179
Expect(err).ToNot(HaveOccurred())
177180

178181
err = (&GroupReconciler{
179-
Client: k8sManager.GetClient(),
180-
Scheme: k8sManager.GetScheme(),
181-
ApiClient: testClient,
182+
Client: k8sManager.GetClient(),
183+
Scheme: k8sManager.GetScheme(),
184+
ApiClient: testClient,
185+
ControllerDomain: testControllerDomain,
182186
}).SetupWithManager(k8sManager)
183187
Expect(err).ToNot(HaveOccurred())
184188

185189
err = (&AlertChannelReconciler{
186-
Client: k8sManager.GetClient(),
187-
Scheme: k8sManager.GetScheme(),
188-
ApiClient: testClient,
190+
Client: k8sManager.GetClient(),
191+
Scheme: k8sManager.GetScheme(),
192+
ApiClient: testClient,
193+
ControllerDomain: testControllerDomain,
189194
}).SetupWithManager(k8sManager)
190195
Expect(err).ToNot(HaveOccurred())
191196

internal/controller/networking/ingress_controller.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import (
3333
// IngressReconciler reconciles a Ingress object
3434
type IngressReconciler struct {
3535
client.Client
36-
Scheme *runtime.Scheme
36+
Scheme *runtime.Scheme
37+
ControllerDomain string
3738
}
3839

3940
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;update;patch
@@ -54,6 +55,8 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
5455
ingress := &networkingv1.Ingress{}
5556
apiCheck := &checklyv1alpha1.ApiCheck{}
5657

58+
annotationEnabled := fmt.Sprintf("%s/enabled", r.ControllerDomain)
59+
5760
// Check if ingress object is still present
5861
err := r.Get(ctx, req.NamespacedName, ingress)
5962
if err != nil {
@@ -67,7 +70,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
6770
logger.Info("Ingress Object found")
6871

6972
// Check if annotation is present on the object
70-
checklyAnnotation := ingress.Annotations["k8s.checklyhq.com/enabled"] == "true"
73+
checklyAnnotation := ingress.Annotations[annotationEnabled] == "true"
7174
if !checklyAnnotation {
7275
// Annotation may have been removed or updated, we have to determine if we need to delete a previously created ApiCheck resource
7376
logger.Info("annotation is not present, checking if ApiCheck was created")
@@ -138,7 +141,7 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
138141

139142
func (r *IngressReconciler) gatherApiCheckData(ingress *networkingv1.Ingress) (apiCheckSpec checklyv1alpha1.ApiCheckSpec, err error) {
140143

141-
annotationHost := "k8s.checklyhq.com"
144+
annotationHost := r.ControllerDomain
142145
annotationPath := fmt.Sprintf("%s/path", annotationHost)
143146
annotationEndpoint := fmt.Sprintf("%s/endpoint", annotationHost)
144147
annotationSuccess := fmt.Sprintf("%s/success", annotationHost)

internal/controller/networking/ingress_controller_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ var _ = Describe("Ingress Controller", func() {
4545
}
4646

4747
annotation := make(map[string]string)
48-
annotation["k8s.checklyhq.com/enabled"] = "true"
49-
annotation["k8s.checklyhq.com/path"] = testPath
50-
annotation["k8s.checklyhq.com/success"] = testSuccessCode
51-
annotation["k8s.checklyhq.com/group"] = testGroup
48+
annotation["testing.domain.tld/enabled"] = "true"
49+
annotation["testing.domain.tld/path"] = testPath
50+
annotation["testing.domain.tld/success"] = testSuccessCode
51+
annotation["testing.domain.tld/group"] = testGroup
5252

5353
rules := make([]networkingv1.IngressRule, 0)
5454
rules = append(rules, networkingv1.IngressRule{
@@ -116,10 +116,10 @@ var _ = Describe("Ingress Controller", func() {
116116
// Update
117117
updatePath := "baaz"
118118
updateHost := "foo.update"
119-
annotation["k8s.checklyhq.com/path"] = updatePath
120-
annotation["k8s.checklyhq.com/endpoint"] = updateHost
121-
annotation["k8s.checklyhq.com/success"] = ""
122-
annotation["k8s.checklyhq.com/muted"] = "false"
119+
annotation["testing.domain.tld/path"] = updatePath
120+
annotation["testing.domain.tld/endpoint"] = updateHost
121+
annotation["testing.domain.tld/success"] = ""
122+
annotation["testing.domain.tld/muted"] = "false"
123123
ingress = &networkingv1.Ingress{
124124
ObjectMeta: metav1.ObjectMeta{
125125
Name: key.Name,
@@ -164,7 +164,7 @@ var _ = Describe("Ingress Controller", func() {
164164
}, timeout, interval).Should(BeTrue())
165165

166166
// Remove enabled label
167-
annotation["k8s.checklyhq.com/enabled"] = "false"
167+
annotation["testing.domain.tld/enabled"] = "false"
168168
ingress = &networkingv1.Ingress{
169169
ObjectMeta: metav1.ObjectMeta{
170170
Name: key.Name,
@@ -221,10 +221,10 @@ var _ = Describe("Ingress Controller", func() {
221221
}
222222

223223
annotation := make(map[string]string)
224-
annotation["k8s.checklyhq.com/enabled"] = "false"
225-
annotation["k8s.checklyhq.com/path"] = testPath
226-
annotation["k8s.checklyhq.com/success"] = testSuccessCode
227-
annotation["k8s.checklyhq.com/muted"] = "false"
224+
annotation["testing.domain.tld/enabled"] = "false"
225+
annotation["testing.domain.tld/path"] = testPath
226+
annotation["testing.domain.tld/success"] = testSuccessCode
227+
annotation["testing.domain.tld/muted"] = "false"
228228

229229
rules := make([]networkingv1.IngressRule, 0)
230230
rules = append(rules, networkingv1.IngressRule{
@@ -255,7 +255,7 @@ var _ = Describe("Ingress Controller", func() {
255255

256256
updated := &networkingv1.Ingress{}
257257
Expect(k8sClient.Get(context.Background(), key, updated)).Should(Succeed())
258-
annotation["k8s.checklyhq.com/enabled"] = "true"
258+
annotation["testing.domain.tld/enabled"] = "true"
259259
updated.Annotations = annotation
260260
Expect(k8sClient.Update(context.Background(), updated)).Should(Succeed())
261261

internal/controller/networking/suite_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ var _ = BeforeSuite(func() {
8383
})
8484
Expect(err).ToNot(HaveOccurred())
8585

86+
testControllerDomain := "testing.domain.tld"
87+
8688
err = (&IngressReconciler{
87-
Client: k8sManager.GetClient(),
88-
Scheme: k8sManager.GetScheme(),
89+
Client: k8sManager.GetClient(),
90+
Scheme: k8sManager.GetScheme(),
91+
ControllerDomain: testControllerDomain,
8992
}).SetupWithManager(k8sManager)
9093
Expect(err).ToNot(HaveOccurred())
9194

0 commit comments

Comments
 (0)