Skip to content

Commit f67271b

Browse files
authored
Add app.kubernetes.io/component label (#1343)
* Add app.kubernetes.io/component label Signed-off-by: kahirokunn <okinakahiro@gmail.com> * Centralize component and app.kubernetes.io/component values as constants Signed-off-by: kahirokunn <okinakahiro@gmail.com> * Use entrypoint and cluster-config for ConfigMap component labels Signed-off-by: kahirokunn <okinakahiro@gmail.com> * Keep selectors unchanged for StatefulSet/Deployment upgrade compatibility Signed-off-by: kahirokunn <okinakahiro@gmail.com> * Suppress revive var-naming lint for internal/controller/util package Signed-off-by: kahirokunn <okinakahiro@gmail.com> --------- Signed-off-by: kahirokunn <okinakahiro@gmail.com>
1 parent 8e16efc commit f67271b

17 files changed

+214
-47
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ linters:
5353
- name: redefines-builtin-id
5454
exclusions:
5555
generated: strict
56+
rules:
57+
- linters: [revive]
58+
text: "var-naming: avoid meaningless package names"
59+
path: "internal/controller/util/"
5660
paths:
5761
# Ignore issues in generated files.
5862
- zz_generated.*\.go$

config/samples/_v1beta1_k0smotroncluster.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ metadata:
55
app.kubernetes.io/name: cluster
66
app.kubernetes.io/instance: sample
77
app.kubernetes.io/part-of: k0smotron
8+
app.kubernetes.io/component: cluster
89
app.kubernetes.io/managed-by: kustomize
910
app.kubernetes.io/created-by: k0smotron
1011
name: sample

internal/controller/bootstrap/controlplane_bootstrap_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ func (c *ControlPlaneController) Reconcile(ctx context.Context, req ctrl.Request
296296
Name: config.Name,
297297
Namespace: config.Namespace,
298298
Labels: map[string]string{
299-
clusterv1.ClusterNameLabel: scope.Cluster.Name,
299+
clusterv1.ClusterNameLabel: scope.Cluster.Name,
300+
"app.kubernetes.io/component": util.ComponentBootstrap,
300301
},
301302
OwnerReferences: []metav1.OwnerReference{
302303
{

internal/controller/bootstrap/worker_bootstrap_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ func (r *Controller) resolveFilesForIngress(ctx context.Context, scope *Scope) (
467467
func createBootstrapSecret(scope *Scope, bootstrapData []byte, format string) *corev1.Secret {
468468
// Initialize labels with cluster-name label
469469
labels := map[string]string{
470-
clusterv1.ClusterNameLabel: scope.Cluster.Name,
470+
clusterv1.ClusterNameLabel: scope.Cluster.Name,
471+
"app.kubernetes.io/component": util.ComponentBootstrap,
471472
}
472473

473474
// Copy labels from secretMetadata if specified

internal/controller/controlplane/k0s_controlplane_controller.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,9 @@ token = ` + frpToken + `
793793
ObjectMeta: metav1.ObjectMeta{
794794
Name: frpsCMName,
795795
Namespace: kcp.GetNamespace(),
796+
Labels: map[string]string{
797+
"app.kubernetes.io/component": util.ComponentTunneling,
798+
},
796799
},
797800
Data: map[string]string{
798801
"frps.ini": frpsConfig,
@@ -805,6 +808,11 @@ token = ` + frpToken + `
805808
return fmt.Errorf("error creating ConfigMap: %w", err)
806809
}
807810

811+
// Deployment selector is immutable after creation. Add app.kubernetes.io/component only to metadata and template labels.
812+
frpsSelectorLabels := map[string]string{
813+
"k0smotron_cluster": kcp.GetName(),
814+
"app": "frps",
815+
}
808816
frpsDeployment := appsv1.Deployment{
809817
TypeMeta: metav1.TypeMeta{
810818
APIVersion: "apps/v1",
@@ -813,19 +821,22 @@ token = ` + frpToken + `
813821
ObjectMeta: metav1.ObjectMeta{
814822
Name: fmt.Sprintf(FRPDeploymentNameTemplate, kcp.GetName()),
815823
Namespace: kcp.GetNamespace(),
824+
Labels: map[string]string{
825+
"k0smotron_cluster": kcp.GetName(),
826+
"app": "frps",
827+
"app.kubernetes.io/component": util.ComponentTunneling,
828+
},
816829
},
817830
Spec: appsv1.DeploymentSpec{
818831
Selector: &metav1.LabelSelector{
819-
MatchLabels: map[string]string{
820-
"k0smotron_cluster": kcp.GetName(),
821-
"app": "frps",
822-
},
832+
MatchLabels: frpsSelectorLabels,
823833
},
824834
Template: corev1.PodTemplateSpec{
825835
ObjectMeta: metav1.ObjectMeta{
826836
Labels: map[string]string{
827-
"k0smotron_cluster": kcp.GetName(),
828-
"app": "frps",
837+
"k0smotron_cluster": kcp.GetName(),
838+
"app": "frps",
839+
"app.kubernetes.io/component": util.ComponentTunneling,
829840
},
830841
},
831842
Spec: corev1.PodSpec{
@@ -882,12 +893,14 @@ token = ` + frpToken + `
882893
ObjectMeta: metav1.ObjectMeta{
883894
Name: fmt.Sprintf(FRPServiceNameTemplate, kcp.GetName()),
884895
Namespace: kcp.GetNamespace(),
896+
Labels: map[string]string{
897+
"k0smotron_cluster": kcp.GetName(),
898+
"app": "frps",
899+
"app.kubernetes.io/component": util.ComponentTunneling,
900+
},
885901
},
886902
Spec: corev1.ServiceSpec{
887-
Selector: map[string]string{
888-
"k0smotron_cluster": kcp.GetName(),
889-
"app": "frps",
890-
},
903+
Selector: frpsSelectorLabels,
891904
Ports: []corev1.ServicePort{{
892905
Name: "api",
893906
Protocol: corev1.ProtocolTCP,

internal/controller/k0smotron.io/jointokenrequest_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ func (r *JoinTokenRequestReconciler) reconcileSecret(ctx context.Context, jtr km
173173

174174
func (r *JoinTokenRequestReconciler) generateSecret(jtr *km.JoinTokenRequest, token string) (v1.Secret, error) {
175175
labels := map[string]string{
176-
clusterLabel: jtr.Spec.ClusterRef.Name,
177-
"k0smotron.io/cluster-uid": string(jtr.Status.ClusterUID),
178-
"k0smotron.io/role": jtr.Spec.Role,
179-
"k0smotron.io/token-request": jtr.Name,
176+
clusterLabel: jtr.Spec.ClusterRef.Name,
177+
"k0smotron.io/cluster-uid": string(jtr.Status.ClusterUID),
178+
"k0smotron.io/role": jtr.Spec.Role,
179+
"k0smotron.io/token-request": jtr.Name,
180+
"app.kubernetes.io/component": util.ComponentJointoken,
180181
}
181182
for k, v := range jtr.Labels {
182183
labels[k] = v

internal/controller/k0smotron.io/k0smotroncluster_configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (scope *kmcScope) generateConfig(kmc *km.Cluster, sans []string) (v1.Config
100100
ObjectMeta: metav1.ObjectMeta{
101101
Name: kmc.GetConfigMapName(),
102102
Namespace: kmc.Namespace,
103-
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
103+
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentClusterConfig),
104104
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
105105
},
106106
Data: map[string]string{

internal/controller/k0smotron.io/k0smotroncluster_entrypoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (scope *kmcScope) generateEntrypointCM(kmc *km.Cluster) (v1.ConfigMap, erro
5656
ObjectMeta: metav1.ObjectMeta{
5757
Name: kmc.GetEntrypointConfigMapName(),
5858
Namespace: kmc.Namespace,
59-
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
59+
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentEntrypoint),
6060
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
6161
},
6262
Data: map[string]string{

internal/controller/k0smotron.io/k0smotroncluster_etcd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ func (scope *kmcScope) reconcileEtcdStatefulSet(ctx context.Context, kmc *km.Clu
218218
}
219219

220220
func generateEtcdStatefulSet(kmc *km.Cluster, existingSts *apps.StatefulSet, replicas int32) apps.StatefulSet {
221-
labels := kcontrollerutil.LabelsForEtcdK0smotronCluster(kmc)
221+
// StatefulSet selector is immutable after creation. Add app.kubernetes.io/component only to metadata and template labels.
222+
selectorLabels := kcontrollerutil.LabelsForEtcdK0smotronCluster(kmc)
223+
labels := make(map[string]string, len(selectorLabels)+1)
224+
for k, v := range selectorLabels {
225+
labels[k] = v
226+
}
227+
labels["app.kubernetes.io/component"] = kcontrollerutil.ComponentEtcd
222228

223229
size := kmc.Spec.Etcd.Persistence.Size
224230

@@ -263,7 +269,7 @@ func generateEtcdStatefulSet(kmc *km.Cluster, existingSts *apps.StatefulSet, rep
263269
Spec: apps.StatefulSetSpec{
264270
ServiceName: kmc.GetEtcdServiceName(),
265271
Selector: &metav1.LabelSelector{
266-
MatchLabels: labels,
272+
MatchLabels: selectorLabels,
267273
},
268274
Replicas: &replicas,
269275
PodManagementPolicy: apps.ParallelPodManagement,

internal/controller/k0smotron.io/k0smotroncluster_ingress.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (scope *kmcScope) generateIngressManifestsConfigMap(kmc *km.Cluster) (corev
8888
ObjectMeta: metav1.ObjectMeta{
8989
Name: kmc.GetIngressManifestsConfigMapName(),
9090
Namespace: kmc.Namespace,
91+
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentIngress),
9192
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
9293
},
9394
Data: map[string]string{
@@ -210,6 +211,7 @@ func (scope *kmcScope) generateKonnectivityIngressConfigMap(kmc *km.Cluster) (co
210211
ObjectMeta: metav1.ObjectMeta{
211212
Name: kmc.GetIngressManifestsConfigMapName() + "-konnectivity",
212213
Namespace: kmc.Namespace,
214+
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentIngress),
213215
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
214216
},
215217
Data: map[string]string{
@@ -327,7 +329,7 @@ func (scope *kmcScope) generateIngress(kmc *km.Cluster) v1.Ingress {
327329
Name: kmc.GetIngressName(),
328330
Namespace: kmc.Namespace,
329331
Annotations: annotations,
330-
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
332+
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentIngress),
331333
},
332334
Spec: v1.IngressSpec{
333335
IngressClassName: kmc.Spec.Ingress.ClassName,

0 commit comments

Comments
 (0)