Skip to content

Commit

Permalink
fix set cmd bug caused by MergePatchType (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: hantmac <[email protected]>
  • Loading branch information
hantmac authored Mar 9, 2022
1 parent fb8c560 commit 1d835fb
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
9 changes: 5 additions & 4 deletions pkg/cmd/set/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (

// selectContainers allows one or more containers to be matched against a string or wildcard
func selectContainers(containers []v1.Container, spec string) ([]*v1.Container, []*v1.Container) {
var out []*v1.Container
var skipped []*v1.Container
out := []*v1.Container{}
skipped := []*v1.Container{}
for i, c := range containers {
if selectString(c.Name, spec) {
out = append(out, &containers[i])
Expand All @@ -54,6 +54,7 @@ func selectString(s, spec string) bool {
pos := 0
match := true
parts := strings.Split(spec, "*")
Loop:
for i, part := range parts {
if len(part) == 0 {
continue
Expand All @@ -69,7 +70,7 @@ func selectString(s, spec string) bool {
// last part does not exactly match remaining part of string
case i == (len(parts)-1) && len(s) != (len(part)+next):
match = false
break
break Loop
default:
pos = next
}
Expand Down Expand Up @@ -131,7 +132,7 @@ func findEnv(env []v1.EnvVar, name string) (v1.EnvVar, bool) {
}

func updateEnv(existing []v1.EnvVar, env []v1.EnvVar, remove []string) []v1.EnvVar {
var out []v1.EnvVar
out := []v1.EnvVar{}
covered := sets.NewString(remove...)
for _, e := range existing {
if covered.Has(e.Name) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/set/set_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (o *SetImageOptions) Run() error {
actual, err := resource.
NewHelper(info.Client, info.Mapping).
DryRun(o.DryRunStrategy == cmdutil.DryRunServer).
Patch(info.Namespace, info.Name, types.MergePatchType, patch.Patch, nil)
Patch(info.Namespace, info.Name, types.MergePatchType, patch.After, nil)
if err != nil {
allErrs = append(allErrs, fmt.Errorf("failed to patch image update to pod template: %v", err))
continue
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/set/set_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ func TestSetImageRemoteWithSpecificContainers(t *testing.T) {
if err != nil {
return nil, err
}
assert.Contains(t, string(bytes), `"image":"`+"thingy"+`","name":`+`"nginx"`, fmt.Sprintf("image not updated for %#v", input.object))
assert.NotContains(t, string(bytes), `"image":"`+"thingy"+`","name":`+`"busybox"`, fmt.Sprintf("image updated for %#v", input.object))
assert.Contains(t, string(bytes), `"name":"`+"nginx"+`","image":`+`"thingy"`, fmt.Sprintf("image not updated for %#v", input.object))
assert.NotContains(t, string(bytes), `"name":"`+"busybox"+`","image":`+`"thingy"`, fmt.Sprintf("image updated for %#v", input.object))
return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: objBody(input.object)}, nil
default:
t.Errorf("%s: unexpected request: %s %#v\n%#v", "image", req.Method, req.URL, req)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/set/set_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (o *SetSelectorOptions) RunSelector() error {
actual, err := resource.
NewHelper(info.Client, info.Mapping).
DryRun(o.dryRunStrategy == cmdutil.DryRunServer).
Patch(info.Namespace, info.Name, types.MergePatchType, patch.Patch, nil)
Patch(info.Namespace, info.Name, types.MergePatchType, patch.After, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/set/set_serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (o *SetServiceAccountOptions) Run() error {
actual, err := resource.
NewHelper(info.Client, info.Mapping).
DryRun(o.dryRunStrategy == cmdutil.DryRunServer).
Patch(info.Namespace, info.Name, types.MergePatchType, patch.Patch, nil)
Patch(info.Namespace, info.Name, types.MergePatchType, patch.After, nil)
if err != nil {
patchErrs = append(patchErrs, fmt.Errorf("failed to patch ServiceAccountName %v", err))
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/set/set_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (o *SubjectOptions) Run(fn updateSubjects) error {
actual, err := resource.
NewHelper(info.Client, info.Mapping).
DryRun(o.DryRunStrategy == cmdutil.DryRunServer).
Patch(info.Namespace, info.Name, types.MergePatchType, patch.Patch, nil)
Patch(info.Namespace, info.Name, types.MergePatchType, patch.After, nil)
if err != nil {
allErrs = append(allErrs, fmt.Errorf("failed to patch subjects to rolebinding: %v", err))
continue
Expand Down

0 comments on commit 1d835fb

Please sign in to comment.