Skip to content

Commit

Permalink
feat: retain pod spec containers resources when sync pod reource
Browse files Browse the repository at this point in the history
  • Loading branch information
wy-lucky committed Jan 19, 2024
1 parent efd5d30 commit 39a8684
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/controllers/sync/dispatch/retain.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,18 @@ func retainContainers(desiredContainers, clusterContainers []interface{}) error
}

func retainContainer(desiredContainer, clusterContainer map[string]interface{}) error {
resources, exists, err := unstructured.NestedFieldNoCopy(clusterContainer, "resources")
if err != nil {
return err
}

Check warning on line 476 in pkg/controllers/sync/dispatch/retain.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/retain.go#L475-L476

Added lines #L475 - L476 were not covered by tests
if exists {
if err = unstructured.SetNestedField(desiredContainer, resources, "resources"); err != nil {
return err
}

Check warning on line 480 in pkg/controllers/sync/dispatch/retain.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/sync/dispatch/retain.go#L479-L480

Added lines #L479 - L480 were not covered by tests
} else {
unstructured.RemoveNestedField(desiredContainer, "resources")
}

if _, _, exists := findServiceAccountVolumeMount(desiredContainer); !exists {
if volumeMnt, idx, exists := findServiceAccountVolumeMount(clusterContainer); exists {
// The logic for retaining service account volume mounts is the same as retaining service account volumes.
Expand Down
69 changes: 69 additions & 0 deletions pkg/controllers/sync/dispatch/retain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ are Copyright 2023 The KubeAdmiral Authors.
package dispatch

import (
"reflect"
"testing"

"github.com/onsi/gomega"
Expand Down Expand Up @@ -201,3 +202,71 @@ func TestMergeStringMaps(t *testing.T) {
})
}
}

func Test_retainContainer(t *testing.T) {
type args struct {
desiredContainer map[string]interface{}
clusterContainer map[string]interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "nil retain test",
args: args{
desiredContainer: map[string]interface{}{
"name": "container-1",
"resources": map[string]interface{}{
"cpu": "500m",
"memory": "512Mi",
}},

Check failure on line 224 in pkg/controllers/sync/dispatch/retain_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
clusterContainer: map[string]interface{}{
"name": "container-1",
},
},
},
{
name: "empty retain test",
args: args{
desiredContainer: map[string]interface{}{
"name": "container-1",
"resources": map[string]interface{}{
"cpu": "500m",
"memory": "512Mi",
}},

Check failure on line 238 in pkg/controllers/sync/dispatch/retain_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
clusterContainer: map[string]interface{}{
"name": "container-1",
"resources": map[string]interface{}{}},

Check failure on line 241 in pkg/controllers/sync/dispatch/retain_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
},
},
{
name: "normal retain test",
args: args{
desiredContainer: map[string]interface{}{
"name": "container-1",
"resources": map[string]interface{}{
"cpu": "500m",
"memory": "512Mi",
}},

Check failure on line 252 in pkg/controllers/sync/dispatch/retain_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
clusterContainer: map[string]interface{}{
"name": "container-1",
"resources": map[string]interface{}{
"cpu": "100m",
"memory": "100Mi",
}},

Check failure on line 258 in pkg/controllers/sync/dispatch/retain_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := retainContainer(tt.args.desiredContainer, tt.args.clusterContainer); (err != nil) != tt.wantErr {
t.Errorf("retainContainer() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(tt.args.desiredContainer, tt.args.clusterContainer) {
t.Errorf("retainContainer did not retain the resources field correctly")
}
})
}
}

0 comments on commit 39a8684

Please sign in to comment.