Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate Clusters resourceCount from BundleDeployments instead of GitRepos #3102

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/single-cluster/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var _ = Describe("Checks status updates happen for a simple deployment", Ordered
out, err := k.Get("cluster", "local", "-n", "fleet-local", "-o", "jsonpath='{.status.display.readyBundles}'")
g.Expect(err).ToNot(HaveOccurred(), out)

// Expected 2 bundles instead of just 1 because fleet-agent is also included here
g.Expect(out).Should(Equal("'2/2'"))
}).Should(Succeed())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/rancher/fleet/internal/cmd/controller/summary"
"github.com/rancher/fleet/internal/resourcestatus"
v1alpha1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/durations"
"github.com/rancher/fleet/pkg/sharding"
Expand Down Expand Up @@ -154,7 +155,7 @@ func setStatus(list *v1alpha1.BundleDeploymentList, gitrepo *v1alpha1.GitRepo) e
return err
}

setResources(list, gitrepo)
resourcestatus.SetGitRepoResources(list, gitrepo)

summary.SetReadyConditions(&gitrepo.Status, "Bundle", gitrepo.Status.Summary)

Expand Down
63 changes: 32 additions & 31 deletions internal/cmd/controller/reconciler/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"context"
"fmt"
"reflect"
"slices"
"sort"
"time"

"github.com/rancher/fleet/internal/cmd/controller/summary"
"github.com/rancher/fleet/internal/metrics"
"github.com/rancher/fleet/internal/resourcestatus"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/durations"
"github.com/rancher/fleet/pkg/sharding"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

fleetutil "github.com/rancher/fleet/internal/cmd/controller/errorutil"
"github.com/rancher/wrangler/v3/pkg/condition"
Expand Down Expand Up @@ -43,11 +46,6 @@ var LongRetry = wait.Backoff{
Jitter: 0.1,
}

type repoKey struct {
repo string
ns string
}

// ClusterReconciler reconciles a Cluster object
type ClusterReconciler struct {
client.Client
Expand Down Expand Up @@ -107,6 +105,14 @@ func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func indexByNamespacedName[T metav1.Object](list []T) map[types.NamespacedName]T {
res := make(map[types.NamespacedName]T, len(list))
for _, obj := range list {
res[types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}] = obj
}
return res
}

//+kubebuilder:rbac:groups=fleet.cattle.io,resources=clusters,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=fleet.cattle.io,resources=clusters/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=fleet.cattle.io,resources=clusters/finalizers,verbs=update
Expand Down Expand Up @@ -146,20 +152,21 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if err != nil {
return ctrl.Result{}, r.updateErrorStatus(ctx, req.NamespacedName, cluster.Status, err)
}

deleted := map[types.UID]bool{}
for _, bundle := range cleanup {
for _, bd := range bundleDeployments.Items {
if bd.Labels[fleet.BundleLabel] == bundle.Name && bd.Labels[fleet.BundleNamespaceLabel] == bundle.Namespace {
logger.V(1).Info("cleaning up bundleDeployment not matching the cluster", "bundledeployment", bd)
err := r.Delete(ctx, &bd)
if err != nil {
logger.V(1).Info("deleting bundleDeployment returned an error", "error", err)
}
deleted[bd.GetUID()] = true
toDeleteBundles := indexByNamespacedName(cleanup)

// Delete BundleDeployments for Bundles being removed while getting a filtered items list
bundleDeployments.Items = slices.DeleteFunc(bundleDeployments.Items, func(bd fleet.BundleDeployment) bool {
bundleNamespace := bd.Labels[fleet.BundleNamespaceLabel]
bundleName := bd.Labels[fleet.BundleLabel]
if _, ok := toDeleteBundles[types.NamespacedName{Namespace: bundleNamespace, Name: bundleName}]; ok {
logger.V(1).Info("cleaning up bundleDeployment not matching the cluster", "bundledeployment", bd)
if err := r.Delete(ctx, &bd); err != nil {
logger.V(1).Info("deleting bundleDeployment returned an error", "error", err)
}
return true
}
}
return false
})

// Count the number of gitrepo, bundledeployemt and deployed resources for this cluster
cluster.Status.DesiredReadyGitRepos = 0
Expand All @@ -171,33 +178,27 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return bundleDeployments.Items[i].Name < bundleDeployments.Items[j].Name
})

repos := map[repoKey]bool{}
for _, bd := range bundleDeployments.Items {
// do not count bundledeployments that were just deleted
if deleted[bd.GetUID()] {
continue
}
resourcestatus.SetClusterResources(bundleDeployments, cluster)

bd := bd
repos := map[types.NamespacedName]bool{}
for _, bd := range bundleDeployments.Items {
state := summary.GetDeploymentState(&bd)
summary.IncrementState(&cluster.Status.Summary, bd.Name, state, summary.MessageFromDeployment(&bd), bd.Status.ModifiedStatus, bd.Status.NonReadyStatus)
cluster.Status.Summary.DesiredReady++

repo := bd.Labels[fleet.RepoLabel]
ns := bd.Labels[fleet.BundleNamespaceLabel]
if repo != "" && ns != "" {
repoNamespace, repoName := bd.Labels[fleet.BundleNamespaceLabel], bd.Labels[fleet.RepoLabel]
if repoNamespace != "" && repoName != "" {
// a gitrepo is ready if its bundledeployments are ready, take previous state into account
repos[repoKey{repo: repo, ns: ns}] = (state == fleet.Ready) || repos[repoKey{repo: repo, ns: ns}]
repoKey := types.NamespacedName{Namespace: repoNamespace, Name: repoName}
repos[repoKey] = (state == fleet.Ready) || repos[repoKey]
}
}

// a cluster is ready if all its gitrepos are ready and the resources are ready too
allReady := true
for repo, ready := range repos {
gitrepo := &fleet.GitRepo{}
err := r.Get(ctx, types.NamespacedName{Namespace: repo.ns, Name: repo.repo}, gitrepo)
if err == nil {
summary.IncrementResourceCounts(&cluster.Status.ResourceCounts, gitrepo.Status.ResourceCounts)
if err := r.Get(ctx, repo, gitrepo); err == nil {
cluster.Status.DesiredReadyGitRepos++
if ready {
cluster.Status.ReadyGitRepos++
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reconciler
package resourcestatus

import (
"encoding/json"
Expand All @@ -8,14 +8,23 @@ import (
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
)

func setResources(list *fleet.BundleDeploymentList, gitrepo *fleet.GitRepo) {
func SetGitRepoResources(list *fleet.BundleDeploymentList, gitrepo *fleet.GitRepo) {
s := summaryState(gitrepo.Status.Summary)
r, errors := fromResources(list, s)
gitrepo.Status.ResourceErrors = errors
gitrepo.Status.ResourceCounts = countResources(r)
gitrepo.Status.Resources = merge(r)
}

func SetClusterResources(list *fleet.BundleDeploymentList, cluster *fleet.Cluster) {
s := summaryState(cluster.Status.Summary)
r, _ := fromResources(list, s)
cluster.Status.ResourceCounts = countResources(r)
}

// merge takes a list of GitRepo resources and deduplicates resources deployed to multiple clusters,
// ensuring that for such resources, the output contains a single resource entry with a field summarizing
// its status on each cluster.
func merge(resources []fleet.GitRepoResource) []fleet.GitRepoResource {
merged := map[string]fleet.GitRepoResource{}
for _, resource := range resources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reconciler
package resourcestatus

import (
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -114,7 +114,7 @@ var _ = Describe("Resourcekey", func() {
})

It("returns a list", func() {
setResources(list, gitrepo)
SetGitRepoResources(list, gitrepo)

Expect(gitrepo.Status.Resources).To(HaveLen(2))
Expect(gitrepo.Status.Resources).To(ContainElement(fleet.GitRepoResource{
Expand Down
Loading