Skip to content

Commit 8290204

Browse files
leon-apeapecloud-bot
authored andcommitted
fix: Addon job resources configuration (#10223)
(cherry picked from commit e25fa9d)
1 parent 24b6c31 commit 8290204

6 files changed

Lines changed: 89 additions & 10 deletions

File tree

cmd/manager/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ func validateRequiredToParseConfigs() error {
266266
return err
267267
}
268268
}
269+
if jobResources := viper.GetString(constant.CfgKeyAddonJobResources); jobResources != "" {
270+
resources := corev1.ResourceRequirements{}
271+
if err := json.Unmarshal([]byte(jobResources), &resources); err != nil {
272+
return err
273+
}
274+
}
269275
if err := validateTolerations(viper.GetString(constant.CfgKeyCtrlrMgrTolerations)); err != nil {
270276
return err
271277
}

controllers/extensions/addon_controller_stages.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,9 @@ func setSharedVolume(addon *extensionsv1alpha1.Addon, helmJobPodSpec *corev1.Pod
503503
}
504504

505505
// setInitContainer sets init containers to copy dependent charts to shared volume
506-
func setInitContainer(addon *extensionsv1alpha1.Addon, helmJobPodSpec *corev1.PodSpec) {
506+
func setInitContainer(addon *extensionsv1alpha1.Addon, helmJobPodSpec *corev1.PodSpec) error {
507507
if !useLocalCharts(addon) {
508-
return
508+
return nil
509509
}
510510

511511
fromPath := addon.Spec.Helm.ChartsPathInImage
@@ -515,7 +515,7 @@ func setInitContainer(addon *extensionsv1alpha1.Addon, helmJobPodSpec *corev1.Po
515515
copyChartsContainer := corev1.Container{
516516
Name: "copy-charts",
517517
Image: intctrlutil.ReplaceImageRegistry(addon.Spec.Helm.ChartsImage),
518-
ImagePullPolicy: corev1.PullPolicy(viper.GetString(constant.CfgAddonChartsImgPullPolicy)),
518+
ImagePullPolicy: corev1.PullPolicy(viper.GetString(constant.CfgKeyAddonChartsImgPullPolicy)),
519519
Command: []string{"sh", "-c", fmt.Sprintf("cp %s/* /mnt/charts", fromPath)},
520520
VolumeMounts: []corev1.VolumeMount{
521521
{
@@ -524,8 +524,11 @@ func setInitContainer(addon *extensionsv1alpha1.Addon, helmJobPodSpec *corev1.Po
524524
},
525525
},
526526
}
527-
intctrlutil.InjectZeroResourcesLimitsIfEmpty(&copyChartsContainer)
527+
if err := setAddonJobResourcesOrZero(&copyChartsContainer); err != nil {
528+
return err
529+
}
528530
helmJobPodSpec.InitContainers = append(helmJobPodSpec.InitContainers, copyChartsContainer)
531+
return nil
529532
}
530533

531534
func (r *helmTypeInstallStage) Handle(ctx context.Context) {
@@ -687,7 +690,10 @@ func (r *helmTypeInstallStage) Handle(ctx context.Context) {
687690
// we will copy the charts from charts image to shared volume. Addon container will use the
688691
// charts from shared volume to install the addon.
689692
setSharedVolume(addon, helmJobPodSpec)
690-
setInitContainer(addon, helmJobPodSpec)
693+
if err := setInitContainer(addon, helmJobPodSpec); err != nil {
694+
r.setRequeueWithErr(err, "")
695+
return
696+
}
691697

692698
if err := r.reconciler.Create(ctx, helmInstallJob); err != nil {
693699
r.setRequeueWithErr(err, "")
@@ -914,7 +920,7 @@ func createHelmJobProto(addon *extensionsv1alpha1.Addon) (*batchv1.Job, error) {
914920
container := corev1.Container{
915921
Name: getJobMainContainerName(addon),
916922
Image: viper.GetString(constant.KBToolsImage),
917-
ImagePullPolicy: corev1.PullPolicy(viper.GetString(constant.CfgAddonJobImgPullPolicy)),
923+
ImagePullPolicy: corev1.PullPolicy(viper.GetString(constant.CfgKeyAddonJobImgPullPolicy)),
918924
Command: []string{"helm"},
919925
Env: []corev1.EnvVar{
920926
{
@@ -932,7 +938,9 @@ func createHelmJobProto(addon *extensionsv1alpha1.Addon) (*batchv1.Job, error) {
932938
},
933939
VolumeMounts: []corev1.VolumeMount{},
934940
}
935-
intctrlutil.InjectZeroResourcesLimitsIfEmpty(&container)
941+
if err := setAddonJobResourcesOrZero(&container); err != nil {
942+
return nil, err
943+
}
936944

937945
helmProtoJob := &batchv1.Job{
938946
ObjectMeta: metav1.ObjectMeta{
@@ -1003,6 +1011,31 @@ func createHelmJobProto(addon *extensionsv1alpha1.Addon) (*batchv1.Job, error) {
10031011
return helmProtoJob, nil
10041012
}
10051013

1014+
func getAddonJobResources() (*corev1.ResourceRequirements, error) {
1015+
value := viper.GetString(constant.CfgKeyAddonJobResources)
1016+
if value == "" {
1017+
return nil, nil
1018+
}
1019+
resources := &corev1.ResourceRequirements{}
1020+
if err := json.Unmarshal([]byte(value), resources); err != nil {
1021+
return nil, err
1022+
}
1023+
return resources, nil
1024+
}
1025+
1026+
func setAddonJobResourcesOrZero(container *corev1.Container) error {
1027+
resources, err := getAddonJobResources()
1028+
if err != nil {
1029+
return err
1030+
}
1031+
if resources != nil {
1032+
container.Resources = *resources
1033+
return nil
1034+
}
1035+
intctrlutil.InjectZeroResourcesLimitsIfEmpty(container)
1036+
return nil
1037+
}
1038+
10061039
func enabledAddonWithDefaultValues(ctx context.Context, stageCtx *stageCtx,
10071040
addon *extensionsv1alpha1.Addon, reason, message string) {
10081041
setInstallSpec := func(di *extensionsv1alpha1.AddonDefaultInstallSpecItem) {

controllers/extensions/addon_controller_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
batchv1 "k8s.io/api/batch/v1"
3232
corev1 "k8s.io/api/core/v1"
3333
apierrors "k8s.io/apimachinery/pkg/api/errors"
34+
"k8s.io/apimachinery/pkg/api/resource"
3435
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3536
"k8s.io/apimachinery/pkg/types"
3637
"k8s.io/apimachinery/pkg/version"
@@ -96,6 +97,37 @@ var _ = Describe("Addon controller", func() {
9697
cleanEnv()
9798
})
9899

100+
Context("Addon job resources", func() {
101+
AfterEach(func() {
102+
viper.Set(constant.CfgKeyAddonJobResources, "")
103+
})
104+
105+
It("should apply configured resources to addon job containers", func() {
106+
viper.Set(constant.CfgKeyAddonJobResources, `{"requests":{"cpu":"10m","memory":"16Mi"},"limits":{"cpu":"100m","memory":"64Mi"}}`)
107+
addon := &extensionsv1alpha1.Addon{
108+
ObjectMeta: metav1.ObjectMeta{Name: "test-addon"},
109+
Spec: extensionsv1alpha1.AddonSpec{
110+
Helm: &extensionsv1alpha1.HelmTypeInstallSpec{
111+
ChartLocationURL: "file://test-chart",
112+
ChartsImage: "test-charts-image",
113+
},
114+
},
115+
}
116+
117+
job, err := createHelmJobProto(addon)
118+
Expect(err).ShouldNot(HaveOccurred())
119+
Expect(job.Spec.Template.Spec.Containers[0].Resources.Requests).Should(HaveKeyWithValue(corev1.ResourceCPU, resource.MustParse("10m")))
120+
Expect(job.Spec.Template.Spec.Containers[0].Resources.Requests).Should(HaveKeyWithValue(corev1.ResourceMemory, resource.MustParse("16Mi")))
121+
Expect(job.Spec.Template.Spec.Containers[0].Resources.Limits).Should(HaveKeyWithValue(corev1.ResourceCPU, resource.MustParse("100m")))
122+
Expect(job.Spec.Template.Spec.Containers[0].Resources.Limits).Should(HaveKeyWithValue(corev1.ResourceMemory, resource.MustParse("64Mi")))
123+
124+
Expect(setInitContainer(addon, &job.Spec.Template.Spec)).Should(Succeed())
125+
Expect(job.Spec.Template.Spec.InitContainers).Should(HaveLen(1))
126+
Expect(job.Spec.Template.Spec.InitContainers[0].Resources.Requests).Should(HaveKeyWithValue(corev1.ResourceCPU, resource.MustParse("10m")))
127+
Expect(job.Spec.Template.Spec.InitContainers[0].Resources.Requests).Should(HaveKeyWithValue(corev1.ResourceMemory, resource.MustParse("16Mi")))
128+
})
129+
})
130+
99131
Context("Addon controller test", func() {
100132
var addon *extensionsv1alpha1.Addon
101133
var key types.NamespacedName

deploy/helm/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ spec:
164164
value: {{ .jobTTL | quote }}
165165
- name: ADDON_JOB_IMAGE_PULL_POLICY
166166
value: {{ .jobImagePullPolicy | default "IfNotPresent" }}
167+
{{- with .jobResources }}
168+
- name: ADDON_JOB_RESOURCES
169+
value: {{ toJson . | quote }}
170+
{{- end }}
167171
{{- end }}
168172
- name: KUBEBLOCKS_ADDON_HELM_INSTALL_OPTIONS
169173
value: {{ join " " .Values.addonHelmInstallOptions }}

deploy/helm/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ addonController:
452452
enabled: true
453453
jobTTL: "5m"
454454
jobImagePullPolicy: IfNotPresent
455+
## @param addonController.jobResources - resources for addon install/uninstall jobs.
456+
## Set this when namespace LimitRange policies require non-zero requests.
457+
jobResources: {}
455458

456459

457460
## @param keepAddons - keep Addon CR objects when delete this chart.

pkg/constant/viper_config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ const (
3333
CfgHostPortExcludeRanges = "HOST_PORT_EXCLUDE_RANGES"
3434

3535
// addon config keys
36-
CfgKeyAddonJobTTL = "ADDON_JOB_TTL"
37-
CfgAddonJobImgPullPolicy = "ADDON_JOB_IMAGE_PULL_POLICY"
36+
CfgKeyAddonJobTTL = "ADDON_JOB_TTL"
37+
CfgKeyAddonJobImgPullPolicy = "ADDON_JOB_IMAGE_PULL_POLICY"
38+
CfgKeyAddonJobResources = "ADDON_JOB_RESOURCES"
3839

3940
// addon charts config keys
40-
CfgAddonChartsImgPullPolicy = "KUBEBLOCKS_ADDON_CHARTS_IMAGE_PULL_POLICY"
41+
CfgKeyAddonChartsImgPullPolicy = "KUBEBLOCKS_ADDON_CHARTS_IMAGE_PULL_POLICY"
4142

4243
// storage config keys
4344
CfgKeyDefaultStorageClass = "DEFAULT_STORAGE_CLASS"

0 commit comments

Comments
 (0)