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

implement GoMemLimitModificationEnabled feature flag #387

Merged
merged 1 commit into from
Mar 21, 2024
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
7 changes: 2 additions & 5 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ const (
VerticalScalingBasedOnPreferredMaxReplicas FeatureFlag = "VerticalScalingBasedOnPreferredMaxReplicas"

// Stage: alpha (default: disabled)
// Description: Enable the feature to modify GOMAXPROCS/GOMEMLIMIT based on the resource requests in the Pod mutating webhook.
// Tracked at:
// - https://github.com/mercari/tortoise/issues/319
// - https://github.com/mercari/tortoise/issues/320
GolangEnvModification FeatureFlag = "GolangEnvModification"
// Description: Enable the feature to modify GOMEMLIMIT based on the memory request in the Pod mutating webhook.
GoMemLimitModificationEnabled FeatureFlag = "GoMemLimitModificationEnabled"
)

func Contains(flags []FeatureFlag, flag FeatureFlag) bool {
Expand Down
25 changes: 13 additions & 12 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (

type Service struct {
// For example, if it's 3 and Pod's resource request is 100m, the limit will be changed to 300m.
resourceLimitMultiplier map[string]int64
minimumCPULimit resource.Quantity
controllerFetcher controllerfetcher.ControllerFetcher
golangEnvModificationEnabled bool
resourceLimitMultiplier map[string]int64
minimumCPULimit resource.Quantity
controllerFetcher controllerfetcher.ControllerFetcher
goMemLimitModificationEnabled bool
}

func New(
Expand All @@ -35,10 +35,10 @@ func New(
}
minCPULim := resource.MustParse(minimumCPULimit)
return &Service{
resourceLimitMultiplier: resourceLimitMultiplier,
minimumCPULimit: minCPULim,
controllerFetcher: cf,
golangEnvModificationEnabled: features.Contains(featureFlags, features.GolangEnvModification),
resourceLimitMultiplier: resourceLimitMultiplier,
minimumCPULimit: minCPULim,
controllerFetcher: cf,
goMemLimitModificationEnabled: features.Contains(featureFlags, features.GoMemLimitModificationEnabled),
}, nil
}

Expand Down Expand Up @@ -102,10 +102,6 @@ func (s *Service) ModifyPodResource(pod *v1.Pod, t *v1beta3.Tortoise) {
}
}

if !s.golangEnvModificationEnabled {
return
}

// Update GOMEMLIMIT and GOMAXPROCS
for i, container := range pod.Spec.Containers {
for j, env := range container.Env {
Expand Down Expand Up @@ -134,6 +130,11 @@ func (s *Service) ModifyPodResource(pod *v1.Pod, t *v1beta3.Tortoise) {

}

if !s.goMemLimitModificationEnabled {
// don't modify GOMEMLIMIT
continue
}

if env.Name == "GOMEMLIMIT" {
changeRatio, ok := requestChangeRatio[containerNameAndResource{
containerName: container.Name,
Expand Down
25 changes: 11 additions & 14 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func TestService_ModifyPodResource(t *testing.T) {
{
name: "Tortoise is Auto; GOMEMLIMIT and GOMAXPROCS are updated based on the recommendation",
fields: fields{
featureFlags: []features.FeatureFlag{features.GolangEnvModification},
featureFlags: []features.FeatureFlag{features.GoMemLimitModificationEnabled},
},
args: args{
pod: &v1.Pod{
Expand Down Expand Up @@ -753,10 +753,7 @@ func TestService_ModifyPodResource(t *testing.T) {
},
},
{
name: "Tortoise is Auto; GOMEMLIMIT and GOMAXPROCS are ignored when no feature flag is enabled",
fields: fields{
featureFlags: []features.FeatureFlag{},
},
name: "Tortoise is Auto; GOMEMLIMIT is ignored if no feature flag",
args: args{
pod: &v1.Pod{
Spec: v1.PodSpec{
Expand All @@ -770,7 +767,7 @@ func TestService_ModifyPodResource(t *testing.T) {
},
{
Name: "GOMEMLIMIT",
Value: "100Mi",
Value: "100MiB",
},
},
Resources: v1.ResourceRequirements{
Expand All @@ -793,7 +790,7 @@ func TestService_ModifyPodResource(t *testing.T) {
},
{
Name: "GOMEMLIMIT",
Value: "100Mi",
Value: "100MiB",
},
},
Resources: v1.ResourceRequirements{
Expand Down Expand Up @@ -829,7 +826,7 @@ func TestService_ModifyPodResource(t *testing.T) {
ContainerName: "istio-proxy",
Resource: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("50m"),
v1.ResourceMemory: resource.MustParse("200Mi"),
v1.ResourceMemory: resource.MustParse("2000Mi"),
},
},
},
Expand All @@ -845,11 +842,11 @@ func TestService_ModifyPodResource(t *testing.T) {
Env: []v1.EnvVar{
{
Name: "GOMAXPROCS",
Value: "1",
Value: "2",
},
{
Name: "GOMEMLIMIT",
Value: "100Mi",
Value: "100MiB",
},
},
Resources: v1.ResourceRequirements{
Expand All @@ -868,21 +865,21 @@ func TestService_ModifyPodResource(t *testing.T) {
Env: []v1.EnvVar{
{
Name: "GOMAXPROCS",
Value: "1",
Value: "1", // It wants to be 0.5, but GOMAXPROCS should be an integer. So, we ceil it to 1.
},
{
Name: "GOMEMLIMIT",
Value: "100Mi",
Value: "100MiB",
},
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("50m"),
v1.ResourceMemory: resource.MustParse("200Mi"),
v1.ResourceMemory: resource.MustParse("2000Mi"),
},
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("200m"),
v1.ResourceMemory: resource.MustParse("400Mi"),
v1.ResourceMemory: resource.MustParse("4000Mi"),
},
},
},
Expand Down
Loading