From 477ee3a5309c0624b519961dfdca2e3e44aed5d6 Mon Sep 17 00:00:00 2001 From: Zhonghu Xu Date: Wed, 11 Oct 2023 15:40:13 +0800 Subject: [PATCH] cleanup fleet label and ownerref Signed-off-by: Zhonghu Xu --- pkg/fleet-manager/controlplane.go | 22 +++++----------------- pkg/fleet-manager/fleet.go | 4 ++-- pkg/fleet-manager/fleet_plugin.go | 2 +- pkg/fleet-manager/fleet_plugin_metric.go | 4 ++-- pkg/fleet-manager/util.go | 10 +++++----- 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/pkg/fleet-manager/controlplane.go b/pkg/fleet-manager/controlplane.go index bc4852005..38b2231bf 100644 --- a/pkg/fleet-manager/controlplane.go +++ b/pkg/fleet-manager/controlplane.go @@ -67,13 +67,7 @@ func (f *FleetManager) reconcileControlPlane(ctx context.Context, fleet *fleetap return nil } - ownerref := metav1.OwnerReference{ - APIVersion: fleetapi.GroupVersion.String(), - Kind: FleetKind, - Name: fleet.Name, - UID: fleet.UID, - } - + ownerref := ownerReference(fleet) sa := corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Namespace: namespace, @@ -81,7 +75,7 @@ func (f *FleetManager) reconcileControlPlane(ctx context.Context, fleet *fleetap Labels: map[string]string{ FleetLabel: fleet.Name, }, - OwnerReferences: []metav1.OwnerReference{ownerref}, + OwnerReferences: []metav1.OwnerReference{*ownerref}, }, TypeMeta: metav1.TypeMeta{ Kind: "ServiceAccount", @@ -121,7 +115,7 @@ func (f *FleetManager) reconcileControlPlane(ctx context.Context, fleet *fleetap Labels: map[string]string{ FleetLabel: fleet.Name, }, - OwnerReferences: []metav1.OwnerReference{ownerref}, + OwnerReferences: []metav1.OwnerReference{*ownerref}, }, TypeMeta: metav1.TypeMeta{ Kind: "Pod", @@ -201,13 +195,7 @@ func (f *FleetManager) deleteControlPlane(ctx context.Context, fleet *fleetapi.F return nil } - ownerref := metav1.OwnerReference{ - APIVersion: fleetapi.GroupVersion.String(), - Kind: FleetKind, - Name: fleet.Name, - UID: fleet.UID, - } - + ownerref := ownerReference(fleet) // pod not found, create it initCmd := "echo y | karmadactl deinit -n " + namespace pod = corev1.Pod{ @@ -217,7 +205,7 @@ func (f *FleetManager) deleteControlPlane(ctx context.Context, fleet *fleetapi.F Labels: map[string]string{ FleetLabel: fleet.Name, }, - OwnerReferences: []metav1.OwnerReference{ownerref}, + OwnerReferences: []metav1.OwnerReference{*ownerref}, }, TypeMeta: metav1.TypeMeta{ Kind: "Pod", diff --git a/pkg/fleet-manager/fleet.go b/pkg/fleet-manager/fleet.go index 433a50adc..a39717767 100644 --- a/pkg/fleet-manager/fleet.go +++ b/pkg/fleet-manager/fleet.go @@ -40,11 +40,11 @@ import ( ) const ( - FleetKind = "Fleet" FleetFinalizer = "fleet.kurator.dev" RequeueAfter = 5 * time.Second - FleetLabel = "fleet.kurator.dev/fleet-name" + // TODO: remove as we have `FleetNameLabel` + FleetLabel = "fleet.kurator.dev/fleet-name" ) // FleetManager reconciles a Cluster object diff --git a/pkg/fleet-manager/fleet_plugin.go b/pkg/fleet-manager/fleet_plugin.go index d8206f449..00ab83759 100644 --- a/pkg/fleet-manager/fleet_plugin.go +++ b/pkg/fleet-manager/fleet_plugin.go @@ -123,7 +123,7 @@ func (f *FleetManager) reconcilePluginResources(ctx context.Context, fleet *flee helmRepos := &sourcev1beta2.HelmRepositoryList{} helmReleases := &hrapiv2b1.HelmReleaseList{} - fleetLabels := fleetResourceLables(fleet.Name) + fleetLabels := fleetResourceLabels(fleet.Name) if err := f.Client.List(ctx, helmRepos, client.InNamespace(fleet.Namespace), fleetLabels); err != nil { log.Error(err, "failed to list helm repository") return ctrl.Result{}, err diff --git a/pkg/fleet-manager/fleet_plugin_metric.go b/pkg/fleet-manager/fleet_plugin_metric.go index bdd2c4974..ba5d559a4 100644 --- a/pkg/fleet-manager/fleet_plugin_metric.go +++ b/pkg/fleet-manager/fleet_plugin_metric.go @@ -136,7 +136,7 @@ func (f *FleetManager) reconcileSidecarRemoteService(ctx context.Context, fleet ObjectMeta: metav1.ObjectMeta{ Name: svcNN.Name, Namespace: svcNN.Namespace, - Labels: fleetMetricResourceLables(fleet.Name), + Labels: fleetMetricResourceLabels(fleet.Name), OwnerReferences: []metav1.OwnerReference{ ownerReference, }, @@ -269,7 +269,7 @@ func (f *FleetManager) reconcileMetricPlugin(ctx context.Context, fleet *fleetap ObjectMeta: metav1.ObjectMeta{ Name: objSecret.Name, Namespace: MonitoringNamespace, // TODO: make it configurable - Labels: fleetMetricResourceLables(fleet.Name), + Labels: fleetMetricResourceLabels(fleet.Name), }, Data: objSecret.Data, } diff --git a/pkg/fleet-manager/util.go b/pkg/fleet-manager/util.go index ee690ca4f..23df554ed 100644 --- a/pkg/fleet-manager/util.go +++ b/pkg/fleet-manager/util.go @@ -34,16 +34,16 @@ const ( ManagedByFleetManager = "fleet-manager" ) -func fleetResourceLables(fleetName string) client.MatchingLabels { +func fleetResourceLabels(fleetName string) client.MatchingLabels { return map[string]string{ - ManagedByLabel: "fleet-manager", + ManagedByLabel: ManagedByFleetManager, FleetNameLabel: fleetName, } } -func fleetMetricResourceLables(fleetName string) client.MatchingLabels { +func fleetMetricResourceLabels(fleetName string) client.MatchingLabels { return map[string]string{ - ManagedByLabel: "fleet-manager", + ManagedByLabel: ManagedByFleetManager, FleetNameLabel: fleetName, FleetPluginName: "metric", } @@ -83,7 +83,7 @@ func convertToSubset(endpoints sets.Set[string]) []corev1.EndpointSubset { func ownerReference(fleet *fleetv1a1.Fleet) *metav1.OwnerReference { return &metav1.OwnerReference{ APIVersion: fleetv1a1.GroupVersion.String(), - Kind: "Fleet", // TODO: use pkg typemeta + Kind: fleet.Kind, Name: fleet.Name, UID: fleet.UID, }