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

Add PerClusterResourceCounts to GitRepo status #3209

Merged
merged 5 commits into from
Jan 22, 2025
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
80 changes: 80 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6622,6 +6622,46 @@ spec:
except for changes to .metadata or .status.'
format: int64
type: integer
perClusterResourceCounts:
additionalProperties:
description: ResourceCounts contains the number of resources in
each state.
properties:
desiredReady:
description: DesiredReady is the number of resources that
should be ready.
type: integer
missing:
description: Missing is the number of missing resources.
type: integer
modified:
description: Modified is the number of resources that have
been modified.
type: integer
notReady:
description: 'NotReady is the number of not ready resources.
Resources are not

ready if they do not match any other state.'
type: integer
orphaned:
description: Orphaned is the number of orphaned resources.
type: integer
ready:
description: Ready is the number of ready resources.
type: integer
unknown:
description: Unknown is the number of resources in an unknown
state.
type: integer
waitApplied:
description: WaitApplied is the number of resources that are
waiting to be applied.
type: integer
type: object
description: PerClusterResourceCounts contains the number of resources
in each state over all bundles, per cluster.
type: object
readyClusters:
description: 'ReadyClusters is the lowest number of clusters that
are ready over
Expand Down Expand Up @@ -8417,6 +8457,46 @@ spec:
BundleState according to StateRank.'
type: string
type: object
perClusterResourceCounts:
additionalProperties:
description: ResourceCounts contains the number of resources in
each state.
properties:
desiredReady:
description: DesiredReady is the number of resources that
should be ready.
type: integer
missing:
description: Missing is the number of missing resources.
type: integer
modified:
description: Modified is the number of resources that have
been modified.
type: integer
notReady:
description: 'NotReady is the number of not ready resources.
Resources are not

ready if they do not match any other state.'
type: integer
orphaned:
description: Orphaned is the number of orphaned resources.
type: integer
ready:
description: Ready is the number of ready resources.
type: integer
unknown:
description: Unknown is the number of resources in an unknown
state.
type: integer
waitApplied:
description: WaitApplied is the number of resources that are
waiting to be applied.
type: integer
type: object
description: PerClusterResourceCounts contains the number of resources
in each state over all bundles, per cluster.
type: object
readyClusters:
description: 'ReadyClusters is the lowest number of clusters that
are ready over
Expand Down
70 changes: 27 additions & 43 deletions internal/resourcestatus/resourcekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import (
"sort"
"strings"

"github.com/rancher/fleet/internal/cmd/controller/summary"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
)

func SetResources(list *fleet.BundleDeploymentList, status *fleet.StatusBase) {
s := summaryState(status.Summary)
r, errors := fromResources(list, s)
r, errors := fromResources(list)
status.ResourceErrors = errors
status.ResourceCounts = countResources(r)
status.Resources = merge(r)
status.ResourceCounts = sumResourceCounts(list)
status.PerClusterResourceCounts = resourceCountsPerCluster(list)
}

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

// merge takes a list of GitRepo resources and deduplicates resources deployed to multiple clusters,
Expand Down Expand Up @@ -52,27 +51,30 @@ func key(resource fleet.Resource) string {
return resource.Type + "/" + resource.ID
}

func summaryState(summary fleet.BundleSummary) string {
if summary.WaitApplied > 0 {
return "WaitApplied"
}
if summary.ErrApplied > 0 {
return "ErrApplied"
func resourceCountsPerCluster(list *fleet.BundleDeploymentList) map[string]*fleet.ResourceCounts {
res := make(map[string]*fleet.ResourceCounts)
for _, bd := range list.Items {
clusterID := bd.Labels[fleet.ClusterNamespaceLabel] + "/" + bd.Labels[fleet.ClusterLabel]
if _, ok := res[clusterID]; !ok {
res[clusterID] = &fleet.ResourceCounts{}
}
summary.IncrementResourceCounts(res[clusterID], bd.Status.ResourceCounts)
}
return ""
return res
}

// fromResources inspects all bundledeployments for this GitRepo and returns a list of
// Resources and error messages.
//
// It populates gitrepo status resources from bundleDeployments. BundleDeployment.Status.Resources is the list of deployed resources.
func fromResources(list *fleet.BundleDeploymentList, summaryState string) ([]fleet.Resource, []string) {
func fromResources(list *fleet.BundleDeploymentList) ([]fleet.Resource, []string) {
var (
resources []fleet.Resource
errors []string
)

for _, bd := range list.Items {
state := summary.GetDeploymentState(&bd)
bdResources := bundleDeploymentResources(bd)
incomplete, err := addState(bd, bdResources)

Expand All @@ -84,7 +86,7 @@ func fromResources(list *fleet.BundleDeploymentList, summaryState string) ([]fle
}

for k, perCluster := range bdResources {
resource := toResourceState(k, perCluster, incomplete, summaryState)
resource := toResourceState(k, perCluster, incomplete, string(state))
resources = append(resources, resource)
}
}
Expand All @@ -94,7 +96,7 @@ func fromResources(list *fleet.BundleDeploymentList, summaryState string) ([]fle
return resources, errors
}

func toResourceState(k fleet.ResourceKey, perCluster []fleet.ResourcePerClusterState, incomplete bool, summaryState string) fleet.Resource {
func toResourceState(k fleet.ResourceKey, perCluster []fleet.ResourcePerClusterState, incomplete bool, bdState string) fleet.Resource {
resource := fleet.Resource{
APIVersion: k.APIVersion,
Kind: k.Kind,
Expand All @@ -116,13 +118,13 @@ func toResourceState(k fleet.ResourceKey, perCluster []fleet.ResourcePerClusterS
// fallback to state from gitrepo summary
if resource.State == "" {
if resource.IncompleteState {
if summaryState != "" {
resource.State = summaryState
if bdState != "" {
resource.State = bdState
} else {
resource.State = "Unknown"
}
} else if summaryState != "" {
resource.State = summaryState
} else if bdState != "" {
resource.State = bdState
} else {
resource.State = "Ready"
}
Expand Down Expand Up @@ -237,28 +239,10 @@ func bundleDeploymentResources(bd fleet.BundleDeployment) map[fleet.ResourceKey]
return bdResources
}

func countResources(resources []fleet.Resource) fleet.ResourceCounts {
counts := fleet.ResourceCounts{}

for _, resource := range resources {
counts.DesiredReady++
switch resource.State {
case "Ready":
counts.Ready++
case "WaitApplied":
counts.WaitApplied++
case "Modified":
counts.Modified++
case "Orphan":
counts.Orphaned++
case "Missing":
counts.Missing++
case "Unknown":
counts.Unknown++
default:
counts.NotReady++
}
func sumResourceCounts(list *fleet.BundleDeploymentList) fleet.ResourceCounts {
var res fleet.ResourceCounts
for _, bd := range list.Items {
summary.IncrementResourceCounts(&res, bd.Status.ResourceCounts)
}

return counts
return res
}
Loading
Loading