Merge pull request #125758 from SataQiu/improve-validateSupportedVersion-20240627

kubeadm: improve the error/warning messages of `validateSupportedVersion` to include the checked resource kind
This commit is contained in:
Kubernetes Prow Robot 2024-06-27 07:45:17 -07:00 committed by GitHub
commit 41f21823f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 21 deletions

View File

@ -64,7 +64,7 @@ func MarshalKubeadmConfigObject(obj runtime.Object, gv schema.GroupVersion) ([]b
// validateSupportedVersion checks if the supplied GroupVersion is not on the lists of old unsupported or deprecated GVs.
// If it is, an error is returned.
func validateSupportedVersion(gv schema.GroupVersion, allowDeprecated, allowExperimental bool) error {
func validateSupportedVersion(gvk schema.GroupVersionKind, allowDeprecated, allowExperimental bool) error {
// The support matrix will look something like this now and in the future:
// v1.10 and earlier: v1alpha1
// v1.11: v1alpha1 read-only, writes only v1alpha2 config
@ -91,18 +91,18 @@ func validateSupportedVersion(gv schema.GroupVersion, allowDeprecated, allowExpe
"kubeadm.k8s.io/v1beta3": {},
}
gvString := gv.String()
gvString := gvk.GroupVersion().String()
if useKubeadmVersion := oldKnownAPIVersions[gvString]; useKubeadmVersion != "" {
return errors.Errorf("your configuration file uses an old API spec: %q. Please use kubeadm %s instead and run 'kubeadm config migrate --old-config old.yaml --new-config new.yaml', which will write the new, similar spec using a newer API version.", gv.String(), useKubeadmVersion)
return errors.Errorf("your configuration file uses an old API spec: %q (kind: %q). Please use kubeadm %s instead and run 'kubeadm config migrate --old-config old.yaml --new-config new.yaml', which will write the new, similar spec using a newer API version.", gvString, gvk.Kind, useKubeadmVersion)
}
if _, present := deprecatedAPIVersions[gvString]; present && !allowDeprecated {
klog.Warningf("your configuration file uses a deprecated API spec: %q. Please use 'kubeadm config migrate --old-config old.yaml --new-config new.yaml', which will write the new, similar spec using a newer API version.", gv.String())
klog.Warningf("your configuration file uses a deprecated API spec: %q (kind: %q). Please use 'kubeadm config migrate --old-config old.yaml --new-config new.yaml', which will write the new, similar spec using a newer API version.", gvString, gvk.Kind)
}
if _, present := experimentalAPIVersions[gvString]; present && !allowExperimental {
return errors.Errorf("experimental API spec: %q is not allowed. You can use the --%s flag if the command supports it.", gv, options.AllowExperimentalAPI)
return errors.Errorf("experimental API spec: %q (kind: %q) is not allowed. You can use the --%s flag if the command supports it.", gvString, gvk.Kind, options.AllowExperimentalAPI)
}
return nil
@ -225,7 +225,7 @@ func validateKnownGVKs(gvks []schema.GroupVersionKind) error {
// Skip legacy known GVs so that they don't return errors.
// This makes the function return errors only for GVs that where never known.
if err := validateSupportedVersion(gvk.GroupVersion(), true, true); err != nil {
if err := validateSupportedVersion(gvk, true, true); err != nil {
continue
}

View File

@ -43,69 +43,77 @@ const KubeadmGroupName = "kubeadm.k8s.io"
func TestValidateSupportedVersion(t *testing.T) {
tests := []struct {
gv schema.GroupVersion
gvk schema.GroupVersionKind
allowDeprecated bool
allowExperimental bool
expectedErr bool
}{
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1alpha1",
Kind: "InitConfiguration",
},
expectedErr: true,
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1alpha2",
Kind: "InitConfiguration",
},
expectedErr: true,
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1alpha3",
Kind: "InitConfiguration",
},
expectedErr: true,
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1beta1",
Kind: "InitConfiguration",
},
expectedErr: true,
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1beta2",
Kind: "InitConfiguration",
},
expectedErr: true,
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1beta3",
Kind: "ClusterConfiguration",
},
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: "foo.k8s.io",
Version: "v1",
Kind: "InitConfiguration",
},
},
{
gv: schema.GroupVersion{
gvk: schema.GroupVersionKind{
Group: KubeadmGroupName,
Version: "v1beta4",
Kind: "ResetConfiguration",
},
},
}
for _, rt := range tests {
t.Run(fmt.Sprintf("%s/allowDeprecated:%t", rt.gv, rt.allowDeprecated), func(t *testing.T) {
err := validateSupportedVersion(rt.gv, rt.allowDeprecated, rt.allowExperimental)
t.Run(fmt.Sprintf("%s/allowDeprecated:%t", rt.gvk.GroupVersion(), rt.allowDeprecated), func(t *testing.T) {
err := validateSupportedVersion(rt.gvk, rt.allowDeprecated, rt.allowExperimental)
if rt.expectedErr && err == nil {
t.Error("unexpected success")
} else if !rt.expectedErr && err != nil {

View File

@ -322,7 +322,7 @@ func documentMapToInitConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
fileContent := gvkmap[gvk]
// first, check if this GVK is supported and possibly not deprecated
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
if err := validateSupportedVersion(gvk, allowDeprecated, allowExperimental); err != nil {
return nil, err
}

View File

@ -106,7 +106,7 @@ func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
}
// check if this version is supported and possibly not deprecated
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
if err := validateSupportedVersion(gvk, allowDeprecated, allowExperimental); err != nil {
return nil, err
}

View File

@ -110,7 +110,7 @@ func documentMapToResetConfiguration(gvkmap kubeadmapi.DocumentMap, allowDepreca
}
// check if this version is supported and possibly not deprecated
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
if err := validateSupportedVersion(gvk, allowDeprecated, allowExperimental); err != nil {
return nil, err
}

View File

@ -45,7 +45,7 @@ func documentMapToUpgradeConfiguration(gvkmap kubeadmapi.DocumentMap, allowDepre
for gvk, bytes := range gvkmap {
// check if this version is supported and possibly not deprecated
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, true); err != nil {
if err := validateSupportedVersion(gvk, allowDeprecated, true); err != nil {
return nil, err
}