From f825b979418663c989d497b868a1259722a9b472 Mon Sep 17 00:00:00 2001 From: Rubhan Azeem Date: Wed, 19 Jun 2024 02:48:53 +0200 Subject: [PATCH] Use bundledeployment to store resources in gitrepo status (#2523) --- internal/cmd/controller/grutil/resourcekey.go | 123 +++++++++--------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/internal/cmd/controller/grutil/resourcekey.go b/internal/cmd/controller/grutil/resourcekey.go index 184510a6ac..2163cd4df4 100644 --- a/internal/cmd/controller/grutil/resourcekey.go +++ b/internal/cmd/controller/grutil/resourcekey.go @@ -6,6 +6,7 @@ import ( "sort" "strings" + "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -27,29 +28,31 @@ func bundleErrorState(summary fleet.BundleSummary) string { return bundleErrorState } -// fromResourceKey lists all bundles for this GitRepo and returns a list of +// fromResourceKey lists all bundledeployments for this GitRepo and returns a list of // GitRepoResource states for all resources // -// NOTE: This functions uses the ResourceKey to identify resources. This key is -// not accurate. It's generated by running helm template on the upstream -// cluster, not the actual bundle deployments. -// BundleDeployment.Status.Resources is the list of deployed resources. +// It populates gitrepo status resources from bundleDeployments. BundleDeployment.Status.Resources is the list of deployed resources. func fromResourceKey(ctx context.Context, c client.Client, namespace, name string, bundleErrorState string) ([]fleet.GitRepoResource, []string) { var ( resources []fleet.GitRepoResource errors []string ) - bundles := &fleet.BundleList{} - err := c.List(ctx, bundles, client.InNamespace(namespace), client.MatchingLabels{fleet.RepoLabel: name}) + bdList := &fleet.BundleDeploymentList{} + err := c.List(ctx, bdList, client.MatchingLabels{ + fleet.RepoLabel: name, + fleet.BundleNamespaceLabel: namespace, + }) if err != nil { errors = append(errors, err.Error()) return resources, errors } - for _, bundle := range bundles.Items { - bundleResources := bundleResources(bundle) - incomplete, err := addState(bundle, bundleResources) + for _, bd := range bdList.Items { + bd := bd // fix gosec warning regarding "Implicit memory aliasing in for loop" + bdResources := bundleDeploymentResources(bd) + incomplete, err := addState(bd, bdResources) + if len(err) > 0 { incomplete = true for _, err := range err { @@ -57,7 +60,7 @@ func fromResourceKey(ctx context.Context, c client.Client, namespace, name strin } } - for k, state := range bundleResources { + for k, state := range bdResources { resource := toResourceState(k, state, incomplete, bundleErrorState) resources = append(resources, resource) } @@ -124,64 +127,58 @@ func toType(resource fleet.GitRepoResource) (string, string) { return t, resource.Namespace + "/" + resource.Name } -func addState(bundle fleet.Bundle, resources map[fleet.ResourceKey][]fleet.ResourcePerClusterState) (bool, []error) { +func addState(bd fleet.BundleDeployment, resources map[fleet.ResourceKey][]fleet.ResourcePerClusterState) (bool, []error) { var ( incomplete bool errors []error ) - if len(bundle.Status.Summary.NonReadyResources) >= 10 { + if len(bd.Status.NonReadyStatus) >= 10 || len(bd.Status.ModifiedStatus) >= 10 { incomplete = true } - for _, nonReadyResource := range bundle.Status.Summary.NonReadyResources { - if len(nonReadyResource.NonReadyStatus) >= 10 || len(nonReadyResource.ModifiedStatus) >= 10 { - incomplete = true + cluster := bd.Labels[v1alpha1.ClusterNamespaceLabel] + "/" + bd.Labels[v1alpha1.ClusterLabel] + for _, nonReady := range bd.Status.NonReadyStatus { + key := fleet.ResourceKey{ + Kind: nonReady.Kind, + APIVersion: nonReady.APIVersion, + Namespace: nonReady.Namespace, + Name: nonReady.Name, } - - for _, nonReady := range nonReadyResource.NonReadyStatus { - key := fleet.ResourceKey{ - Kind: nonReady.Kind, - APIVersion: nonReady.APIVersion, - Namespace: nonReady.Namespace, - Name: nonReady.Name, - } - state := fleet.ResourcePerClusterState{ - State: nonReady.Summary.State, - Error: nonReady.Summary.Error, - Transitioning: nonReady.Summary.Transitioning, - Message: strings.Join(nonReady.Summary.Message, "; "), - ClusterID: nonReadyResource.Name, - } - appendState(resources, key, state) + state := fleet.ResourcePerClusterState{ + State: nonReady.Summary.State, + Error: nonReady.Summary.Error, + Transitioning: nonReady.Summary.Transitioning, + Message: strings.Join(nonReady.Summary.Message, "; "), + ClusterID: cluster, } + appendState(resources, key, state) + } - for _, modified := range nonReadyResource.ModifiedStatus { - key := fleet.ResourceKey{ - Kind: modified.Kind, - APIVersion: modified.APIVersion, - Namespace: modified.Namespace, - Name: modified.Name, - } - state := fleet.ResourcePerClusterState{ - State: "Modified", - ClusterID: nonReadyResource.Name, - } - if modified.Delete { - state.State = "Orphaned" - } else if modified.Create { - state.State = "Missing" - } else if len(modified.Patch) > 0 { - state.Patch = &fleet.GenericMap{} - err := json.Unmarshal([]byte(modified.Patch), state.Patch) - if err != nil { - errors = append(errors, err) - } + for _, modified := range bd.Status.ModifiedStatus { + key := fleet.ResourceKey{ + Kind: modified.Kind, + APIVersion: modified.APIVersion, + Namespace: modified.Namespace, + Name: modified.Name, + } + state := fleet.ResourcePerClusterState{ + State: "Modified", + ClusterID: cluster, + } + if modified.Delete { + state.State = "Orphaned" + } else if modified.Create { + state.State = "Missing" + } else if len(modified.Patch) > 0 { + state.Patch = &fleet.GenericMap{} + err := json.Unmarshal([]byte(modified.Patch), state.Patch) + if err != nil { + errors = append(errors, err) } - appendState(resources, key, state) } + appendState(resources, key, state) } - return incomplete, errors } @@ -202,12 +199,18 @@ func appendState(states map[fleet.ResourceKey][]fleet.ResourcePerClusterState, k } } -func bundleResources(bundle fleet.Bundle) map[fleet.ResourceKey][]fleet.ResourcePerClusterState { - bundleResources := map[fleet.ResourceKey][]fleet.ResourcePerClusterState{} - for _, resourceKey := range bundle.Status.ResourceKey { - bundleResources[resourceKey] = []fleet.ResourcePerClusterState{} +func bundleDeploymentResources(bd fleet.BundleDeployment) map[fleet.ResourceKey][]fleet.ResourcePerClusterState { + bdResources := map[fleet.ResourceKey][]fleet.ResourcePerClusterState{} + for _, resource := range bd.Status.Resources { + resourceKey := fleet.ResourceKey{ + Kind: resource.Kind, + APIVersion: resource.APIVersion, + Name: resource.Name, + Namespace: resource.Namespace, + } + bdResources[resourceKey] = []fleet.ResourcePerClusterState{} } - return bundleResources + return bdResources } func countResources(status fleet.GitRepoStatus) fleet.GitRepoStatus {