Skip to content

Commit 83ac620

Browse files
committed
Fix ControlPlaneComponentHealthCheckSeconds validation in
KubeadmConfigSpec.Validate Signed-off-by: Stefan Büringer [email protected]
1 parent a5e21a3 commit 83ac620

File tree

7 files changed

+53
-31
lines changed

7 files changed

+53
-31
lines changed

api/bootstrap/kubeadm/v1beta2/kubeadmconfig_types.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ type KubeadmConfigSpec struct {
143143
}
144144

145145
// Validate ensures the KubeadmConfigSpec is valid.
146-
func (c *KubeadmConfigSpec) Validate(pathPrefix *field.Path) field.ErrorList {
146+
func (c *KubeadmConfigSpec) Validate(isKCP bool, pathPrefix *field.Path) field.ErrorList {
147147
var allErrs field.ErrorList
148148

149149
allErrs = append(allErrs, c.validateFiles(pathPrefix)...)
@@ -166,29 +166,34 @@ func (c *KubeadmConfigSpec) Validate(pathPrefix *field.Path) field.ErrorList {
166166
}
167167
}
168168

169-
// Validate timeouts
170-
// Note: When v1beta1 will be removed, we can drop this limitation.
171-
tInit := "unset"
172-
if c.InitConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds != nil {
173-
tInit = fmt.Sprintf("%d", *c.InitConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds)
174-
}
175-
tJoin := "unset"
176-
if c.JoinConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds != nil {
177-
tJoin = fmt.Sprintf("%d", *c.JoinConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds)
178-
}
179-
if tInit != tJoin {
180-
allErrs = append(allErrs,
181-
field.Invalid(
182-
pathPrefix.Child("initConfiguration", "timeouts", "controlPlaneComponentHealthCheckSeconds"),
183-
tInit,
184-
fmt.Sprintf("controlPlaneComponentHealthCheckSeconds must be set to the same value both in initConfiguration.timeouts (%s) and in joinConfiguration.timeouts (%s)", tInit, tJoin),
185-
),
186-
field.Invalid(
187-
pathPrefix.Child("joinConfiguration", "timeouts", "controlPlaneComponentHealthCheckSeconds"),
188-
tJoin,
189-
fmt.Sprintf("controlPlaneComponentHealthCheckSeconds must be set to the same value both in initConfiguration.timeouts (%s) and in joinConfiguration.timeouts (%s)", tInit, tJoin),
190-
),
191-
)
169+
// Only ensure ControlPlaneComponentHealthCheckSeconds fields are equal for KubeadmControlPlane and KubeadmControlPlaneTemplate.
170+
// In KubeadmConfig objects usually only one of InitConfiguration or JoinConfiguration is defined as a Machine uses
171+
// either kubeadm init or kubeadm join, but not both.
172+
if isKCP {
173+
// Validate timeouts
174+
// Note: When v1beta1 will be removed, we can drop this limitation.
175+
tInit := "unset"
176+
if c.InitConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds != nil {
177+
tInit = fmt.Sprintf("%d", *c.InitConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds)
178+
}
179+
tJoin := "unset"
180+
if c.JoinConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds != nil {
181+
tJoin = fmt.Sprintf("%d", *c.JoinConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds)
182+
}
183+
if tInit != tJoin {
184+
allErrs = append(allErrs,
185+
field.Invalid(
186+
pathPrefix.Child("initConfiguration", "timeouts", "controlPlaneComponentHealthCheckSeconds"),
187+
tInit,
188+
fmt.Sprintf("controlPlaneComponentHealthCheckSeconds must be set to the same value both in initConfiguration.timeouts (%s) and in joinConfiguration.timeouts (%s)", tInit, tJoin),
189+
),
190+
field.Invalid(
191+
pathPrefix.Child("joinConfiguration", "timeouts", "controlPlaneComponentHealthCheckSeconds"),
192+
tJoin,
193+
fmt.Sprintf("controlPlaneComponentHealthCheckSeconds must be set to the same value both in initConfiguration.timeouts (%s) and in joinConfiguration.timeouts (%s)", tInit, tJoin),
194+
),
195+
)
196+
}
192197
}
193198

194199
return allErrs

bootstrap/kubeadm/internal/webhooks/kubeadmconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (webhook *KubeadmConfig) ValidateDelete(_ context.Context, _ runtime.Object
7070
}
7171

7272
func (webhook *KubeadmConfig) validate(c bootstrapv1.KubeadmConfigSpec, name string) error {
73-
allErrs := c.Validate(field.NewPath("spec"))
73+
allErrs := c.Validate(false, field.NewPath("spec"))
7474

7575
if len(allErrs) == 0 {
7676
return nil

bootstrap/kubeadm/internal/webhooks/kubeadmconfig_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
450450
},
451451
},
452452
},
453-
"invalid ControlPlaneComponentHealthCheckSeconds": {
453+
"valid ControlPlaneComponentHealthCheckSeconds (JoinConfiguration not defined)": {
454454
in: &bootstrapv1.KubeadmConfig{
455455
ObjectMeta: metav1.ObjectMeta{
456456
Name: "baz",
@@ -464,7 +464,23 @@ func TestKubeadmConfigValidate(t *testing.T) {
464464
},
465465
},
466466
},
467-
expectErr: true,
467+
expectErr: false,
468+
},
469+
"valid ControlPlaneComponentHealthCheckSeconds (InitConfiguration not defined)": {
470+
in: &bootstrapv1.KubeadmConfig{
471+
ObjectMeta: metav1.ObjectMeta{
472+
Name: "baz",
473+
Namespace: metav1.NamespaceDefault,
474+
},
475+
Spec: bootstrapv1.KubeadmConfigSpec{
476+
JoinConfiguration: bootstrapv1.JoinConfiguration{
477+
Timeouts: bootstrapv1.Timeouts{
478+
ControlPlaneComponentHealthCheckSeconds: ptr.To[int32](10),
479+
},
480+
},
481+
},
482+
},
483+
expectErr: false,
468484
},
469485
"valid ControlPlaneComponentHealthCheckSeconds": {
470486
in: &bootstrapv1.KubeadmConfig{

bootstrap/kubeadm/internal/webhooks/kubeadmconfigtemplate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (webhook *KubeadmConfigTemplate) ValidateDelete(_ context.Context, _ runtim
9494
func (webhook *KubeadmConfigTemplate) validate(r *bootstrapv1.KubeadmConfigTemplateSpec, name string) error {
9595
var allErrs field.ErrorList
9696

97-
allErrs = append(allErrs, r.Template.Spec.Validate(field.NewPath("spec", "template", "spec"))...)
97+
allErrs = append(allErrs, r.Template.Spec.Validate(false, field.NewPath("spec", "template", "spec"))...)
9898
// Validate the metadata of the template.
9999
allErrs = append(allErrs, r.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
100100

controlplane/kubeadm/internal/webhooks/kubeadm_control_plane.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (webhook *KubeadmControlPlane) ValidateCreate(_ context.Context, obj runtim
9999
spec := k.Spec
100100
allErrs := validateKubeadmControlPlaneSpec(spec, field.NewPath("spec"))
101101
allErrs = append(allErrs, validateClusterConfiguration(nil, &spec.KubeadmConfigSpec.ClusterConfiguration, field.NewPath("spec", "kubeadmConfigSpec", "clusterConfiguration"))...)
102-
allErrs = append(allErrs, spec.KubeadmConfigSpec.Validate(field.NewPath("spec", "kubeadmConfigSpec"))...)
102+
allErrs = append(allErrs, spec.KubeadmConfigSpec.Validate(true, field.NewPath("spec", "kubeadmConfigSpec"))...)
103103
if len(allErrs) > 0 {
104104
return nil, apierrors.NewInvalid(clusterv1.GroupVersion.WithKind("KubeadmControlPlane").GroupKind(), k.Name, allErrs)
105105
}
@@ -261,7 +261,7 @@ func (webhook *KubeadmControlPlane) ValidateUpdate(_ context.Context, oldObj, ne
261261
allErrs = append(allErrs, webhook.validateVersion(oldK, newK)...)
262262
allErrs = append(allErrs, validateClusterConfiguration(&oldK.Spec.KubeadmConfigSpec.ClusterConfiguration, &newK.Spec.KubeadmConfigSpec.ClusterConfiguration, field.NewPath("spec", "kubeadmConfigSpec", "clusterConfiguration"))...)
263263
allErrs = append(allErrs, webhook.validateCoreDNSVersion(oldK, newK)...)
264-
allErrs = append(allErrs, newK.Spec.KubeadmConfigSpec.Validate(field.NewPath("spec", "kubeadmConfigSpec"))...)
264+
allErrs = append(allErrs, newK.Spec.KubeadmConfigSpec.Validate(true, field.NewPath("spec", "kubeadmConfigSpec"))...)
265265

266266
if len(allErrs) > 0 {
267267
return nil, apierrors.NewInvalid(clusterv1.GroupVersion.WithKind("KubeadmControlPlane").GroupKind(), newK.Name, allErrs)

controlplane/kubeadm/internal/webhooks/kubeadmcontrolplanetemplate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (webhook *KubeadmControlPlaneTemplate) ValidateCreate(_ context.Context, ob
6969
spec := k.Spec.Template.Spec
7070
allErrs := validateKubeadmControlPlaneTemplateResourceSpec(spec, field.NewPath("spec", "template", "spec"))
7171
allErrs = append(allErrs, validateClusterConfiguration(nil, &spec.KubeadmConfigSpec.ClusterConfiguration, field.NewPath("spec", "template", "spec", "kubeadmConfigSpec", "clusterConfiguration"))...)
72-
allErrs = append(allErrs, spec.KubeadmConfigSpec.Validate(field.NewPath("spec", "template", "spec", "kubeadmConfigSpec"))...)
72+
allErrs = append(allErrs, spec.KubeadmConfigSpec.Validate(true, field.NewPath("spec", "template", "spec", "kubeadmConfigSpec"))...)
7373
// Validate the metadata of the KubeadmControlPlaneTemplateResource
7474
allErrs = append(allErrs, k.Spec.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
7575
if len(allErrs) > 0 {

test/e2e/data/infrastructure-docker/main/clusterclass-quick-start-runtimesdk-v1beta1.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ spec:
9898
kubeadmConfigSpec:
9999
clusterConfiguration:
100100
apiServer:
101+
timeoutForControlPlane: 20m
101102
# host.docker.internal is required by kubetest when running on MacOS because of the way ports are proxied.
102103
certSANs: [localhost, 127.0.0.1, 0.0.0.0, host.docker.internal]
103104
initConfiguration:

0 commit comments

Comments
 (0)