Merge pull request #119313 from chendave/migrate_config
kubeadm: Support `kubeadm config migrate` for ResetConfiguration
This commit is contained in:
		@@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user