Skip to content
Open
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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ linters:
- name: redefines-builtin-id
exclusions:
generated: strict
rules:
- linters: [revive]
text: "var-naming: avoid meaningless package names"
path: "internal/controller/util/"
paths:
# Ignore issues in generated files.
- zz_generated.*\.go$
Expand Down
1 change: 1 addition & 0 deletions config/samples/_v1beta1_k0smotroncluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata:
app.kubernetes.io/name: cluster
app.kubernetes.io/instance: sample
app.kubernetes.io/part-of: k0smotron
app.kubernetes.io/component: cluster
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: k0smotron
name: sample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ func (c *ControlPlaneController) Reconcile(ctx context.Context, req ctrl.Request
Name: config.Name,
Namespace: config.Namespace,
Labels: map[string]string{
clusterv1.ClusterNameLabel: scope.Cluster.Name,
clusterv1.ClusterNameLabel: scope.Cluster.Name,
"app.kubernetes.io/component": util.ComponentBootstrap,
},
OwnerReferences: []metav1.OwnerReference{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ func (r *Controller) resolveFilesForIngress(ctx context.Context, scope *Scope) (
func createBootstrapSecret(scope *Scope, bootstrapData []byte, format string) *corev1.Secret {
// Initialize labels with cluster-name label
labels := map[string]string{
clusterv1.ClusterNameLabel: scope.Cluster.Name,
clusterv1.ClusterNameLabel: scope.Cluster.Name,
"app.kubernetes.io/component": util.ComponentBootstrap,
}

// Copy labels from secretMetadata if specified
Expand Down
33 changes: 23 additions & 10 deletions internal/controller/controlplane/k0s_controlplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ token = ` + frpToken + `
ObjectMeta: metav1.ObjectMeta{
Name: frpsCMName,
Namespace: kcp.GetNamespace(),
Labels: map[string]string{
"app.kubernetes.io/component": util.ComponentTunneling,
},
},
Data: map[string]string{
"frps.ini": frpsConfig,
Expand All @@ -805,6 +808,11 @@ token = ` + frpToken + `
return fmt.Errorf("error creating ConfigMap: %w", err)
}

// Deployment selector is immutable after creation. Add app.kubernetes.io/component only to metadata and template labels.
frpsSelectorLabels := map[string]string{
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
}
frpsDeployment := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Expand All @@ -813,19 +821,22 @@ token = ` + frpToken + `
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(FRPDeploymentNameTemplate, kcp.GetName()),
Namespace: kcp.GetNamespace(),
Labels: map[string]string{
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
"app.kubernetes.io/component": util.ComponentTunneling,
},
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
},
MatchLabels: frpsSelectorLabels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
"app.kubernetes.io/component": util.ComponentTunneling,
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -882,12 +893,14 @@ token = ` + frpToken + `
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(FRPServiceNameTemplate, kcp.GetName()),
Namespace: kcp.GetNamespace(),
Labels: map[string]string{
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
"app.kubernetes.io/component": util.ComponentTunneling,
},
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"k0smotron_cluster": kcp.GetName(),
"app": "frps",
},
Selector: frpsSelectorLabels,
Ports: []corev1.ServicePort{{
Name: "api",
Protocol: corev1.ProtocolTCP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ func (r *JoinTokenRequestReconciler) reconcileSecret(ctx context.Context, jtr km

func (r *JoinTokenRequestReconciler) generateSecret(jtr *km.JoinTokenRequest, token string) (v1.Secret, error) {
labels := map[string]string{
clusterLabel: jtr.Spec.ClusterRef.Name,
"k0smotron.io/cluster-uid": string(jtr.Status.ClusterUID),
"k0smotron.io/role": jtr.Spec.Role,
"k0smotron.io/token-request": jtr.Name,
clusterLabel: jtr.Spec.ClusterRef.Name,
"k0smotron.io/cluster-uid": string(jtr.Status.ClusterUID),
"k0smotron.io/role": jtr.Spec.Role,
"k0smotron.io/token-request": jtr.Name,
"app.kubernetes.io/component": util.ComponentJointoken,
}
for k, v := range jtr.Labels {
labels[k] = v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (scope *kmcScope) generateConfig(kmc *km.Cluster, sans []string) (v1.Config
ObjectMeta: metav1.ObjectMeta{
Name: kmc.GetConfigMapName(),
Namespace: kmc.Namespace,
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentClusterConfig),
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
},
Data: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (scope *kmcScope) generateEntrypointCM(kmc *km.Cluster) (v1.ConfigMap, erro
ObjectMeta: metav1.ObjectMeta{
Name: kmc.GetEntrypointConfigMapName(),
Namespace: kmc.Namespace,
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentEntrypoint),
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
},
Data: map[string]string{
Expand Down
10 changes: 8 additions & 2 deletions internal/controller/k0smotron.io/k0smotroncluster_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ func (scope *kmcScope) reconcileEtcdStatefulSet(ctx context.Context, kmc *km.Clu
}

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

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

Expand Down Expand Up @@ -263,7 +269,7 @@ func generateEtcdStatefulSet(kmc *km.Cluster, existingSts *apps.StatefulSet, rep
Spec: apps.StatefulSetSpec{
ServiceName: kmc.GetEtcdServiceName(),
Selector: &metav1.LabelSelector{
MatchLabels: labels,
MatchLabels: selectorLabels,
},
Replicas: &replicas,
PodManagementPolicy: apps.ParallelPodManagement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (scope *kmcScope) generateIngressManifestsConfigMap(kmc *km.Cluster) (corev
ObjectMeta: metav1.ObjectMeta{
Name: kmc.GetIngressManifestsConfigMapName(),
Namespace: kmc.Namespace,
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentIngress),
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
},
Data: map[string]string{
Expand Down Expand Up @@ -210,6 +211,7 @@ func (scope *kmcScope) generateKonnectivityIngressConfigMap(kmc *km.Cluster) (co
ObjectMeta: metav1.ObjectMeta{
Name: kmc.GetIngressManifestsConfigMapName() + "-konnectivity",
Namespace: kmc.Namespace,
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentIngress),
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
},
Data: map[string]string{
Expand Down Expand Up @@ -327,7 +329,7 @@ func (scope *kmcScope) generateIngress(kmc *km.Cluster) v1.Ingress {
Name: kmc.GetIngressName(),
Namespace: kmc.Namespace,
Annotations: annotations,
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentIngress),
},
Spec: v1.IngressSpec{
IngressClassName: kmc.Spec.Ingress.ClassName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (scope *kmcScope) reconcileKubeConfigSecret(ctx context.Context, management
ObjectMeta: metav1.ObjectMeta{
Name: kmc.GetAdminConfigSecretName(),
Namespace: kmc.Namespace,
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentKubeconfig),
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
},
StringData: map[string]string{"value": processedOutput},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (scope *kmcScope) generateMonitoringCM(kmc *km.Cluster) (v1.ConfigMap, erro
ObjectMeta: metav1.ObjectMeta{
Name: kmc.GetMonitoringConfigMapName(),
Namespace: kmc.Namespace,
Labels: kcontrollerutil.LabelsForK0smotronCluster(kmc),
Labels: kcontrollerutil.LabelsForK0smotronComponent(kmc, kcontrollerutil.ComponentMonitoring),
Annotations: kcontrollerutil.AnnotationsForK0smotronCluster(kmc),
},
Data: map[string]string{
Expand Down
19 changes: 13 additions & 6 deletions internal/controller/k0smotron.io/k0smotroncluster_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ func generateService(kmc *km.Cluster) v1.Service {
})
}

// Copy both Cluster level labels and Service labels
labels := map[string]string{}
// Selector must match pods (same as main); ObjectMeta gets app.kubernetes.io/component.
selectorLabels := map[string]string{}
for k, v := range util.LabelsForK0smotronCluster(kmc) {
labels[k] = v
selectorLabels[k] = v
}
for k, v := range kmc.Spec.Service.Labels {
labels[k] = v
selectorLabels[k] = v
}
metadataLabels := map[string]string{}
for k, v := range util.LabelsForK0smotronComponent(kmc, util.ComponentControlPlane) {
metadataLabels[k] = v
}
for k, v := range kmc.Spec.Service.Labels {
metadataLabels[k] = v
}

// Copy both Cluster level annotations and Service annotations
Expand All @@ -112,12 +119,12 @@ func generateService(kmc *km.Cluster) v1.Service {
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: kmc.Namespace,
Labels: labels,
Labels: metadataLabels,
Annotations: annotations,
},
Spec: v1.ServiceSpec{
Type: kmc.Spec.Service.Type,
Selector: labels,
Selector: selectorLabels,
Ports: ports,
ExternalTrafficPolicy: kmc.Spec.Service.ExternalTrafficPolicy,
},
Expand Down
37 changes: 21 additions & 16 deletions internal/controller/k0smotron.io/k0smotroncluster_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

km "github.com/k0sproject/k0smotron/api/k0smotron.io/v1beta1"
"github.com/k0sproject/k0smotron/internal/controller/util"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -43,9 +44,10 @@ func TestClusterReconciler_serviceLabels(t *testing.T) {
Spec: km.ClusterSpec{},
},
want: map[string]string{
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"app.kubernetes.io/component": util.ComponentControlPlane,
},
},
{
Expand All @@ -60,10 +62,11 @@ func TestClusterReconciler_serviceLabels(t *testing.T) {
Spec: km.ClusterSpec{},
},
want: map[string]string{
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"test": "test",
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"app.kubernetes.io/component": util.ComponentControlPlane,
"test": "test",
},
},
{
Expand All @@ -84,11 +87,12 @@ func TestClusterReconciler_serviceLabels(t *testing.T) {
},
},
want: map[string]string{
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"test": "test",
"foo": "bar",
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"app.kubernetes.io/component": util.ComponentControlPlane,
"test": "test",
"foo": "bar",
},
},
{
Expand All @@ -109,10 +113,11 @@ func TestClusterReconciler_serviceLabels(t *testing.T) {
},
},
want: map[string]string{
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"test": "foobar",
"app": "k0smotron",
"cluster": "test",
"component": "cluster",
"app.kubernetes.io/component": util.ComponentControlPlane,
"test": "foobar",
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func findStatefulSetPod(ctx context.Context, statefulSet string, namespace strin

func (scope *kmcScope) generateStatefulSet(kmc *km.Cluster) (apps.StatefulSet, error) {

labels := util.LabelsForK0smotronControlPlane(kmc)
// StatefulSet selector is immutable after creation. Add app.kubernetes.io/component only to metadata and template labels.
selectorLabels := util.LabelsForK0smotronControlPlane(kmc)
labels := make(map[string]string, len(selectorLabels)+1)
for k, v := range selectorLabels {
labels[k] = v
}
labels["app.kubernetes.io/component"] = util.ComponentControlPlane
annotations := util.AnnotationsForK0smotronCluster(kmc)

statefulSet := apps.StatefulSet{
Expand All @@ -69,7 +75,7 @@ func (scope *kmcScope) generateStatefulSet(kmc *km.Cluster) (apps.StatefulSet, e
},
Spec: apps.StatefulSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
MatchLabels: selectorLabels,
},
Replicas: &kmc.Spec.Replicas,
Template: v1.PodTemplateSpec{
Expand Down
Loading
Loading