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

feat(hatchery:k8s): set memory and CPU limits for workers and services #6187

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 23 additions & 10 deletions engine/hatchery/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (h *HatcheryKubernetes) SpawnWorker(ctx context.Context, spawnArgs hatchery
log.Warn(ctx, "spawnKubernetesDockerWorker> %s unable to parse memory requirement %d: %v", logJob, memory, err)
return err
}
break
}
}

Expand Down Expand Up @@ -283,6 +284,11 @@ func (h *HatcheryKubernetes) SpawnWorker(ctx context.Context, spawnArgs hatchery
Args: []string{cmd},
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse(h.Config.DefaultCPU),
apiv1.ResourceMemory: resource.MustParse(fmt.Sprintf("%d", memory)),
},
Limits: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse(h.Config.DefaultCPU),
apiv1.ResourceMemory: resource.MustParse(fmt.Sprintf("%d", memory)),
},
},
Expand Down Expand Up @@ -318,23 +324,30 @@ func (h *HatcheryKubernetes) SpawnWorker(ctx context.Context, spawnArgs hatchery
//value= "postgres:latest env_1=blabla env_2=blabla"" => we can add env variables in requirement name
img, envm := hatchery.ParseRequirementModel(serv.Value)

servContainer := apiv1.Container{
Name: fmt.Sprintf("service-%d-%s", serv.ID, strings.ToLower(serv.Name)),
Image: img,
}

serviceMemory := resource.MustParse(fmt.Sprintf("%d", h.Config.DefaultServiceMemory))
if sm, ok := envm["CDS_SERVICE_MEMORY"]; ok {
mq, err := resource.ParseQuantity(sm)
var err error
serviceMemory, err = resource.ParseQuantity(sm)
if err != nil {
log.Warn(ctx, "hatchery> kubernetes> SpawnWorker> Unable to parse CDS_SERVICE_MEMORY value '%s': %s", sm, err)
continue
}
servContainer.Resources = apiv1.ResourceRequirements{
delete(envm, "CDS_SERVICE_MEMORY")
}

servContainer := apiv1.Container{
Name: fmt.Sprintf("service-%d-%s", serv.ID, strings.ToLower(serv.Name)),
Image: img,
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{
apiv1.ResourceMemory: mq,
apiv1.ResourceCPU: resource.MustParse(h.Config.DefaultServiceCPU),
apiv1.ResourceMemory: serviceMemory,
},
}
delete(envm, "CDS_SERVICE_MEMORY")
Limits: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse(h.Config.DefaultServiceCPU),
apiv1.ResourceMemory: serviceMemory,
},
},
}

if sa, ok := envm["CDS_SERVICE_ARGS"]; ok {
Expand Down
6 changes: 6 additions & 0 deletions engine/hatchery/kubernetes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ var containerServiceNameRegexp = regexp.MustCompile(`service-([0-9]+)-(.*)`)
// HatcheryConfiguration is the configuration for local hatchery
type HatcheryConfiguration struct {
service.HatcheryCommonConfiguration `mapstructure:"commonConfiguration" toml:"commonConfiguration" json:"commonConfiguration"`
// DefaultCPU Worker default CPU
DefaultCPU string `mapstructure:"defaultCPU" toml:"defaultCPU" default:"500m" commented:"false" comment:"Worker default CPU" json:"defaultCPU"`
// DefaultMemory Worker default memory
DefaultMemory int `mapstructure:"defaultMemory" toml:"defaultMemory" default:"1024" commented:"false" comment:"Worker default memory in Mo" json:"defaultMemory"`
// DefaultServiceCPU Service default CPU
DefaultServiceCPU string `mapstructure:"defaultServiceCPU" toml:"defaultServiceCPU" default:"256m" commented:"false" comment:"Service default CPU" json:"defaultServiceCPU"`
// DefaultServiceMemory Service default memory
DefaultServiceMemory int `mapstructure:"defaultServiceMemory" toml:"defaultServiceMemory" default:"512" commented:"false" comment:"Service default memory in Mo" json:"defaultServiceMemory"`
// Namespace is the kubernetes namespace in which workers are spawned"
Namespace string `mapstructure:"namespace" toml:"namespace" default:"cds" commented:"false" comment:"Kubernetes namespace in which workers are spawned" json:"namespace"`
// KubernetesMasterURL Address of kubernetes master
Expand Down