Skip to content

Commit

Permalink
feat: expose custom labels for control plane machines (#59)
Browse files Browse the repository at this point in the history
* feat: expose custom labels for control plane machines
  • Loading branch information
lukebond committed Oct 14, 2023
1 parent c7047a7 commit 962aad7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions controlplane/api/v1beta1/kthreescontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions controlplane/api/v1beta1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions controlplane/controllers/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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")),
},
Expand Down
17 changes: 10 additions & 7 deletions pkg/k3s/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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")),
},
Expand Down

0 comments on commit 962aad7

Please sign in to comment.