diff --git a/cmd/kubeadm/app/util/config/common.go b/cmd/kubeadm/app/util/config/common.go index 33e341e66d0..064b9afb3fb 100644 --- a/cmd/kubeadm/app/util/config/common.go +++ b/cmd/kubeadm/app/util/config/common.go @@ -289,6 +289,19 @@ func MigrateOldConfig(oldConfig []byte, allowExperimental bool) ([]byte, error) newConfig = append(newConfig, b) } + // Migrate ResetConfiguration if there is any + if kubeadmutil.GroupVersionKindsHasResetConfiguration(gvks...) { + o, err := documentMapToResetConfiguration(gvkmap, true, allowExperimental) + if err != nil { + return []byte{}, err + } + b, err := MarshalKubeadmConfigObject(o, gv) + if err != nil { + return []byte{}, err + } + newConfig = append(newConfig, b) + } + return bytes.Join(newConfig, []byte(constants.YAMLDocumentSeparator)), nil } diff --git a/cmd/kubeadm/app/util/config/common_test.go b/cmd/kubeadm/app/util/config/common_test.go index 8a5ee108cb6..3891d9b5a1a 100644 --- a/cmd/kubeadm/app/util/config/common_test.go +++ b/cmd/kubeadm/app/util/config/common_test.go @@ -425,6 +425,31 @@ func TestMigrateOldConfig(t *testing.T) { allowExperimental: false, expectErr: true, }, + { + name: "ResetConfiguration gets migrated from experimental API", + oldCfg: dedent.Dedent(fmt.Sprintf(` + apiVersion: %s + kind: ResetConfiguration + force: true + cleanupTmpDir: true + criSocket: unix:///var/run/containerd/containerd.sock + certificatesDir: /etc/kubernetes/pki + `, gvExperimental)), + expectedKinds: []string{ + constants.ResetConfigurationKind, + }, + allowExperimental: true, + expectErr: false, + }, + { + name: "ResetConfiguration from experimental API cannot be migrated", + oldCfg: dedent.Dedent(fmt.Sprintf(` + apiVersion: %s + kind: ResetConfiguration + `, gvExperimental)), + allowExperimental: false, + expectErr: true, + }, } for _, test := range tests { diff --git a/cmd/kubeadm/app/util/marshal.go b/cmd/kubeadm/app/util/marshal.go index 33a3c689c3e..6be6b87ade8 100644 --- a/cmd/kubeadm/app/util/marshal.go +++ b/cmd/kubeadm/app/util/marshal.go @@ -158,3 +158,8 @@ func GroupVersionKindsHasInitConfiguration(gvks ...schema.GroupVersionKind) bool func GroupVersionKindsHasJoinConfiguration(gvks ...schema.GroupVersionKind) bool { return GroupVersionKindsHasKind(gvks, constants.JoinConfigurationKind) } + +// GroupVersionKindsHasResetConfiguration returns whether the following gvk slice contains a ResetConfiguration object +func GroupVersionKindsHasResetConfiguration(gvks ...schema.GroupVersionKind) bool { + return GroupVersionKindsHasKind(gvks, constants.ResetConfigurationKind) +} diff --git a/cmd/kubeadm/app/util/marshal_test.go b/cmd/kubeadm/app/util/marshal_test.go index e31bd3b0b44..4cb2aa3f42e 100644 --- a/cmd/kubeadm/app/util/marshal_test.go +++ b/cmd/kubeadm/app/util/marshal_test.go @@ -402,3 +402,38 @@ func TestGroupVersionKindsHasJoinConfiguration(t *testing.T) { }) } } + +func TestGroupVersionKindsHasResetConfiguration(t *testing.T) { + var tests = []struct { + name string + gvks []schema.GroupVersionKind + kind string + expected bool + }{ + { + name: "NoResetConfiguration", + gvks: []schema.GroupVersionKind{ + {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"}, + }, + expected: false, + }, + { + name: "ResetConfigurationFound", + gvks: []schema.GroupVersionKind{ + {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"}, + {Group: "bar.k8s.io", Version: "v2", Kind: "ResetConfiguration"}, + }, + expected: true, + }, + } + + for _, rt := range tests { + t.Run(rt.name, func(t2 *testing.T) { + + actual := GroupVersionKindsHasResetConfiguration(rt.gvks...) + if rt.expected != actual { + t2.Errorf("expected gvks has ResetConfiguration: %t\n\tactual: %t\n", rt.expected, actual) + } + }) + } +}