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

Consistently print errors to stderr and add newlines to cordon #68656

Merged
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
18 changes: 9 additions & 9 deletions pkg/kubectl/cmd/drain/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,24 +728,24 @@ func (o *DrainOptions) RunCordonOrUncordon(desired bool) error {
if nodeInfo.Mapping.GroupVersionKind.Kind == "Node" {
obj, err := scheme.Scheme.ConvertToVersion(nodeInfo.Object, nodeInfo.Mapping.GroupVersionKind.GroupVersion())
if err != nil {
fmt.Printf("error: unable to %s node %q: %v", cordonOrUncordon, nodeInfo.Name, err)
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: %v\n", cordonOrUncordon, nodeInfo.Name, err)
continue
}
oldData, err := json.Marshal(obj)
if err != nil {
fmt.Printf("error: unable to %s node %q: %v", cordonOrUncordon, nodeInfo.Name, err)
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: %v\n", cordonOrUncordon, nodeInfo.Name, err)
continue
}
node, ok := obj.(*corev1.Node)
if !ok {
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: unexpected Type%T, expected Node", cordonOrUncordon, nodeInfo.Name, obj)
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: unexpected Type%T, expected Node\n", cordonOrUncordon, nodeInfo.Name, obj)
continue
}
unsched := node.Spec.Unschedulable
if unsched == desired {
printObj, err := o.ToPrinter(already(desired))
if err != nil {
fmt.Printf("error: %v", err)
fmt.Fprintf(o.ErrOut, "error: %v\n", err)
continue
}
printObj(cmdutil.AsDefaultVersionedOrOriginal(nodeInfo.Object, nodeInfo.Mapping), o.Out)
Expand All @@ -755,31 +755,31 @@ func (o *DrainOptions) RunCordonOrUncordon(desired bool) error {
node.Spec.Unschedulable = desired
newData, err := json.Marshal(obj)
if err != nil {
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: %v", cordonOrUncordon, nodeInfo.Name, err)
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: %v\n", cordonOrUncordon, nodeInfo.Name, err)
continue
}
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, obj)
if err != nil {
fmt.Printf("error: unable to %s node %q: %v", cordonOrUncordon, nodeInfo.Name, err)
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: %v\n", cordonOrUncordon, nodeInfo.Name, err)
continue
}
_, err = helper.Patch(o.Namespace, nodeInfo.Name, types.StrategicMergePatchType, patchBytes, nil)
if err != nil {
fmt.Printf("error: unable to %s node %q: %v", cordonOrUncordon, nodeInfo.Name, err)
fmt.Fprintf(o.ErrOut, "error: unable to %s node %q: %v\n", cordonOrUncordon, nodeInfo.Name, err)
continue
}
}
printObj, err := o.ToPrinter(changed(desired))
if err != nil {
fmt.Fprintf(o.ErrOut, "%v", err)
fmt.Fprintf(o.ErrOut, "%v\n", err)
continue
}
printObj(cmdutil.AsDefaultVersionedOrOriginal(nodeInfo.Object, nodeInfo.Mapping), o.Out)
}
} else {
printObj, err := o.ToPrinter("skipped")
if err != nil {
fmt.Fprintf(o.ErrOut, "%v", err)
fmt.Fprintf(o.ErrOut, "%v\n", err)
continue
}
printObj(cmdutil.AsDefaultVersionedOrOriginal(nodeInfo.Object, nodeInfo.Mapping), o.Out)
Expand Down
7 changes: 4 additions & 3 deletions pkg/kubectl/cmd/drain/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func TestDrain(t *testing.T) {
pods: []corev1.Pod{ds_pod_with_emptyDir},
rcs: []corev1.ReplicationController{rc},
args: []string{"node", "--ignore-daemonsets"},
expectWarning: "WARNING: Ignoring DaemonSet-managed pods: bar\n",
expectWarning: "WARNING: Ignoring DaemonSet-managed pods: bar",
expectFatal: false,
expectDelete: false,
},
Expand Down Expand Up @@ -855,8 +855,9 @@ func TestDrain(t *testing.T) {
t.Fatalf("%s: expected warning, but found no stderr output", test.description)
}

if errBuf.String() != test.expectWarning {
t.Fatalf("%s: actual warning message did not match expected warning message.\n Expecting: %s\n Got: %s", test.description, test.expectWarning, errBuf.String())
// Mac and Bazel on Linux behave differently when returning newlines
if a, e := errBuf.String(), test.expectWarning; !strings.Contains(a, e) {
t.Fatalf("%s: actual warning message did not match expected warning message.\n Expecting:\n%v\n Got:\n%v", test.description, e, a)
}
}
})
Expand Down