From e4869609a330b881c91a13d8f38747131afff151 Mon Sep 17 00:00:00 2001 From: Olivier Michallat Date: Mon, 12 Jun 2023 16:39:47 -0700 Subject: [PATCH] Remove usages of deprecated created-by label (fixes #842) (#997) --- CHANGELOG/CHANGELOG-1.8.md | 3 +- apis/k8ssandra/v1alpha1/constants.go | 13 ++----- .../control/k8ssandratask_controller.go | 1 - .../cassandra_telemetry_reconciler.go | 1 - controllers/k8ssandra/cleanup.go | 10 ++--- controllers/k8ssandra/reaper.go | 4 +- controllers/k8ssandra/stargate.go | 2 +- .../reaper/reaper_telemetry_reconciler.go | 1 - .../stargate/stargate_telemetry_reconciler.go | 1 - docs/content/en/contribute/labels/_index.md | 39 +++++++++++++++++++ pkg/cassandra/datacenter.go | 15 +++---- pkg/labels/labels.go | 22 +++++------ pkg/nodeconfig/generate.go | 16 ++++---- pkg/nodeconfig/generate_test.go | 2 +- pkg/reaper/deployment.go | 2 + pkg/reaper/deployment_test.go | 2 - pkg/reaper/labels.go | 14 +++---- pkg/reaper/service_test.go | 2 - pkg/reaper/vector.go | 18 +++++---- pkg/stargate/config.go | 17 ++++---- pkg/stargate/deployments.go | 2 - pkg/stargate/service.go | 1 - pkg/stargate/stargate.go | 19 +++++---- pkg/stargate/vector.go | 18 +++++---- .../cassandra_agent/cassandra_agent_config.go | 2 +- pkg/telemetry/prom_reaper_servicemonitor.go | 4 +- pkg/telemetry/prom_stargate_servicemonitor.go | 4 +- pkg/telemetry/vector.go | 18 +++++---- 28 files changed, 138 insertions(+), 115 deletions(-) create mode 100644 docs/content/en/contribute/labels/_index.md diff --git a/CHANGELOG/CHANGELOG-1.8.md b/CHANGELOG/CHANGELOG-1.8.md index 8a778fe43..f74e464bc 100644 --- a/CHANGELOG/CHANGELOG-1.8.md +++ b/CHANGELOG/CHANGELOG-1.8.md @@ -15,4 +15,5 @@ When cutting a new release, update the `unreleased` heading to the tag being gen ## unreleased -* [CHANGE] [#985](https://github.com/k8ssandra/k8ssandra-operator/issues/985) CI/CD does not produce images for some commits \ No newline at end of file +* [CHANGE] [#985](https://github.com/k8ssandra/k8ssandra-operator/issues/985) CI/CD does not produce images for some commits +* [ENHANCEMENT] [#842](https://github.com/k8ssandra/k8ssandra-operator/issues/842) Remove usages of deprecated created-by label diff --git a/apis/k8ssandra/v1alpha1/constants.go b/apis/k8ssandra/v1alpha1/constants.go index 1a5c334e0..2d61209e4 100644 --- a/apis/k8ssandra/v1alpha1/constants.go +++ b/apis/k8ssandra/v1alpha1/constants.go @@ -34,13 +34,9 @@ const ( RebuildDcAnnotation = "k8ssandra.io/rebuild-dc" - RebuildLabel = "k8ssandra.io/rebuild" - NameLabel = "app.kubernetes.io/name" NameLabelValue = "k8ssandra-operator" - InstanceLabel = "app.kubernetes.io/instance" - VersionLabel = "app.kubernetes.io/version" ManagedByLabel = "app.kubernetes.io/managed-by" ComponentLabel = "app.kubernetes.io/component" @@ -49,12 +45,6 @@ const ( ComponentLabelValueReaper = "reaper" ComponentLabelTelemetry = "telemetry" - CreatedByLabel = "app.kubernetes.io/created-by" - CreatedByLabelValueK8ssandraClusterController = "k8ssandracluster-controller" - CreatedByLabelValueStargateController = "stargate-controller" - CreatedByLabelValueReaperController = "reaper-controller" - CreatedByLabelValueK8ssandraTaskController = "k8ssandratask-controller" - PartOfLabel = "app.kubernetes.io/part-of" PartOfLabelValue = "k8ssandra" @@ -62,6 +52,9 @@ const ( ReplicatedByLabel = "k8ssandra.io/replicated-by" ReplicatedByLabelValue = "k8ssandracluster-controller" + CleanedUpByLabel = "k8ssandra.io/cleaned-up-by" + CleanedUpByLabelValue = "k8ssandracluster-controller" + K8ssandraClusterNameLabel = "k8ssandra.io/cluster-name" K8ssandraClusterNamespaceLabel = "k8ssandra.io/cluster-namespace" diff --git a/controllers/control/k8ssandratask_controller.go b/controllers/control/k8ssandratask_controller.go index fae424be5..f7e996b46 100644 --- a/controllers/control/k8ssandratask_controller.go +++ b/controllers/control/k8ssandratask_controller.go @@ -293,7 +293,6 @@ func newCassandraTask(kTask *api.K8ssandraTask, namespace string, dcName string) k8capi.NameLabel: k8capi.NameLabelValue, k8capi.PartOfLabel: k8capi.PartOfLabelValue, k8capi.ComponentLabel: k8capi.ComponentLabelValueCassandra, - k8capi.CreatedByLabel: k8capi.CreatedByLabelValueK8ssandraTaskController, api.K8ssandraTaskNameLabel: kTask.Name, api.K8ssandraTaskNamespaceLabel: kTask.Namespace, }, diff --git a/controllers/k8ssandra/cassandra_telemetry_reconciler.go b/controllers/k8ssandra/cassandra_telemetry_reconciler.go index 1251969bb..73923115c 100644 --- a/controllers/k8ssandra/cassandra_telemetry_reconciler.go +++ b/controllers/k8ssandra/cassandra_telemetry_reconciler.go @@ -92,7 +92,6 @@ func mustLabels(klusterName string, klusterNamespace string, dcName string, addi additionalLabels[k8ssandraapi.DatacenterLabel] = dcName additionalLabels[k8ssandraapi.K8ssandraClusterNamespaceLabel] = klusterNamespace additionalLabels[k8ssandraapi.ComponentLabel] = k8ssandraapi.ComponentLabelTelemetry - additionalLabels[k8ssandraapi.CreatedByLabel] = k8ssandraapi.CreatedByLabelValueK8ssandraClusterController return additionalLabels } diff --git a/controllers/k8ssandra/cleanup.go b/controllers/k8ssandra/cleanup.go index 4baedefe2..c9a0879ee 100644 --- a/controllers/k8ssandra/cleanup.go +++ b/controllers/k8ssandra/cleanup.go @@ -65,7 +65,7 @@ func (r *K8ssandraClusterReconciler) checkDeletion(ctx context.Context, kc *api. hasErrors = true } - selector := k8ssandralabels.PartOfLabels(kcKey) + selector := k8ssandralabels.CleanedUpByLabels(kcKey) stargateList := &stargateapi.StargateList{} options := client.ListOptions{ Namespace: namespace, @@ -218,7 +218,7 @@ func (r *K8ssandraClusterReconciler) findStargateForDeletion( dcName string, remoteClient client.Client) (*stargateapi.Stargate, client.Client, error) { - selector := k8ssandralabels.PartOfLabels(kcKey) + selector := k8ssandralabels.CleanedUpByLabels(kcKey) options := &client.ListOptions{LabelSelector: labels.SelectorFromSet(selector)} stargateList := &stargateapi.StargateList{} stargateName := kcKey.Name + "-" + dcName + "-stargate" @@ -257,7 +257,7 @@ func (r *K8ssandraClusterReconciler) findReaperForDeletion( dcName string, remoteClient client.Client) (*reaperapi.Reaper, client.Client, error) { - selector := k8ssandralabels.PartOfLabels(kcKey) + selector := k8ssandralabels.CleanedUpByLabels(kcKey) options := &client.ListOptions{LabelSelector: labels.SelectorFromSet(selector)} reaperList := &reaperapi.ReaperList{} reaperName := kcKey.Name + "-" + dcName + "-reaper" @@ -295,7 +295,7 @@ func (r *K8ssandraClusterReconciler) findDcForDeletion( kcKey client.ObjectKey, dcName string, remoteClient client.Client) (*cassdcapi.CassandraDatacenter, client.Client, error) { - selector := k8ssandralabels.PartOfLabels(kcKey) + selector := k8ssandralabels.CleanedUpByLabels(kcKey) options := &client.ListOptions{LabelSelector: labels.SelectorFromSet(selector)} dcList := &cassdcapi.CassandraDatacenterList{} @@ -335,7 +335,7 @@ func (r *K8ssandraClusterReconciler) deleteK8ssandraConfigMaps( remoteClient client.Client, kcLogger logr.Logger, ) (hasErrors bool) { - selector := k8ssandralabels.PartOfLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.SanitizedName()}) + selector := k8ssandralabels.CleanedUpByLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.SanitizedName()}) configMaps := &corev1.ConfigMapList{} options := client.ListOptions{ Namespace: namespace, diff --git a/controllers/k8ssandra/reaper.go b/controllers/k8ssandra/reaper.go index ee4e60416..89acfce02 100644 --- a/controllers/k8ssandra/reaper.go +++ b/controllers/k8ssandra/reaper.go @@ -164,7 +164,7 @@ func (r *K8ssandraClusterReconciler) reconcileReaper( logger.Error(err, "Failed to get Reaper resource") return result.Error(err) } - } else if k8ssandralabels.IsPartOf(actualReaper, kcKey) { + } else if k8ssandralabels.IsCleanedUpBy(actualReaper, kcKey) { if err = remoteClient.Delete(ctx, actualReaper); err != nil { logger.Error(err, "Failed to delete Reaper resource") return result.Error(err) @@ -187,7 +187,7 @@ func (r *K8ssandraClusterReconciler) deleteReapers( remoteClient client.Client, kcLogger logr.Logger, ) (hasErrors bool) { - selector := k8ssandralabels.PartOfLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name}) + selector := k8ssandralabels.CleanedUpByLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name}) reaperList := &reaperapi.ReaperList{} options := client.ListOptions{ Namespace: namespace, diff --git a/controllers/k8ssandra/stargate.go b/controllers/k8ssandra/stargate.go index 0db34be3e..9c1032dea 100644 --- a/controllers/k8ssandra/stargate.go +++ b/controllers/k8ssandra/stargate.go @@ -96,7 +96,7 @@ func (r *K8ssandraClusterReconciler) reconcileStargate( logger.Error(err, "Failed to get Stargate") return result.Error(err) } - } else if labels.IsPartOf(actualStargate, kcKey) { + } else if labels.IsCleanedUpBy(actualStargate, kcKey) { if err := remoteClient.Delete(ctx, actualStargate); err != nil { logger.Error(err, "Failed to delete Stargate") return result.Error(err) diff --git a/controllers/reaper/reaper_telemetry_reconciler.go b/controllers/reaper/reaper_telemetry_reconciler.go index 3a581ff09..ea31fd143 100644 --- a/controllers/reaper/reaper_telemetry_reconciler.go +++ b/controllers/reaper/reaper_telemetry_reconciler.go @@ -97,7 +97,6 @@ func mustLabels(reaperName string, additionalLabels map[string]string) map[strin additionalLabels[k8ssandraapi.PartOfLabel] = k8ssandraapi.PartOfLabelValue additionalLabels[reaperapi.ReaperLabel] = reaperName additionalLabels[k8ssandraapi.ComponentLabel] = k8ssandraapi.ComponentLabelTelemetry - additionalLabels[k8ssandraapi.CreatedByLabel] = k8ssandraapi.CreatedByLabelValueK8ssandraClusterController return additionalLabels } diff --git a/controllers/stargate/stargate_telemetry_reconciler.go b/controllers/stargate/stargate_telemetry_reconciler.go index f1aa47e2b..e4e9f7478 100644 --- a/controllers/stargate/stargate_telemetry_reconciler.go +++ b/controllers/stargate/stargate_telemetry_reconciler.go @@ -85,7 +85,6 @@ func mustLabels(stargateName string, additionalLabels map[string]string) map[str additionalLabels[k8ssandraapi.PartOfLabel] = k8ssandraapi.PartOfLabelValue additionalLabels[stargateapi.StargateLabel] = stargateName additionalLabels[k8ssandraapi.ComponentLabel] = k8ssandraapi.ComponentLabelTelemetry - additionalLabels[k8ssandraapi.CreatedByLabel] = k8ssandraapi.CreatedByLabelValueK8ssandraClusterController return additionalLabels } diff --git a/docs/content/en/contribute/labels/_index.md b/docs/content/en/contribute/labels/_index.md new file mode 100644 index 000000000..ae9168041 --- /dev/null +++ b/docs/content/en/contribute/labels/_index.md @@ -0,0 +1,39 @@ +--- +title: "Kubernetes labels" +linkTitle: "Kubernetes labels" +toc_hide: false +weight: 10 +description: How Kubernetes labels are used on K8ssandra objects. +--- + +The operator places the following labels on the objects it creates: + +## Standard Kubernetes labels + +From [recommended labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/): + +| Label | Value | +|-------------------------------|----------------------------------------------------------| +| `app.kubernetes.io/name` | `k8ssandra-operator` | +| `app.kubernetes.io/part-of` | `k8ssandra` | +| `app.kubernetes.io/component` | One of: `cassandra`, `stargate`, `reaper` or `telemetry` | + +These labels are purely informational. + +The keys and values are defined in [constants.go](https://github.com/k8ssandra/k8ssandra-operator/blob/main/apis/k8ssandra/v1alpha1/constants.go). + +## Labels specific to the operator + +Objects associated with a `K8ssandraCluster` are labelled with `k8ssandra.io/cluster-namespace` and +`k8ssandra.io/cluster-name`. This is used: + +* to establish watches. See the `SetupWithManager()` function in + [k8ssandracluster_controller.go](https://github.com/k8ssandra/k8ssandra-operator/blob/main/controllers/k8ssandra/k8ssandracluster_controller.go). +* in conjunction with `k8ssandra.io/cleaned-up-by`, to mark objects that should get cleaned up when the + `K8ssandraCluster` is deleted, see + [cleanup.go](https://github.com/k8ssandra/k8ssandra-operator/blob/main/controllers/k8ssandra/cleanup.go). + Note that we only use this mechanism for relationships that cross Kubernetes contexts; when the objects are in the same + Kubernetes cluster, we rely on standard owner references instead. + +[labels.go](https://github.com/k8ssandra/k8ssandra-operator/blob/main/pkg/labels/labels.go) provides functions to +manipulate those sets of labels. \ No newline at end of file diff --git a/pkg/cassandra/datacenter.go b/pkg/cassandra/datacenter.go index 7113c4cbf..f05877306 100644 --- a/pkg/cassandra/datacenter.go +++ b/pkg/cassandra/datacenter.go @@ -3,7 +3,6 @@ package cassandra import ( "fmt" "github.com/Masterminds/semver/v3" - "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" "github.com/k8ssandra/cass-operator/pkg/reconciliation" @@ -11,6 +10,7 @@ import ( "github.com/k8ssandra/k8ssandra-operator/pkg/encryption" goalesceutils "github.com/k8ssandra/k8ssandra-operator/pkg/goalesce" "github.com/k8ssandra/k8ssandra-operator/pkg/images" + "github.com/k8ssandra/k8ssandra-operator/pkg/labels" "github.com/k8ssandra/k8ssandra-operator/pkg/utils" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -153,14 +153,11 @@ func NewDatacenter(klusterKey types.NamespacedName, template *DatacenterConfig) Namespace: namespace, Name: template.Meta.Name, Annotations: map[string]string{}, - Labels: map[string]string{ - api.NameLabel: api.NameLabelValue, - api.PartOfLabel: api.PartOfLabelValue, - api.ComponentLabel: api.ComponentLabelValueCassandra, - api.CreatedByLabel: api.CreatedByLabelValueK8ssandraClusterController, - api.K8ssandraClusterNameLabel: klusterKey.Name, - api.K8ssandraClusterNamespaceLabel: klusterKey.Namespace, - }, + Labels: utils.MergeMap(map[string]string{ + api.NameLabel: api.NameLabelValue, + api.PartOfLabel: api.PartOfLabelValue, + api.ComponentLabel: api.ComponentLabelValueCassandra, + }, labels.CleanedUpByLabels(klusterKey)), }, Spec: cassdcapi.CassandraDatacenterSpec{ Size: template.Size, diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 0a769a955..d2456914e 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -85,22 +85,20 @@ func ReplicatedByLabels(klusterKey client.ObjectKey) map[string]string { } } -// IsPartOf returns true if this component was created by the k8ssandra-cluster controller, and belongs to the -// K8ssandraCluster resource specified by klusterKey. klusterKey refers to the namespace and name of the -// K8ssandraCluster. -func IsPartOf(component Labeled, klusterKey client.ObjectKey) bool { - return HasLabelWithValue(component, k8ssandraapi.CreatedByLabel, k8ssandraapi.CreatedByLabelValueK8ssandraClusterController) && +// IsCleanedUpBy returns whether this component is labelled to be cleaned up by the k8ssandra-cluster controller when +// the given K8ssandraCluster is deleted. +func IsCleanedUpBy(component Labeled, klusterKey client.ObjectKey) bool { + return HasLabelWithValue(component, k8ssandraapi.CleanedUpByLabel, k8ssandraapi.CleanedUpByLabelValue) && HasLabelWithValue(component, k8ssandraapi.K8ssandraClusterNameLabel, klusterKey.Name) && HasLabelWithValue(component, k8ssandraapi.K8ssandraClusterNamespaceLabel, klusterKey.Namespace) } -// PartOfLabels returns the labels used to identify a component created by the k8ssandra-cluster controller, and -// belonging to the K8ssandraCluster resource specified by klusterKey, which is namespace and name of the -// K8ssandraCluster. -func PartOfLabels(klusterKey client.ObjectKey) map[string]string { - // TODO add k8ssandraapi.PartOfLabel entry here, already done for telemetry objects elsewhere +// CleanedUpByLabels returns the labels used to indicate that a component should be cleaned up by the k8ssandra-cluster +// controller when the given K8ssandraCluster is deleted. +// (This is only used for cross-context references, when ownerReferences cannot be used). +func CleanedUpByLabels(klusterKey client.ObjectKey) map[string]string { return map[string]string{ - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueK8ssandraClusterController, + k8ssandraapi.CleanedUpByLabel: k8ssandraapi.CleanedUpByLabelValue, k8ssandraapi.K8ssandraClusterNameLabel: klusterKey.Name, k8ssandraapi.K8ssandraClusterNamespaceLabel: klusterKey.Namespace, } @@ -108,7 +106,7 @@ func PartOfLabels(klusterKey client.ObjectKey) map[string]string { // IsOwnedByK8ssandraController returns true if this component was created by the k8ssandra-cluster controller. func IsOwnedByK8ssandraController(component Labeled) bool { - return HasLabelWithValue(component, k8ssandraapi.CreatedByLabel, k8ssandraapi.CreatedByLabelValueK8ssandraClusterController) && + return HasLabelWithValue(component, k8ssandraapi.CleanedUpByLabel, k8ssandraapi.CleanedUpByLabelValue) && HasLabel(component, k8ssandraapi.K8ssandraClusterNameLabel) && HasLabel(component, k8ssandraapi.K8ssandraClusterNamespaceLabel) } diff --git a/pkg/nodeconfig/generate.go b/pkg/nodeconfig/generate.go index c6ea184b1..7adefd1cd 100644 --- a/pkg/nodeconfig/generate.go +++ b/pkg/nodeconfig/generate.go @@ -2,6 +2,7 @@ package nodeconfig import ( "fmt" + "github.com/k8ssandra/k8ssandra-operator/pkg/labels" "strings" k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" @@ -46,14 +47,13 @@ func newPerNodeConfigMap(kcKey, configKey types.NamespacedName) *corev1.ConfigMa ObjectMeta: metav1.ObjectMeta{ Name: configKey.Name, Namespace: configKey.Namespace, - Labels: map[string]string{ - k8ssandraapi.NameLabel: k8ssandraapi.NameLabelValue, - k8ssandraapi.PartOfLabel: k8ssandraapi.PartOfLabelValue, - k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueCassandra, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueK8ssandraClusterController, - k8ssandraapi.K8ssandraClusterNameLabel: kcKey.Name, - k8ssandraapi.K8ssandraClusterNamespaceLabel: kcKey.Namespace, - }, + Labels: utils.MergeMap( + map[string]string{ + k8ssandraapi.NameLabel: k8ssandraapi.NameLabelValue, + k8ssandraapi.PartOfLabel: k8ssandraapi.PartOfLabelValue, + k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueCassandra, + }, + labels.CleanedUpByLabels(kcKey)), }, } } diff --git a/pkg/nodeconfig/generate_test.go b/pkg/nodeconfig/generate_test.go index a90f2889c..cfff87a48 100644 --- a/pkg/nodeconfig/generate_test.go +++ b/pkg/nodeconfig/generate_test.go @@ -52,10 +52,10 @@ func TestNewDefaultPerNodeConfigMap(t *testing.T) { Namespace: "ns1", Name: "kc1-dc1-per-node-config", Labels: map[string]string{ - "app.kubernetes.io/created-by": "k8ssandracluster-controller", "app.kubernetes.io/name": "k8ssandra-operator", "app.kubernetes.io/component": "cassandra", "app.kubernetes.io/part-of": "k8ssandra", + "k8ssandra.io/cleaned-up-by": "k8ssandracluster-controller", "k8ssandra.io/cluster-name": "kc1", "k8ssandra.io/cluster-namespace": "ns1", }, diff --git a/pkg/reaper/deployment.go b/pkg/reaper/deployment.go index eff0caae4..0b0476fff 100644 --- a/pkg/reaper/deployment.go +++ b/pkg/reaper/deployment.go @@ -46,6 +46,8 @@ var defaultImage = images.Image{ func NewDeployment(reaper *api.Reaper, dc *cassdcapi.CassandraDatacenter, keystorePassword *string, truststorePassword *string, logger logr.Logger, authVars ...*corev1.EnvVar) *appsv1.Deployment { selector := metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ + // Note: managed-by shouldn't be used here, but we're keeping it for backwards compatibility, since changing + // a deployment's selector is a breaking change. { Key: v1alpha1.ManagedByLabel, Operator: metav1.LabelSelectorOpIn, diff --git a/pkg/reaper/deployment_test.go b/pkg/reaper/deployment_test.go index c2a4a9a53..066e45899 100644 --- a/pkg/reaper/deployment_test.go +++ b/pkg/reaper/deployment_test.go @@ -562,7 +562,6 @@ func TestLabelsAnnotations(t *testing.T) { k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueReaper, k8ssandraapi.ManagedByLabel: k8ssandraapi.NameLabelValue, reaperapi.ReaperLabel: reaper.Name, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueReaperController, "common": "everywhere", "override": "commonLevel", } @@ -573,7 +572,6 @@ func TestLabelsAnnotations(t *testing.T) { k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueReaper, k8ssandraapi.ManagedByLabel: k8ssandraapi.NameLabelValue, reaperapi.ReaperLabel: reaper.Name, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueReaperController, "common": "everywhere", "override": "podLevel", "pod-label": "pod-label-value", diff --git a/pkg/reaper/labels.go b/pkg/reaper/labels.go index 9b67d0623..042d94f5e 100644 --- a/pkg/reaper/labels.go +++ b/pkg/reaper/labels.go @@ -3,7 +3,9 @@ package reaper import ( k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" reaperapi "github.com/k8ssandra/k8ssandra-operator/apis/reaper/v1alpha1" + k8ssandralabels "github.com/k8ssandra/k8ssandra-operator/pkg/labels" "github.com/k8ssandra/k8ssandra-operator/pkg/utils" + "sigs.k8s.io/controller-runtime/pkg/client" ) var commonLabels = map[string]string{ @@ -14,18 +16,14 @@ var commonLabels = map[string]string{ } func createResourceLabels(kc *k8ssandraapi.K8ssandraCluster) map[string]string { - labels := map[string]string{ - k8ssandraapi.K8ssandraClusterNameLabel: kc.Name, - k8ssandraapi.K8ssandraClusterNamespaceLabel: kc.Namespace, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueK8ssandraClusterController, - } - return utils.MergeMap(commonLabels, labels) + return utils.MergeMap( + commonLabels, + k8ssandralabels.CleanedUpByLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name})) } func getConstantLabels(r *reaperapi.Reaper) map[string]string { labels := map[string]string{ - reaperapi.ReaperLabel: r.Name, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueReaperController, + reaperapi.ReaperLabel: r.Name, } kcName, nameFound := r.Labels[k8ssandraapi.K8ssandraClusterNameLabel] diff --git a/pkg/reaper/service_test.go b/pkg/reaper/service_test.go index e8e442ee1..abc9e2214 100644 --- a/pkg/reaper/service_test.go +++ b/pkg/reaper/service_test.go @@ -26,7 +26,6 @@ func TestNewService(t *testing.T) { k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueReaper, k8ssandraapi.ManagedByLabel: k8ssandraapi.NameLabelValue, reaperapi.ReaperLabel: reaper.Name, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueReaperController, } serviceLabels := map[string]string{ @@ -35,7 +34,6 @@ func TestNewService(t *testing.T) { k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueReaper, k8ssandraapi.ManagedByLabel: k8ssandraapi.NameLabelValue, reaperapi.ReaperLabel: reaper.Name, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueReaperController, "common": "everywhere", "service-label": "service-label-value", "override": "serviceLevel", diff --git a/pkg/reaper/vector.go b/pkg/reaper/vector.go index 222a28b82..21ad41612 100644 --- a/pkg/reaper/vector.go +++ b/pkg/reaper/vector.go @@ -2,6 +2,9 @@ package reaper import ( "fmt" + "github.com/k8ssandra/k8ssandra-operator/pkg/labels" + "github.com/k8ssandra/k8ssandra-operator/pkg/utils" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/go-logr/logr" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" @@ -31,14 +34,13 @@ func CreateVectorConfigMap(namespace, vectorToml string, dc cassdcapi.CassandraD ObjectMeta: metav1.ObjectMeta{ Name: VectorAgentConfigMapName(dc.Spec.ClusterName, dc.Name), Namespace: namespace, - Labels: map[string]string{ - k8ssandra.NameLabel: k8ssandra.NameLabelValue, - k8ssandra.PartOfLabel: k8ssandra.PartOfLabelValue, - k8ssandra.ComponentLabel: k8ssandra.ComponentLabelValueReaper, - k8ssandra.CreatedByLabel: k8ssandra.CreatedByLabelValueK8ssandraClusterController, - k8ssandra.K8ssandraClusterNameLabel: dc.Labels[k8ssandra.K8ssandraClusterNameLabel], - k8ssandra.K8ssandraClusterNamespaceLabel: namespace, - }, + Labels: utils.MergeMap( + map[string]string{ + k8ssandra.NameLabel: k8ssandra.NameLabelValue, + k8ssandra.PartOfLabel: k8ssandra.PartOfLabelValue, + k8ssandra.ComponentLabel: k8ssandra.ComponentLabelValueReaper, + }, + labels.CleanedUpByLabels(client.ObjectKey{Namespace: namespace, Name: dc.Labels[k8ssandra.K8ssandraClusterNameLabel]})), }, Data: map[string]string{ "vector.toml": vectorToml, diff --git a/pkg/stargate/config.go b/pkg/stargate/config.go index e89885ec7..60fed798f 100644 --- a/pkg/stargate/config.go +++ b/pkg/stargate/config.go @@ -1,6 +1,8 @@ package stargate import ( + "github.com/k8ssandra/k8ssandra-operator/pkg/labels" + "sigs.k8s.io/controller-runtime/pkg/client" "strings" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" @@ -42,14 +44,13 @@ func CreateStargateConfigMap(namespace, cassandraYaml, stargateCqlYaml string, d ObjectMeta: metav1.ObjectMeta{ Name: GeneratedConfigMapName(dc.Spec.ClusterName, dc.Name), Namespace: namespace, - Labels: map[string]string{ - k8ssandra.NameLabel: k8ssandra.NameLabelValue, - k8ssandra.PartOfLabel: k8ssandra.PartOfLabelValue, - k8ssandra.ComponentLabel: k8ssandra.ComponentLabelValueStargate, - k8ssandra.CreatedByLabel: k8ssandra.CreatedByLabelValueK8ssandraClusterController, - k8ssandra.K8ssandraClusterNameLabel: dc.Labels[k8ssandra.K8ssandraClusterNameLabel], - k8ssandra.K8ssandraClusterNamespaceLabel: namespace, - }, + Labels: utils.MergeMap( + map[string]string{ + k8ssandra.NameLabel: k8ssandra.NameLabelValue, + k8ssandra.PartOfLabel: k8ssandra.PartOfLabelValue, + k8ssandra.ComponentLabel: k8ssandra.ComponentLabelValueStargate, + }, + labels.CleanedUpByLabels(client.ObjectKey{Namespace: namespace, Name: dc.Labels[k8ssandra.K8ssandraClusterNameLabel]})), }, Data: map[string]string{ "cassandra.yaml": cassandraYaml, diff --git a/pkg/stargate/deployments.go b/pkg/stargate/deployments.go index 2f5858ec1..e109e443c 100644 --- a/pkg/stargate/deployments.go +++ b/pkg/stargate/deployments.go @@ -467,7 +467,6 @@ func createDeploymentLabels(stargate *api.Stargate) map[string]string { coreapi.NameLabel: coreapi.NameLabelValue, coreapi.PartOfLabel: coreapi.PartOfLabelValue, coreapi.ComponentLabel: coreapi.ComponentLabelValueStargate, - coreapi.CreatedByLabel: coreapi.CreatedByLabelValueStargateController, api.StargateLabel: stargate.Name, } @@ -482,7 +481,6 @@ func createPodMeta(stargate *api.Stargate, deploymentName string) meta.Tags { coreapi.NameLabel: coreapi.NameLabelValue, coreapi.PartOfLabel: coreapi.PartOfLabelValue, coreapi.ComponentLabel: coreapi.ComponentLabelValueStargate, - coreapi.CreatedByLabel: coreapi.CreatedByLabelValueStargateController, api.StargateLabel: stargate.Name, api.StargateDeploymentLabel: deploymentName, } diff --git a/pkg/stargate/service.go b/pkg/stargate/service.go index 8b666184f..5633e3ae7 100644 --- a/pkg/stargate/service.go +++ b/pkg/stargate/service.go @@ -56,7 +56,6 @@ func createServiceMeta(stargate *api.Stargate) meta.Tags { coreapi.NameLabel: coreapi.NameLabelValue, coreapi.PartOfLabel: coreapi.PartOfLabelValue, coreapi.ComponentLabel: coreapi.ComponentLabelValueStargate, - coreapi.CreatedByLabel: coreapi.CreatedByLabelValueStargateController, api.StargateLabel: stargate.Name, } diff --git a/pkg/stargate/stargate.go b/pkg/stargate/stargate.go index 79f6c927a..684992842 100644 --- a/pkg/stargate/stargate.go +++ b/pkg/stargate/stargate.go @@ -6,11 +6,13 @@ import ( api "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" stargateapi "github.com/k8ssandra/k8ssandra-operator/apis/stargate/v1alpha1" "github.com/k8ssandra/k8ssandra-operator/pkg/cassandra" + k8ssandralabels "github.com/k8ssandra/k8ssandra-operator/pkg/labels" "github.com/k8ssandra/k8ssandra-operator/pkg/meta" "github.com/k8ssandra/k8ssandra-operator/pkg/utils" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" ) func ResourceName(dc *cassdcapi.CassandraDatacenter) string { @@ -68,14 +70,15 @@ func NewStargate( } func createResourceMeta(stargateTemplate *stargateapi.StargateDatacenterTemplate, kc *api.K8ssandraCluster) meta.Tags { - labels := map[string]string{ - api.NameLabel: api.NameLabelValue, - api.PartOfLabel: api.PartOfLabelValue, - api.ComponentLabel: api.ComponentLabelValueStargate, - api.CreatedByLabel: api.CreatedByLabelValueK8ssandraClusterController, - api.K8ssandraClusterNameLabel: kc.Name, - api.K8ssandraClusterNamespaceLabel: kc.Namespace, - } + labels := utils.MergeMap( + map[string]string{ + api.NameLabel: api.NameLabelValue, + api.PartOfLabel: api.PartOfLabelValue, + api.ComponentLabel: api.ComponentLabelValueStargate, + api.K8ssandraClusterNameLabel: kc.Name, + api.K8ssandraClusterNamespaceLabel: kc.Namespace, + }, + k8ssandralabels.CleanedUpByLabels(client.ObjectKey{Namespace: kc.Namespace, Name: kc.Name})) var annotations map[string]string if m := stargateTemplate.ResourceMeta; m != nil { diff --git a/pkg/stargate/vector.go b/pkg/stargate/vector.go index 2b4b44e9b..c03523340 100644 --- a/pkg/stargate/vector.go +++ b/pkg/stargate/vector.go @@ -2,6 +2,9 @@ package stargate import ( "fmt" + "github.com/k8ssandra/k8ssandra-operator/pkg/labels" + "github.com/k8ssandra/k8ssandra-operator/pkg/utils" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/go-logr/logr" cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1" @@ -31,14 +34,13 @@ func CreateVectorConfigMap(namespace, vectorToml string, dc cassdcapi.CassandraD ObjectMeta: metav1.ObjectMeta{ Name: VectorAgentConfigMapName(dc.Spec.ClusterName, dc.Name), Namespace: namespace, - Labels: map[string]string{ - k8ssandra.NameLabel: k8ssandra.NameLabelValue, - k8ssandra.PartOfLabel: k8ssandra.PartOfLabelValue, - k8ssandra.ComponentLabel: k8ssandra.ComponentLabelValueStargate, - k8ssandra.CreatedByLabel: k8ssandra.CreatedByLabelValueK8ssandraClusterController, - k8ssandra.K8ssandraClusterNameLabel: dc.Labels[k8ssandra.K8ssandraClusterNameLabel], - k8ssandra.K8ssandraClusterNamespaceLabel: namespace, - }, + Labels: utils.MergeMap( + map[string]string{ + k8ssandra.NameLabel: k8ssandra.NameLabelValue, + k8ssandra.PartOfLabel: k8ssandra.PartOfLabelValue, + k8ssandra.ComponentLabel: k8ssandra.ComponentLabelValueStargate, + }, + labels.CleanedUpByLabels(client.ObjectKey{Namespace: namespace, Name: dc.Labels[k8ssandra.K8ssandraClusterNameLabel]})), }, Data: map[string]string{ "vector.toml": vectorToml, diff --git a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go index dd84c1964..aeec80c8f 100644 --- a/pkg/telemetry/cassandra_agent/cassandra_agent_config.go +++ b/pkg/telemetry/cassandra_agent/cassandra_agent_config.go @@ -152,7 +152,7 @@ func (c Configurator) ReconcileTelemetryAgentConfig(dc *cassdcapi.CassandraDatac Name: c.Kluster.Name, Namespace: c.Kluster.Namespace, } - partOfLabels := labels.PartOfLabels(KlKey) + partOfLabels := labels.CleanedUpByLabels(KlKey) desiredCm.SetLabels(partOfLabels) recRes := reconciliation.ReconcileObject(c.Ctx, c.RemoteClient, c.RequeueDelay, *desiredCm) diff --git a/pkg/telemetry/prom_reaper_servicemonitor.go b/pkg/telemetry/prom_reaper_servicemonitor.go index 74b4e5034..c211a35b9 100644 --- a/pkg/telemetry/prom_reaper_servicemonitor.go +++ b/pkg/telemetry/prom_reaper_servicemonitor.go @@ -5,7 +5,6 @@ import ( reaperapi "github.com/k8ssandra/k8ssandra-operator/apis/reaper/v1alpha1" "os" - k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" "github.com/k8ssandra/k8ssandra-operator/pkg/annotations" promapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -52,8 +51,7 @@ func (cfg PrometheusResourcer) NewReaperServiceMonitor() (promapi.ServiceMonitor Spec: promapi.ServiceMonitorSpec{ Selector: metav1.LabelSelector{ MatchLabels: map[string]string{ - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueReaperController, - reaperapi.ReaperLabel: cfg.MonitoringTargetName, + reaperapi.ReaperLabel: cfg.MonitoringTargetName, }, }, NamespaceSelector: promapi.NamespaceSelector{ diff --git a/pkg/telemetry/prom_stargate_servicemonitor.go b/pkg/telemetry/prom_stargate_servicemonitor.go index 3ca8870ee..34fe0f15f 100644 --- a/pkg/telemetry/prom_stargate_servicemonitor.go +++ b/pkg/telemetry/prom_stargate_servicemonitor.go @@ -5,7 +5,6 @@ import ( stargateapi "github.com/k8ssandra/k8ssandra-operator/apis/stargate/v1alpha1" "os" - k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1" "github.com/k8ssandra/k8ssandra-operator/pkg/annotations" promapi "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -138,8 +137,7 @@ func (cfg PrometheusResourcer) NewStargateServiceMonitor() (promapi.ServiceMonit Spec: promapi.ServiceMonitorSpec{ Selector: metav1.LabelSelector{ MatchLabels: map[string]string{ - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueStargateController, - stargateapi.StargateLabel: cfg.MonitoringTargetName, + stargateapi.StargateLabel: cfg.MonitoringTargetName, }, }, NamespaceSelector: promapi.NamespaceSelector{ diff --git a/pkg/telemetry/vector.go b/pkg/telemetry/vector.go index 3db934e42..885824a86 100644 --- a/pkg/telemetry/vector.go +++ b/pkg/telemetry/vector.go @@ -2,6 +2,9 @@ package telemetry import ( "fmt" + "github.com/k8ssandra/k8ssandra-operator/pkg/labels" + "github.com/k8ssandra/k8ssandra-operator/pkg/utils" + "sigs.k8s.io/controller-runtime/pkg/client" "strings" "github.com/go-logr/logr" @@ -292,14 +295,13 @@ func BuildVectorAgentConfigMap(namespace, k8cName, dcName, k8cNamespace, vectorT ObjectMeta: metav1.ObjectMeta{ Name: VectorAgentConfigMapName(k8cName, dcName), Namespace: namespace, - Labels: map[string]string{ - k8ssandraapi.NameLabel: k8ssandraapi.NameLabelValue, - k8ssandraapi.PartOfLabel: k8ssandraapi.PartOfLabelValue, - k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueCassandra, - k8ssandraapi.CreatedByLabel: k8ssandraapi.CreatedByLabelValueK8ssandraClusterController, - k8ssandraapi.K8ssandraClusterNameLabel: k8cName, - k8ssandraapi.K8ssandraClusterNamespaceLabel: k8cNamespace, - }, + Labels: utils.MergeMap( + map[string]string{ + k8ssandraapi.NameLabel: k8ssandraapi.NameLabelValue, + k8ssandraapi.PartOfLabel: k8ssandraapi.PartOfLabelValue, + k8ssandraapi.ComponentLabel: k8ssandraapi.ComponentLabelValueCassandra, + }, + labels.CleanedUpByLabels(client.ObjectKey{Namespace: k8cNamespace, Name: k8cName})), }, Data: map[string]string{ "vector.toml": vectorToml,