Skip to content

Commit

Permalink
Enable cpu specification in jobConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
shunki-fujita committed Oct 18, 2023
1 parent 8b8123d commit f8d636e
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 25 deletions.
11 changes: 11 additions & 0 deletions api/v1beta2/job_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions charts/moco/templates/generated/crds/moco_crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2447,6 +2447,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:
Expand Down Expand Up @@ -2529,6 +2538,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
Expand Down Expand Up @@ -13566,6 +13583,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:
Expand Down Expand Up @@ -13648,6 +13674,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
Expand Down
17 changes: 17 additions & 0 deletions config/crd/bases/moco.cybozu.com_backuppolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,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:
Expand Down Expand Up @@ -2734,6 +2743,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
Expand Down
17 changes: 17 additions & 0 deletions config/crd/bases/moco.cybozu.com_mysqlclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10425,6 +10425,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:
Expand Down Expand Up @@ -10516,6 +10525,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2643,6 +2643,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:
Expand Down Expand Up @@ -2733,6 +2742,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10435,6 +10435,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:
Expand Down Expand Up @@ -10526,6 +10535,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
Expand Down
50 changes: 28 additions & 22 deletions controllers/mysqlcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -1546,13 +1547,13 @@ func (r *MySQLClusterReconciler) reconcileV1RestoreJob(ctx context.Context, req
resources := corev1ac.ResourceRequirements()
if jc.Memory != nil {
resources.WithRequests(corev1.ResourceList{
corev1.ResourceCPU: *resource.NewQuantity(int64(jc.Threads), resource.DecimalSI),
corev1.ResourceCPU: *jc.CPU,
corev1.ResourceMemory: *jc.Memory,
})
} else {
resources.WithRequests(
corev1.ResourceList{
corev1.ResourceCPU: *resource.NewQuantity(int64(jc.Threads), resource.DecimalSI),
corev1.ResourceCPU: *jc.CPU,
},
)
}
Expand All @@ -1561,6 +1562,11 @@ func (r *MySQLClusterReconciler) reconcileV1RestoreJob(ctx context.Context, req
corev1.ResourceMemory: *jc.MaxMemory,
})
}
if jc.MaxCPU != nil {
resources.WithLimits(corev1.ResourceList{
corev1.ResourceCPU: *jc.MaxCPU,
})
}
if noJobResource {
resources = corev1ac.ResourceRequirements()
}
Expand Down
17 changes: 14 additions & 3 deletions controllers/mysqlcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")}}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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")}}
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions docs/crd_mysqlcluster_v1beta2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 requested 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 |
Expand Down

0 comments on commit f8d636e

Please sign in to comment.