Skip to content

Commit e4deee8

Browse files
committed
Fix e2e tests
1 parent e35938f commit e4deee8

File tree

15 files changed

+120
-93
lines changed

15 files changed

+120
-93
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package defaulting contains defaulting code that is used in CABPK and KCP.
18+
package defaulting
19+
20+
import bootstrapv1 "sigs.k8s.io/cluster-api/api/bootstrap/kubeadm/v1beta2"
21+
22+
// ApplyPreviousKubeadmConfigDefaults defaults a KubeadmConfig with default values we used in the past.
23+
// This is done in multiple places (webhooks and KCP controller) to ensure no rollouts are triggered now that
24+
// we removed this defaulting from webhooks.
25+
func ApplyPreviousKubeadmConfigDefaults(c *bootstrapv1.KubeadmConfigSpec) {
26+
if c.Format == "" {
27+
c.Format = bootstrapv1.CloudConfig
28+
}
29+
if c.InitConfiguration != nil && c.InitConfiguration.NodeRegistration.ImagePullPolicy == "" {
30+
c.InitConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
31+
}
32+
if c.JoinConfiguration != nil && c.JoinConfiguration.NodeRegistration.ImagePullPolicy == "" {
33+
c.JoinConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
34+
}
35+
if c.JoinConfiguration != nil && c.JoinConfiguration.Discovery.File != nil {
36+
if kfg := c.JoinConfiguration.Discovery.File.KubeConfig; kfg != nil {
37+
if kfg.User.Exec != nil {
38+
if kfg.User.Exec.APIVersion == "" {
39+
kfg.User.Exec.APIVersion = "client.authentication.k8s.io/v1"
40+
}
41+
}
42+
}
43+
}
44+
}

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,10 +1053,14 @@ func (r *KubeadmConfigReconciler) resolveDiscoveryKubeConfig(cfg *bootstrapv1.Fi
10531053
}
10541054
}
10551055
if cfg.KubeConfig.User.Exec != nil {
1056+
apiVersion := cfg.KubeConfig.User.Exec.APIVersion
1057+
if apiVersion == "" {
1058+
apiVersion = "client.authentication.k8s.io/v1"
1059+
}
10561060
user.Exec = &clientcmdv1.ExecConfig{
10571061
Command: cfg.KubeConfig.User.Exec.Command,
10581062
Args: cfg.KubeConfig.User.Exec.Args,
1059-
APIVersion: cfg.KubeConfig.User.Exec.APIVersion,
1063+
APIVersion: apiVersion,
10601064
ProvideClusterInfo: ptr.Deref(cfg.KubeConfig.User.Exec.ProvideClusterInfo, false),
10611065
InteractiveMode: "Never",
10621066
}
@@ -1356,6 +1360,11 @@ func (r *KubeadmConfigReconciler) computeClusterConfigurationAndAdditionalData(c
13561360
func (r *KubeadmConfigReconciler) storeBootstrapData(ctx context.Context, scope *Scope, data []byte) error {
13571361
log := ctrl.LoggerFrom(ctx)
13581362

1363+
format := scope.Config.Spec.Format
1364+
if format == "" {
1365+
format = bootstrapv1.CloudConfig
1366+
}
1367+
13591368
secret := &corev1.Secret{
13601369
ObjectMeta: metav1.ObjectMeta{
13611370
Name: scope.Config.Name,
@@ -1375,7 +1384,7 @@ func (r *KubeadmConfigReconciler) storeBootstrapData(ctx context.Context, scope
13751384
},
13761385
Data: map[string][]byte{
13771386
"value": data,
1378-
"format": []byte(scope.Config.Spec.Format),
1387+
"format": []byte(format),
13791388
},
13801389
Type: clusterv1.ClusterSecretType,
13811390
}

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,8 @@ func TestBootstrapDataFormat(t *testing.T) {
827827
clusterInitialized: true,
828828
},
829829
{
830-
name: "Empty format field",
830+
name: "Empty format field",
831+
format: bootstrapv1.CloudConfig,
831832
},
832833
}
833834

@@ -2315,8 +2316,7 @@ func TestKubeadmConfigReconciler_ResolveDiscoveryFileKubeConfig(t *testing.T) {
23152316
KubeConfig: &bootstrapv1.FileDiscoveryKubeConfig{
23162317
User: bootstrapv1.KubeConfigUser{
23172318
Exec: &bootstrapv1.KubeConfigAuthExec{
2318-
APIVersion: "client.authentication.k8s.io/v1",
2319-
Command: "/usr/bin/bootstrap",
2319+
Command: "/usr/bin/bootstrap",
23202320
Env: []bootstrapv1.KubeConfigAuthExecEnv{
23212321
{Name: "ENV_TEST", Value: "value"},
23222322
},

bootstrap/kubeadm/internal/webhooks/kubeadmconfigtemplate.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2828

2929
bootstrapv1 "sigs.k8s.io/cluster-api/api/bootstrap/kubeadm/v1beta2"
30+
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/defaulting"
3031
"sigs.k8s.io/cluster-api/util/topology"
3132
)
3233

@@ -53,38 +54,16 @@ func (webhook *KubeadmConfigTemplate) Default(ctx context.Context, obj runtime.O
5354

5455
req, err := admission.RequestFromContext(ctx)
5556
if err != nil {
56-
return apierrors.NewBadRequest(fmt.Sprintf("expected a admission.Request inside context: %v", err))
57+
return apierrors.NewBadRequest(fmt.Sprintf("expected an admission.Request inside context: %v", err))
5758
}
5859

5960
if topology.IsDryRunRequest(req, c) {
60-
applyPreviousKubeadmConfigDefaults(&c.Spec.Template.Spec)
61+
defaulting.ApplyPreviousKubeadmConfigDefaults(&c.Spec.Template.Spec)
6162
}
6263

6364
return nil
6465
}
6566

66-
// applyPreviousKubeadmConfigDefaults defaults a KubeadmConfig.
67-
func applyPreviousKubeadmConfigDefaults(c *bootstrapv1.KubeadmConfigSpec) {
68-
if c.Format == "" {
69-
c.Format = bootstrapv1.CloudConfig
70-
}
71-
if c.InitConfiguration != nil && c.InitConfiguration.NodeRegistration.ImagePullPolicy == "" {
72-
c.InitConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
73-
}
74-
if c.JoinConfiguration != nil && c.JoinConfiguration.NodeRegistration.ImagePullPolicy == "" {
75-
c.JoinConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
76-
}
77-
if c.JoinConfiguration != nil && c.JoinConfiguration.Discovery.File != nil {
78-
if kfg := c.JoinConfiguration.Discovery.File.KubeConfig; kfg != nil {
79-
if kfg.User.Exec != nil {
80-
if kfg.User.Exec.APIVersion == "" {
81-
kfg.User.Exec.APIVersion = "client.authentication.k8s.io/v1"
82-
}
83-
}
84-
}
85-
}
86-
}
87-
8867
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
8968
func (webhook *KubeadmConfigTemplate) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
9069
c, ok := obj.(*bootstrapv1.KubeadmConfigTemplate)

bootstrap/kubeadm/internal/webhooks/kubeadmconfigtemplate_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ func TestKubeadmConfigTemplateDefault(t *testing.T) {
4444
webhook := &KubeadmConfigTemplate{}
4545
t.Run("for KubeadmConfigTemplate", util.CustomDefaultValidateTest(admission.NewContextWithRequest(ctx, admission.Request{}), updateDefaultingKubeadmConfigTemplate, webhook))
4646

47+
// Expect no defaulting.
4748
original := kubeadmConfigTemplate.DeepCopy()
4849
g.Expect(webhook.Default(admission.NewContextWithRequest(ctx, admission.Request{}), kubeadmConfigTemplate)).To(Succeed())
4950
g.Expect(kubeadmConfigTemplate).To(BeComparableTo(original))
5051

52+
// Expect defaulting for dry-run request.
5153
ctx = admission.NewContextWithRequest(ctx, admission.Request{
5254
AdmissionRequest: admissionv1.AdmissionRequest{
5355
DryRun: ptr.To(true),

controllers/external/util.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,11 @@ type GenerateTemplateInput struct {
191191

192192
// GenerateTemplate generates an object with the given template input.
193193
func GenerateTemplate(in *GenerateTemplateInput) (*unstructured.Unstructured, error) {
194-
// Note: if spec.template is not set we are intentionally using an empty object here.
195-
template, _, err := unstructured.NestedMap(in.Template.Object, "spec", "template")
196-
if err != nil {
194+
template, found, err := unstructured.NestedMap(in.Template.Object, "spec", "template")
195+
if !found {
196+
// If spec.template is not set we are intentionally using an empty object here.
197+
template = nil
198+
} else if err != nil {
197199
return nil, errors.Wrapf(err, "failed to retrieve Spec.Template map on %v %q", in.Template.GroupVersionKind(), in.Template.GetName())
198200
}
199201

controllers/external/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) {
362362
g.Expect(cloneSpec).To(BeComparableTo(expectedSpec))
363363
}
364364

365-
func TestCloneTemplateMissingSpecTemplate(t *testing.T) {
365+
func TestCloneTemplateMissingSpec(t *testing.T) {
366366
g := NewWithT(t)
367367

368368
templateName := "aquaTemplate"

controlplane/kubeadm/internal/filters.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
bootstrapv1 "sigs.k8s.io/cluster-api/api/bootstrap/kubeadm/v1beta2"
3232
controlplanev1 "sigs.k8s.io/cluster-api/api/controlplane/kubeadm/v1beta2"
3333
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
34+
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/defaulting"
3435
"sigs.k8s.io/cluster-api/internal/util/compare"
3536
"sigs.k8s.io/cluster-api/util/collections"
3637
)
@@ -321,8 +322,8 @@ func matchInitOrJoinConfiguration(machineConfig *bootstrapv1.KubeadmConfig, kcp
321322
// *Note* This assumes that newly added default values never
322323
// introduce a semantic difference to the unset value.
323324
// But that is something that is ensured by our API guarantees.
324-
applyPreviousKubeadmConfigDefaults(kcpConfig)
325-
applyPreviousKubeadmConfigDefaults(&machineConfig.Spec)
325+
defaulting.ApplyPreviousKubeadmConfigDefaults(kcpConfig)
326+
defaulting.ApplyPreviousKubeadmConfigDefaults(&machineConfig.Spec)
326327

327328
// cleanups all the fields that are not relevant for the comparison.
328329
cleanupConfigFields(kcpConfig, machineConfig)
@@ -334,28 +335,6 @@ func matchInitOrJoinConfiguration(machineConfig *bootstrapv1.KubeadmConfig, kcp
334335
return match, diff, nil
335336
}
336337

337-
// applyPreviousKubeadmConfigDefaults defaults a KubeadmConfig.
338-
func applyPreviousKubeadmConfigDefaults(c *bootstrapv1.KubeadmConfigSpec) {
339-
if c.Format == "" {
340-
c.Format = bootstrapv1.CloudConfig
341-
}
342-
if c.InitConfiguration != nil && c.InitConfiguration.NodeRegistration.ImagePullPolicy == "" {
343-
c.InitConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
344-
}
345-
if c.JoinConfiguration != nil && c.JoinConfiguration.NodeRegistration.ImagePullPolicy == "" {
346-
c.JoinConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
347-
}
348-
if c.JoinConfiguration != nil && c.JoinConfiguration.Discovery.File != nil {
349-
if kfg := c.JoinConfiguration.Discovery.File.KubeConfig; kfg != nil {
350-
if kfg.User.Exec != nil {
351-
if kfg.User.Exec.APIVersion == "" {
352-
kfg.User.Exec.APIVersion = "client.authentication.k8s.io/v1"
353-
}
354-
}
355-
}
356-
}
357-
}
358-
359338
// getAdjustedKcpConfig takes the KubeadmConfigSpec from KCP and applies the transformations required
360339
// to allow a comparison with the KubeadmConfig referenced from the machine.
361340
// NOTE: The KCP controller applies a set of transformations when creating a KubeadmConfig referenced from the machine,

controlplane/kubeadm/internal/webhooks/kubeadmcontrolplanetemplate.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/webhook"
2929
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3030

31-
bootstrapv1 "sigs.k8s.io/cluster-api/api/bootstrap/kubeadm/v1beta2"
3231
controlplanev1 "sigs.k8s.io/cluster-api/api/controlplane/kubeadm/v1beta2"
3332
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
33+
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/defaulting"
3434
"sigs.k8s.io/cluster-api/feature"
3535
"sigs.k8s.io/cluster-api/internal/util/compare"
3636
)
@@ -91,8 +91,8 @@ func (webhook *KubeadmControlPlaneTemplate) ValidateUpdate(_ context.Context, ol
9191
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmControlPlaneTemplate but got a %T", newObj))
9292
}
9393

94-
applyPreviousKubeadmConfigDefaults(&oldK.Spec.Template.Spec.KubeadmConfigSpec)
95-
applyPreviousKubeadmConfigDefaults(&newK.Spec.Template.Spec.KubeadmConfigSpec)
94+
defaulting.ApplyPreviousKubeadmConfigDefaults(&oldK.Spec.Template.Spec.KubeadmConfigSpec)
95+
defaulting.ApplyPreviousKubeadmConfigDefaults(&newK.Spec.Template.Spec.KubeadmConfigSpec)
9696

9797
// In Cluster API < v1.11 the RolloutStrategy field was defaulted.
9898
// The defaulting was dropped with Cluster API v1.11.
@@ -123,28 +123,6 @@ func (webhook *KubeadmControlPlaneTemplate) ValidateUpdate(_ context.Context, ol
123123
return nil, apierrors.NewInvalid(clusterv1.GroupVersion.WithKind("KubeadmControlPlaneTemplate").GroupKind(), newK.Name, allErrs)
124124
}
125125

126-
// applyPreviousKubeadmConfigDefaults defaults a KubeadmConfig.
127-
func applyPreviousKubeadmConfigDefaults(c *bootstrapv1.KubeadmConfigSpec) {
128-
if c.Format == "" {
129-
c.Format = bootstrapv1.CloudConfig
130-
}
131-
if c.InitConfiguration != nil && c.InitConfiguration.NodeRegistration.ImagePullPolicy == "" {
132-
c.InitConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
133-
}
134-
if c.JoinConfiguration != nil && c.JoinConfiguration.NodeRegistration.ImagePullPolicy == "" {
135-
c.JoinConfiguration.NodeRegistration.ImagePullPolicy = "IfNotPresent"
136-
}
137-
if c.JoinConfiguration != nil && c.JoinConfiguration.Discovery.File != nil {
138-
if kfg := c.JoinConfiguration.Discovery.File.KubeConfig; kfg != nil {
139-
if kfg.User.Exec != nil {
140-
if kfg.User.Exec.APIVersion == "" {
141-
kfg.User.Exec.APIVersion = "client.authentication.k8s.io/v1"
142-
}
143-
}
144-
}
145-
}
146-
}
147-
148126
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
149127
func (webhook *KubeadmControlPlaneTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
150128
return nil, nil

internal/controllers/topology/cluster/patches/engine.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package patches
1919

2020
import (
21+
"bytes"
2122
"context"
2223
"fmt"
2324
"runtime/debug"
@@ -464,12 +465,26 @@ func applyPatchToRequest(ctx context.Context, req *runtimehooksv1.GeneratePatche
464465
return errors.Wrap(err, "failed to apply patch: error decoding json patch (RFC6902)")
465466
}
466467

468+
if len(jsonPatch) == 0 {
469+
// Return if there are no patches, nothing to do.
470+
// If the requestItem.Object does not have a spec and we don't have a patch that adds one,
471+
// patchTemplateSpec below would fail, so let's return early.
472+
return nil
473+
}
474+
467475
patchedTemplate, err = jsonPatch.Apply(requestItem.Object.Raw)
468476
if err != nil {
469477
log.Error(err, fmt.Sprintf("Failed to apply patch with uid %q: error applying json patch (RFC6902)", requestItem.UID), "patch", string(patch.Patch))
470478
return errors.Wrap(err, "failed to apply patch: error applying json patch (RFC6902)")
471479
}
472480
case runtimehooksv1.JSONMergePatchType:
481+
if len(patch.Patch) == 0 || bytes.Equal(patch.Patch, []byte("{}")) {
482+
// Return if there are no patches, nothing to do.
483+
// If the requestItem.Object does not have a spec and we don't have a patch that adds one,
484+
// patchTemplateSpec below would fail, so let's return early.
485+
return nil
486+
}
487+
473488
log.V(5).Info("Accumulating JSON merge patch", "patch", string(patch.Patch))
474489
patchedTemplate, err = jsonpatch.MergePatch(requestItem.Object.Raw, patch.Patch)
475490
if err != nil {

0 commit comments

Comments
 (0)