Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix kubectl unlabel response msg #104372

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions staging/src/k8s.io/kubectl/pkg/cmd/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ import (
"k8s.io/kubectl/pkg/util/templates"
)

const (
MsgNotLabeled = "not labeled"
MsgLabeled = "labeled"
MsgUnLabeled = "unlabeled"
)

// LabelOptions have the data required to perform the label operation
type LabelOptions struct {
// Filename options
Expand Down Expand Up @@ -390,9 +396,12 @@ func (o *LabelOptions) RunLabel() error {
}

func updateDataChangeMsg(oldObj []byte, newObj []byte) string {
msg := "not labeled"
msg := MsgNotLabeled
if !reflect.DeepEqual(oldObj, newObj) {
msg = "labeled"
msg = MsgLabeled
if len(newObj) < len(oldObj) {
astraw99 marked this conversation as resolved.
Show resolved Hide resolved
msg = MsgUnLabeled
}
}
return msg
}
Expand Down
143 changes: 142 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 @@ -24,10 +24,11 @@ import (
"strings"
"testing"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/rest/fake"
Expand Down Expand Up @@ -565,3 +566,143 @@ func TestLabelResourceVersion(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}

func TestLabelMsg(t *testing.T) {
tests := []struct {
obj runtime.Object
overwrite bool
resourceVersion string
labels map[string]string
remove []string
expectObj runtime.Object
expectMsg string
expectErr bool
}{
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{"a": "b"},
expectMsg: MsgNotLabeled,
expectErr: true,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{},
},
labels: map[string]string{"a": "b"},
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
expectMsg: MsgLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{"a": "c"},
overwrite: true,
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "c"},
},
},
expectMsg: MsgLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{"c": "d"},
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b", "c": "d"},
},
},
expectMsg: MsgLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{"c": "d"},
resourceVersion: "2",
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b", "c": "d"},
ResourceVersion: "2",
},
},
expectMsg: MsgLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
},
labels: map[string]string{},
remove: []string{"a"},
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{},
},
},
expectMsg: MsgUnLabeled,
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"a": "b", "c": "d"},
},
},
labels: map[string]string{"e": "f"},
remove: []string{"a"},
expectObj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"c": "d",
"e": "f",
},
},
},
expectMsg: MsgLabeled,
},
}

for _, test := range tests {
oldData, err := json.Marshal(test.obj)
if err != nil {
t.Errorf("unexpected error: %v %v", err, test)
}

err = labelFunc(test.obj, test.overwrite, test.resourceVersion, test.labels, test.remove)
if test.expectErr && err == nil {
t.Errorf("unexpected non-error: %v", test)
continue
}
if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v %v", err, test)
}

newObj, err := json.Marshal(test.obj)
if err != nil {
t.Errorf("unexpected error: %v %v", err, test)
}

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