diff --git a/api/v1alpha4/conversion.go b/api/v1alpha4/conversion.go index c948e53cdaa6..b298b3cd94a1 100644 --- a/api/v1alpha4/conversion.go +++ b/api/v1alpha4/conversion.go @@ -227,3 +227,9 @@ func Convert_v1beta1_Topology_To_v1alpha4_Topology(in *clusterv1.Topology, out * // spec.topology.variables has been added with v1beta1. return autoConvert_v1beta1_Topology_To_v1alpha4_Topology(in, out, s) } + +// Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology is an autogenerated conversion function. +func Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(in *clusterv1.MachineDeploymentTopology, out *MachineDeploymentTopology, s apiconversion.Scope) error { + // MachineDeploymentTopology.FailureDomain has been added with v1beta1. + return autoConvert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(in, out, s) +} diff --git a/api/v1alpha4/zz_generated.conversion.go b/api/v1alpha4/zz_generated.conversion.go index 9930a1726b1c..b467aaa68b3b 100644 --- a/api/v1alpha4/zz_generated.conversion.go +++ b/api/v1alpha4/zz_generated.conversion.go @@ -280,11 +280,6 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*v1beta1.MachineDeploymentTopology)(nil), (*MachineDeploymentTopology)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(a.(*v1beta1.MachineDeploymentTopology), b.(*MachineDeploymentTopology), scope) - }); err != nil { - return err - } if err := s.AddGeneratedConversionFunc((*MachineHealthCheck)(nil), (*v1beta1.MachineHealthCheck)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha4_MachineHealthCheck_To_v1beta1_MachineHealthCheck(a.(*MachineHealthCheck), b.(*v1beta1.MachineHealthCheck), scope) }); err != nil { @@ -475,6 +470,11 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*v1beta1.MachineDeploymentTopology)(nil), (*MachineDeploymentTopology)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(a.(*v1beta1.MachineDeploymentTopology), b.(*MachineDeploymentTopology), scope) + }); err != nil { + return err + } if err := s.AddConversionFunc((*v1beta1.Topology)(nil), (*Topology)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1beta1_Topology_To_v1alpha4_Topology(a.(*v1beta1.Topology), b.(*Topology), scope) }); err != nil { @@ -1231,15 +1231,11 @@ func autoConvert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeployment } out.Class = in.Class out.Name = in.Name + // WARNING: in.FailureDomain requires manual conversion: does not exist in peer-type out.Replicas = (*int32)(unsafe.Pointer(in.Replicas)) return nil } -// Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology is an autogenerated conversion function. -func Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(in *v1beta1.MachineDeploymentTopology, out *MachineDeploymentTopology, s conversion.Scope) error { - return autoConvert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(in, out, s) -} - func autoConvert_v1alpha4_MachineHealthCheck_To_v1beta1_MachineHealthCheck(in *MachineHealthCheck, out *v1beta1.MachineHealthCheck, s conversion.Scope) error { out.ObjectMeta = in.ObjectMeta if err := Convert_v1alpha4_MachineHealthCheckSpec_To_v1beta1_MachineHealthCheckSpec(&in.Spec, &out.Spec, s); err != nil { @@ -1697,7 +1693,15 @@ func autoConvert_v1alpha4_Topology_To_v1beta1_Topology(in *Topology, out *v1beta if err := Convert_v1alpha4_ControlPlaneTopology_To_v1beta1_ControlPlaneTopology(&in.ControlPlane, &out.ControlPlane, s); err != nil { return err } - out.Workers = (*v1beta1.WorkersTopology)(unsafe.Pointer(in.Workers)) + if in.Workers != nil { + in, out := &in.Workers, &out.Workers + *out = new(v1beta1.WorkersTopology) + if err := Convert_v1alpha4_WorkersTopology_To_v1beta1_WorkersTopology(*in, *out, s); err != nil { + return err + } + } else { + out.Workers = nil + } return nil } @@ -1713,7 +1717,15 @@ func autoConvert_v1beta1_Topology_To_v1alpha4_Topology(in *v1beta1.Topology, out if err := Convert_v1beta1_ControlPlaneTopology_To_v1alpha4_ControlPlaneTopology(&in.ControlPlane, &out.ControlPlane, s); err != nil { return err } - out.Workers = (*WorkersTopology)(unsafe.Pointer(in.Workers)) + if in.Workers != nil { + in, out := &in.Workers, &out.Workers + *out = new(WorkersTopology) + if err := Convert_v1beta1_WorkersTopology_To_v1alpha4_WorkersTopology(*in, *out, s); err != nil { + return err + } + } else { + out.Workers = nil + } // WARNING: in.Variables requires manual conversion: does not exist in peer-type return nil } @@ -1763,7 +1775,17 @@ func Convert_v1beta1_WorkersClass_To_v1alpha4_WorkersClass(in *v1beta1.WorkersCl } func autoConvert_v1alpha4_WorkersTopology_To_v1beta1_WorkersTopology(in *WorkersTopology, out *v1beta1.WorkersTopology, s conversion.Scope) error { - out.MachineDeployments = *(*[]v1beta1.MachineDeploymentTopology)(unsafe.Pointer(&in.MachineDeployments)) + if in.MachineDeployments != nil { + in, out := &in.MachineDeployments, &out.MachineDeployments + *out = make([]v1beta1.MachineDeploymentTopology, len(*in)) + for i := range *in { + if err := Convert_v1alpha4_MachineDeploymentTopology_To_v1beta1_MachineDeploymentTopology(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.MachineDeployments = nil + } return nil } @@ -1773,7 +1795,17 @@ func Convert_v1alpha4_WorkersTopology_To_v1beta1_WorkersTopology(in *WorkersTopo } func autoConvert_v1beta1_WorkersTopology_To_v1alpha4_WorkersTopology(in *v1beta1.WorkersTopology, out *WorkersTopology, s conversion.Scope) error { - out.MachineDeployments = *(*[]MachineDeploymentTopology)(unsafe.Pointer(&in.MachineDeployments)) + if in.MachineDeployments != nil { + in, out := &in.MachineDeployments, &out.MachineDeployments + *out = make([]MachineDeploymentTopology, len(*in)) + for i := range *in { + if err := Convert_v1beta1_MachineDeploymentTopology_To_v1alpha4_MachineDeploymentTopology(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.MachineDeployments = nil + } return nil } diff --git a/api/v1beta1/cluster_types.go b/api/v1beta1/cluster_types.go index cffb72eee316..a619e905d80b 100644 --- a/api/v1beta1/cluster_types.go +++ b/api/v1beta1/cluster_types.go @@ -143,6 +143,11 @@ type MachineDeploymentTopology struct { // the values are hashed together. Name string `json:"name"` + // FailureDomain is the failure domain the machines will be created in. + // Must match a key in the FailureDomains map stored on the cluster object. + // +optional + FailureDomain *string `json:"failureDomain,omitempty"` + // Replicas is the number of worker nodes belonging to this set. // If the value is nil, the MachineDeployment is created without the number of Replicas (defaulting to zero) // and it's assumed that an external entity (like cluster autoscaler) is responsible for the management diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index cd0aa99c5217..d168397fed83 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -897,6 +897,11 @@ func (in *MachineDeploymentStrategy) DeepCopy() *MachineDeploymentStrategy { func (in *MachineDeploymentTopology) DeepCopyInto(out *MachineDeploymentTopology) { *out = *in in.Metadata.DeepCopyInto(&out.Metadata) + if in.FailureDomain != nil { + in, out := &in.FailureDomain, &out.FailureDomain + *out = new(string) + **out = **in + } if in.Replicas != nil { in, out := &in.Replicas, &out.Replicas *out = new(int32) diff --git a/config/crd/bases/cluster.x-k8s.io_clusters.yaml b/config/crd/bases/cluster.x-k8s.io_clusters.yaml index c6ed190b5edd..accb8ca852ae 100644 --- a/config/crd/bases/cluster.x-k8s.io_clusters.yaml +++ b/config/crd/bases/cluster.x-k8s.io_clusters.yaml @@ -922,6 +922,11 @@ spec: ClusterClass object mentioned in the `Cluster.Spec.Class` field. type: string + failureDomain: + description: FailureDomain is the failure domain the + machines will be created in. Must match a key in the + FailureDomains map stored on the cluster object. + type: string metadata: description: Metadata is the metadata applied to the machines of the MachineDeployment. At runtime this diff --git a/controllers/topology/desired_state.go b/controllers/topology/desired_state.go index 96e43ee8311d..450b994389f7 100644 --- a/controllers/topology/desired_state.go +++ b/controllers/topology/desired_state.go @@ -435,6 +435,7 @@ func computeMachineDeployment(_ context.Context, s *scope.Scope, desiredControlP Version: pointer.String(version), Bootstrap: clusterv1.Bootstrap{ConfigRef: contract.ObjToRef(desiredMachineDeployment.BootstrapTemplate)}, InfrastructureRef: *contract.ObjToRef(desiredMachineDeployment.InfrastructureMachineTemplate), + FailureDomain: machineDeploymentTopology.FailureDomain, }, }, }, diff --git a/controllers/topology/desired_state_test.go b/controllers/topology/desired_state_test.go index 7710933cd1bd..0758685e0b3e 100644 --- a/controllers/topology/desired_state_test.go +++ b/controllers/topology/desired_state_test.go @@ -226,7 +226,7 @@ func TestComputeControlPlane(t *testing.T) { clusterClass := builder.ClusterClass(metav1.NamespaceDefault, "class1"). WithControlPlaneMetadata(labels, annotations). WithControlPlaneTemplate(controlPlaneTemplate).Build() - //TODO: Replace with object builder. + // TODO: Replace with object builder. // current cluster objects version := "v1.21.2" replicas := int32(3) @@ -722,13 +722,15 @@ func TestComputeMachineDeployment(t *testing.T) { } replicas := int32(5) + failureDomain := "always-up-region" mdTopology := clusterv1.MachineDeploymentTopology{ Metadata: clusterv1.ObjectMeta{ Labels: map[string]string{"foo": "baz"}, }, - Class: "linux-worker", - Name: "big-pool-of-machines", - Replicas: &replicas, + Class: "linux-worker", + Name: "big-pool-of-machines", + Replicas: &replicas, + FailureDomain: &failureDomain, } t.Run("Generates the machine deployment and the referenced templates", func(t *testing.T) { @@ -755,6 +757,7 @@ func TestComputeMachineDeployment(t *testing.T) { actualMd := actual.Object g.Expect(*actualMd.Spec.Replicas).To(Equal(replicas)) + g.Expect(*actualMd.Spec.Template.Spec.FailureDomain).To(Equal(failureDomain)) g.Expect(actualMd.Spec.ClusterName).To(Equal("cluster1")) g.Expect(actualMd.Name).To(ContainSubstring("cluster1")) g.Expect(actualMd.Name).To(ContainSubstring("big-pool-of-machines")) @@ -810,6 +813,7 @@ func TestComputeMachineDeployment(t *testing.T) { actualMd := actual.Object g.Expect(*actualMd.Spec.Replicas).NotTo(Equal(currentReplicas)) + g.Expect(*actualMd.Spec.Template.Spec.FailureDomain).To(Equal(failureDomain)) g.Expect(actualMd.Name).To(Equal("existing-deployment-1")) g.Expect(actualMd.Labels).To(HaveKeyWithValue(clusterv1.ClusterTopologyMachineDeploymentLabelName, "big-pool-of-machines"))