Skip to content

Commit ebced8d

Browse files
authored
Merge pull request #148 from topolvm/add-parameter-for-pieprobe
specify computing resources for PieProbe
2 parents c9de8c9 + 1bc2214 commit ebced8d

File tree

6 files changed

+232
-5
lines changed

6 files changed

+232
-5
lines changed

api/pie/v1alpha1/pieprobe_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type PieProbeSpec struct {
4242
//+kubebuilder:validation:Optional
4343
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="disableMountProbes is immutable"
4444
DisableMountProbes bool `json:"disableMountProbes"`
45+
46+
//+kubebuilder:validation:Optional
47+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
4548
}
4649

4750
// PieProbeStatus defines the observed state of PieProbe

api/pie/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,65 @@ spec:
165165
x-kubernetes-validations:
166166
- message: pvcCapacity is immutable
167167
rule: self == oldSelf
168+
resources:
169+
description: ResourceRequirements describes the compute resource requirements.
170+
properties:
171+
claims:
172+
description: |-
173+
Claims lists the names of resources, defined in spec.resourceClaims,
174+
that are used by this container.
175+
176+
This is an alpha field and requires enabling the
177+
DynamicResourceAllocation feature gate.
178+
179+
This field is immutable. It can only be set for containers.
180+
items:
181+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
182+
properties:
183+
name:
184+
description: |-
185+
Name must match the name of one entry in pod.spec.resourceClaims of
186+
the Pod where this field is used. It makes that resource available
187+
inside a container.
188+
type: string
189+
request:
190+
description: |-
191+
Request is the name chosen for a request in the referenced claim.
192+
If empty, everything from the claim is made available, otherwise
193+
only the result of this request.
194+
type: string
195+
required:
196+
- name
197+
type: object
198+
type: array
199+
x-kubernetes-list-map-keys:
200+
- name
201+
x-kubernetes-list-type: map
202+
limits:
203+
additionalProperties:
204+
anyOf:
205+
- type: integer
206+
- type: string
207+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
208+
x-kubernetes-int-or-string: true
209+
description: |-
210+
Limits describes the maximum amount of compute resources allowed.
211+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
212+
type: object
213+
requests:
214+
additionalProperties:
215+
anyOf:
216+
- type: integer
217+
- type: string
218+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
219+
x-kubernetes-int-or-string: true
220+
description: |-
221+
Requests describes the minimum amount of compute resources required.
222+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
223+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
224+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
225+
type: object
226+
type: object
168227
required:
169228
- monitoringStorageClass
170229
- nodeSelector

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,65 @@ spec:
165165
x-kubernetes-validations:
166166
- message: pvcCapacity is immutable
167167
rule: self == oldSelf
168+
resources:
169+
description: ResourceRequirements describes the compute resource requirements.
170+
properties:
171+
claims:
172+
description: |-
173+
Claims lists the names of resources, defined in spec.resourceClaims,
174+
that are used by this container.
175+
176+
This is an alpha field and requires enabling the
177+
DynamicResourceAllocation feature gate.
178+
179+
This field is immutable. It can only be set for containers.
180+
items:
181+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
182+
properties:
183+
name:
184+
description: |-
185+
Name must match the name of one entry in pod.spec.resourceClaims of
186+
the Pod where this field is used. It makes that resource available
187+
inside a container.
188+
type: string
189+
request:
190+
description: |-
191+
Request is the name chosen for a request in the referenced claim.
192+
If empty, everything from the claim is made available, otherwise
193+
only the result of this request.
194+
type: string
195+
required:
196+
- name
197+
type: object
198+
type: array
199+
x-kubernetes-list-map-keys:
200+
- name
201+
x-kubernetes-list-type: map
202+
limits:
203+
additionalProperties:
204+
anyOf:
205+
- type: integer
206+
- type: string
207+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
208+
x-kubernetes-int-or-string: true
209+
description: |-
210+
Limits describes the maximum amount of compute resources allowed.
211+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
212+
type: object
213+
requests:
214+
additionalProperties:
215+
anyOf:
216+
- type: integer
217+
- type: string
218+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
219+
x-kubernetes-int-or-string: true
220+
description: |-
221+
Requests describes the minimum amount of compute resources required.
222+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
223+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
224+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
225+
type: object
226+
type: object
168227
required:
169228
- monitoringStorageClass
170229
- nodeSelector

internal/controller/pie/pieprobe_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ func (r *PieProbeReconciler) createOrUpdateJob(
392392
container := &cronjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0]
393393
container.Name = constants.ProbeContainerName
394394
container.Image = r.containerImage
395+
container.Resources = pieProbe.Spec.Resources
395396

396397
var userID int64 = 1001
397398
var groupID int64 = 1001

internal/controller/pie/pieprobe_controller_test.go

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,107 @@ func deletePieProbeAndReferencingResources(ctx context.Context, pieProbe *piev1a
134134
return nil
135135
}
136136

137+
var _ = Describe("PieProbe controller for specifying resources", func() {
138+
ctx := context.Background()
139+
var stopFunc func()
140+
141+
nodeSelector := corev1.NodeSelector{
142+
NodeSelectorTerms: []corev1.NodeSelectorTerm{
143+
{
144+
MatchExpressions: []corev1.NodeSelectorRequirement{
145+
{
146+
Key: "key1",
147+
Operator: corev1.NodeSelectorOpIn,
148+
Values: []string{"value1"},
149+
},
150+
},
151+
},
152+
},
153+
}
154+
155+
BeforeEach(func() {
156+
skipNameValidation := true
157+
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
158+
Scheme: scheme,
159+
Metrics: metricsserver.Options{BindAddress: "0"},
160+
Controller: config.Controller{
161+
SkipNameValidation: &skipNameValidation,
162+
},
163+
})
164+
Expect(err).NotTo(HaveOccurred())
165+
166+
err = prepareObjects(ctx)
167+
Expect(err).NotTo(HaveOccurred())
168+
169+
pieProbeReconciler := NewPieProbeController(
170+
k8sClient,
171+
"dummy.image",
172+
"http://localhost:8082",
173+
)
174+
err = pieProbeReconciler.SetupWithManager(mgr)
175+
Expect(err).NotTo(HaveOccurred())
176+
177+
ctx, cancel := context.WithCancel(ctx)
178+
stopFunc = cancel
179+
go func() {
180+
err := mgr.Start(ctx)
181+
if err != nil {
182+
panic(err)
183+
}
184+
}()
185+
time.Sleep(100 * time.Millisecond)
186+
})
187+
188+
AfterEach(func() {
189+
stopFunc()
190+
time.Sleep(100 * time.Millisecond)
191+
})
192+
193+
DescribeTable("should create CronJob with the resource specified in the PieProbe resource",
194+
func(limits, requests corev1.ResourceList) {
195+
By("creating a new CronJob via PieProbe with .spec.resources")
196+
pieProbe := &piev1alpha1.PieProbe{
197+
ObjectMeta: metav1.ObjectMeta{
198+
Namespace: "default",
199+
Name: "pie-probe-sc",
200+
},
201+
Spec: piev1alpha1.PieProbeSpec{
202+
MonitoringStorageClass: "sc",
203+
NodeSelector: nodeSelector,
204+
ProbePeriod: 1,
205+
Resources: corev1.ResourceRequirements{
206+
Limits: limits,
207+
Requests: requests,
208+
},
209+
},
210+
}
211+
_, err := ctrl.CreateOrUpdate(ctx, k8sClient, pieProbe, func() error { return nil })
212+
Expect(err).NotTo(HaveOccurred())
213+
defer deletePieProbeAndReferencingResources(ctx, pieProbe)
214+
215+
By("checking the CronJob's resource specified")
216+
Eventually(func(g Gomega) {
217+
var cronjobList batchv1.CronJobList
218+
err := k8sClient.List(ctx, &cronjobList, client.MatchingLabels(map[string]string{
219+
"storage-class": "sc",
220+
}))
221+
g.Expect(err).NotTo(HaveOccurred())
222+
g.Expect(cronjobList.Items).To(HaveLen(3))
223+
224+
cronjob := cronjobList.Items[0]
225+
resources := cronjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Resources
226+
227+
g.Expect(resources.Limits).To(Equal(limits))
228+
g.Expect(resources.Requests).To(Equal(requests))
229+
}).Should(Succeed())
230+
},
231+
Entry("When both cpu limits and requests are not specified", nil, nil),
232+
Entry("When only cpu limits are specified", corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}, nil),
233+
Entry("When only cpu requests are specified", nil, corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("50m")}),
234+
Entry("When both cpu limits and requests are specified", corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}, corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("50m")}),
235+
)
236+
})
237+
137238
var _ = Describe("PieProbe controller", func() {
138239
ctx := context.Background()
139240
var stopFunc func()
@@ -280,7 +381,8 @@ var _ = Describe("PieProbe controller", func() {
280381
}).Should(Succeed())
281382

282383
By("cleaning up PVCs and CronJobs for sc2")
283-
deletePieProbeAndReferencingResources(ctx, pieProbe2)
384+
err = deletePieProbeAndReferencingResources(ctx, pieProbe2)
385+
Expect(err).NotTo(HaveOccurred())
284386
})
285387

286388
It("should create only mount probes if .spec.disableProvisionProbes is true", func() {
@@ -303,7 +405,7 @@ var _ = Describe("PieProbe controller", func() {
303405
By("checking mount probes exist and provision probes DO NOT exist")
304406
Eventually(func(g Gomega) {
305407
var cronjobList batchv1.CronJobList
306-
err = k8sClient.List(context.Background(), &cronjobList, client.MatchingLabels(map[string]string{
408+
err = k8sClient.List(ctx, &cronjobList, client.MatchingLabels(map[string]string{
307409
"storage-class": "sc2",
308410
}))
309411
g.Expect(err).NotTo(HaveOccurred())
@@ -314,7 +416,8 @@ var _ = Describe("PieProbe controller", func() {
314416
}).Should(Succeed())
315417

316418
By("cleaning up PVCs and CronJobs for sc2")
317-
deletePieProbeAndReferencingResources(ctx, pieProbe2)
419+
err = deletePieProbeAndReferencingResources(ctx, pieProbe2)
420+
Expect(err).NotTo(HaveOccurred())
318421
})
319422

320423
It("should create only provision probes if .spec.disableMountProbes is true", func() {
@@ -348,7 +451,8 @@ var _ = Describe("PieProbe controller", func() {
348451
}).Should(Succeed())
349452

350453
By("cleaning up PVCs and CronJobs for sc2")
351-
deletePieProbeAndReferencingResources(ctx, pieProbe2)
454+
err = deletePieProbeAndReferencingResources(ctx, pieProbe2)
455+
Expect(err).NotTo(HaveOccurred())
352456
})
353457

354458
It("should reject to edit monitoringStorageClass", func() {
@@ -378,7 +482,7 @@ var _ = Describe("PieProbe controller", func() {
378482
var cronJobList batchv1.CronJobList
379483
err := k8sClient.List(ctx, &cronJobList)
380484
g.Expect(err).NotTo(HaveOccurred())
381-
g.Expect(len(cronJobList.Items)).Should(Equal(3))
485+
g.Expect(cronJobList.Items).To(HaveLen(3))
382486
for _, cronJob := range cronJobList.Items {
383487
g.Expect(cronJob.OwnerReferences).Should(Equal([]metav1.OwnerReference{{
384488
APIVersion: "pie.topolvm.io/v1alpha1",

0 commit comments

Comments
 (0)