Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix #2738, provide zero limits resources for sidecar containers if not specified #3997

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/cli/cmd/bench/template/bench_sysbench_template.cue
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

// required, command line input options for parameters and flags
options: {
flag: int
flag: int
mode: string
driver: string
database: string
value: string
value: string
}

// required, k8s api resource content
Expand Down
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
5 changes: 4 additions & 1 deletion internal/controller/builder/cue/deployment_template.cue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ component: {
compDefName: string
workloadType: string
replicas: int
podSpec: containers: [...]
podSpec: {
containers: [...]
enableServiceLinks: false
}
volumeClaimTemplates: [...]
}

Expand Down
5 changes: 4 additions & 1 deletion internal/controller/builder/cue/statefulset_template.cue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ component: {
name: string
workloadType: string
replicas: int
podSpec: containers: [...]
podSpec: {
containers: [...]
enableServiceLinks: false
}
volumeClaimTemplates: [...]
}

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