From 6cfd7724010dc486c6b2195fdde7a0d3ea25de52 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Fri, 10 Apr 2020 23:40:28 +0300 Subject: [PATCH] kubeadm: fix misleading warning for authz modes kubeadm init prints: W0410 23:02:10.119723 13040 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC" Add a new function compareAuthzModes() and a unit test for it. Make sure the warning is printed only if the user modes don't match the defaults. --- .../app/phases/controlplane/manifests.go | 23 +++++++++-- .../app/phases/controlplane/manifests_test.go | 40 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/cmd/kubeadm/app/phases/controlplane/manifests.go b/cmd/kubeadm/app/phases/controlplane/manifests.go index ae9cd77e259..aaf65f2df10 100644 --- a/cmd/kubeadm/app/phases/controlplane/manifests.go +++ b/cmd/kubeadm/app/phases/controlplane/manifests.go @@ -222,16 +222,31 @@ func getAuthzModes(authzModeExtraArgs string) string { // only return the user provided mode if at least one was valid if len(mode) > 0 { - klog.Warningf("the default kube-apiserver authorization-mode is %q; using %q", - strings.Join(defaultMode, ","), - strings.Join(mode, ","), - ) + if !compareAuthzModes(defaultMode, mode) { + klog.Warningf("the default kube-apiserver authorization-mode is %q; using %q", + strings.Join(defaultMode, ","), + strings.Join(mode, ","), + ) + } return strings.Join(mode, ",") } } return strings.Join(defaultMode, ",") } +// compareAuthzModes compares two given authz modes and returns false if they do not match +func compareAuthzModes(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, m := range a { + if m != b[i] { + return false + } + } + return true +} + func isValidAuthzMode(authzMode string) bool { allModes := []string{ kubeadmconstants.ModeNode, diff --git a/cmd/kubeadm/app/phases/controlplane/manifests_test.go b/cmd/kubeadm/app/phases/controlplane/manifests_test.go index 4c7bf2419e1..bb2802b6eb5 100644 --- a/cmd/kubeadm/app/phases/controlplane/manifests_test.go +++ b/cmd/kubeadm/app/phases/controlplane/manifests_test.go @@ -1144,3 +1144,43 @@ func TestIsValidAuthzMode(t *testing.T) { }) } } + +func TestCompareAuthzModes(t *testing.T) { + var tests = []struct { + name string + modesA []string + modesB []string + equal bool + }{ + { + name: "modes match", + modesA: []string{"a", "b", "c"}, + modesB: []string{"a", "b", "c"}, + equal: true, + }, + { + name: "modes order does not match", + modesA: []string{"a", "c", "b"}, + modesB: []string{"a", "b", "c"}, + }, + { + name: "modes do not match; A has less modes", + modesA: []string{"a", "b"}, + modesB: []string{"a", "b", "c"}, + }, + { + name: "modes do not match; B has less modes", + modesA: []string{"a", "b", "c"}, + modesB: []string{"a", "b"}, + }, + } + + for _, rt := range tests { + t.Run(rt.name, func(t *testing.T) { + equal := compareAuthzModes(rt.modesA, rt.modesB) + if equal != rt.equal { + t.Errorf("failed compareAuthzModes:\nexpected:\n%v\nsaw:\n%v", rt.equal, equal) + } + }) + } +}