From 962aad7b968fee93f44221e12c7df2cbc0cf9a6d Mon Sep 17 00:00:00 2001 From: Luke Bond Date: Sat, 14 Oct 2023 14:52:59 +0100 Subject: [PATCH] feat: expose custom labels for control plane machines (#59) * feat: expose custom labels for control plane machines --- ...cluster.x-k8s.io_kthreescontrolplanes.yaml | 26 +++++++++++++++++++ .../api/v1beta1/kthreescontrolplane_types.go | 18 +++++++++++++ .../api/v1beta1/zz_generated.deepcopy.go | 17 ++++++++++++ ...cluster.x-k8s.io_kthreescontrolplanes.yaml | 26 +++++++++++++++++++ controlplane/controllers/scale.go | 6 ++--- pkg/k3s/control_plane.go | 17 +++++++----- 6 files changed, 100 insertions(+), 10 deletions(-) diff --git a/bootstrap/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml b/bootstrap/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml index ab498d29..067dbfa2 100644 --- a/bootstrap/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml +++ b/bootstrap/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml @@ -288,6 +288,32 @@ spec: description: Version specifies the k3s version type: string type: object + machineTemplate: + description: MachineTemplate contains information about how machines + should be shaped when creating or updating a control plane. + properties: + metadata: + description: 'Standard object''s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata' + properties: + annotations: + additionalProperties: + type: string + description: 'Annotations is an unstructured key value map + stored with a resource that may be set by external tools + to store and retrieve arbitrary metadata. They are not queryable + and should be preserved when modifying objects. More info: + http://kubernetes.io/docs/user-guide/annotations' + type: object + labels: + additionalProperties: + type: string + description: 'Map of string keys and values that can be used + to organize and categorize (scope and select) objects. May + match selectors of replication controllers and services. + More info: http://kubernetes.io/docs/user-guide/labels' + type: object + type: object + type: object nodeDrainTimeout: description: 'NodeDrainTimeout is the total amount of time that the controller will spend on draining a controlplane node The default diff --git a/controlplane/api/v1beta1/kthreescontrolplane_types.go b/controlplane/api/v1beta1/kthreescontrolplane_types.go index 3559080d..fc184562 100644 --- a/controlplane/api/v1beta1/kthreescontrolplane_types.go +++ b/controlplane/api/v1beta1/kthreescontrolplane_types.go @@ -49,6 +49,9 @@ type KThreesControlPlaneSpec struct { // InfrastructureTemplate is a required reference to a custom resource // offered by an infrastructure provider. + // In the next API version we will move this into the + // `KThreesControlPlaneMachineTemplate` struct. See + // https://github.com/cluster-api-provider-k3s/cluster-api-k3s/issues/62 InfrastructureTemplate corev1.ObjectReference `json:"infrastructureTemplate"` // KThreesConfigSpec is a KThreesConfigSpec @@ -67,6 +70,21 @@ type KThreesControlPlaneSpec struct { // NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` // +optional NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"` + + // MachineTemplate contains information about how machines should be shaped + // when creating or updating a control plane. + MachineTemplate KThreesControlPlaneMachineTemplate `json:"machineTemplate,omitempty"` +} + +// MachineTemplate contains information about how machines should be shaped +// when creating or updating a control plane. +// In the next API version we will move the InfrastructureTemplate field into +// this struct. See https://github.com/cluster-api-provider-k3s/cluster-api-k3s/issues/62 +type KThreesControlPlaneMachineTemplate 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"` } // KThreesControlPlaneStatus defines the observed state of KThreesControlPlane. diff --git a/controlplane/api/v1beta1/zz_generated.deepcopy.go b/controlplane/api/v1beta1/zz_generated.deepcopy.go index e95d9e8f..9550c57e 100644 --- a/controlplane/api/v1beta1/zz_generated.deepcopy.go +++ b/controlplane/api/v1beta1/zz_generated.deepcopy.go @@ -86,6 +86,22 @@ func (in *KThreesControlPlaneList) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *KThreesControlPlaneMachineTemplate) DeepCopyInto(out *KThreesControlPlaneMachineTemplate) { + *out = *in + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KThreesControlPlaneMachineTemplate. +func (in *KThreesControlPlaneMachineTemplate) DeepCopy() *KThreesControlPlaneMachineTemplate { + if in == nil { + return nil + } + out := new(KThreesControlPlaneMachineTemplate) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *KThreesControlPlaneSpec) DeepCopyInto(out *KThreesControlPlaneSpec) { *out = *in @@ -105,6 +121,7 @@ func (in *KThreesControlPlaneSpec) DeepCopyInto(out *KThreesControlPlaneSpec) { *out = new(v1.Duration) **out = **in } + in.MachineTemplate.DeepCopyInto(&out.MachineTemplate) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KThreesControlPlaneSpec. diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml index ab498d29..067dbfa2 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml @@ -288,6 +288,32 @@ spec: description: Version specifies the k3s version type: string type: object + machineTemplate: + description: MachineTemplate contains information about how machines + should be shaped when creating or updating a control plane. + properties: + metadata: + description: 'Standard object''s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata' + properties: + annotations: + additionalProperties: + type: string + description: 'Annotations is an unstructured key value map + stored with a resource that may be set by external tools + to store and retrieve arbitrary metadata. They are not queryable + and should be preserved when modifying objects. More info: + http://kubernetes.io/docs/user-guide/annotations' + type: object + labels: + additionalProperties: + type: string + description: 'Map of string keys and values that can be used + to organize and categorize (scope and select) objects. May + match selectors of replication controllers and services. + More info: http://kubernetes.io/docs/user-guide/labels' + type: object + type: object + type: object nodeDrainTimeout: description: 'NodeDrainTimeout is the total amount of time that the controller will spend on draining a controlplane node The default diff --git a/controlplane/controllers/scale.go b/controlplane/controllers/scale.go index 07c0303a..a1666bbd 100644 --- a/controlplane/controllers/scale.go +++ b/controlplane/controllers/scale.go @@ -256,7 +256,7 @@ func (r *KThreesControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte Namespace: kcp.Namespace, OwnerRef: infraCloneOwner, ClusterName: cluster.Name, - Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name), + Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name, kcp.Spec.MachineTemplate), }) if err != nil { // Safe to return early here since no resources have been created yet. @@ -321,7 +321,7 @@ func (r *KThreesControlPlaneReconciler) generateKThreesConfig(ctx context.Contex ObjectMeta: metav1.ObjectMeta{ Name: names.SimpleNameGenerator.GenerateName(kcp.Name + "-"), Namespace: kcp.Namespace, - Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name), + Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name, kcp.Spec.MachineTemplate), OwnerReferences: []metav1.OwnerReference{owner}, }, Spec: *spec, @@ -347,7 +347,7 @@ func (r *KThreesControlPlaneReconciler) generateMachine(ctx context.Context, kcp ObjectMeta: metav1.ObjectMeta{ Name: names.SimpleNameGenerator.GenerateName(kcp.Name + "-"), Namespace: kcp.Namespace, - Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name), + Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name, kcp.Spec.MachineTemplate), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KThreesControlPlane")), }, diff --git a/pkg/k3s/control_plane.go b/pkg/k3s/control_plane.go index af0e169b..162a0b6e 100644 --- a/pkg/k3s/control_plane.go +++ b/pkg/k3s/control_plane.go @@ -198,7 +198,7 @@ func (c *ControlPlane) GenerateKThreesConfig(spec *bootstrapv1.KThreesConfigSpec ObjectMeta: metav1.ObjectMeta{ Name: names.SimpleNameGenerator.GenerateName(c.KCP.Name + "-"), Namespace: c.KCP.Namespace, - Labels: ControlPlaneLabelsForCluster(c.Cluster.Name), + Labels: ControlPlaneLabelsForCluster(c.Cluster.Name, c.KCP.Spec.MachineTemplate), OwnerReferences: []metav1.OwnerReference{owner}, }, Spec: *spec, @@ -207,12 +207,15 @@ func (c *ControlPlane) GenerateKThreesConfig(spec *bootstrapv1.KThreesConfigSpec } // ControlPlaneLabelsForCluster returns a set of labels to add to a control plane machine for this specific cluster. -func ControlPlaneLabelsForCluster(clusterName string) map[string]string { - return map[string]string{ - clusterv1.ClusterNameLabel: clusterName, - clusterv1.MachineControlPlaneNameLabel: "", - clusterv1.MachineControlPlaneLabel: "", +func ControlPlaneLabelsForCluster(clusterName string, machineTemplate controlplanev1.KThreesControlPlaneMachineTemplate) map[string]string { + labels := make(map[string]string) + for key, value := range machineTemplate.ObjectMeta.Labels { + labels[key] = value } + labels[clusterv1.ClusterNameLabel] = clusterName + labels[clusterv1.MachineControlPlaneNameLabel] = "" + labels[clusterv1.MachineControlPlaneLabel] = "" + return labels } // NewMachine returns a machine configured to be a part of the control plane. @@ -221,7 +224,7 @@ func (c *ControlPlane) NewMachine(infraRef, bootstrapRef *corev1.ObjectReference ObjectMeta: metav1.ObjectMeta{ Name: names.SimpleNameGenerator.GenerateName(c.KCP.Name + "-"), Namespace: c.KCP.Namespace, - Labels: ControlPlaneLabelsForCluster(c.Cluster.Name), + Labels: ControlPlaneLabelsForCluster(c.Cluster.Name, c.KCP.Spec.MachineTemplate), OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(c.KCP, controlplanev1.GroupVersion.WithKind("KThreesControlPlane")), },