Skip to content

Commit

Permalink
[BUGFIX]Fix thinruntime resource overridden (fluid-cloudnative#2548)
Browse files Browse the repository at this point in the history
* Fix thinruntime fuse resource overridden

Signed-off-by: dongyun.xzh <[email protected]>

* Fix thinruntime worker resource overridden

Signed-off-by: dongyun.xzh <[email protected]>

---------

Signed-off-by: dongyun.xzh <[email protected]>
  • Loading branch information
TrafalgarZZZ authored Feb 1, 2023
1 parent c7b32e6 commit 157865c
Show file tree
Hide file tree
Showing 56 changed files with 6,002 additions and 1,660 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ require (
github.com/go-logr/logr v1.2.0
github.com/golang/glog v1.0.0
github.com/golang/mock v1.5.0
github.com/google/go-cmp v0.5.5
github.com/kubernetes-csi/drivers v1.0.2
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/pkg/errors v0.9.1
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.19.1
golang.org/x/net v0.0.0-20210825183410-e898025ed96a
google.golang.org/grpc v1.40.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.23.0
k8s.io/apimachinery v0.24.0
k8s.io/client-go v0.23.0
Expand Down Expand Up @@ -104,7 +106,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/google/uuid v1.1.2 // indirect
Expand All @@ -122,6 +123,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.28.0 // indirect
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,18 @@ github.com/storageos/go-api v2.2.0+incompatible/go.mod h1:ZrLn+e0ZuF3Y65PNF6dIwb
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down Expand Up @@ -1119,8 +1124,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
37 changes: 33 additions & 4 deletions pkg/ddc/thin/transform_fuse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
"github.com/fluid-cloudnative/fluid/pkg/utils/testutil"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
Expand Down Expand Up @@ -293,6 +296,14 @@ func TestThinEngine_transformFuse(t1 *testing.T) {
Image: "test",
ImageTag: "v1",
ImagePullPolicy: "Always",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
// Should be inherited
corev1.ResourceCPU: resource.MustParse("100m"),
// Should be overridden
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
},
Env: []corev1.EnvVar{{
Name: "a",
Value: "b",
Expand Down Expand Up @@ -324,6 +335,15 @@ func TestThinEngine_transformFuse(t1 *testing.T) {
Spec: datav1alpha1.ThinRuntimeSpec{
ThinRuntimeProfileName: "test",
Fuse: datav1alpha1.ThinFuseSpec{
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("200m"),
corev1.ResourceMemory: resource.MustParse("4Gi"),
},
},
Env: []corev1.EnvVar{{
Name: "b",
ValueFrom: &corev1.EnvVarSource{
Expand Down Expand Up @@ -386,8 +406,14 @@ func TestThinEngine_transformFuse(t1 *testing.T) {
ImagePullPolicy: "Always",
TargetPath: "/thin/fluid/test/thin-fuse",
Resources: common.Resources{
Requests: map[corev1.ResourceName]string{},
Limits: map[corev1.ResourceName]string{},
Requests: map[corev1.ResourceName]string{
corev1.ResourceCPU: "100m",
corev1.ResourceMemory: "1Gi",
},
Limits: map[corev1.ResourceName]string{
corev1.ResourceCPU: "200m",
corev1.ResourceMemory: "4Gi",
},
},
HostNetwork: true,
Envs: []corev1.EnvVar{{
Expand Down Expand Up @@ -467,8 +493,11 @@ func TestThinEngine_transformFuse(t1 *testing.T) {
if err := t.transformFuse(runtime, profile, dataset, value); err != nil {
t1.Errorf("transformFuse() error = %v", err)
}
if !reflect.DeepEqual(value.Fuse.ConfigValue, wantValue.Fuse.ConfigValue) {
t1.Errorf("transformFuse() \ngot = %v, \nwant = %v", value.Fuse, wantValue.Fuse)

if !testutil.DeepEqualIgnoringSliceOrder(t1, value.Fuse, wantValue.Fuse) {
valueYaml, _ := yaml.Marshal(value.Fuse)
wantYaml, _ := yaml.Marshal(wantValue.Fuse)
t1.Errorf("transformFuse() \ngot = %v, \nwant = %v", string(valueYaml), string(wantYaml))
}
})
}
44 changes: 22 additions & 22 deletions pkg/ddc/thin/transform_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,60 @@ import (
)

func (t *ThinEngine) transformResourcesForWorker(resources corev1.ResourceRequirements, value *ThinValue) {
value.Worker.Resources = common.Resources{
Requests: common.ResourceList{},
Limits: common.ResourceList{},
if value.Worker.Resources.Requests == nil {
value.Worker.Resources.Requests = common.ResourceList{}
}

if value.Worker.Resources.Limits == nil {
value.Worker.Resources.Limits = common.ResourceList{}
}

if resources.Limits != nil {
t.Log.Info("setting worker Resources limit")
if resources.Limits.Cpu() != nil {
quantity := resources.Limits[corev1.ResourceCPU]
if quantity, ok := resources.Limits[corev1.ResourceCPU]; ok {
value.Worker.Resources.Limits[corev1.ResourceCPU] = quantity.String()
}
if resources.Limits.Memory() != nil {
quantity := resources.Limits[corev1.ResourceMemory]
if quantity, ok := resources.Limits[corev1.ResourceMemory]; ok {
value.Worker.Resources.Limits[corev1.ResourceMemory] = quantity.String()
}
}

if resources.Requests != nil {
t.Log.Info("setting worker Resources request")
if resources.Requests.Cpu() != nil {
quantity := resources.Requests[corev1.ResourceCPU]
if quantity, ok := resources.Requests[corev1.ResourceCPU]; ok {
value.Worker.Resources.Requests[corev1.ResourceCPU] = quantity.String()
}
if resources.Requests.Memory() != nil {
quantity := resources.Requests[corev1.ResourceMemory]
if quantity, ok := resources.Requests[corev1.ResourceMemory]; ok {
value.Worker.Resources.Requests[corev1.ResourceMemory] = quantity.String()
}
}
}

func (t *ThinEngine) transformResourcesForFuse(resources corev1.ResourceRequirements, value *ThinValue) {
value.Fuse.Resources = common.Resources{
Requests: common.ResourceList{},
Limits: common.ResourceList{},
if value.Fuse.Resources.Requests == nil {
value.Fuse.Resources.Requests = common.ResourceList{}
}

if value.Fuse.Resources.Limits == nil {
value.Fuse.Resources.Limits = common.ResourceList{}
}

if resources.Limits != nil {
t.Log.Info("setting fuse Resources limit")
if resources.Limits.Cpu() != nil {
quantity := resources.Limits[corev1.ResourceCPU]
if quantity, ok := resources.Limits[corev1.ResourceCPU]; ok {
value.Fuse.Resources.Limits[corev1.ResourceCPU] = quantity.String()
}
if resources.Limits.Memory() != nil {
quantity := resources.Limits[corev1.ResourceMemory]
if quantity, ok := resources.Limits[corev1.ResourceMemory]; ok {
value.Fuse.Resources.Limits[corev1.ResourceMemory] = quantity.String()
}
}

if resources.Requests != nil {
t.Log.Info("setting fuse Resources request")
if resources.Requests.Cpu() != nil {
quantity := resources.Requests[corev1.ResourceCPU]
if quantity, ok := resources.Requests[corev1.ResourceCPU]; ok {
value.Fuse.Resources.Requests[corev1.ResourceCPU] = quantity.String()
}
if resources.Requests.Memory() != nil {
quantity := resources.Requests[corev1.ResourceMemory]
if quantity, ok := resources.Requests[corev1.ResourceMemory]; ok {
value.Fuse.Resources.Requests[corev1.ResourceMemory] = quantity.String()
}
}
Expand Down
36 changes: 31 additions & 5 deletions pkg/ddc/thin/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package thin

import (
"reflect"
"testing"

datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
"github.com/fluid-cloudnative/fluid/pkg/utils/testutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"testing"
)

func TestThinEngine_transformTolerations(t *testing.T) {
Expand Down Expand Up @@ -244,6 +247,14 @@ func TestThinEngine_transformWorkers(t1 *testing.T) {
Image: "test",
ImageTag: "v1",
ImagePullPolicy: "Always",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
// should be inherited
corev1.ResourceCPU: resource.MustParse("100m"),
// should be overridden
corev1.ResourceMemory: resource.MustParse("2Gi"),
},
},
Env: []corev1.EnvVar{{
Name: "a",
Value: "b",
Expand Down Expand Up @@ -276,6 +287,15 @@ func TestThinEngine_transformWorkers(t1 *testing.T) {
ThinRuntimeProfileName: "test",
Worker: datav1alpha1.ThinCompTemplateSpec{
Replicas: 1,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("200m"),
corev1.ResourceMemory: resource.MustParse("4Gi"),
},
},
Env: []corev1.EnvVar{{
Name: "b",
ValueFrom: &corev1.EnvVarSource{
Expand Down Expand Up @@ -328,8 +348,14 @@ func TestThinEngine_transformWorkers(t1 *testing.T) {
ImageTag: "v1",
ImagePullPolicy: "Always",
Resources: common.Resources{
Requests: map[corev1.ResourceName]string{},
Limits: map[corev1.ResourceName]string{},
Requests: map[corev1.ResourceName]string{
corev1.ResourceCPU: "100m",
corev1.ResourceMemory: "1Gi",
},
Limits: map[corev1.ResourceName]string{
corev1.ResourceCPU: "200m",
corev1.ResourceMemory: "4Gi",
},
},
HostNetwork: true,
Envs: []corev1.EnvVar{{
Expand Down Expand Up @@ -400,7 +426,7 @@ func TestThinEngine_transformWorkers(t1 *testing.T) {
if err := t.transformWorkers(runtime, profile, value); err != nil {
t1.Errorf("transformWorkers() error = %v", err)
}
if !reflect.DeepEqual(value, wantValue) {
if !testutil.DeepEqualIgnoringSliceOrder(t1, value, wantValue) {
t1.Errorf("parseFromProfile() got = %v, want = %v", value, wantValue)
}
})
Expand Down
43 changes: 43 additions & 0 deletions pkg/utils/testutil/deepequal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package testutil

import (
"reflect"

"github.com/stretchr/testify/assert"
)

// DeepEqualIgnoringSliceOrder is much like reflect.DeepEqual but ignores order of slices
// in any structs. This is a function only used for unit tests since many objects in Kubernetes
// should be considered same ignoring the slice order(e.g. VolumeMounts, Volumes, EnvVar, etc.)
// NOTE: The func cannot handle recursive composite types like [][]interface, map[X][]interface, etc.
func DeepEqualIgnoringSliceOrder(t assert.TestingT, x interface{}, y interface{}) bool {
if x == nil || y == nil {
return x == y
}
v1 := reflect.ValueOf(x)
v2 := reflect.ValueOf(y)
if v1.Type() != v2.Type() {
return false
}

switch v1.Kind() {
case reflect.Array:
return assert.ElementsMatch(t, v1.Interface(), v2.Interface())
case reflect.Slice:
return assert.ElementsMatch(t, v1.Interface(), v2.Interface())
case reflect.Ptr:
if v1.Pointer() == v2.Pointer() {
return true
}
return DeepEqualIgnoringSliceOrder(t, v1.Elem().Interface(), v2.Elem().Interface())
case reflect.Struct:
for i, n := 0, v1.NumField(); i < n; i++ {
if !DeepEqualIgnoringSliceOrder(t, v1.Field(i).Interface(), v2.Field(i).Interface()) {
return false
}
}
return true
default:
return reflect.DeepEqual(x, y)
}
}
30 changes: 12 additions & 18 deletions vendor/github.com/agiledragon/gomonkey/v2/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 157865c

Please sign in to comment.