Skip to content

Commit 07a9e53

Browse files
authored
Merge pull request #6096 from sbueringer/pr-fix-kcp-rollout-main
🐛 KCP: don't rollout machines when format is defaulted
2 parents cf849bb + 6ae0a73 commit 07a9e53

17 files changed

+223
-6
lines changed

bootstrap/kubeadm/api/v1beta1/kubeadmconfig_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ type KubeadmConfigSpec struct {
7979

8080
// Format specifies the output format of the bootstrap data
8181
// +optional
82-
// +kubebuilder:default=cloud-config
8382
Format Format `json:"format,omitempty"`
8483

8584
// Verbosity is the number for the kubeadm log level verbosity.

bootstrap/kubeadm/api/v1beta1/kubeadmconfig_webhook.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ func (c *KubeadmConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
4343
Complete()
4444
}
4545

46+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfig,mutating=true,failurePolicy=fail,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs,versions=v1beta1,name=default.kubeadmconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
47+
48+
var _ webhook.Defaulter = &KubeadmConfig{}
49+
50+
// Default implements webhook.Defaulter so a webhook will be registered for the type.
51+
func (c *KubeadmConfig) Default() {
52+
DefaultKubeadmConfigSpec(&c.Spec)
53+
}
54+
55+
// DefaultKubeadmConfigSpec defaults a KubeadmConfigSpec.
56+
func DefaultKubeadmConfigSpec(r *KubeadmConfigSpec) {
57+
if r.Format == "" {
58+
r.Format = CloudConfig
59+
}
60+
}
61+
4662
// +kubebuilder:webhook:verbs=create;update,path=/validate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfig,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs,versions=v1beta1,name=validation.kubeadmconfig.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4763

4864
var _ webhook.Validator = &KubeadmConfig{}

bootstrap/kubeadm/api/v1beta1/kubeadmconfig_webhook_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,41 @@ import (
2525
"k8s.io/utils/pointer"
2626

2727
"sigs.k8s.io/cluster-api/feature"
28+
utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
2829
)
2930

30-
func TestClusterValidate(t *testing.T) {
31+
func TestKubeadmConfigDefault(t *testing.T) {
32+
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
33+
34+
g := NewWithT(t)
35+
36+
kubeadmConfig := &KubeadmConfig{
37+
ObjectMeta: metav1.ObjectMeta{
38+
Namespace: "foo",
39+
},
40+
Spec: KubeadmConfigSpec{},
41+
}
42+
updateDefaultingKubeadmConfig := kubeadmConfig.DeepCopy()
43+
updateDefaultingKubeadmConfig.Spec.Verbosity = pointer.Int32Ptr(4)
44+
t.Run("for KubeadmConfig", utildefaulting.DefaultValidateTest(updateDefaultingKubeadmConfig))
45+
46+
kubeadmConfig.Default()
47+
48+
g.Expect(kubeadmConfig.Spec.Format).To(Equal(CloudConfig))
49+
50+
ignitionKubeadmConfig := &KubeadmConfig{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Namespace: "foo",
53+
},
54+
Spec: KubeadmConfigSpec{
55+
Format: Ignition,
56+
},
57+
}
58+
ignitionKubeadmConfig.Default()
59+
g.Expect(ignitionKubeadmConfig.Spec.Format).To(Equal(Ignition))
60+
}
61+
62+
func TestKubeadmConfigValidate(t *testing.T) {
3163
cases := map[string]struct {
3264
in *KubeadmConfig
3365
enableIgnitionFeature bool

bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplate_webhook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ func (r *KubeadmConfigTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error
3030
Complete()
3131
}
3232

33+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfigtemplate,mutating=true,failurePolicy=fail,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigtemplates,versions=v1beta1,name=default.kubeadmconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
34+
35+
var _ webhook.Defaulter = &KubeadmConfigTemplate{}
36+
37+
// Default implements webhook.Defaulter so a webhook will be registered for the type.
38+
func (r *KubeadmConfigTemplate) Default() {
39+
DefaultKubeadmConfigSpec(&r.Spec.Template.Spec)
40+
}
41+
3342
// +kubebuilder:webhook:verbs=create;update,path=/validate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfigtemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigtemplates,versions=v1beta1,name=validation.kubeadmconfigtemplate.bootstrap.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3443

3544
var _ webhook.Validator = &KubeadmConfigTemplate{}

bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplate_webhook_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ import (
2121

2222
. "github.com/onsi/gomega"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/utils/pointer"
2425

2526
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
27+
utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
2628
)
2729

30+
func TestKubeadmConfigTemplateDefault(t *testing.T) {
31+
g := NewWithT(t)
32+
33+
kubeadmConfigTemplate := &bootstrapv1.KubeadmConfigTemplate{
34+
ObjectMeta: metav1.ObjectMeta{
35+
Namespace: "foo",
36+
},
37+
}
38+
updateDefaultingKubeadmConfigTemplate := kubeadmConfigTemplate.DeepCopy()
39+
updateDefaultingKubeadmConfigTemplate.Spec.Template.Spec.Verbosity = pointer.Int32Ptr(4)
40+
t.Run("for KubeadmConfigTemplate", utildefaulting.DefaultValidateTest(updateDefaultingKubeadmConfigTemplate))
41+
42+
kubeadmConfigTemplate.Default()
43+
44+
g.Expect(kubeadmConfigTemplate.Spec.Template.Spec.Format).To(Equal(bootstrapv1.CloudConfig))
45+
}
46+
2847
func TestKubeadmConfigTemplateValidation(t *testing.T) {
2948
cases := map[string]struct {
3049
in *bootstrapv1.KubeadmConfigTemplate

bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,6 @@ spec:
24642464
type: object
24652465
type: array
24662466
format:
2467-
default: cloud-config
24682467
description: Format specifies the output format of the bootstrap data
24692468
enum:
24702469
- cloud-config

bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,6 @@ spec:
24782478
type: object
24792479
type: array
24802480
format:
2481-
default: cloud-config
24822481
description: Format specifies the output format of the bootstrap
24832482
data
24842483
enum:

bootstrap/kubeadm/config/default/webhookcainjection_patch.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
22
apiVersion: admissionregistration.k8s.io/v1
3+
kind: MutatingWebhookConfiguration
4+
metadata:
5+
name: mutating-webhook-configuration
6+
annotations:
7+
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
8+
---
9+
apiVersion: admissionregistration.k8s.io/v1
310
kind: ValidatingWebhookConfiguration
411
metadata:
512
name: validating-webhook-configuration

bootstrap/kubeadm/config/webhook/manifests.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
---
22
apiVersion: admissionregistration.k8s.io/v1
3+
kind: MutatingWebhookConfiguration
4+
metadata:
5+
creationTimestamp: null
6+
name: mutating-webhook-configuration
7+
webhooks:
8+
- admissionReviewVersions:
9+
- v1
10+
- v1beta1
11+
clientConfig:
12+
service:
13+
name: webhook-service
14+
namespace: system
15+
path: /mutate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfig
16+
failurePolicy: Fail
17+
name: default.kubeadmconfig.bootstrap.cluster.x-k8s.io
18+
rules:
19+
- apiGroups:
20+
- bootstrap.cluster.x-k8s.io
21+
apiVersions:
22+
- v1beta1
23+
operations:
24+
- CREATE
25+
- UPDATE
26+
resources:
27+
- kubeadmconfigs
28+
sideEffects: None
29+
- admissionReviewVersions:
30+
- v1
31+
- v1beta1
32+
clientConfig:
33+
service:
34+
name: webhook-service
35+
namespace: system
36+
path: /mutate-bootstrap-cluster-x-k8s-io-v1beta1-kubeadmconfigtemplate
37+
failurePolicy: Fail
38+
name: default.kubeadmconfigtemplate.bootstrap.cluster.x-k8s.io
39+
rules:
40+
- apiGroups:
41+
- bootstrap.cluster.x-k8s.io
42+
apiVersions:
43+
- v1beta1
44+
operations:
45+
- CREATE
46+
- UPDATE
47+
resources:
48+
- kubeadmconfigtemplates
49+
sideEffects: None
50+
---
51+
apiVersion: admissionregistration.k8s.io/v1
352
kind: ValidatingWebhookConfiguration
453
metadata:
554
creationTimestamp: null

controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func defaultKubeadmControlPlaneSpec(s *KubeadmControlPlaneSpec, namespace string
6868
s.Version = "v" + s.Version
6969
}
7070

71+
bootstrapv1.DefaultKubeadmConfigSpec(&s.KubeadmConfigSpec)
72+
7173
s.RolloutStrategy = defaultRolloutStrategy(s.RolloutStrategy)
7274
}
7375

0 commit comments

Comments
 (0)