Skip to content

Commit 962aad7

Browse files
authored
feat: expose custom labels for control plane machines (#59)
* feat: expose custom labels for control plane machines
1 parent c7047a7 commit 962aad7

File tree

6 files changed

+100
-10
lines changed

6 files changed

+100
-10
lines changed

bootstrap/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,32 @@ spec:
288288
description: Version specifies the k3s version
289289
type: string
290290
type: object
291+
machineTemplate:
292+
description: MachineTemplate contains information about how machines
293+
should be shaped when creating or updating a control plane.
294+
properties:
295+
metadata:
296+
description: 'Standard object''s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata'
297+
properties:
298+
annotations:
299+
additionalProperties:
300+
type: string
301+
description: 'Annotations is an unstructured key value map
302+
stored with a resource that may be set by external tools
303+
to store and retrieve arbitrary metadata. They are not queryable
304+
and should be preserved when modifying objects. More info:
305+
http://kubernetes.io/docs/user-guide/annotations'
306+
type: object
307+
labels:
308+
additionalProperties:
309+
type: string
310+
description: 'Map of string keys and values that can be used
311+
to organize and categorize (scope and select) objects. May
312+
match selectors of replication controllers and services.
313+
More info: http://kubernetes.io/docs/user-guide/labels'
314+
type: object
315+
type: object
316+
type: object
291317
nodeDrainTimeout:
292318
description: 'NodeDrainTimeout is the total amount of time that the
293319
controller will spend on draining a controlplane node The default

controlplane/api/v1beta1/kthreescontrolplane_types.go

+18
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type KThreesControlPlaneSpec struct {
4949

5050
// InfrastructureTemplate is a required reference to a custom resource
5151
// offered by an infrastructure provider.
52+
// In the next API version we will move this into the
53+
// `KThreesControlPlaneMachineTemplate` struct. See
54+
// https://github.com/cluster-api-provider-k3s/cluster-api-k3s/issues/62
5255
InfrastructureTemplate corev1.ObjectReference `json:"infrastructureTemplate"`
5356

5457
// KThreesConfigSpec is a KThreesConfigSpec
@@ -67,6 +70,21 @@ type KThreesControlPlaneSpec struct {
6770
// NOTE: NodeDrainTimeout is different from `kubectl drain --timeout`
6871
// +optional
6972
NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"`
73+
74+
// MachineTemplate contains information about how machines should be shaped
75+
// when creating or updating a control plane.
76+
MachineTemplate KThreesControlPlaneMachineTemplate `json:"machineTemplate,omitempty"`
77+
}
78+
79+
// MachineTemplate contains information about how machines should be shaped
80+
// when creating or updating a control plane.
81+
// In the next API version we will move the InfrastructureTemplate field into
82+
// this struct. See https://github.com/cluster-api-provider-k3s/cluster-api-k3s/issues/62
83+
type KThreesControlPlaneMachineTemplate struct {
84+
// Standard object's metadata.
85+
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
86+
// +optional
87+
ObjectMeta clusterv1.ObjectMeta `json:"metadata,omitempty"`
7088
}
7189

7290
// KThreesControlPlaneStatus defines the observed state of KThreesControlPlane.

controlplane/api/v1beta1/zz_generated.deepcopy.go

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,32 @@ spec:
288288
description: Version specifies the k3s version
289289
type: string
290290
type: object
291+
machineTemplate:
292+
description: MachineTemplate contains information about how machines
293+
should be shaped when creating or updating a control plane.
294+
properties:
295+
metadata:
296+
description: 'Standard object''s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata'
297+
properties:
298+
annotations:
299+
additionalProperties:
300+
type: string
301+
description: 'Annotations is an unstructured key value map
302+
stored with a resource that may be set by external tools
303+
to store and retrieve arbitrary metadata. They are not queryable
304+
and should be preserved when modifying objects. More info:
305+
http://kubernetes.io/docs/user-guide/annotations'
306+
type: object
307+
labels:
308+
additionalProperties:
309+
type: string
310+
description: 'Map of string keys and values that can be used
311+
to organize and categorize (scope and select) objects. May
312+
match selectors of replication controllers and services.
313+
More info: http://kubernetes.io/docs/user-guide/labels'
314+
type: object
315+
type: object
316+
type: object
291317
nodeDrainTimeout:
292318
description: 'NodeDrainTimeout is the total amount of time that the
293319
controller will spend on draining a controlplane node The default

controlplane/controllers/scale.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (r *KThreesControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
256256
Namespace: kcp.Namespace,
257257
OwnerRef: infraCloneOwner,
258258
ClusterName: cluster.Name,
259-
Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name),
259+
Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name, kcp.Spec.MachineTemplate),
260260
})
261261
if err != nil {
262262
// Safe to return early here since no resources have been created yet.
@@ -321,7 +321,7 @@ func (r *KThreesControlPlaneReconciler) generateKThreesConfig(ctx context.Contex
321321
ObjectMeta: metav1.ObjectMeta{
322322
Name: names.SimpleNameGenerator.GenerateName(kcp.Name + "-"),
323323
Namespace: kcp.Namespace,
324-
Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name),
324+
Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name, kcp.Spec.MachineTemplate),
325325
OwnerReferences: []metav1.OwnerReference{owner},
326326
},
327327
Spec: *spec,
@@ -347,7 +347,7 @@ func (r *KThreesControlPlaneReconciler) generateMachine(ctx context.Context, kcp
347347
ObjectMeta: metav1.ObjectMeta{
348348
Name: names.SimpleNameGenerator.GenerateName(kcp.Name + "-"),
349349
Namespace: kcp.Namespace,
350-
Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name),
350+
Labels: k3s.ControlPlaneLabelsForCluster(cluster.Name, kcp.Spec.MachineTemplate),
351351
OwnerReferences: []metav1.OwnerReference{
352352
*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KThreesControlPlane")),
353353
},

pkg/k3s/control_plane.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (c *ControlPlane) GenerateKThreesConfig(spec *bootstrapv1.KThreesConfigSpec
198198
ObjectMeta: metav1.ObjectMeta{
199199
Name: names.SimpleNameGenerator.GenerateName(c.KCP.Name + "-"),
200200
Namespace: c.KCP.Namespace,
201-
Labels: ControlPlaneLabelsForCluster(c.Cluster.Name),
201+
Labels: ControlPlaneLabelsForCluster(c.Cluster.Name, c.KCP.Spec.MachineTemplate),
202202
OwnerReferences: []metav1.OwnerReference{owner},
203203
},
204204
Spec: *spec,
@@ -207,12 +207,15 @@ func (c *ControlPlane) GenerateKThreesConfig(spec *bootstrapv1.KThreesConfigSpec
207207
}
208208

209209
// ControlPlaneLabelsForCluster returns a set of labels to add to a control plane machine for this specific cluster.
210-
func ControlPlaneLabelsForCluster(clusterName string) map[string]string {
211-
return map[string]string{
212-
clusterv1.ClusterNameLabel: clusterName,
213-
clusterv1.MachineControlPlaneNameLabel: "",
214-
clusterv1.MachineControlPlaneLabel: "",
210+
func ControlPlaneLabelsForCluster(clusterName string, machineTemplate controlplanev1.KThreesControlPlaneMachineTemplate) map[string]string {
211+
labels := make(map[string]string)
212+
for key, value := range machineTemplate.ObjectMeta.Labels {
213+
labels[key] = value
215214
}
215+
labels[clusterv1.ClusterNameLabel] = clusterName
216+
labels[clusterv1.MachineControlPlaneNameLabel] = ""
217+
labels[clusterv1.MachineControlPlaneLabel] = ""
218+
return labels
216219
}
217220

218221
// 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
221224
ObjectMeta: metav1.ObjectMeta{
222225
Name: names.SimpleNameGenerator.GenerateName(c.KCP.Name + "-"),
223226
Namespace: c.KCP.Namespace,
224-
Labels: ControlPlaneLabelsForCluster(c.Cluster.Name),
227+
Labels: ControlPlaneLabelsForCluster(c.Cluster.Name, c.KCP.Spec.MachineTemplate),
225228
OwnerReferences: []metav1.OwnerReference{
226229
*metav1.NewControllerRef(c.KCP, controlplanev1.GroupVersion.WithKind("KThreesControlPlane")),
227230
},

0 commit comments

Comments
 (0)