Skip to content

Commit 8a23b56

Browse files
authored
fix(groups): Add group namespace field (#22)
We'll be able to tell the controller where the group resource is, it could be in a separate namespace This was based on an implementation from the cassandra operator seen here: https://github.com/k8ssandra/cass-operator/blob/master/controllers/control/cassandratask_controller.go#L135-L138 This fixes #21.
1 parent d8e343f commit 8a23b56

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

apis/checkly/v1alpha1/apicheck_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type ApiCheckSpec struct {
4545

4646
// Group determines in which group does the check belong to
4747
Group string `json:"group"`
48+
49+
// GroupNamespace determine in which namespace was the group defined
50+
GroupNamespace string `json:"groupnamespace,omitempty"`
4851
}
4952

5053
// ApiCheckStatus defines the observed state of ApiCheck

config/crd/bases/k8s.checklyhq.com_apichecks.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ spec:
6464
description: Group determines in which group does the check belong
6565
to
6666
type: string
67+
groupnamespace:
68+
description: GroupNamespace determine in which namespace was the group
69+
defined
70+
type: string
6771
maxresponsetime:
6872
description: MaxResponseTime determines what the maximum number of
6973
miliseconds can pass before the check fails, default 15000

config/samples/checkly_v1alpha1_apicheck.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ spec:
1010
frequency: 10 # Default 5
1111
muted: true # Default "false"
1212
group: "group-sample"
13+
groupnamespace: "default" # If not specified, the controller assumes the group is in the same namespace

controllers/checkly/apicheck_controller.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
117117
// Lookup group ID
118118
// ////////////////////////////
119119
group := &checklyv1alpha1.Group{}
120-
err = r.Get(ctx, types.NamespacedName{Name: apiCheck.Spec.Group, Namespace: apiCheck.Namespace}, group)
120+
var groupNamespace string
121+
if apiCheck.Spec.GroupNamespace == "" {
122+
groupNamespace = apiCheck.Namespace
123+
} else {
124+
groupNamespace = apiCheck.Spec.GroupNamespace
125+
}
126+
err = r.Get(ctx, types.NamespacedName{Name: apiCheck.Spec.Group, Namespace: groupNamespace}, group)
121127
if err != nil {
122128
if errors.IsNotFound(err) {
123129
// The resource has been deleted

controllers/checkly/apicheck_controller_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,35 @@ var _ = Describe("ApiCheck Controller", func() {
126126
}
127127
}, timeout, interval).Should(BeTrue())
128128

129+
// Update
130+
groupUpdateNS := "kube-system"
131+
groupUpdate := &checklyv1alpha1.Group{
132+
ObjectMeta: metav1.ObjectMeta{
133+
Name: groupKey.Name,
134+
Namespace: groupUpdateNS,
135+
},
136+
}
137+
Expect(k8sClient.Create(context.Background(), groupUpdate)).Should(Succeed())
138+
139+
apiCheckRaw := &checklyv1alpha1.ApiCheck{}
140+
Expect(k8sClient.Get(context.Background(), key, apiCheckRaw)).Should(Succeed())
141+
apiCheckRaw.Spec.GroupNamespace = groupUpdateNS
142+
Expect(k8sClient.Update(context.Background(), apiCheckRaw)).Should(Succeed())
143+
144+
By("Expecting groupnamespace to change")
145+
Eventually(func() bool {
146+
f := &checklyv1alpha1.ApiCheck{}
147+
err := k8sClient.Get(context.Background(), key, f)
148+
if err == nil && f.Spec.GroupNamespace == groupUpdateNS {
149+
return true
150+
}
151+
return false
152+
153+
}, timeout, interval).Should(BeTrue())
154+
129155
// Delete
130156
Expect(k8sClient.Delete(context.Background(), group)).Should(Succeed())
157+
Expect(k8sClient.Delete(context.Background(), groupUpdate)).Should(Succeed())
131158

132159
By("Expecting to delete successfully")
133160
Eventually(func() error {

0 commit comments

Comments
 (0)