mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 04:52:08 +00:00
kubeadm: restore and generalize the TestMigrateOldConfig test
The test required two APIs to be available to test for migration. Keep it simple and use a variable "gv" on top of the function body to easily swap the version to be tested once an old API is deleted. e.g. currently v1beta3 is the "old" API, v1beta4 is the "new" one. Ultimately, this test only makes sure that the expected kinds are available post migration.
This commit is contained in:
parent
c78204dc06
commit
fd15c6791a
@ -21,13 +21,16 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/lithammer/dedent"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/util/version"
|
"k8s.io/apimachinery/pkg/util/version"
|
||||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||||
|
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
kubeadmapiv1old "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||||
|
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
|
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const KubeadmGroupName = "kubeadm.k8s.io"
|
const KubeadmGroupName = "kubeadm.k8s.io"
|
||||||
@ -213,209 +216,228 @@ func TestVerifyAPIServerBindAddress(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: re-include TestMigrateOldConfigFromFile once a new API version is added after v1beta3.
|
// NOTE: do not delete this test once an older API is removed and there is only one API left.
|
||||||
// see <link-to-commit-foo> of how this unit test function
|
// Update the inline "gv" and "gvExperimental" variables, to have the GroupVersion String of
|
||||||
// looked before it was removed with the removal of v1beta2.
|
// the API to be tested. If there are no experimental APIs make "gvExperimental" point to
|
||||||
// func TestMigrateOldConfigFromFile(t *testing.T) {
|
// an non-experimental API.
|
||||||
// tests := []struct {
|
func TestMigrateOldConfig(t *testing.T) {
|
||||||
// desc string
|
var (
|
||||||
// oldCfg string
|
gv = kubeadmapiv1old.SchemeGroupVersion.String()
|
||||||
// expectedKinds []string
|
gvExperimental = kubeadmapiv1.SchemeGroupVersion.String()
|
||||||
// expectErr bool
|
)
|
||||||
// }{
|
tests := []struct {
|
||||||
// {
|
desc string
|
||||||
// desc: "empty file produces empty result",
|
oldCfg string
|
||||||
// oldCfg: "",
|
expectedKinds []string
|
||||||
// expectErr: false,
|
expectErr bool
|
||||||
// },
|
allowExperimental bool
|
||||||
// {
|
}{
|
||||||
// desc: "bad config produces error",
|
{
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
desc: "empty file produces empty result",
|
||||||
// apiVersion: %s
|
oldCfg: "",
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
expectErr: false,
|
||||||
// expectErr: true,
|
},
|
||||||
// },
|
{
|
||||||
// {
|
desc: "bad config produces error",
|
||||||
// desc: "InitConfiguration only gets migrated",
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
apiVersion: %s
|
||||||
// apiVersion: %s
|
`, gv)),
|
||||||
// kind: InitConfiguration
|
expectErr: true,
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
},
|
||||||
// expectedKinds: []string{
|
{
|
||||||
// constants.InitConfigurationKind,
|
desc: "InitConfiguration only gets migrated",
|
||||||
// constants.ClusterConfigurationKind,
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// },
|
apiVersion: %s
|
||||||
// expectErr: false,
|
kind: InitConfiguration
|
||||||
// },
|
`, gv)),
|
||||||
// {
|
expectedKinds: []string{
|
||||||
// desc: "ClusterConfiguration only gets migrated",
|
constants.InitConfigurationKind,
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.ClusterConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: ClusterConfiguration
|
expectErr: false,
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
},
|
||||||
// expectedKinds: []string{
|
{
|
||||||
// constants.InitConfigurationKind,
|
desc: "ClusterConfiguration only gets migrated",
|
||||||
// constants.ClusterConfigurationKind,
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// },
|
apiVersion: %s
|
||||||
// expectErr: false,
|
kind: ClusterConfiguration
|
||||||
// },
|
`, gv)),
|
||||||
// {
|
expectedKinds: []string{
|
||||||
// desc: "JoinConfiguration only gets migrated",
|
constants.InitConfigurationKind,
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.ClusterConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: JoinConfiguration
|
expectErr: false,
|
||||||
// discovery:
|
},
|
||||||
// bootstrapToken:
|
{
|
||||||
// token: abcdef.0123456789abcdef
|
desc: "JoinConfiguration only gets migrated",
|
||||||
// apiServerEndpoint: kube-apiserver:6443
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// unsafeSkipCAVerification: true
|
apiVersion: %s
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
kind: JoinConfiguration
|
||||||
// expectedKinds: []string{
|
discovery:
|
||||||
// constants.JoinConfigurationKind,
|
bootstrapToken:
|
||||||
// },
|
token: abcdef.0123456789abcdef
|
||||||
// expectErr: false,
|
apiServerEndpoint: kube-apiserver:6443
|
||||||
// },
|
unsafeSkipCAVerification: true
|
||||||
// {
|
`, gv)),
|
||||||
// desc: "Init + Cluster Configurations are migrated",
|
expectedKinds: []string{
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.JoinConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: InitConfiguration
|
expectErr: false,
|
||||||
// ---
|
},
|
||||||
// apiVersion: %[1]s
|
{
|
||||||
// kind: ClusterConfiguration
|
desc: "Init + Cluster Configurations are migrated",
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// expectedKinds: []string{
|
apiVersion: %s
|
||||||
// constants.InitConfigurationKind,
|
kind: InitConfiguration
|
||||||
// constants.ClusterConfigurationKind,
|
---
|
||||||
// },
|
apiVersion: %[1]s
|
||||||
// expectErr: false,
|
kind: ClusterConfiguration
|
||||||
// },
|
`, gv)),
|
||||||
// {
|
expectedKinds: []string{
|
||||||
// desc: "Init + Join Configurations are migrated",
|
constants.InitConfigurationKind,
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.ClusterConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: InitConfiguration
|
expectErr: false,
|
||||||
// ---
|
},
|
||||||
// apiVersion: %[1]s
|
{
|
||||||
// kind: JoinConfiguration
|
desc: "Init + Join Configurations are migrated",
|
||||||
// discovery:
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// bootstrapToken:
|
apiVersion: %s
|
||||||
// token: abcdef.0123456789abcdef
|
kind: InitConfiguration
|
||||||
// apiServerEndpoint: kube-apiserver:6443
|
---
|
||||||
// unsafeSkipCAVerification: true
|
apiVersion: %[1]s
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
kind: JoinConfiguration
|
||||||
// expectedKinds: []string{
|
discovery:
|
||||||
// constants.InitConfigurationKind,
|
bootstrapToken:
|
||||||
// constants.ClusterConfigurationKind,
|
token: abcdef.0123456789abcdef
|
||||||
// constants.JoinConfigurationKind,
|
apiServerEndpoint: kube-apiserver:6443
|
||||||
// },
|
unsafeSkipCAVerification: true
|
||||||
// expectErr: false,
|
`, gv)),
|
||||||
// },
|
expectedKinds: []string{
|
||||||
// {
|
constants.InitConfigurationKind,
|
||||||
// desc: "Cluster + Join Configurations are migrated",
|
constants.ClusterConfigurationKind,
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.JoinConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: ClusterConfiguration
|
expectErr: false,
|
||||||
// ---
|
},
|
||||||
// apiVersion: %[1]s
|
{
|
||||||
// kind: JoinConfiguration
|
desc: "Cluster + Join Configurations are migrated",
|
||||||
// discovery:
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// bootstrapToken:
|
apiVersion: %s
|
||||||
// token: abcdef.0123456789abcdef
|
kind: ClusterConfiguration
|
||||||
// apiServerEndpoint: kube-apiserver:6443
|
---
|
||||||
// unsafeSkipCAVerification: true
|
apiVersion: %[1]s
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
kind: JoinConfiguration
|
||||||
// expectedKinds: []string{
|
discovery:
|
||||||
// constants.InitConfigurationKind,
|
bootstrapToken:
|
||||||
// constants.ClusterConfigurationKind,
|
token: abcdef.0123456789abcdef
|
||||||
// constants.JoinConfigurationKind,
|
apiServerEndpoint: kube-apiserver:6443
|
||||||
// },
|
unsafeSkipCAVerification: true
|
||||||
// expectErr: false,
|
`, gv)),
|
||||||
// },
|
expectedKinds: []string{
|
||||||
// {
|
constants.InitConfigurationKind,
|
||||||
// desc: "Init + Cluster + Join Configurations are migrated",
|
constants.ClusterConfigurationKind,
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.JoinConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: InitConfiguration
|
expectErr: false,
|
||||||
// ---
|
},
|
||||||
// apiVersion: %[1]s
|
{
|
||||||
// kind: ClusterConfiguration
|
desc: "Init + Cluster + Join Configurations are migrated",
|
||||||
// ---
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// apiVersion: %[1]s
|
apiVersion: %s
|
||||||
// kind: JoinConfiguration
|
kind: InitConfiguration
|
||||||
// discovery:
|
---
|
||||||
// bootstrapToken:
|
apiVersion: %[1]s
|
||||||
// token: abcdef.0123456789abcdef
|
kind: ClusterConfiguration
|
||||||
// apiServerEndpoint: kube-apiserver:6443
|
---
|
||||||
// unsafeSkipCAVerification: true
|
apiVersion: %[1]s
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
kind: JoinConfiguration
|
||||||
// expectedKinds: []string{
|
discovery:
|
||||||
// constants.InitConfigurationKind,
|
bootstrapToken:
|
||||||
// constants.ClusterConfigurationKind,
|
token: abcdef.0123456789abcdef
|
||||||
// constants.JoinConfigurationKind,
|
apiServerEndpoint: kube-apiserver:6443
|
||||||
// },
|
unsafeSkipCAVerification: true
|
||||||
// expectErr: false,
|
`, gv)),
|
||||||
// },
|
expectedKinds: []string{
|
||||||
// {
|
constants.InitConfigurationKind,
|
||||||
// desc: "component configs are not migrated",
|
constants.ClusterConfigurationKind,
|
||||||
// oldCfg: dedent.Dedent(fmt.Sprintf(`
|
constants.JoinConfigurationKind,
|
||||||
// apiVersion: %s
|
},
|
||||||
// kind: InitConfiguration
|
expectErr: false,
|
||||||
// ---
|
},
|
||||||
// apiVersion: %[1]s
|
{
|
||||||
// kind: ClusterConfiguration
|
desc: "component configs are not migrated",
|
||||||
// ---
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
// apiVersion: %[1]s
|
apiVersion: %s
|
||||||
// kind: JoinConfiguration
|
kind: InitConfiguration
|
||||||
// discovery:
|
---
|
||||||
// bootstrapToken:
|
apiVersion: %[1]s
|
||||||
// token: abcdef.0123456789abcdef
|
kind: ClusterConfiguration
|
||||||
// apiServerEndpoint: kube-apiserver:6443
|
---
|
||||||
// unsafeSkipCAVerification: true
|
apiVersion: %[1]s
|
||||||
// ---
|
kind: JoinConfiguration
|
||||||
// apiVersion: kubeproxy.config.k8s.io/v1alpha1
|
discovery:
|
||||||
// kind: KubeProxyConfiguration
|
bootstrapToken:
|
||||||
// ---
|
token: abcdef.0123456789abcdef
|
||||||
// apiVersion: kubelet.config.k8s.io/v1beta1
|
apiServerEndpoint: kube-apiserver:6443
|
||||||
// kind: KubeletConfiguration
|
unsafeSkipCAVerification: true
|
||||||
// `, kubeadmapiv1old.SchemeGroupVersion.String())),
|
---
|
||||||
// expectedKinds: []string{
|
apiVersion: kubeproxy.config.k8s.io/v1alpha1
|
||||||
// constants.InitConfigurationKind,
|
kind: KubeProxyConfiguration
|
||||||
// constants.ClusterConfigurationKind,
|
---
|
||||||
// constants.JoinConfigurationKind,
|
apiVersion: kubelet.config.k8s.io/v1beta1
|
||||||
// },
|
kind: KubeletConfiguration
|
||||||
// expectErr: false,
|
`, gv)),
|
||||||
// },
|
expectedKinds: []string{
|
||||||
// }
|
constants.InitConfigurationKind,
|
||||||
|
constants.ClusterConfigurationKind,
|
||||||
|
constants.JoinConfigurationKind,
|
||||||
|
},
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "ClusterConfiguration gets migrated from experimental API",
|
||||||
|
oldCfg: dedent.Dedent(fmt.Sprintf(`
|
||||||
|
apiVersion: %s
|
||||||
|
kind: ClusterConfiguration
|
||||||
|
`, gvExperimental)),
|
||||||
|
expectedKinds: []string{
|
||||||
|
constants.InitConfigurationKind,
|
||||||
|
constants.ClusterConfigurationKind,
|
||||||
|
},
|
||||||
|
allowExperimental: true,
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// for _, test := range tests {
|
for _, test := range tests {
|
||||||
// t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
// b, err := MigrateOldConfig([]byte(test.oldCfg))
|
b, err := MigrateOldConfig([]byte(test.oldCfg), test.allowExperimental)
|
||||||
// if test.expectErr {
|
if test.expectErr {
|
||||||
// if err == nil {
|
if err == nil {
|
||||||
// t.Fatalf("unexpected success:\n%s", b)
|
t.Fatalf("unexpected success:\n%s", b)
|
||||||
// }
|
}
|
||||||
// } else {
|
} else {
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// t.Fatalf("unexpected failure: %v", err)
|
t.Fatalf("unexpected failure: %v", err)
|
||||||
// }
|
}
|
||||||
// gvks, err := kubeadmutil.GroupVersionKindsFromBytes(b)
|
gvks, err := kubeadmutil.GroupVersionKindsFromBytes(b)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// t.Fatalf("unexpected error returned by GroupVersionKindsFromBytes: %v", err)
|
t.Fatalf("unexpected error returned by GroupVersionKindsFromBytes: %v", err)
|
||||||
// }
|
}
|
||||||
// if len(gvks) != len(test.expectedKinds) {
|
if len(gvks) != len(test.expectedKinds) {
|
||||||
// t.Fatalf("length mismatch between resulting gvks and expected kinds:\n\tlen(gvks)=%d\n\tlen(expectedKinds)=%d",
|
t.Fatalf("length mismatch between resulting gvks and expected kinds:\n\tlen(gvks)=%d\n\tlen(expectedKinds)=%d",
|
||||||
// len(gvks), len(test.expectedKinds))
|
len(gvks), len(test.expectedKinds))
|
||||||
// }
|
}
|
||||||
// for _, expectedKind := range test.expectedKinds {
|
for _, expectedKind := range test.expectedKinds {
|
||||||
// if !kubeadmutil.GroupVersionKindsHasKind(gvks, expectedKind) {
|
if !kubeadmutil.GroupVersionKindsHasKind(gvks, expectedKind) {
|
||||||
// t.Fatalf("migration failed to produce config kind: %s", expectedKind)
|
t.Fatalf("migration failed to produce config kind: %s", expectedKind)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
func TestIsKubeadmPrereleaseVersion(t *testing.T) {
|
func TestIsKubeadmPrereleaseVersion(t *testing.T) {
|
||||||
validVersionInfo := &apimachineryversion.Info{Major: "1", GitVersion: "v1.23.0-alpha.1"}
|
validVersionInfo := &apimachineryversion.Info{Major: "1", GitVersion: "v1.23.0-alpha.1"}
|
||||||
|
Loading…
Reference in New Issue
Block a user