Skip to content

Commit

Permalink
Merge pull request #4644 from vincepri/kcp-metadata
Browse files Browse the repository at this point in the history
⚠️ KubeadmControlPlane: Support metadata for machines and propagate to all templates
  • Loading branch information
k8s-ci-robot authored May 24, 2021
2 parents 9acee8c + 95f428e commit 04c2635
Show file tree
Hide file tree
Showing 27 changed files with 665 additions and 229 deletions.
4 changes: 3 additions & 1 deletion controlplane/kubeadm/api/v1alpha3/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package v1alpha3
import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4"

utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)
Expand All @@ -38,6 +37,7 @@ func (src *KubeadmControlPlane) ConvertTo(destRaw conversion.Hub) error {
}

dest.Spec.RolloutStrategy = restored.Spec.RolloutStrategy
dest.Spec.MachineTemplate.ObjectMeta = restored.Spec.MachineTemplate.ObjectMeta

return nil
}
Expand Down Expand Up @@ -69,10 +69,12 @@ func (dest *KubeadmControlPlaneList) ConvertFrom(srcRaw conversion.Hub) error {

func Convert_v1alpha4_KubeadmControlPlaneSpec_To_v1alpha3_KubeadmControlPlaneSpec(in *v1alpha4.KubeadmControlPlaneSpec, out *KubeadmControlPlaneSpec, s apiconversion.Scope) error {
out.UpgradeAfter = in.RolloutAfter
out.InfrastructureTemplate = in.MachineTemplate.InfrastructureRef
return autoConvert_v1alpha4_KubeadmControlPlaneSpec_To_v1alpha3_KubeadmControlPlaneSpec(in, out, s)
}

func Convert_v1alpha3_KubeadmControlPlaneSpec_To_v1alpha4_KubeadmControlPlaneSpec(in *KubeadmControlPlaneSpec, out *v1alpha4.KubeadmControlPlaneSpec, s apiconversion.Scope) error {
out.RolloutAfter = in.UpgradeAfter
out.MachineTemplate.InfrastructureRef = in.InfrastructureTemplate
return autoConvert_v1alpha3_KubeadmControlPlaneSpec_To_v1alpha4_KubeadmControlPlaneSpec(in, out, s)
}
7 changes: 4 additions & 3 deletions controlplane/kubeadm/api/v1alpha3/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ import (

func TestKubeadmControlPlaneConversion(t *testing.T) {
g := NewWithT(t)

ns, err := testEnv.CreateNamespace(ctx, fmt.Sprintf("conversion-webhook-%s", util.RandomString(5)))
g.Expect(err).ToNot(HaveOccurred())
infraMachineTemplateName := fmt.Sprintf("test-machinetemplate-%s", util.RandomString(5))
controlPlaneName := fmt.Sprintf("test-controlpane-%s", util.RandomString(5))
controlPane := &KubeadmControlPlane{
controlPlane := &KubeadmControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: controlPlaneName,
Namespace: ns.Name,
Expand Down Expand Up @@ -86,8 +87,8 @@ func TestKubeadmControlPlaneConversion(t *testing.T) {
},
}

g.Expect(testEnv.Create(ctx, controlPane)).To(Succeed())
g.Expect(testEnv.Create(ctx, controlPlane)).To(Succeed())
defer func(do ...client.Object) {
g.Expect(testEnv.Cleanup(ctx, do...)).To(Succeed())
}(ns, controlPane)
}(ns, controlPlane)
}
4 changes: 2 additions & 2 deletions controlplane/kubeadm/api/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions controlplane/kubeadm/api/v1alpha4/kubeadm_control_plane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ type KubeadmControlPlaneSpec struct {
// Version defines the desired Kubernetes version.
Version string `json:"version"`

// InfrastructureTemplate is a required reference to a custom resource
// offered by an infrastructure provider.
InfrastructureTemplate corev1.ObjectReference `json:"infrastructureTemplate"`
// MachineTemplate contains information about how machines
// should be shaped when creating or updating a control plane.
MachineTemplate KubeadmControlPlaneMachineTemplate `json:"machineTemplate"`

// KubeadmConfigSpec is a KubeadmConfigSpec
// to use for initializing and joining machines to the control plane.
Expand All @@ -85,6 +85,17 @@ type KubeadmControlPlaneSpec struct {
RolloutStrategy *RolloutStrategy `json:"rolloutStrategy,omitempty"`
}

type KubeadmControlPlaneMachineTemplate struct {
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
// +optional
ObjectMeta clusterv1.ObjectMeta `json:"metadata,omitempty"`

// InfrastructureRef is a required reference to a custom resource
// offered by an infrastructure provider.
InfrastructureRef corev1.ObjectReference `json:"infrastructureRef"`
}

// RolloutStrategy describes how to replace existing machines
// with new ones.
type RolloutStrategy struct {
Expand Down
35 changes: 28 additions & 7 deletions controlplane/kubeadm/api/v1alpha4/kubeadm_control_plane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (in *KubeadmControlPlane) Default() {
in.Spec.Replicas = &replicas
}

if in.Spec.InfrastructureTemplate.Namespace == "" {
in.Spec.InfrastructureTemplate.Namespace = in.Namespace
if in.Spec.MachineTemplate.InfrastructureRef.Namespace == "" {
in.Spec.MachineTemplate.InfrastructureRef.Namespace = in.Namespace
}

if !strings.HasPrefix(in.Spec.Version, "v") {
Expand Down Expand Up @@ -112,7 +112,7 @@ const (
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (in *KubeadmControlPlane) ValidateUpdate(old runtime.Object) error {
// add a * to indicate everything beneath is ok.
// For example, {"spec", "*"} will allow any path under "spec" to change, such as spec.infrastructureTemplate.name
// For example, {"spec", "*"} will allow any path under "spec" to change.
allowedPaths := [][]string{
{"metadata", "*"},
{spec, kubeadmConfigSpec, clusterConfiguration, "etcd", "local", "imageRepository"},
Expand All @@ -130,7 +130,8 @@ func (in *KubeadmControlPlane) ValidateUpdate(old runtime.Object) error {
{spec, kubeadmConfigSpec, files},
{spec, kubeadmConfigSpec, "verbosity"},
{spec, kubeadmConfigSpec, users},
{spec, "infrastructureTemplate", "name"},
{spec, "machineTemplate", "metadata"},
{spec, "machineTemplate", "infrastructureRef", "name"},
{spec, "replicas"},
{spec, "version"},
{spec, "rolloutAfter"},
Expand Down Expand Up @@ -274,12 +275,32 @@ func (in *KubeadmControlPlane) validateCommon() (allErrs field.ErrorList) {
}
}

if in.Spec.InfrastructureTemplate.Namespace != in.Namespace {
if in.Spec.MachineTemplate.InfrastructureRef.APIVersion == "" {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "infrastructureTemplate", "namespace"),
in.Spec.InfrastructureTemplate.Namespace,
field.NewPath("spec", "machineTemplate", "infrastructure", "apiVersion"),
in.Spec.MachineTemplate.InfrastructureRef.APIVersion,
"cannot be empty",
),
)
}
if in.Spec.MachineTemplate.InfrastructureRef.Kind == "" {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "machineTemplate", "infrastructure", "kind"),
in.Spec.MachineTemplate.InfrastructureRef.Kind,
"cannot be empty",
),
)
}
if in.Spec.MachineTemplate.InfrastructureRef.Namespace != in.Namespace {
allErrs = append(
allErrs,
field.Invalid(
field.NewPath("spec", "machineTemplate", "infrastructure", "namespace"),
in.Spec.MachineTemplate.InfrastructureRef.Namespace,
"must match metadata.namespace",
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,29 @@ func TestKubeadmControlPlaneDefault(t *testing.T) {
Namespace: "foo",
},
Spec: KubeadmControlPlaneSpec{
Version: "v1.18.3",
InfrastructureTemplate: corev1.ObjectReference{},
RolloutStrategy: &RolloutStrategy{},
Version: "v1.18.3",
MachineTemplate: KubeadmControlPlaneMachineTemplate{
InfrastructureRef: corev1.ObjectReference{
APIVersion: "test/v1alpha1",
Kind: "UnknownInfraMachine",
Name: "foo",
},
},
RolloutStrategy: &RolloutStrategy{},
},
}
updateDefaultingValidationKCP := kcp.DeepCopy()
updateDefaultingValidationKCP.Spec.Version = "v1.18.3"
updateDefaultingValidationKCP.Spec.InfrastructureTemplate = corev1.ObjectReference{
Namespace: "foo",
updateDefaultingValidationKCP.Spec.MachineTemplate.InfrastructureRef = corev1.ObjectReference{
APIVersion: "test/v1alpha1",
Kind: "UnknownInfraMachine",
Name: "foo",
Namespace: "foo",
}
t.Run("for KubeadmControlPLane", utildefaulting.DefaultValidateTest(updateDefaultingValidationKCP))
kcp.Default()

g.Expect(kcp.Spec.InfrastructureTemplate.Namespace).To(Equal(kcp.Namespace))
g.Expect(kcp.Spec.MachineTemplate.InfrastructureRef.Namespace).To(Equal(kcp.Namespace))
g.Expect(kcp.Spec.Version).To(Equal("v1.18.3"))
g.Expect(kcp.Spec.RolloutStrategy.Type).To(Equal(RollingUpdateStrategyType))
g.Expect(kcp.Spec.RolloutStrategy.RollingUpdate.MaxSurge.IntVal).To(Equal(int32(1)))
Expand All @@ -63,9 +72,13 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
Namespace: "foo",
},
Spec: KubeadmControlPlaneSpec{
InfrastructureTemplate: corev1.ObjectReference{
Namespace: "foo",
Name: "infraTemplate",
MachineTemplate: KubeadmControlPlaneMachineTemplate{
InfrastructureRef: corev1.ObjectReference{
APIVersion: "test/v1alpha1",
Kind: "UnknownInfraMachine",
Namespace: "foo",
Name: "infraTemplate",
},
},
Replicas: pointer.Int32Ptr(1),
Version: "v1.19.0",
Expand All @@ -84,7 +97,7 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
invalidMaxSurge.Spec.RolloutStrategy.RollingUpdate.MaxSurge.IntVal = int32(3)

invalidNamespace := valid.DeepCopy()
invalidNamespace.Spec.InfrastructureTemplate.Namespace = "bar"
invalidNamespace.Spec.MachineTemplate.InfrastructureRef.Namespace = "bar"

missingReplicas := valid.DeepCopy()
missingReplicas.Spec.Replicas = nil
Expand Down Expand Up @@ -190,9 +203,13 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
Namespace: "foo",
},
Spec: KubeadmControlPlaneSpec{
InfrastructureTemplate: corev1.ObjectReference{
Namespace: "foo",
Name: "infraTemplate",
MachineTemplate: KubeadmControlPlaneMachineTemplate{
InfrastructureRef: corev1.ObjectReference{
APIVersion: "test/v1alpha1",
Kind: "UnknownInfraMachine",
Namespace: "foo",
Name: "infraTemplate",
},
},
Replicas: pointer.Int32Ptr(1),
RolloutStrategy: &RolloutStrategy{
Expand Down Expand Up @@ -300,7 +317,7 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
},
},
}
validUpdate.Spec.InfrastructureTemplate.Name = "orange"
validUpdate.Spec.MachineTemplate.InfrastructureRef.Name = "orange"
validUpdate.Spec.Replicas = pointer.Int32Ptr(5)
now := metav1.NewTime(time.Now())
validUpdate.Spec.RolloutAfter = &now
Expand All @@ -312,7 +329,7 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
scaleToEven.Spec.Replicas = pointer.Int32Ptr(2)

invalidNamespace := before.DeepCopy()
invalidNamespace.Spec.InfrastructureTemplate.Namespace = "bar"
invalidNamespace.Spec.MachineTemplate.InfrastructureRef.Namespace = "bar"

missingReplicas := before.DeepCopy()
missingReplicas.Spec.Replicas = nil
Expand Down Expand Up @@ -804,9 +821,13 @@ func TestKubeadmControlPlaneValidateUpdateAfterDefaulting(t *testing.T) {
},
Spec: KubeadmControlPlaneSpec{
Version: "v1.19.0",
InfrastructureTemplate: corev1.ObjectReference{
Namespace: "foo",
Name: "infraTemplate",
MachineTemplate: KubeadmControlPlaneMachineTemplate{
InfrastructureRef: corev1.ObjectReference{
APIVersion: "test/v1alpha1",
Kind: "UnknownInfraMachine",
Namespace: "foo",
Name: "infraTemplate",
},
},
},
}
Expand Down Expand Up @@ -836,7 +857,7 @@ func TestKubeadmControlPlaneValidateUpdateAfterDefaulting(t *testing.T) {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(Succeed())
g.Expect(tt.kcp.Spec.InfrastructureTemplate.Namespace).To(Equal(tt.before.Namespace))
g.Expect(tt.kcp.Spec.MachineTemplate.InfrastructureRef.Namespace).To(Equal(tt.before.Namespace))
g.Expect(tt.kcp.Spec.Version).To(Equal("v1.19.0"))
g.Expect(tt.kcp.Spec.RolloutStrategy.Type).To(Equal(RollingUpdateStrategyType))
g.Expect(tt.kcp.Spec.RolloutStrategy.RollingUpdate.MaxSurge.IntVal).To(Equal(int32(1)))
Expand Down
19 changes: 18 additions & 1 deletion controlplane/kubeadm/api/v1alpha4/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 04c2635

Please sign in to comment.