From 0f033d1828186ed94c66ebcb153d74543916ac23 Mon Sep 17 00:00:00 2001 From: shunki-fujita Date: Wed, 18 Oct 2023 05:28:11 +0000 Subject: [PATCH] Enable cpu specification in jobConfig --- api/v1beta1/job_types.go | 11 +++ api/v1beta1/zz_generated.conversion.go | 4 + api/v1beta1/zz_generated.deepcopy.go | 10 +++ api/v1beta2/backuppolicy_webhook_test.go | 3 + api/v1beta2/job_types.go | 11 +++ api/v1beta2/zz_generated.deepcopy.go | 10 +++ .../templates/generated/crds/moco_crds.yaml | 68 ++++++++++++++++ .../bases/moco.cybozu.com_backuppolicies.yaml | 34 ++++++++ .../bases/moco.cybozu.com_mysqlclusters.yaml | 34 ++++++++ ...nition_backuppolicies.moco.cybozu.com.yaml | 34 ++++++++ ...inition_mysqlclusters.moco.cybozu.com.yaml | 34 ++++++++ controllers/mysqlcluster_controller.go | 81 ++++++++++--------- controllers/mysqlcluster_controller_test.go | 17 +++- docs/crd_backuppolicy_v1beta1.md | 2 + docs/crd_backuppolicy_v1beta2.md | 2 + docs/crd_mysqlcluster_v1beta1.md | 2 + docs/crd_mysqlcluster_v1beta2.md | 2 + 17 files changed, 317 insertions(+), 42 deletions(-) diff --git a/api/v1beta1/job_types.go b/api/v1beta1/job_types.go index efceefcf2..7bd4c73b2 100644 --- a/api/v1beta1/job_types.go +++ b/api/v1beta1/job_types.go @@ -30,6 +30,17 @@ type JobConfig struct { // +optional Threads int `json:"threads,omitempty"` + // CPU is the amount of CPU requested for the Pod. + // +kubebuilder:default=4 + // +nullable + // +optional + CPU *resource.Quantity `json:"cpu,omitempty"` + + // MaxCPU is the amount of maximum CPU for the Pod. + // +nullable + // +optional + MaxCPU *resource.Quantity `json:"maxCpu,omitempty"` + // Memory is the amount of memory requested for the Pod. // +kubebuilder:default="4Gi" // +nullable diff --git a/api/v1beta1/zz_generated.conversion.go b/api/v1beta1/zz_generated.conversion.go index 5168719be..09ba74f0c 100644 --- a/api/v1beta1/zz_generated.conversion.go +++ b/api/v1beta1/zz_generated.conversion.go @@ -512,6 +512,8 @@ func autoConvert__JobConfig_To_v1beta2_JobConfig(in *JobConfig, out *v1beta2.Job return err } out.Threads = in.Threads + out.CPU = (*resource.Quantity)(unsafe.Pointer(in.CPU)) + out.MaxCPU = (*resource.Quantity)(unsafe.Pointer(in.MaxCPU)) out.Memory = (*resource.Quantity)(unsafe.Pointer(in.Memory)) out.MaxMemory = (*resource.Quantity)(unsafe.Pointer(in.MaxMemory)) out.EnvFrom = *(*[]v1beta2.EnvFromSourceApplyConfiguration)(unsafe.Pointer(&in.EnvFrom)) @@ -536,6 +538,8 @@ func autoConvert_v1beta2_JobConfig_To__JobConfig(in *v1beta2.JobConfig, out *Job return err } out.Threads = in.Threads + out.CPU = (*resource.Quantity)(unsafe.Pointer(in.CPU)) + out.MaxCPU = (*resource.Quantity)(unsafe.Pointer(in.MaxCPU)) out.Memory = (*resource.Quantity)(unsafe.Pointer(in.Memory)) out.MaxMemory = (*resource.Quantity)(unsafe.Pointer(in.MaxMemory)) out.EnvFrom = *(*[]EnvFromSourceApplyConfiguration)(unsafe.Pointer(&in.EnvFrom)) diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 4f4dad4d2..39dc70596 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -169,6 +169,16 @@ func (in *JobConfig) DeepCopyInto(out *JobConfig) { *out = *in out.BucketConfig = in.BucketConfig in.WorkVolume.DeepCopyInto(&out.WorkVolume) + if in.CPU != nil { + in, out := &in.CPU, &out.CPU + x := (*in).DeepCopy() + *out = &x + } + if in.MaxCPU != nil { + in, out := &in.MaxCPU, &out.MaxCPU + x := (*in).DeepCopy() + *out = &x + } if in.Memory != nil { in, out := &in.Memory, &out.Memory x := (*in).DeepCopy() diff --git a/api/v1beta2/backuppolicy_webhook_test.go b/api/v1beta2/backuppolicy_webhook_test.go index 0bbb7980b..70d9fb09d 100644 --- a/api/v1beta2/backuppolicy_webhook_test.go +++ b/api/v1beta2/backuppolicy_webhook_test.go @@ -45,6 +45,9 @@ var _ = Describe("BackupPolicy Webhook", func() { Expect(err).NotTo(HaveOccurred()) Expect(r.Spec.JobConfig.Threads).To(Equal(4)) + Expect(r.Spec.JobConfig.CPU).NotTo(BeNil()) + Expect(r.Spec.JobConfig.CPU.Value()).To(Equal(int64(4))) + Expect(r.Spec.JobConfig.MaxCPU).To(BeNil()) Expect(r.Spec.JobConfig.Memory).NotTo(BeNil()) Expect(r.Spec.JobConfig.Memory.Value()).To(Equal(int64(4) << 30)) Expect(r.Spec.JobConfig.MaxMemory).To(BeNil()) diff --git a/api/v1beta2/job_types.go b/api/v1beta2/job_types.go index d947a327c..60f13ed93 100644 --- a/api/v1beta2/job_types.go +++ b/api/v1beta2/job_types.go @@ -30,6 +30,17 @@ type JobConfig struct { // +optional Threads int `json:"threads,omitempty"` + // CPU is the amount of CPU requested for the Pod. + // +kubebuilder:default=4 + // +nullable + // +optional + CPU *resource.Quantity `json:"cpu,omitempty"` + + // MaxCPU is the amount of maximum CPU for the Pod. + // +nullable + // +optional + MaxCPU *resource.Quantity `json:"maxCpu,omitempty"` + // Memory is the amount of memory requested for the Pod. // +kubebuilder:default="4Gi" // +nullable diff --git a/api/v1beta2/zz_generated.deepcopy.go b/api/v1beta2/zz_generated.deepcopy.go index b8a55d61f..6843b116e 100644 --- a/api/v1beta2/zz_generated.deepcopy.go +++ b/api/v1beta2/zz_generated.deepcopy.go @@ -169,6 +169,16 @@ func (in *JobConfig) DeepCopyInto(out *JobConfig) { *out = *in out.BucketConfig = in.BucketConfig in.WorkVolume.DeepCopyInto(&out.WorkVolume) + if in.CPU != nil { + in, out := &in.CPU, &out.CPU + x := (*in).DeepCopy() + *out = &x + } + if in.MaxCPU != nil { + in, out := &in.MaxCPU, &out.MaxCPU + x := (*in).DeepCopy() + *out = &x + } if in.Memory != nil { in, out := &in.Memory, &out.Memory x := (*in).DeepCopy() diff --git a/charts/moco/templates/generated/crds/moco_crds.yaml b/charts/moco/templates/generated/crds/moco_crds.yaml index 72eb87c2e..03ac69c17 100644 --- a/charts/moco/templates/generated/crds/moco_crds.yaml +++ b/charts/moco/templates/generated/crds/moco_crds.yaml @@ -434,6 +434,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -516,6 +525,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -2447,6 +2464,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -2529,6 +2555,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -7699,6 +7733,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -7781,6 +7824,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -13566,6 +13617,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -13648,6 +13708,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer diff --git a/config/crd/bases/moco.cybozu.com_backuppolicies.yaml b/config/crd/bases/moco.cybozu.com_backuppolicies.yaml index b1e71e593..d573e4b6d 100644 --- a/config/crd/bases/moco.cybozu.com_backuppolicies.yaml +++ b/config/crd/bases/moco.cybozu.com_backuppolicies.yaml @@ -474,6 +474,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -564,6 +573,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -2644,6 +2661,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -2734,6 +2760,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer diff --git a/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml b/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml index ab0859112..85887c6ef 100644 --- a/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml +++ b/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml @@ -4011,6 +4011,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -4102,6 +4111,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -10425,6 +10442,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -10516,6 +10542,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer diff --git a/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_backuppolicies.moco.cybozu.com.yaml b/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_backuppolicies.moco.cybozu.com.yaml index a861ea025..943dab08f 100644 --- a/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_backuppolicies.moco.cybozu.com.yaml +++ b/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_backuppolicies.moco.cybozu.com.yaml @@ -473,6 +473,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -563,6 +572,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -2643,6 +2660,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -2733,6 +2759,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer diff --git a/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml b/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml index c6d62c033..f6ef4ace5 100644 --- a/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml +++ b/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml @@ -4021,6 +4021,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -4112,6 +4121,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer @@ -10435,6 +10452,15 @@ spec: required: - bucketName type: object + cpu: + anyOf: + - type: integer + - type: string + default: 4 + description: CPU is the amount of CPU requested for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true env: description: List of environment variables to set in the contai items: @@ -10526,6 +10552,14 @@ spec: type: object type: object type: array + maxCpu: + anyOf: + - type: integer + - type: string + description: MaxCPU is the amount of maximum CPU for the Pod. + nullable: true + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true maxMemory: anyOf: - type: integer diff --git a/controllers/mysqlcluster_controller.go b/controllers/mysqlcluster_controller.go index 62eb1089b..d4049e807 100644 --- a/controllers/mysqlcluster_controller.go +++ b/controllers/mysqlcluster_controller.go @@ -26,7 +26,6 @@ import ( "k8s.io/apimachinery/pkg/api/equality" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -1168,25 +1167,27 @@ func (r *MySQLClusterReconciler) reconcileV1BackupJob(ctx context.Context, req c args = append(args, cluster.Namespace, cluster.Name) resources := corev1ac.ResourceRequirements() - if jc.Memory != nil { - resources.WithRequests(corev1.ResourceList{ - corev1.ResourceCPU: *resource.NewQuantity(int64(jc.Threads), resource.DecimalSI), - corev1.ResourceMemory: *jc.Memory, - }) - } else { - resources.WithRequests( - corev1.ResourceList{ - corev1.ResourceCPU: *resource.NewQuantity(int64(jc.Threads), resource.DecimalSI), - }, - ) - } - if jc.MaxMemory != nil { - resources.WithLimits(corev1.ResourceList{ - corev1.ResourceMemory: *jc.MaxMemory, - }) - } - if noJobResource { - resources = corev1ac.ResourceRequirements() + if !noJobResource { + request := corev1.ResourceList{} + if jc.CPU != nil { + request[corev1.ResourceCPU] = *jc.CPU + } + if jc.Memory != nil { + request[corev1.ResourceMemory] = *jc.Memory + } + if len(request) > 0 { + resources.WithRequests(request) + } + limit := corev1.ResourceList{} + if jc.MaxCPU != nil { + limit[corev1.ResourceCPU] = *jc.MaxCPU + } + if jc.MaxMemory != nil { + limit[corev1.ResourceMemory] = *jc.MaxMemory + } + if len(limit) > 0 { + resources.WithLimits(limit) + } } container := corev1ac.Container(). @@ -1544,25 +1545,27 @@ func (r *MySQLClusterReconciler) reconcileV1RestoreJob(ctx context.Context, req args = append(args, cluster.Spec.Restore.RestorePoint.UTC().Format(constants.BackupTimeFormat)) resources := corev1ac.ResourceRequirements() - if jc.Memory != nil { - resources.WithRequests(corev1.ResourceList{ - corev1.ResourceCPU: *resource.NewQuantity(int64(jc.Threads), resource.DecimalSI), - corev1.ResourceMemory: *jc.Memory, - }) - } else { - resources.WithRequests( - corev1.ResourceList{ - corev1.ResourceCPU: *resource.NewQuantity(int64(jc.Threads), resource.DecimalSI), - }, - ) - } - if jc.MaxMemory != nil { - resources.WithLimits(corev1.ResourceList{ - corev1.ResourceMemory: *jc.MaxMemory, - }) - } - if noJobResource { - resources = corev1ac.ResourceRequirements() + if !noJobResource { + request := corev1.ResourceList{} + if jc.CPU != nil { + request[corev1.ResourceCPU] = *jc.CPU + } + if jc.Memory != nil { + request[corev1.ResourceMemory] = *jc.Memory + } + if len(request) > 0 { + resources.WithRequests(request) + } + limit := corev1.ResourceList{} + if jc.MaxCPU != nil { + limit[corev1.ResourceCPU] = *jc.MaxCPU + } + if jc.MaxMemory != nil { + limit[corev1.ResourceMemory] = *jc.MaxMemory + } + if len(limit) > 0 { + resources.WithLimits(limit) + } } container := corev1ac.Container(). diff --git a/controllers/mysqlcluster_controller_test.go b/controllers/mysqlcluster_controller_test.go index 4b5914292..2b746ef62 100644 --- a/controllers/mysqlcluster_controller_test.go +++ b/controllers/mysqlcluster_controller_test.go @@ -1113,6 +1113,8 @@ var _ = Describe("MySQLCluster reconciler", func() { jc := &bp.Spec.JobConfig jc.Threads = 3 jc.ServiceAccountName = "foo" + jc.CPU = resource.NewQuantity(1, resource.DecimalSI) + jc.MaxCPU = resource.NewQuantity(4, resource.DecimalSI) jc.Memory = resource.NewQuantity(1<<30, resource.DecimalSI) jc.MaxMemory = resource.NewQuantity(10<<30, resource.DecimalSI) jc.Env = []mocov1beta2.EnvVarApplyConfiguration{{Name: pointer.String("TEST"), Value: pointer.String("123")}} @@ -1205,7 +1207,9 @@ var _ = Describe("MySQLCluster reconciler", func() { Expect(c.Env).To(HaveLen(2)) Expect(c.VolumeMounts).To(HaveLen(2)) cpuReq := c.Resources.Requests[corev1.ResourceCPU] - Expect(cpuReq.Value()).To(BeNumerically("==", 3)) + Expect(cpuReq.Value()).To(BeNumerically("==", 1)) + cpuLim := c.Resources.Limits[corev1.ResourceCPU] + Expect(cpuLim.Value()).To(BeNumerically("==", 4)) memReq := c.Resources.Requests[corev1.ResourceMemory] Expect(memReq.Value()).To(BeNumerically("==", 1<<30)) memLim := c.Resources.Limits[corev1.ResourceMemory] @@ -1234,6 +1238,8 @@ var _ = Describe("MySQLCluster reconciler", func() { jc = &bp.Spec.JobConfig jc.Threads = 1 jc.ServiceAccountName = "oof" + jc.CPU = nil + jc.MaxCPU = nil jc.Memory = nil jc.MaxMemory = nil jc.Env = nil @@ -1285,7 +1291,8 @@ var _ = Describe("MySQLCluster reconciler", func() { Expect(c.EnvFrom).To(BeEmpty()) Expect(c.Env).To(HaveLen(1)) cpuReq = c.Resources.Requests[corev1.ResourceCPU] - Expect(cpuReq.Value()).To(BeNumerically("==", 1)) + Expect(cpuReq.Value()).To(BeNumerically("==", 4)) + Expect(c.Resources.Limits).NotTo(HaveKey(corev1.ResourceCPU)) memReq = c.Resources.Requests[corev1.ResourceMemory] Expect(memReq.Value()).To(BeNumerically("==", 4<<30)) Expect(c.Resources.Limits).NotTo(HaveKey(corev1.ResourceMemory)) @@ -1343,6 +1350,8 @@ var _ = Describe("MySQLCluster reconciler", func() { jc := &cluster.Spec.Restore.JobConfig jc.Threads = 3 jc.ServiceAccountName = "foo" + jc.CPU = resource.NewQuantity(1, resource.DecimalSI) + jc.MaxCPU = resource.NewQuantity(4, resource.DecimalSI) jc.Memory = resource.NewQuantity(1<<30, resource.DecimalSI) jc.MaxMemory = resource.NewQuantity(10<<30, resource.DecimalSI) jc.Env = []mocov1beta2.EnvVarApplyConfiguration{{Name: pointer.String("TEST"), Value: pointer.String("123")}} @@ -1430,7 +1439,9 @@ var _ = Describe("MySQLCluster reconciler", func() { Expect(c.Env).To(HaveLen(2)) Expect(c.VolumeMounts).To(HaveLen(2)) cpuReq := c.Resources.Requests[corev1.ResourceCPU] - Expect(cpuReq.Value()).To(BeNumerically("==", 3)) + Expect(cpuReq.Value()).To(BeNumerically("==", 1)) + cpuLim := c.Resources.Limits[corev1.ResourceCPU] + Expect(cpuLim.Value()).To(BeNumerically("==", 4)) memReq := c.Resources.Requests[corev1.ResourceMemory] Expect(memReq.Value()).To(BeNumerically("==", 1<<30)) memLim := c.Resources.Limits[corev1.ResourceMemory] diff --git a/docs/crd_backuppolicy_v1beta1.md b/docs/crd_backuppolicy_v1beta1.md index 885c29c3e..4f1aa3fb6 100644 --- a/docs/crd_backuppolicy_v1beta1.md +++ b/docs/crd_backuppolicy_v1beta1.md @@ -73,6 +73,8 @@ JobConfig is a set of parameters for backup and restore job Pods. | bucketConfig | Specifies how to access an object storage bucket. | [BucketConfig](#bucketconfig) | true | | workVolume | WorkVolume is the volume source for the working directory. Since the backup or restore task can use a lot of bytes in the working directory, You should always give a volume with enough capacity.\n\nThe recommended volume source is a generic ephemeral volume. https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#generic-ephemeral-volumes | [VolumeSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#VolumeSourceApplyConfiguration) | true | | threads | Threads is the number of threads used for backup or restoration. | int | false | +| cpu | CPU is the amount of CPU requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | +| maxCpu | MaxCPU is the amount of maximum CPU for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | memory | Memory is the amount of memory requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | maxMemory | MaxMemory is the amount of maximum memory for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | envFrom | List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence.\n\nYou can configure S3 bucket access parameters through environment variables. See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config#EnvConfig | [][EnvFromSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#EnvFromSourceApplyConfiguration) | false | diff --git a/docs/crd_backuppolicy_v1beta2.md b/docs/crd_backuppolicy_v1beta2.md index 885c29c3e..4f1aa3fb6 100644 --- a/docs/crd_backuppolicy_v1beta2.md +++ b/docs/crd_backuppolicy_v1beta2.md @@ -73,6 +73,8 @@ JobConfig is a set of parameters for backup and restore job Pods. | bucketConfig | Specifies how to access an object storage bucket. | [BucketConfig](#bucketconfig) | true | | workVolume | WorkVolume is the volume source for the working directory. Since the backup or restore task can use a lot of bytes in the working directory, You should always give a volume with enough capacity.\n\nThe recommended volume source is a generic ephemeral volume. https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#generic-ephemeral-volumes | [VolumeSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#VolumeSourceApplyConfiguration) | true | | threads | Threads is the number of threads used for backup or restoration. | int | false | +| cpu | CPU is the amount of CPU requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | +| maxCpu | MaxCPU is the amount of maximum CPU for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | memory | Memory is the amount of memory requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | maxMemory | MaxMemory is the amount of maximum memory for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | envFrom | List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence.\n\nYou can configure S3 bucket access parameters through environment variables. See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config#EnvConfig | [][EnvFromSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#EnvFromSourceApplyConfiguration) | false | diff --git a/docs/crd_mysqlcluster_v1beta1.md b/docs/crd_mysqlcluster_v1beta1.md index 1dc2e63f3..f82d11b5f 100644 --- a/docs/crd_mysqlcluster_v1beta1.md +++ b/docs/crd_mysqlcluster_v1beta1.md @@ -194,6 +194,8 @@ JobConfig is a set of parameters for backup and restore job Pods. | bucketConfig | Specifies how to access an object storage bucket. | [BucketConfig](#bucketconfig) | true | | workVolume | WorkVolume is the volume source for the working directory. Since the backup or restore task can use a lot of bytes in the working directory, You should always give a volume with enough capacity.\n\nThe recommended volume source is a generic ephemeral volume. https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#generic-ephemeral-volumes | [VolumeSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#VolumeSourceApplyConfiguration) | true | | threads | Threads is the number of threads used for backup or restoration. | int | false | +| cpu | CPU is the amount of CPU requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | +| maxCpu | MaxCPU is the amount of maximum CPU for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | memory | Memory is the amount of memory requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | maxMemory | MaxMemory is the amount of maximum memory for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | envFrom | List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence.\n\nYou can configure S3 bucket access parameters through environment variables. See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config#EnvConfig | [][EnvFromSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#EnvFromSourceApplyConfiguration) | false | diff --git a/docs/crd_mysqlcluster_v1beta2.md b/docs/crd_mysqlcluster_v1beta2.md index e4cc184b0..1f85b7c3d 100644 --- a/docs/crd_mysqlcluster_v1beta2.md +++ b/docs/crd_mysqlcluster_v1beta2.md @@ -208,6 +208,8 @@ JobConfig is a set of parameters for backup and restore job Pods. | bucketConfig | Specifies how to access an object storage bucket. | [BucketConfig](#bucketconfig) | true | | workVolume | WorkVolume is the volume source for the working directory. Since the backup or restore task can use a lot of bytes in the working directory, You should always give a volume with enough capacity.\n\nThe recommended volume source is a generic ephemeral volume. https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#generic-ephemeral-volumes | [VolumeSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#VolumeSourceApplyConfiguration) | true | | threads | Threads is the number of threads used for backup or restoration. | int | false | +| cpu | CPU is the amount of CPU requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | +| maxCpu | MaxCPU is the amount of maximum CPU for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | memory | Memory is the amount of memory requested for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | maxMemory | MaxMemory is the amount of maximum memory for the Pod. | *[resource.Quantity](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity) | false | | envFrom | List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence.\n\nYou can configure S3 bucket access parameters through environment variables. See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config#EnvConfig | [][EnvFromSourceApplyConfiguration](https://pkg.go.dev/k8s.io/client-go/applyconfigurations/core/v1#EnvFromSourceApplyConfiguration) | false |