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: added min resource constraint for kubernetes #687

Merged
merged 10 commits into from Jun 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -262,6 +262,8 @@ func createStartServiceOperation(
cpuAllocationMillicpus := serviceConfig.GetCPUAllocationMillicpus()
memoryAllocationMegabytes := serviceConfig.GetMemoryAllocationMegabytes()
privateIPAddrPlaceholder := serviceConfig.GetPrivateIPAddrPlaceholder()
minCpuAllocationMilliCpus := serviceConfig.GetMinCPUAllocationMillicpus()
minMemoryAllocationMegabytes := serviceConfig.GetMinMemoryAllocationMegabytes()

matchingObjectAndResources, found := servicesObjectsAndResources[serviceUuid]
if !found {
Expand Down Expand Up @@ -314,12 +316,13 @@ func createStartServiceOperation(
podContainers, err := getUserServicePodContainerSpecs(
containerImageName,
entrypointArgs,
cmdArgs,
envVars,
cmdArgs, envVars,
privatePorts,
userServiceContainerVolumeMounts,
cpuAllocationMillicpus,
memoryAllocationMegabytes,
minCpuAllocationMilliCpus,
minMemoryAllocationMegabytes,
)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating the container specs for the user service pod with image '%v'", containerImageName)
Expand Down Expand Up @@ -511,10 +514,10 @@ func getUserServicePodContainerSpecs(
containerMounts []apiv1.VolumeMount,
cpuAllocationMillicpus uint64,
memoryAllocationMegabytes uint64,
) (
[]apiv1.Container,
error,
) {
minCpuAllocationMilliCpus uint64,
minMemoryAllocationMegabytes uint64,
) ([]apiv1.Container, error) {

var containerEnvVars []apiv1.EnvVar
for varName, varValue := range envVarStrs {
envVar := apiv1.EnvVar{
Expand All @@ -535,13 +538,22 @@ func getUserServicePodContainerSpecs(
// 0 is considered the empty value (meaning the field was never set), so if either fields are 0, that resource is left unbounded
if cpuAllocationMillicpus != 0 {
resourceLimitsList[apiv1.ResourceCPU] = *resource.NewMilliQuantity(int64(cpuAllocationMillicpus), resource.DecimalSI)
resourceRequestsList[apiv1.ResourceCPU] = *resource.NewMilliQuantity(int64(cpuAllocationMillicpus), resource.DecimalSI)
}

if minCpuAllocationMilliCpus != 0 {
resourceRequestsList[apiv1.ResourceCPU] = *resource.NewMilliQuantity(int64(minCpuAllocationMilliCpus), resource.DecimalSI)
}

if memoryAllocationMegabytes != 0 {
memoryAllocationInBytes := convertMegabytesToBytes(memoryAllocationMegabytes)
resourceLimitsList[apiv1.ResourceMemory] = *resource.NewQuantity(int64(memoryAllocationInBytes), resource.DecimalSI)
resourceRequestsList[apiv1.ResourceMemory] = *resource.NewQuantity(int64(memoryAllocationInBytes), resource.DecimalSI)
}

if minMemoryAllocationMegabytes != 0 {
minMemoryAllocationInBytes := convertMegabytesToBytes(minMemoryAllocationMegabytes)
resourceRequestsList[apiv1.ResourceMemory] = *resource.NewQuantity(int64(minMemoryAllocationInBytes), resource.DecimalSI)
}

resourceRequirements := apiv1.ResourceRequirements{ //nolint:exhaustruct
Limits: resourceLimitsList,
Requests: resourceRequestsList,
Expand Down
Expand Up @@ -27,6 +27,10 @@ type ServiceConfig struct {
memoryAllocationMegabytes uint64

privateIPAddrPlaceholder string

minCpuAllocationMilliCpus uint64

minMemoryAllocationMegabytes uint64
}

func NewServiceConfig(
Expand All @@ -40,6 +44,7 @@ func NewServiceConfig(
cpuAllocationMillicpus uint64,
memoryAllocationMegabytes uint64,
privateIPAddrPlaceholder string) *ServiceConfig {

return &ServiceConfig{
containerImageName: containerImageName,
privatePorts: privatePorts,
Expand All @@ -51,6 +56,10 @@ func NewServiceConfig(
cpuAllocationMillicpus: cpuAllocationMillicpus,
memoryAllocationMegabytes: memoryAllocationMegabytes,
privateIPAddrPlaceholder: privateIPAddrPlaceholder,
// TODO: WILL CHANGE THIS IN NEXT PR
// The minimum resources specification is only available for kubernetes
minCpuAllocationMilliCpus: cpuAllocationMillicpus,
minMemoryAllocationMegabytes: memoryAllocationMegabytes,
}
}

Expand Down Expand Up @@ -93,3 +102,13 @@ func (serviceConfig *ServiceConfig) GetMemoryAllocationMegabytes() uint64 {
func (serviceConfig *ServiceConfig) GetPrivateIPAddrPlaceholder() string {
return serviceConfig.privateIPAddrPlaceholder
}

// only available for Kubernetes
func (serviceConfig *ServiceConfig) GetMinCPUAllocationMillicpus() uint64 {
return serviceConfig.minCpuAllocationMilliCpus
}

// only available for Kubernetes
func (serviceConfig *ServiceConfig) GetMinMemoryAllocationMegabytes() uint64 {
return serviceConfig.minMemoryAllocationMegabytes
}