Skip to content

Commit

Permalink
Merge pull request #107657 from astraw99/fix-label-msg
Browse files Browse the repository at this point in the history
Fix label msg when overwrite flag is set
  • Loading branch information
k8s-ci-robot committed Jan 21, 2022
2 parents c175418 + 83fe357 commit e7e58de
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
8 changes: 4 additions & 4 deletions staging/src/k8s.io/kubectl/pkg/cmd/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (o *LabelOptions) RunLabel() error {
if err != nil {
return err
}
dataChangeMsg = updateDataChangeMsg(oldData, newObj)
dataChangeMsg = updateDataChangeMsg(oldData, newObj, o.overwrite)
outputObj = info.Object
} else {
name, namespace := info.Name, info.Namespace
Expand All @@ -334,7 +334,7 @@ func (o *LabelOptions) RunLabel() error {
if err != nil {
return err
}
dataChangeMsg = updateDataChangeMsg(oldData, newObj)
dataChangeMsg = updateDataChangeMsg(oldData, newObj, o.overwrite)
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newObj)
createdPatch := err == nil
if err != nil {
Expand Down Expand Up @@ -395,11 +395,11 @@ func (o *LabelOptions) RunLabel() error {
})
}

func updateDataChangeMsg(oldObj []byte, newObj []byte) string {
func updateDataChangeMsg(oldObj []byte, newObj []byte, overwrite bool) string {
msg := MsgNotLabeled
if !reflect.DeepEqual(oldObj, newObj) {
msg = MsgLabeled
if len(newObj) < len(oldObj) {
if !overwrite && len(newObj) < len(oldObj) {
msg = MsgUnLabeled
}
}
Expand Down
37 changes: 36 additions & 1 deletion staging/src/k8s.io/kubectl/pkg/cmd/label/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,41 @@ func TestLabelMsg(t *testing.T) {
},
expectMsg: MsgLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"status": "unhealthy"},
},
},
labels: map[string]string{"status": "healthy"},
overwrite: true,
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"status": "healthy",
},
},
},
expectMsg: MsgLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"status": "unhealthy"},
},
},
labels: map[string]string{"status": "healthy"},
overwrite: false,
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"status": "unhealthy",
},
},
},
expectMsg: MsgNotLabeled,
expectErr: true,
},
}

for _, test := range tests {
Expand All @@ -700,7 +735,7 @@ func TestLabelMsg(t *testing.T) {
t.Errorf("unexpected error: %v %v", err, test)
}

dataChangeMsg := updateDataChangeMsg(oldData, newObj)
dataChangeMsg := updateDataChangeMsg(oldData, newObj, test.overwrite)
if dataChangeMsg != test.expectMsg {
t.Errorf("unexpected dataChangeMsg: %v != %v, %v", dataChangeMsg, test.expectMsg, test)
}
Expand Down

0 comments on commit e7e58de

Please sign in to comment.