Skip to content

Commit 41f2182

Browse files
authored
Merge pull request kubernetes#125758 from SataQiu/improve-validateSupportedVersion-20240627
kubeadm: improve the error/warning messages of `validateSupportedVersion` to include the checked resource kind
2 parents 29defc1 + 7120b39 commit 41f2182

File tree

6 files changed

+29
-21
lines changed

6 files changed

+29
-21
lines changed

cmd/kubeadm/app/util/config/common.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func MarshalKubeadmConfigObject(obj runtime.Object, gv schema.GroupVersion) ([]b
6464

6565
// validateSupportedVersion checks if the supplied GroupVersion is not on the lists of old unsupported or deprecated GVs.
6666
// If it is, an error is returned.
67-
func validateSupportedVersion(gv schema.GroupVersion, allowDeprecated, allowExperimental bool) error {
67+
func validateSupportedVersion(gvk schema.GroupVersionKind, allowDeprecated, allowExperimental bool) error {
6868
// The support matrix will look something like this now and in the future:
6969
// v1.10 and earlier: v1alpha1
7070
// v1.11: v1alpha1 read-only, writes only v1alpha2 config
@@ -91,18 +91,18 @@ func validateSupportedVersion(gv schema.GroupVersion, allowDeprecated, allowExpe
9191
"kubeadm.k8s.io/v1beta3": {},
9292
}
9393

94-
gvString := gv.String()
94+
gvString := gvk.GroupVersion().String()
9595

9696
if useKubeadmVersion := oldKnownAPIVersions[gvString]; useKubeadmVersion != "" {
97-
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)
97+
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)
9898
}
9999

100100
if _, present := deprecatedAPIVersions[gvString]; present && !allowDeprecated {
101-
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())
101+
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)
102102
}
103103

104104
if _, present := experimentalAPIVersions[gvString]; present && !allowExperimental {
105-
return errors.Errorf("experimental API spec: %q is not allowed. You can use the --%s flag if the command supports it.", gv, options.AllowExperimentalAPI)
105+
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)
106106
}
107107

108108
return nil
@@ -225,7 +225,7 @@ func validateKnownGVKs(gvks []schema.GroupVersionKind) error {
225225

226226
// Skip legacy known GVs so that they don't return errors.
227227
// This makes the function return errors only for GVs that where never known.
228-
if err := validateSupportedVersion(gvk.GroupVersion(), true, true); err != nil {
228+
if err := validateSupportedVersion(gvk, true, true); err != nil {
229229
continue
230230
}
231231

cmd/kubeadm/app/util/config/common_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,69 +43,77 @@ const KubeadmGroupName = "kubeadm.k8s.io"
4343

4444
func TestValidateSupportedVersion(t *testing.T) {
4545
tests := []struct {
46-
gv schema.GroupVersion
46+
gvk schema.GroupVersionKind
4747
allowDeprecated bool
4848
allowExperimental bool
4949
expectedErr bool
5050
}{
5151
{
52-
gv: schema.GroupVersion{
52+
gvk: schema.GroupVersionKind{
5353
Group: KubeadmGroupName,
5454
Version: "v1alpha1",
55+
Kind: "InitConfiguration",
5556
},
5657
expectedErr: true,
5758
},
5859
{
59-
gv: schema.GroupVersion{
60+
gvk: schema.GroupVersionKind{
6061
Group: KubeadmGroupName,
6162
Version: "v1alpha2",
63+
Kind: "InitConfiguration",
6264
},
6365
expectedErr: true,
6466
},
6567
{
66-
gv: schema.GroupVersion{
68+
gvk: schema.GroupVersionKind{
6769
Group: KubeadmGroupName,
6870
Version: "v1alpha3",
71+
Kind: "InitConfiguration",
6972
},
7073
expectedErr: true,
7174
},
7275
{
73-
gv: schema.GroupVersion{
76+
gvk: schema.GroupVersionKind{
7477
Group: KubeadmGroupName,
7578
Version: "v1beta1",
79+
Kind: "InitConfiguration",
7680
},
7781
expectedErr: true,
7882
},
7983
{
80-
gv: schema.GroupVersion{
84+
gvk: schema.GroupVersionKind{
8185
Group: KubeadmGroupName,
8286
Version: "v1beta2",
87+
Kind: "InitConfiguration",
8388
},
8489
expectedErr: true,
8590
},
8691
{
87-
gv: schema.GroupVersion{
92+
gvk: schema.GroupVersionKind{
8893
Group: KubeadmGroupName,
8994
Version: "v1beta3",
95+
Kind: "ClusterConfiguration",
9096
},
9197
},
9298
{
93-
gv: schema.GroupVersion{
99+
gvk: schema.GroupVersionKind{
94100
Group: "foo.k8s.io",
95101
Version: "v1",
102+
Kind: "InitConfiguration",
96103
},
97104
},
98105
{
99-
gv: schema.GroupVersion{
106+
gvk: schema.GroupVersionKind{
100107
Group: KubeadmGroupName,
101108
Version: "v1beta4",
109+
Kind: "ResetConfiguration",
102110
},
103111
},
104112
}
105113

106114
for _, rt := range tests {
107-
t.Run(fmt.Sprintf("%s/allowDeprecated:%t", rt.gv, rt.allowDeprecated), func(t *testing.T) {
108-
err := validateSupportedVersion(rt.gv, rt.allowDeprecated, rt.allowExperimental)
115+
t.Run(fmt.Sprintf("%s/allowDeprecated:%t", rt.gvk.GroupVersion(), rt.allowDeprecated), func(t *testing.T) {
116+
err := validateSupportedVersion(rt.gvk, rt.allowDeprecated, rt.allowExperimental)
109117
if rt.expectedErr && err == nil {
110118
t.Error("unexpected success")
111119
} else if !rt.expectedErr && err != nil {

cmd/kubeadm/app/util/config/initconfiguration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func documentMapToInitConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
322322
fileContent := gvkmap[gvk]
323323

324324
// first, check if this GVK is supported and possibly not deprecated
325-
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
325+
if err := validateSupportedVersion(gvk, allowDeprecated, allowExperimental); err != nil {
326326
return nil, err
327327
}
328328

cmd/kubeadm/app/util/config/joinconfiguration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func documentMapToJoinConfiguration(gvkmap kubeadmapi.DocumentMap, allowDeprecat
106106
}
107107

108108
// check if this version is supported and possibly not deprecated
109-
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
109+
if err := validateSupportedVersion(gvk, allowDeprecated, allowExperimental); err != nil {
110110
return nil, err
111111
}
112112

cmd/kubeadm/app/util/config/resetconfiguration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func documentMapToResetConfiguration(gvkmap kubeadmapi.DocumentMap, allowDepreca
110110
}
111111

112112
// check if this version is supported and possibly not deprecated
113-
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, allowExperimental); err != nil {
113+
if err := validateSupportedVersion(gvk, allowDeprecated, allowExperimental); err != nil {
114114
return nil, err
115115
}
116116

cmd/kubeadm/app/util/config/upgradeconfiguration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func documentMapToUpgradeConfiguration(gvkmap kubeadmapi.DocumentMap, allowDepre
4545

4646
for gvk, bytes := range gvkmap {
4747
// check if this version is supported and possibly not deprecated
48-
if err := validateSupportedVersion(gvk.GroupVersion(), allowDeprecated, true); err != nil {
48+
if err := validateSupportedVersion(gvk, allowDeprecated, true); err != nil {
4949
return nil, err
5050
}
5151

0 commit comments

Comments
 (0)