Skip to content

Commit

Permalink
chore: fix #2738, provide zero limits resources for sidecar container…
Browse files Browse the repository at this point in the history
…s if not specified
  • Loading branch information
nashtsai committed Jun 27, 2023
1 parent 9103b9e commit 216bd13
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 22 additions & 1 deletion internal/controller/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"

Expand Down Expand Up @@ -114,11 +115,30 @@ func processContainersInjection(reqCtx intctrlutil.RequestCtx,
if err := injectEnvs(cluster, component, envConfigName, &(*cc)[i]); err != nil {
return err
}
injectZeroResourcesLimitsIfEmpty(&(*cc)[i])
}
}
return nil
}

func injectZeroResourcesLimitsIfEmpty(c *corev1.Container) {
zeroValue := resource.MustParse("0")
if c.Resources.Limits == nil {
c.Resources.Limits = corev1.ResourceList{
corev1.ResourceCPU: zeroValue,
corev1.ResourceMemory: zeroValue,
}
return
}

if _, ok := c.Resources.Limits[corev1.ResourceCPU]; !ok {
c.Resources.Limits[corev1.ResourceCPU] = zeroValue
}
if _, ok := c.Resources.Limits[corev1.ResourceMemory]; !ok {
c.Resources.Limits[corev1.ResourceMemory] = zeroValue
}
}

func injectEnvs(cluster *appsv1alpha1.Cluster, component *component.SynthesizedComponent, envConfigName string, c *corev1.Container) error {
// can not use map, it is unordered
envFieldPathSlice := []struct {
Expand Down Expand Up @@ -207,7 +227,6 @@ func injectEnvs(cluster *appsv1alpha1.Cluster, component *component.SynthesizedC
},
},
})

return nil
}

Expand Down Expand Up @@ -656,6 +675,7 @@ func BuildCfgManagerContainer(sidecarRenderedParam *cfgcm.CfgManagerBuildParams,
if err := injectEnvs(sidecarRenderedParam.Cluster, component, sidecarRenderedParam.EnvConfigName, &container); err != nil {
return nil, err
}
injectZeroResourcesLimitsIfEmpty(&container)
return &container, nil
}

Expand Down Expand Up @@ -736,6 +756,7 @@ func BuildCfgManagerToolsContainer(sidecarRenderedParam *cfgcm.CfgManagerBuildPa
if err := injectEnvs(sidecarRenderedParam.Cluster, component, sidecarRenderedParam.EnvConfigName, &toolContainers[i]); err != nil {
return nil, err
}
injectZeroResourcesLimitsIfEmpty(&toolContainers[i])
}
return toolContainers, nil
}
Expand Down
15 changes: 13 additions & 2 deletions internal/testutil/apps/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,21 @@ const (
)

var (
zeroResRequirements = corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("0"),
corev1.ResourceMemory: resource.MustParse("0"),
},
}

statelessNginxComponent = appsv1alpha1.ClusterComponentDefinition{
WorkloadType: appsv1alpha1.Stateless,
CharacterType: "stateless",
PodSpec: &corev1.PodSpec{
Containers: []corev1.Container{{
Name: DefaultNginxContainerName,
Image: NginxImage,
Name: DefaultNginxContainerName,
Image: NginxImage,
Resources: zeroResRequirements,
}},
},
Service: &appsv1alpha1.ServiceSpec{
Expand Down Expand Up @@ -119,6 +127,7 @@ var (
Name: DefaultMySQLContainerName,
Image: ApeCloudMySQLImage,
ImagePullPolicy: corev1.PullIfNotPresent,
Resources: zeroResRequirements,
Ports: []corev1.ContainerPort{
{
Name: "mysql",
Expand Down Expand Up @@ -246,6 +255,7 @@ var (
ImagePullPolicy: corev1.PullIfNotPresent,
VolumeMounts: defaultReplicationRedisVolumeMounts,
Command: []string{"/scripts/init.sh"},
Resources: zeroResRequirements,
}

defaultRedisContainer = corev1.Container{
Expand All @@ -268,6 +278,7 @@ var (
},
},
},
Resources: zeroResRequirements,
}

replicationRedisComponent = appsv1alpha1.ClusterComponentDefinition{
Expand Down

0 comments on commit 216bd13

Please sign in to comment.