Skip to content

Commit df958a6

Browse files
support deploying provision probe or mount probe only
Currently, when a user deploys a PieProbe object, both of provision and mount probes are deployed. However, under some circumstances, it is enough to deploy either provision probes or mount probes only. This commit supports deploying either provision probes or mount probes only, by adding disableProvisionProbes and disableMountProbes fields to PieProbe spec. Signed-off-by: Ryotaro Banno <[email protected]>
1 parent fdba009 commit df958a6

File tree

5 files changed

+187
-28
lines changed

5 files changed

+187
-28
lines changed

api/pie/v1alpha1/pieprobe_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ type PieProbeSpec struct {
3232
//+kubebuilder:validation:Optional
3333
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="pvcCapacity is immutable"
3434
PVCCapacity *resource.Quantity `json:"pvcCapacity"`
35+
36+
//+kubebuilder:default:=false
37+
//+kubebuilder:validation:Optional
38+
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="disableProvisionProbe is immutable"
39+
DisableProvisionProbes bool `json:"disableProvisionProbe"`
40+
41+
//+kubebuilder:default:=false
42+
//+kubebuilder:validation:Optional
43+
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="disableMountProbe is immutable"
44+
DisableMountProbes bool `json:"disableMountProbe"`
3545
}
3646

3747
// PieProbeStatus defines the observed state of PieProbe

charts/pie/templates/pie.topolvm.io_pieprobes.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ spec:
3939
spec:
4040
description: PieProbeSpec defines the desired state of PieProbe
4141
properties:
42+
disableMountProbe:
43+
default: false
44+
type: boolean
45+
x-kubernetes-validations:
46+
- message: disableMountProbe is immutable
47+
rule: self == oldSelf
48+
disableProvisionProbe:
49+
default: false
50+
type: boolean
51+
x-kubernetes-validations:
52+
- message: disableProvisionProbe is immutable
53+
rule: self == oldSelf
4254
monitoringStorageClass:
4355
type: string
4456
x-kubernetes-validations:

config/crd/bases/pie.topolvm.io_pieprobes.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ spec:
3939
spec:
4040
description: PieProbeSpec defines the desired state of PieProbe
4141
properties:
42+
disableMountProbe:
43+
default: false
44+
type: boolean
45+
x-kubernetes-validations:
46+
- message: disableMountProbe is immutable
47+
rule: self == oldSelf
48+
disableProvisionProbe:
49+
default: false
50+
type: boolean
51+
x-kubernetes-validations:
52+
- message: disableProvisionProbe is immutable
53+
rule: self == oldSelf
4254
monitoringStorageClass:
4355
type: string
4456
x-kubernetes-validations:

internal/controller/pie/pieprobe_controller.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,31 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8787
return ctrl.Result{}, nil
8888
}
8989

90-
// Create a provision-probe CronJob for each sc
91-
err = r.createOrUpdateJob(
92-
ctx,
93-
ProvisionProbe,
94-
&pieProbe,
95-
nil,
96-
)
97-
if err != nil {
98-
return ctrl.Result{}, err
90+
if !pieProbe.Spec.DisableProvisionProbes {
91+
if err := r.reconcileProvisionProbe(ctx, &pieProbe); err != nil {
92+
return ctrl.Result{}, err
93+
}
94+
}
95+
96+
if !pieProbe.Spec.DisableMountProbes {
97+
if err := r.reconcileMountProbes(ctx, &pieProbe); err != nil {
98+
return ctrl.Result{}, err
99+
}
99100
}
100101

102+
return ctrl.Result{}, nil
103+
}
104+
105+
func (r *PieProbeReconciler) reconcileProvisionProbe(ctx context.Context, pieProbe *piev1alpha1.PieProbe) error {
106+
// Create a provision-probe CronJob for each sc
107+
return r.createOrUpdateJob(ctx, ProvisionProbe, pieProbe, nil)
108+
}
109+
110+
func (r *PieProbeReconciler) reconcileMountProbes(ctx context.Context, pieProbe *piev1alpha1.PieProbe) error {
101111
// Get a node list and create a PVC and a mount-probe CronJob for each node and sc.
102112
nodeSelector, err := nodeaffinity.NewNodeSelector(&pieProbe.Spec.NodeSelector)
103113
if err != nil {
104-
return ctrl.Result{}, err
114+
return err
105115
}
106116
allNodeList := corev1.NodeList{}
107117
r.client.List(ctx, &allNodeList)
@@ -112,22 +122,13 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
112122
}
113123
availableNodeList = append(availableNodeList, node)
114124

115-
err = r.createOrUpdatePVC(
116-
ctx,
117-
node.Name,
118-
&pieProbe,
119-
)
125+
err = r.createOrUpdatePVC(ctx, node.Name, pieProbe)
120126
if err != nil {
121-
return ctrl.Result{}, err
127+
return err
122128
}
123-
err = r.createOrUpdateJob(
124-
ctx,
125-
MountProbe,
126-
&pieProbe,
127-
&node.Name,
128-
)
129+
err = r.createOrUpdateJob(ctx, MountProbe, pieProbe, &node.Name)
129130
if err != nil {
130-
return ctrl.Result{}, err
131+
return err
131132
}
132133
}
133134

@@ -144,7 +145,7 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
144145
}),
145146
})
146147
if err != nil {
147-
return ctrl.Result{}, err
148+
return err
148149
}
149150
for _, cronJob := range cronJobList.Items {
150151
nodeName := cronJob.GetLabels()[constants.ProbeNodeLabelKey]
@@ -156,7 +157,7 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
156157
}
157158
err := r.deleteCronJob(ctx, &cronJob)
158159
if client.IgnoreNotFound(err) != nil {
159-
return ctrl.Result{}, err
160+
return err
160161
}
161162
}
162163

@@ -168,7 +169,7 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
168169
}),
169170
})
170171
if err != nil {
171-
return ctrl.Result{}, err
172+
return err
172173
}
173174
for _, pvc := range pvcList.Items {
174175
nodeName := pvc.GetLabels()[constants.ProbeNodeLabelKey]
@@ -177,11 +178,11 @@ func (r *PieProbeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
177178
}
178179
err = r.deletePVC(ctx, &pvc)
179180
if client.IgnoreNotFound(err) != nil {
180-
return ctrl.Result{}, err
181+
return err
181182
}
182183
}
183184

184-
return ctrl.Result{}, nil
185+
return nil
185186
}
186187

187188
func (r *PieProbeReconciler) deletePVC(ctx context.Context, pvc *corev1.PersistentVolumeClaim) error {

internal/controller/pie/pieprobe_controller_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,130 @@ var _ = Describe("PieProbe controller", func() {
263263
}
264264
})
265265

266+
It("should create only mount probes if .spec.disableProvisionProbes is true", func() {
267+
By("creating a new PieProbe with .spec.disableProvisionProbes true")
268+
pieProbe2 := &piev1alpha1.PieProbe{
269+
ObjectMeta: metav1.ObjectMeta{
270+
Namespace: "default",
271+
Name: "pie-probe-sc2",
272+
},
273+
Spec: piev1alpha1.PieProbeSpec{
274+
MonitoringStorageClass: "sc2",
275+
NodeSelector: nodeSelector,
276+
ProbePeriod: 1,
277+
DisableProvisionProbes: true,
278+
},
279+
}
280+
_, err := ctrl.CreateOrUpdate(ctx, k8sClient, pieProbe2, func() error { return nil })
281+
Expect(err).NotTo(HaveOccurred())
282+
283+
By("checking mount probes exist and provision probes DO NOT exist")
284+
Eventually(func(g Gomega) {
285+
var cronjobList batchv1.CronJobList
286+
err = k8sClient.List(ctx, &cronjobList)
287+
Expect(err).NotTo(HaveOccurred())
288+
names := []string{}
289+
for _, cronjob := range cronjobList.Items {
290+
if !strings.Contains(cronjob.GetName(), "-sc2-") {
291+
continue
292+
}
293+
names = append(names, cronjob.GetName())
294+
}
295+
g.Expect(names).To(HaveLen(2))
296+
for _, name := range names {
297+
g.Expect(name).To(HavePrefix("mount-pie-probe--"))
298+
}
299+
}).Should(Succeed())
300+
301+
By("cleaning up PVCs and CronJobs for sc2")
302+
err = k8sClient.Delete(ctx, pieProbe2)
303+
Expect(err).NotTo(HaveOccurred())
304+
var pvcList corev1.PersistentVolumeClaimList
305+
err = k8sClient.List(ctx, &pvcList, client.MatchingLabels(map[string]string{
306+
"storage-class": "sc2",
307+
}))
308+
Expect(err).NotTo(HaveOccurred())
309+
for _, pvc := range pvcList.Items {
310+
pvc.ObjectMeta.Finalizers = []string{}
311+
err = k8sClient.Update(ctx, &pvc)
312+
Expect(err).NotTo(HaveOccurred())
313+
err = k8sClient.Delete(ctx, &pvc)
314+
Expect(err).NotTo(HaveOccurred())
315+
}
316+
var cronjobList batchv1.CronJobList
317+
err = k8sClient.List(ctx, &cronjobList)
318+
Expect(err).NotTo(HaveOccurred())
319+
for _, cronjob := range cronjobList.Items {
320+
if !strings.Contains(cronjob.GetName(), "-sc2-") {
321+
continue
322+
}
323+
err = k8sClient.Delete(ctx, &cronjob)
324+
Expect(err).NotTo(HaveOccurred())
325+
}
326+
})
327+
328+
It("should create only provision probes if .spec.disableMountProbes is true", func() {
329+
By("creating a new PieProbe with .spec.disableMountProbes true")
330+
pieProbe2 := &piev1alpha1.PieProbe{
331+
ObjectMeta: metav1.ObjectMeta{
332+
Namespace: "default",
333+
Name: "pie-probe-sc2",
334+
},
335+
Spec: piev1alpha1.PieProbeSpec{
336+
MonitoringStorageClass: "sc2",
337+
NodeSelector: nodeSelector,
338+
ProbePeriod: 1,
339+
DisableMountProbes: true,
340+
},
341+
}
342+
_, err := ctrl.CreateOrUpdate(ctx, k8sClient, pieProbe2, func() error { return nil })
343+
Expect(err).NotTo(HaveOccurred())
344+
345+
By("checking provision probes exist and mount probes DO NOT exist")
346+
Eventually(func(g Gomega) {
347+
var cronjobList batchv1.CronJobList
348+
err = k8sClient.List(ctx, &cronjobList)
349+
Expect(err).NotTo(HaveOccurred())
350+
names := []string{}
351+
for _, cronjob := range cronjobList.Items {
352+
if !strings.Contains(cronjob.GetName(), "-sc2-") {
353+
continue
354+
}
355+
names = append(names, cronjob.GetName())
356+
}
357+
g.Expect(names).To(HaveLen(1))
358+
for _, name := range names {
359+
g.Expect(name).To(HavePrefix("provision-pie-probe--"))
360+
}
361+
}).Should(Succeed())
362+
363+
By("cleaning up PVCs and CronJobs for sc2")
364+
err = k8sClient.Delete(ctx, pieProbe2)
365+
Expect(err).NotTo(HaveOccurred())
366+
var pvcList corev1.PersistentVolumeClaimList
367+
err = k8sClient.List(ctx, &pvcList, client.MatchingLabels(map[string]string{
368+
"storage-class": "sc2",
369+
}))
370+
Expect(err).NotTo(HaveOccurred())
371+
for _, pvc := range pvcList.Items {
372+
pvc.ObjectMeta.Finalizers = []string{}
373+
err = k8sClient.Update(ctx, &pvc)
374+
Expect(err).NotTo(HaveOccurred())
375+
err = k8sClient.Delete(ctx, &pvc)
376+
Expect(err).NotTo(HaveOccurred())
377+
}
378+
var cronjobList batchv1.CronJobList
379+
err = k8sClient.List(ctx, &cronjobList)
380+
Expect(err).NotTo(HaveOccurred())
381+
for _, cronjob := range cronjobList.Items {
382+
if !strings.Contains(cronjob.GetName(), "-sc2-") {
383+
continue
384+
}
385+
err = k8sClient.Delete(ctx, &cronjob)
386+
Expect(err).NotTo(HaveOccurred())
387+
}
388+
})
389+
266390
It("should reject to edit monitoringStorageClass", func() {
267391
By("trying to edit monitoringStorageClass")
268392
var pieProbe piev1alpha1.PieProbe

0 commit comments

Comments
 (0)