Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose custom labels for control plane machines #59

Merged
merged 5 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bootstrap/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 @@ -283,6 +283,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 {
lukebond marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -181,6 +181,12 @@ spec:
items:
type: string
type: array
kubeSchedulerArgs:
description: KubeSchedulerArgs is a customized flag for kube-scheduler
process
items:
type: string
type: array
serviceCidr:
description: 'ServiceCidr Network CIDR to use for services IPs
(default: "10.43.0.0/16")'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ spec:
items:
type: string
type: array
kubeSchedulerArgs:
description: KubeSchedulerArgs is a customized flag for
kube-scheduler process
items:
type: string
type: array
serviceCidr:
description: 'ServiceCidr Network CIDR to use for services
IPs (default: "10.43.0.0/16")'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ spec:
items:
type: string
type: array
kubeSchedulerArgs:
description: KubeSchedulerArgs is a customized flag for kube-scheduler
process
items:
type: string
type: array
serviceCidr:
description: 'ServiceCidr Network CIDR to use for services
IPs (default: "10.43.0.0/16")'
Expand All @@ -277,6 +283,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
Loading