Skip to content

Commit

Permalink
kubeadm: improve the error/warning messages of 'validateSupportedVers…
Browse files Browse the repository at this point in the history
…ion' to include the checked resource kind
  • Loading branch information
SataQiu committed Jun 27, 2024
1 parent 7210f2a commit 7120b39
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
12 changes: 6 additions & 6 deletions cmd/kubeadm/app/util/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
30 changes: 19 additions & 11 deletions cmd/kubeadm/app/util/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/util/config/initconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/util/config/joinconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/util/config/resetconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/util/config/upgradeconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 7120b39

Please sign in to comment.