From a8a41951cb3405d3a16db135875a47191fdf92f7 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Sun, 12 Feb 2023 12:37:07 +0100 Subject: [PATCH] refactor!: keeping labels consistent The label kamaji.clastix.io/soot is deprecated in favour of kamaji.clastix.io/name, every external resource referring to this must be aligned prior to updating to this version. --- internal/builders/controlplane/deployment.go | 2 +- internal/constants/labels.go | 3 +++ internal/resources/api_server_certificate.go | 8 +------- .../api_server_kubelet_client_certificate.go | 8 +------- internal/resources/ca_certificate.go | 8 +------- .../resources/datastore/datastore_certificate.go | 6 +----- .../resources/datastore/datastore_storage_config.go | 8 +------- .../resources/front-proxy-client-certificate.go | 8 +------- internal/resources/front_proxy_ca_certificate.go | 8 +------- internal/resources/k8s_deployment_resource.go | 5 +++-- internal/resources/k8s_ingress_resource.go | 3 +-- internal/resources/k8s_service_resource.go | 4 ++-- internal/resources/konnectivity/agent.go | 8 +------- .../resources/konnectivity/certificate_resource.go | 8 +------- .../konnectivity/cluster_role_binding_resource.go | 6 +++--- .../egress_selector_configuration_resource.go | 2 +- .../resources/konnectivity/kubeconfig_resource.go | 8 +------- .../konnectivity/service_account_resource.go | 12 +++--------- internal/resources/kubeadm_config.go | 2 +- internal/resources/kubeconfig.go | 8 +------- internal/resources/sa_certificate.go | 8 +------- internal/utilities/utilities.go | 13 ++++--------- 22 files changed, 34 insertions(+), 112 deletions(-) diff --git a/internal/builders/controlplane/deployment.go b/internal/builders/controlplane/deployment.go index 5188171..eef899a 100644 --- a/internal/builders/controlplane/deployment.go +++ b/internal/builders/controlplane/deployment.go @@ -785,7 +785,7 @@ func (d *Deployment) buildKine(podSpec *corev1.PodSpec, tcp *kamajiv1alpha1.Tena func (d *Deployment) SetSelector(deploymentSpec *appsv1.DeploymentSpec, tcp *kamajiv1alpha1.TenantControlPlane) { deploymentSpec.Selector = &metav1.LabelSelector{ MatchLabels: map[string]string{ - "kamaji.clastix.io/soot": tcp.GetName(), + "kamaji.clastix.io/name": tcp.GetName(), }, } } diff --git a/internal/constants/labels.go b/internal/constants/labels.go index 9521ca3..f9291af 100644 --- a/internal/constants/labels.go +++ b/internal/constants/labels.go @@ -6,4 +6,7 @@ package constants const ( ProjectNameLabelKey = "kamaji.clastix.io/project" ProjectNameLabelValue = "kamaji" + + ControlPlaneLabelKey = "kamaji.clastix.io/name" + ControlPlaneLabelResource = "kamaji.clastix.io/component" ) diff --git a/internal/resources/api_server_certificate.go b/internal/resources/api_server_certificate.go index 6db9a5f..560a008 100644 --- a/internal/resources/api_server_certificate.go +++ b/internal/resources/api_server_certificate.go @@ -138,13 +138,7 @@ func (r *APIServerCertificate) mutate(ctx context.Context, tenantControlPlane *k utilities.SetObjectChecksum(r.resource, r.resource.Data) - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) return ctrl.SetControllerReference(tenantControlPlane, r.resource, r.Client.Scheme()) } diff --git a/internal/resources/api_server_kubelet_client_certificate.go b/internal/resources/api_server_kubelet_client_certificate.go index 5f74ea4..c607301 100644 --- a/internal/resources/api_server_kubelet_client_certificate.go +++ b/internal/resources/api_server_kubelet_client_certificate.go @@ -136,13 +136,7 @@ func (r *APIServerKubeletClientCertificate) mutate(ctx context.Context, tenantCo kubeadmconstants.APIServerKubeletClientKeyName: certificateKeyPair.PrivateKey, } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) utilities.SetObjectChecksum(r.resource, r.resource.Data) diff --git a/internal/resources/ca_certificate.go b/internal/resources/ca_certificate.go index bf3ff52..e9dc1af 100644 --- a/internal/resources/ca_certificate.go +++ b/internal/resources/ca_certificate.go @@ -124,13 +124,7 @@ func (r *CACertificate) mutate(ctx context.Context, tenantControlPlane *kamajiv1 kubeadmconstants.CAKeyName: ca.PrivateKey, } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) utilities.SetObjectChecksum(r.resource, r.resource.Data) diff --git a/internal/resources/datastore/datastore_certificate.go b/internal/resources/datastore/datastore_certificate.go index bc8e53e..6deda70 100644 --- a/internal/resources/datastore/datastore_certificate.go +++ b/internal/resources/datastore/datastore_certificate.go @@ -142,12 +142,8 @@ func (r *Certificate) mutate(ctx context.Context, tenantControlPlane *kamajiv1al utilities.SetObjectChecksum(r.resource, r.resource.Data) r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), + utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName()), r.resource.GetLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, )) return ctrl.SetControllerReference(tenantControlPlane, r.resource, r.Client.Scheme()) diff --git a/internal/resources/datastore/datastore_storage_config.go b/internal/resources/datastore/datastore_storage_config.go index 9fa1972..74a7b6c 100644 --- a/internal/resources/datastore/datastore_storage_config.go +++ b/internal/resources/datastore/datastore_storage_config.go @@ -107,13 +107,7 @@ func (r *Config) mutate(_ context.Context, tenantControlPlane *kamajiv1alpha1.Te utilities.SetObjectChecksum(r.resource, r.resource.Data) - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) return ctrl.SetControllerReference(tenantControlPlane, r.resource, r.Client.Scheme()) } diff --git a/internal/resources/front-proxy-client-certificate.go b/internal/resources/front-proxy-client-certificate.go index 4f27f60..ac9fa2b 100644 --- a/internal/resources/front-proxy-client-certificate.go +++ b/internal/resources/front-proxy-client-certificate.go @@ -135,13 +135,7 @@ func (r *FrontProxyClientCertificate) mutate(ctx context.Context, tenantControlP kubeadmconstants.FrontProxyClientKeyName: certificateKeyPair.PrivateKey, } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) utilities.SetObjectChecksum(r.resource, r.resource.Data) diff --git a/internal/resources/front_proxy_ca_certificate.go b/internal/resources/front_proxy_ca_certificate.go index aeeb574..f3aa733 100644 --- a/internal/resources/front_proxy_ca_certificate.go +++ b/internal/resources/front_proxy_ca_certificate.go @@ -114,13 +114,7 @@ func (r *FrontProxyCACertificate) mutate(ctx context.Context, tenantControlPlane kubeadmconstants.FrontProxyCAKeyName: ca.PrivateKey, } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) utilities.SetObjectChecksum(r.resource, r.resource.Data) diff --git a/internal/resources/k8s_deployment_resource.go b/internal/resources/k8s_deployment_resource.go index e5da43d..35929cc 100644 --- a/internal/resources/k8s_deployment_resource.go +++ b/internal/resources/k8s_deployment_resource.go @@ -76,7 +76,7 @@ func (r *KubernetesDeploymentResource) mutate(ctx context.Context, tenantControl DataStore: r.DataStore, KineContainerImage: r.KineContainerImage, } - d.SetLabels(r.resource, utilities.MergeMaps(utilities.CommonLabels(tenantControlPlane.GetName()), tenantControlPlane.Spec.ControlPlane.Deployment.AdditionalMetadata.Labels)) + d.SetLabels(r.resource, utilities.MergeMaps(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName()), tenantControlPlane.Spec.ControlPlane.Deployment.AdditionalMetadata.Labels)) d.SetAnnotations(r.resource, utilities.MergeMaps(r.resource.Annotations, tenantControlPlane.Spec.ControlPlane.Deployment.AdditionalMetadata.Annotations)) d.SetTemplateLabels(&r.resource.Spec.Template, r.deploymentTemplateLabels(ctx, tenantControlPlane)) d.SetNodeSelector(&r.resource.Spec.Template.Spec, tenantControlPlane) @@ -135,7 +135,8 @@ func (r *KubernetesDeploymentResource) deploymentTemplateLabels(ctx context.Cont } labels = map[string]string{ - "kamaji.clastix.io/soot": tenantControlPlane.GetName(), + "kamaji.clastix.io/name": tenantControlPlane.GetName(), + "kamaji.clastix.io/component": r.GetName(), "component.kamaji.clastix.io/api-server-certificate": hash(ctx, tenantControlPlane.GetNamespace(), tenantControlPlane.Status.Certificates.APIServer.SecretName), "component.kamaji.clastix.io/api-server-kubelet-client-certificate": hash(ctx, tenantControlPlane.GetNamespace(), tenantControlPlane.Status.Certificates.APIServerKubeletClient.SecretName), "component.kamaji.clastix.io/ca": hash(ctx, tenantControlPlane.GetNamespace(), tenantControlPlane.Status.Certificates.CA.SecretName), diff --git a/internal/resources/k8s_ingress_resource.go b/internal/resources/k8s_ingress_resource.go index fe16fea..45240ad 100644 --- a/internal/resources/k8s_ingress_resource.go +++ b/internal/resources/k8s_ingress_resource.go @@ -69,7 +69,6 @@ func (r *KubernetesIngressResource) Define(_ context.Context, tenantControlPlane ObjectMeta: metav1.ObjectMeta{ Name: tenantControlPlane.GetName(), Namespace: tenantControlPlane.GetNamespace(), - Labels: utilities.CommonLabels(tenantControlPlane.GetName()), }, } @@ -80,7 +79,7 @@ func (r *KubernetesIngressResource) Define(_ context.Context, tenantControlPlane func (r *KubernetesIngressResource) mutate(tenantControlPlane *kamajiv1alpha1.TenantControlPlane) controllerutil.MutateFn { return func() error { - labels := utilities.MergeMaps(r.resource.GetLabels(), tenantControlPlane.Spec.ControlPlane.Ingress.AdditionalMetadata.Labels) + labels := utilities.MergeMaps(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName()), tenantControlPlane.Spec.ControlPlane.Ingress.AdditionalMetadata.Labels) r.resource.SetLabels(labels) annotations := utilities.MergeMaps(r.resource.GetAnnotations(), tenantControlPlane.Spec.ControlPlane.Ingress.AdditionalMetadata.Annotations) diff --git a/internal/resources/k8s_service_resource.go b/internal/resources/k8s_service_resource.go index 9ada578..cf9ad4b 100644 --- a/internal/resources/k8s_service_resource.go +++ b/internal/resources/k8s_service_resource.go @@ -80,14 +80,14 @@ func (r *KubernetesServiceResource) mutate(ctx context.Context, tenantControlPla address, _ := tenantControlPlane.DeclaredControlPlaneAddress(ctx, r.Client) return func() error { - labels := utilities.MergeMaps(utilities.CommonLabels(tenantControlPlane.GetName()), tenantControlPlane.Spec.ControlPlane.Service.AdditionalMetadata.Labels) + labels := utilities.MergeMaps(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName()), tenantControlPlane.Spec.ControlPlane.Service.AdditionalMetadata.Labels) r.resource.SetLabels(labels) annotations := utilities.MergeMaps(r.resource.GetAnnotations(), tenantControlPlane.Spec.ControlPlane.Service.AdditionalMetadata.Annotations) r.resource.SetAnnotations(annotations) r.resource.Spec.Selector = map[string]string{ - "kamaji.clastix.io/soot": tenantControlPlane.GetName(), + "kamaji.clastix.io/name": tenantControlPlane.GetName(), } if len(r.resource.Spec.Ports) == 0 { diff --git a/internal/resources/konnectivity/agent.go b/internal/resources/konnectivity/agent.go index 76856fb..b432756 100644 --- a/internal/resources/konnectivity/agent.go +++ b/internal/resources/konnectivity/agent.go @@ -109,13 +109,7 @@ func (r *Agent) mutate(ctx context.Context, tenantControlPlane *kamajiv1alpha1.T return err } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "k8s-app": AgentName, - "addonmanager.kubernetes.io/mode": "Reconcile", - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) if r.resource.Spec.Selector == nil { r.resource.Spec.Selector = &metav1.LabelSelector{} diff --git a/internal/resources/konnectivity/certificate_resource.go b/internal/resources/konnectivity/certificate_resource.go index 0536938..4f5b62e 100644 --- a/internal/resources/konnectivity/certificate_resource.go +++ b/internal/resources/konnectivity/certificate_resource.go @@ -126,13 +126,7 @@ func (r *CertificateResource) mutate(ctx context.Context, tenantControlPlane *ka corev1.TLSPrivateKeyKey: privKey.Bytes(), } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) utilities.SetObjectChecksum(r.resource, r.resource.Data) diff --git a/internal/resources/konnectivity/cluster_role_binding_resource.go b/internal/resources/konnectivity/cluster_role_binding_resource.go index e735b61..2415058 100644 --- a/internal/resources/konnectivity/cluster_role_binding_resource.go +++ b/internal/resources/konnectivity/cluster_role_binding_resource.go @@ -68,7 +68,7 @@ func (r *ClusterRoleBindingResource) Define(ctx context.Context, tenantControlPl func (r *ClusterRoleBindingResource) CreateOrUpdate(ctx context.Context, tcp *kamajiv1alpha1.TenantControlPlane) (controllerutil.OperationResult, error) { if tcp.Spec.Addons.Konnectivity != nil { - return controllerutil.CreateOrUpdate(ctx, r.tenantClient, r.resource, r.mutate()) + return controllerutil.CreateOrUpdate(ctx, r.tenantClient, r.resource, r.mutate(tcp)) } return controllerutil.OperationResultNone, nil @@ -93,10 +93,10 @@ func (r *ClusterRoleBindingResource) UpdateTenantControlPlaneStatus(_ context.Co return nil } -func (r *ClusterRoleBindingResource) mutate() controllerutil.MutateFn { +func (r *ClusterRoleBindingResource) mutate(tenantControlPlane *kamajiv1alpha1.TenantControlPlane) controllerutil.MutateFn { return func() error { r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), + utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName()), map[string]string{ "kubernetes.io/cluster-service": "true", "addonmanager.kubernetes.io/mode": "Reconcile", diff --git a/internal/resources/konnectivity/egress_selector_configuration_resource.go b/internal/resources/konnectivity/egress_selector_configuration_resource.go index 07a1a99..2e7c33b 100644 --- a/internal/resources/konnectivity/egress_selector_configuration_resource.go +++ b/internal/resources/konnectivity/egress_selector_configuration_resource.go @@ -82,7 +82,7 @@ func (r *EgressSelectorConfigurationResource) UpdateTenantControlPlaneStatus(ctx func (r *EgressSelectorConfigurationResource) mutate(_ context.Context, tenantControlPlane *kamajiv1alpha1.TenantControlPlane) func() error { return func() error { - r.resource.SetLabels(utilities.MergeMaps(r.resource.GetLabels(), utilities.KamajiLabels())) + r.resource.SetLabels(utilities.MergeMaps(r.resource.GetLabels(), utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName()))) configuration := &apiserverv1alpha1.EgressSelectorConfiguration{ TypeMeta: metav1.TypeMeta{ diff --git a/internal/resources/konnectivity/kubeconfig_resource.go b/internal/resources/konnectivity/kubeconfig_resource.go index 27d3190..1033e3d 100644 --- a/internal/resources/konnectivity/kubeconfig_resource.go +++ b/internal/resources/konnectivity/kubeconfig_resource.go @@ -157,13 +157,7 @@ func (r *KubeconfigResource) mutate(ctx context.Context, tenantControlPlane *kam utilities.SetObjectChecksum(r.resource, r.resource.Data) - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) return ctrl.SetControllerReference(tenantControlPlane, r.resource, r.Client.Scheme()) } diff --git a/internal/resources/konnectivity/service_account_resource.go b/internal/resources/konnectivity/service_account_resource.go index 994b2ef..e0b88f9 100644 --- a/internal/resources/konnectivity/service_account_resource.go +++ b/internal/resources/konnectivity/service_account_resource.go @@ -69,7 +69,7 @@ func (r *ServiceAccountResource) Define(ctx context.Context, tenantControlPlane func (r *ServiceAccountResource) CreateOrUpdate(ctx context.Context, tcp *kamajiv1alpha1.TenantControlPlane) (controllerutil.OperationResult, error) { if tcp.Spec.Addons.Konnectivity != nil { - return controllerutil.CreateOrUpdate(ctx, r.tenantClient, r.resource, r.mutate()) + return controllerutil.CreateOrUpdate(ctx, r.tenantClient, r.resource, r.mutate(tcp)) } return controllerutil.OperationResultNone, nil @@ -94,15 +94,9 @@ func (r *ServiceAccountResource) UpdateTenantControlPlaneStatus(_ context.Contex return nil } -func (r *ServiceAccountResource) mutate() controllerutil.MutateFn { +func (r *ServiceAccountResource) mutate(tenantControlPlane *kamajiv1alpha1.TenantControlPlane) controllerutil.MutateFn { return func() error { - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kubernetes.io/cluster-service": "true", - "addonmanager.kubernetes.io/mode": "Reconcile", - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) return nil } diff --git a/internal/resources/kubeadm_config.go b/internal/resources/kubeadm_config.go index 6b9a4d7..57e77fc 100644 --- a/internal/resources/kubeadm_config.go +++ b/internal/resources/kubeadm_config.go @@ -88,7 +88,7 @@ func (r *KubeadmConfigResource) mutate(ctx context.Context, tenantControlPlane * return err } - r.resource.SetLabels(utilities.KamajiLabels()) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) params := kubeadm.Parameters{ TenantControlPlaneAddress: address, diff --git a/internal/resources/kubeconfig.go b/internal/resources/kubeconfig.go index 2c855fd..a4bbaeb 100644 --- a/internal/resources/kubeconfig.go +++ b/internal/resources/kubeconfig.go @@ -174,13 +174,7 @@ func (r *KubeconfigResource) mutate(ctx context.Context, tenantControlPlane *kam r.KubeConfigFileName: kubeconfig, } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) r.resource.SetAnnotations(map[string]string{ constants.Checksum: checksum, diff --git a/internal/resources/sa_certificate.go b/internal/resources/sa_certificate.go index dc47439..a727798 100644 --- a/internal/resources/sa_certificate.go +++ b/internal/resources/sa_certificate.go @@ -113,13 +113,7 @@ func (r *SACertificate) mutate(ctx context.Context, tenantControlPlane *kamajiv1 kubeadmconstants.ServiceAccountPrivateKeyName: sa.PrivateKey, } - r.resource.SetLabels(utilities.MergeMaps( - utilities.KamajiLabels(), - map[string]string{ - "kamaji.clastix.io/name": tenantControlPlane.GetName(), - "kamaji.clastix.io/component": r.GetName(), - }, - )) + r.resource.SetLabels(utilities.KamajiLabels(tenantControlPlane.GetName(), r.GetName())) utilities.SetObjectChecksum(r.resource, r.resource.Data) diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index a0cbba1..524a7d1 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -18,16 +18,11 @@ const ( separator = "-" ) -func KamajiLabels() map[string]string { +func KamajiLabels(tcpName, resourceName string) map[string]string { return map[string]string{ - constants.ProjectNameLabelKey: constants.ProjectNameLabelValue, - } -} - -func CommonLabels(clusterName string) map[string]string { - return map[string]string{ - "kamaji.clastix.io/type": "cluster", - "kamaji.clastix.io/cluster": clusterName, + constants.ProjectNameLabelKey: constants.ProjectNameLabelValue, + constants.ControlPlaneLabelKey: tcpName, + constants.ControlPlaneLabelResource: resourceName, } }