Skip to content

Commit

Permalink
Use bundledeployment to store resources in gitrepo status (#2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubhanazeem authored Jun 19, 2024
1 parent da48fd8 commit f825b97
Showing 1 changed file with 63 additions and 60 deletions.
123 changes: 63 additions & 60 deletions internal/cmd/controller/grutil/resourcekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -27,37 +28,39 @@ 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 {
errors = append(errors, err.Error())
}
}

for k, state := range bundleResources {
for k, state := range bdResources {
resource := toResourceState(k, state, incomplete, bundleErrorState)
resources = append(resources, resource)
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down

0 comments on commit f825b97

Please sign in to comment.