Skip to content

Commit

Permalink
fix the format of GOMEMLIMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho committed Feb 27, 2024
1 parent a7fb3c6 commit e7ae599
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
19 changes: 16 additions & 3 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,26 @@ func (s *Service) ModifyPodResource(pod *v1.Pod, t *v1beta3.Tortoise) {
if !ok {
continue
}
oldNum, err := resource.ParseQuantity(env.Value)
val := env.Value
last := val[len(val)-1]
if last >= '0' && last <= '9' {
// OK
} else if last == 'B' {
// It should end with B.
val = val[:len(val)-1]
} else {
// invalid GOMEMLIMIT, skip
continue
}

oldNum, err := resource.ParseQuantity(val)
if err != nil {
// invalid GOMEMLIMIT, skip
continue
}
newNum := int64(float64(oldNum.MilliValue()) * changeRatio)
pod.Spec.Containers[i].Env[j].Value = resource.NewMilliQuantity(newNum, oldNum.Format).String()
// See GOMEMLIMIT's format: https://pkg.go.dev/runtime#hdr-Environment_Variables
newNum := int(float64(oldNum.Value()) * changeRatio)
pod.Spec.Containers[i].Env[j].Value = strconv.Itoa(newNum)
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package pod

import (
"strconv"
"testing"

"github.com/google/go-cmp/cmp"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"

"github.com/mercari/tortoise/api/v1beta3"
"github.com/mercari/tortoise/pkg/features"
Expand Down Expand Up @@ -470,7 +472,7 @@ func TestService_ModifyPodResource(t *testing.T) {
},
{
Name: "GOMEMLIMIT",
Value: "100Mi",
Value: "100MiB",
},
},
Resources: v1.ResourceRequirements{
Expand All @@ -493,7 +495,7 @@ func TestService_ModifyPodResource(t *testing.T) {
},
{
Name: "GOMEMLIMIT",
Value: "100Mi",
Value: "100MiB",
},
},
Resources: v1.ResourceRequirements{
Expand Down Expand Up @@ -528,7 +530,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 @@ -548,7 +550,7 @@ func TestService_ModifyPodResource(t *testing.T) {
},
{
Name: "GOMEMLIMIT",
Value: "200Mi",
Value: strconv.Itoa(int(ptr.To(resource.MustParse("200Mi")).Value())),
},
},
Resources: v1.ResourceRequirements{
Expand All @@ -571,17 +573,17 @@ func TestService_ModifyPodResource(t *testing.T) {
},
{
Name: "GOMEMLIMIT",
Value: "200Mi",
Value: strconv.Itoa(int(ptr.To(resource.MustParse("2000Mi")).Value())),
},
},
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

0 comments on commit e7ae599

Please sign in to comment.