Skip to content

Commit 5b9ed95

Browse files
committed
feat(resource): add container resources customization
Allow customization of resource requirements (CPU and memory) for all containers deployed by the MongoDB Kubernetes operator: - Add ResourcesSpec to MongoDBCommunitySpec - Implement safe resource retrieval using reflection - Support customization for mongod, agent, and init containers - Fall back to default values when not specified This enhancement gives users more control over the resource allocation for MongoDB deployments, which is particularly useful in production environments with specific resource constraints.
1 parent 3902595 commit 5b9ed95

File tree

3 files changed

+312
-12
lines changed

3 files changed

+312
-12
lines changed

api/v1/mongodbcommunity_types.go

+27
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ type MongoDBCommunitySpec struct {
132132
// MemberConfig
133133
// +optional
134134
MemberConfig []automationconfig.MemberOptions `json:"memberConfig,omitempty"`
135+
136+
// Resources define the resources for the MongoDB pods.
137+
// +kubebuilder:validation:Type=object
138+
// +optional
139+
// +kubebuilder:pruning:PreserveUnknownFields
140+
// +nullable
141+
142+
Resources ResourcesSpec `json:"resources,omitempty"`
135143
}
136144

137145
// MapWrapper is a wrapper for a map to be used by other structs.
@@ -1225,3 +1233,22 @@ type MongoDBCommunityList struct {
12251233
func init() {
12261234
SchemeBuilder.Register(&MongoDBCommunity{}, &MongoDBCommunityList{})
12271235
}
1236+
1237+
// ResourcesSpec specifies the resources for the containers
1238+
type ResourcesSpec struct {
1239+
// Resources for the container mongod
1240+
// +optional
1241+
Mongod *corev1.ResourceRequirements `json:"mongod,omitempty"`
1242+
1243+
// Resources for the container agent
1244+
// +optional
1245+
Agent *corev1.ResourceRequirements `json:"agent,omitempty"`
1246+
1247+
// Resources for the container readiness probe
1248+
// +optional
1249+
ReadinessProbe *corev1.ResourceRequirements `json:"readinessProbe,omitempty"`
1250+
1251+
// Resources for the container version upgrade hook
1252+
// +optional
1253+
VersionUpgradeHook *corev1.ResourceRequirements `json:"versionUpgradeHook,omitempty"`
1254+
}

config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml

+226
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,232 @@ spec:
323323
type: string
324324
type: object
325325
type: array
326+
resources:
327+
description: ResourcesSpec specifies the resources for the containers
328+
nullable: true
329+
properties:
330+
agent:
331+
description: Resources for the container agent
332+
properties:
333+
claims:
334+
description: |-
335+
Claims lists the names of resources, defined in spec.resourceClaims,
336+
that are used by this container.
337+
338+
339+
This is an alpha field and requires enabling the
340+
DynamicResourceAllocation feature gate.
341+
342+
343+
This field is immutable. It can only be set for containers.
344+
items:
345+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
346+
properties:
347+
name:
348+
description: |-
349+
Name must match the name of one entry in pod.spec.resourceClaims of
350+
the Pod where this field is used. It makes that resource available
351+
inside a container.
352+
type: string
353+
required:
354+
- name
355+
type: object
356+
type: array
357+
x-kubernetes-list-map-keys:
358+
- name
359+
x-kubernetes-list-type: map
360+
limits:
361+
additionalProperties:
362+
anyOf:
363+
- type: integer
364+
- type: string
365+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
366+
x-kubernetes-int-or-string: true
367+
description: |-
368+
Limits describes the maximum amount of compute resources allowed.
369+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
370+
type: object
371+
requests:
372+
additionalProperties:
373+
anyOf:
374+
- type: integer
375+
- type: string
376+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
377+
x-kubernetes-int-or-string: true
378+
description: |-
379+
Requests describes the minimum amount of compute resources required.
380+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
381+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
382+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
383+
type: object
384+
type: object
385+
mongod:
386+
description: Resources for the container mongod
387+
properties:
388+
claims:
389+
description: |-
390+
Claims lists the names of resources, defined in spec.resourceClaims,
391+
that are used by this container.
392+
393+
394+
This is an alpha field and requires enabling the
395+
DynamicResourceAllocation feature gate.
396+
397+
398+
This field is immutable. It can only be set for containers.
399+
items:
400+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
401+
properties:
402+
name:
403+
description: |-
404+
Name must match the name of one entry in pod.spec.resourceClaims of
405+
the Pod where this field is used. It makes that resource available
406+
inside a container.
407+
type: string
408+
required:
409+
- name
410+
type: object
411+
type: array
412+
x-kubernetes-list-map-keys:
413+
- name
414+
x-kubernetes-list-type: map
415+
limits:
416+
additionalProperties:
417+
anyOf:
418+
- type: integer
419+
- type: string
420+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
421+
x-kubernetes-int-or-string: true
422+
description: |-
423+
Limits describes the maximum amount of compute resources allowed.
424+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
425+
type: object
426+
requests:
427+
additionalProperties:
428+
anyOf:
429+
- type: integer
430+
- type: string
431+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
432+
x-kubernetes-int-or-string: true
433+
description: |-
434+
Requests describes the minimum amount of compute resources required.
435+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
436+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
437+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
438+
type: object
439+
type: object
440+
readinessProbe:
441+
description: Resources for the container readiness probe
442+
properties:
443+
claims:
444+
description: |-
445+
Claims lists the names of resources, defined in spec.resourceClaims,
446+
that are used by this container.
447+
448+
449+
This is an alpha field and requires enabling the
450+
DynamicResourceAllocation feature gate.
451+
452+
453+
This field is immutable. It can only be set for containers.
454+
items:
455+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
456+
properties:
457+
name:
458+
description: |-
459+
Name must match the name of one entry in pod.spec.resourceClaims of
460+
the Pod where this field is used. It makes that resource available
461+
inside a container.
462+
type: string
463+
required:
464+
- name
465+
type: object
466+
type: array
467+
x-kubernetes-list-map-keys:
468+
- name
469+
x-kubernetes-list-type: map
470+
limits:
471+
additionalProperties:
472+
anyOf:
473+
- type: integer
474+
- type: string
475+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
476+
x-kubernetes-int-or-string: true
477+
description: |-
478+
Limits describes the maximum amount of compute resources allowed.
479+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
480+
type: object
481+
requests:
482+
additionalProperties:
483+
anyOf:
484+
- type: integer
485+
- type: string
486+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
487+
x-kubernetes-int-or-string: true
488+
description: |-
489+
Requests describes the minimum amount of compute resources required.
490+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
491+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
492+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
493+
type: object
494+
type: object
495+
versionUpgradeHook:
496+
description: Resources for the container version upgrade hook
497+
properties:
498+
claims:
499+
description: |-
500+
Claims lists the names of resources, defined in spec.resourceClaims,
501+
that are used by this container.
502+
503+
504+
This is an alpha field and requires enabling the
505+
DynamicResourceAllocation feature gate.
506+
507+
508+
This field is immutable. It can only be set for containers.
509+
items:
510+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
511+
properties:
512+
name:
513+
description: |-
514+
Name must match the name of one entry in pod.spec.resourceClaims of
515+
the Pod where this field is used. It makes that resource available
516+
inside a container.
517+
type: string
518+
required:
519+
- name
520+
type: object
521+
type: array
522+
x-kubernetes-list-map-keys:
523+
- name
524+
x-kubernetes-list-type: map
525+
limits:
526+
additionalProperties:
527+
anyOf:
528+
- type: integer
529+
- type: string
530+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
531+
x-kubernetes-int-or-string: true
532+
description: |-
533+
Limits describes the maximum amount of compute resources allowed.
534+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
535+
type: object
536+
requests:
537+
additionalProperties:
538+
anyOf:
539+
- type: integer
540+
- type: string
541+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
542+
x-kubernetes-int-or-string: true
543+
description: |-
544+
Requests describes the minimum amount of compute resources required.
545+
If Requests is omitted for a container, it defaults to Limits if that is explicitly specified,
546+
otherwise to an implementation-defined value. Requests cannot exceed Limits.
547+
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
548+
type: object
549+
type: object
550+
type: object
551+
x-kubernetes-preserve-unknown-fields: true
326552
security:
327553
description: Security configures security features, such as TLS, and
328554
authentication settings for a deployment

0 commit comments

Comments
 (0)