Skip to content

Commit

Permalink
Add resource counts to gitrepo, cluster, clustergroup
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Sep 24, 2020
1 parent 97af6e1 commit 6925825
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 28 deletions.
4 changes: 4 additions & 0 deletions notes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
uninstalling
store does not differentiate between notfound and error
maxhistory is a global field
install needs max-history because of replace
31 changes: 23 additions & 8 deletions pkg/apis/fleet.cattle.io/v1alpha1/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

var (
RepoLabel = "fleet.cattle.io/repo-name"
RepoLabel = "fleet.cattle.io/repo-name"
BundleNamespaceLabel = "fleet.cattle.io/bundle-namespace"
)

// +genclient
Expand Down Expand Up @@ -63,13 +64,27 @@ type GitTarget struct {
}

type GitRepoStatus struct {
ObservedGeneration int64 `json:"observedGeneration"`
Commit string `json:"commit,omitempty"`
Summary BundleSummary `json:"summary,omitempty"`
Display GitRepoDisplay `json:"display,omitempty"`
Conditions []genericcondition.GenericCondition `json:"conditions,omitempty"`
Resources []GitRepoResource `json:"resources,omitempty"`
ResourceErrors []string `json:"resourceErrors,omitempty"`
ObservedGeneration int64 `json:"observedGeneration"`
Commit string `json:"commit,omitempty"`
ReadyClusters int `json:"readyClusters"`
DesiredReadyClusters int `json:"desiredReadyClusters"`
Summary BundleSummary `json:"summary,omitempty"`
Display GitRepoDisplay `json:"display,omitempty"`
Conditions []genericcondition.GenericCondition `json:"conditions,omitempty"`
Resources []GitRepoResource `json:"resources,omitempty"`
ResourceCounts GitRepoResourceCounts `json:"resourceCounts,omitempty"`
ResourceErrors []string `json:"resourceErrors,omitempty"`
}

type GitRepoResourceCounts struct {
Ready int `json:"ready"`
DesiredReady int `json:"desiredReady"`
WaitApplied int `json:"waitApplied"`
Modified int `json:"modified"`
Orphaned int `json:"orphaned"`
Missing int `json:"missing"`
Unknown int `json:"unknown"`
NotReady int `json:"notReady"`
}

type GitRepoDisplay struct {
Expand Down
13 changes: 9 additions & 4 deletions pkg/apis/fleet.cattle.io/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ClusterGroupStatus struct {
Conditions []genericcondition.GenericCondition `json:"conditions,omitempty"`
Summary BundleSummary `json:"summary,omitempty"`
Display ClusterGroupDisplay `json:"display,omitempty"`
ResourceCounts GitRepoResourceCounts `json:"resourceCounts,omitempty"`
}

type ClusterGroupDisplay struct {
Expand Down Expand Up @@ -64,10 +65,14 @@ type ClusterSpec struct {
}

type ClusterStatus struct {
Conditions []genericcondition.GenericCondition `json:"conditions,omitempty"`
Namespace string `json:"namespace,omitempty"`
Summary BundleSummary `json:"summary,omitempty"`
AgentLastDeployed *metav1.Time `json:"agentLastDeployed,omitempty"`
Conditions []genericcondition.GenericCondition `json:"conditions,omitempty"`
Namespace string `json:"namespace,omitempty"`
Summary BundleSummary `json:"summary,omitempty"`
ResourceCounts GitRepoResourceCounts `json:"resourceCounts,omitempty"`
ReadyGitRepos int `json:"readyGitRepos,omitempty"`
DesiredReadyGitRepos int `json:"desiredReadyGitRepos,omitempty"`

AgentLastDeployed *metav1.Time `json:"agentLastDeployed,omitempty"`

Display ClusterDisplay `json:"display,omitempty"`
Agent AgentStatus `json:"agent,omitempty"`
Expand Down
31 changes: 31 additions & 0 deletions pkg/controllers/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/summary"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/condition"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/generic"
"github.com/rancher/wrangler/pkg/name"
Expand All @@ -24,19 +25,27 @@ type handler struct {
clusters fleetcontrollers.ClusterCache
clusterGroups fleetcontrollers.ClusterGroupCache
bundleDeployment fleetcontrollers.BundleDeploymentCache
gitRepos fleetcontrollers.GitRepoCache
}

type repoKey struct {
repo string
ns string
}

func Register(ctx context.Context,
bundleDeployment fleetcontrollers.BundleDeploymentController,
clusterGroups fleetcontrollers.ClusterGroupCache,
clusters fleetcontrollers.ClusterController,
gitRepos fleetcontrollers.GitRepoCache,
namespaces corecontrollers.NamespaceController, apply apply.Apply) {

h := &handler{
apply: apply,
clusterGroups: clusterGroups,
clusters: clusters.Cache(),
bundleDeployment: bundleDeployment.Cache(),
gitRepos: gitRepos,
}

fleetcontrollers.RegisterClusterGeneratingHandler(ctx,
Expand Down Expand Up @@ -94,16 +103,38 @@ func (h *handler) OnClusterChanged(cluster *fleet.Cluster, status fleet.ClusterS
clusterregistration.KeyHash(cluster.Namespace+"::"+cluster.Name))
status.Namespace = ns
}

status.DesiredReadyGitRepos = 0
status.ReadyGitRepos = 0
status.ResourceCounts = fleet.GitRepoResourceCounts{}
status.Summary = fleet.BundleSummary{}

sort.Slice(bundleDeployments, func(i, j int) bool {
return bundleDeployments[i].Name < bundleDeployments[j].Name
})

repos := map[repoKey]struct{}{}
for _, app := range bundleDeployments {
state := summary.GetDeploymentState(app)
summary.IncrementState(&status.Summary, app.Name, state, summary.MessageFromDeployment(app), app.Status.ModifiedStatus, app.Status.NonReadyStatus)
status.Summary.DesiredReady++

repo := app.Labels[fleet.RepoLabel]
ns := app.Labels[fleet.BundleNamespaceLabel]
if repo != "" && ns != "" {
repos[repoKey{repo: repo, ns: ns}] = struct{}{}
}
}

for repo := range repos {
gitrepo, err := h.gitRepos.Get(repo.ns, repo.repo)
if err == nil {
summary.IncrementResourceCounts(&status.ResourceCounts, gitrepo.Status.ResourceCounts)
}
status.DesiredReadyGitRepos++
if condition.Cond("Ready").IsTrue(gitrepo) {
status.ReadyGitRepos++
}
}

summary.SetReadyConditions(&status, "Bundle", status.Summary)
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/clustergroup/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (h *handler) OnClusterGroup(clusterGroup *fleet.ClusterGroup, status fleet.
}

status.Summary = fleet.BundleSummary{}
status.ResourceCounts = fleet.GitRepoResourceCounts{}
status.ClusterCount = 0
status.NonReadyClusterCount = 0
status.NonReadyClusters = nil
Expand All @@ -89,6 +90,7 @@ func (h *handler) OnClusterGroup(clusterGroup *fleet.ClusterGroup, status fleet.
})

for _, cluster := range clusters {
summary.IncrementResourceCounts(&status.ResourceCounts, cluster.Status.ResourceCounts)
summary.Increment(&status.Summary, cluster.Status.Summary)
status.ClusterCount++
if !summary.IsReady(cluster.Status.Summary) {
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func Register(ctx context.Context, systemNamespace string, cfg clientcmd.ClientC
appCtx.BundleDeployment(),
appCtx.ClusterGroup().Cache(),
appCtx.Cluster(),
appCtx.GitRepo().Cache(),
appCtx.Core.Namespace(),
appCtx.Apply.WithCacheTypes(
appCtx.Core.Namespace()))
Expand Down
9 changes: 1 addition & 8 deletions pkg/controllers/display/displaycontrollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,7 @@ func (h *handler) OnBundleChange(_ *fleet.Bundle, status fleet.BundleStatus) (fl
status.Display.ReadyClusters = fmt.Sprintf("%d/%d",
status.Summary.Ready,
status.Summary.DesiredReady)

var state fleet.BundleState
for _, nonReady := range status.Summary.NonReadyResources {
if fleet.StateRank[nonReady.State] > fleet.StateRank[state] {
state = nonReady.State
}
}
status.Display.State = string(state)
status.Display.State = string(summary.GetSummaryState(status.Summary))

return status, nil
}
Expand Down
66 changes: 62 additions & 4 deletions pkg/controllers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Register(ctx context.Context,
func resolveGitRepo(namespace, name string, obj runtime.Object) ([]relatedresource.Key, error) {
if bundleDeployment, ok := obj.(*fleet.BundleDeployment); ok {
repo := bundleDeployment.Labels[fleet.RepoLabel]
ns := bundleDeployment.Labels["fleet.cattle.io/bundle-namespace"]
ns := bundleDeployment.Labels[fleet.BundleNamespaceLabel]
if repo != "" && ns != "" {
return []relatedresource.Key{{
Namespace: ns,
Expand Down Expand Up @@ -271,7 +271,7 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (

status.ObservedGeneration = gitrepo.Generation

status, err = h.setBundleDeploymentStatus(gitrepo, status)
status, err = h.setBundleStatus(gitrepo, status)
if err != nil {
return nil, status, err
}
Expand Down Expand Up @@ -311,7 +311,8 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (

saName := name.SafeConcatName("git", gitrepo.Name)

status.Resources, status.ResourceErrors = h.display.Render(gitrepo.Namespace, gitrepo.Name, h.isNamespaced)
status.Resources, status.ResourceErrors = h.display.Render(gitrepo.Namespace, gitrepo.Name, status.Summary.WaitApplied > 0, h.isNamespaced)
status = countResources(status)
return []runtime.Object{
configMap,
&corev1.ServiceAccount{
Expand Down Expand Up @@ -428,7 +429,33 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (
}, status, nil
}

func (h *handler) setBundleDeploymentStatus(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (fleet.GitRepoStatus, error) {
func countResources(status fleet.GitRepoStatus) fleet.GitRepoStatus {
status.ResourceCounts = fleet.GitRepoResourceCounts{}

for _, resource := range status.Resources {
status.ResourceCounts.DesiredReady++
switch resource.State {
case "Ready":
status.ResourceCounts.Ready++
case "WaitApplied":
status.ResourceCounts.WaitApplied++
case "Modified":
status.ResourceCounts.Modified++
case "Orphan":
status.ResourceCounts.Orphaned++
case "Missing":
status.ResourceCounts.Missing++
case "Unknown":
status.ResourceCounts.Unknown++
default:
status.ResourceCounts.NotReady++
}
}

return status
}

func (h *handler) setBundleStatus(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (fleet.GitRepoStatus, error) {
if gitrepo.DeletionTimestamp != nil {
return status, nil
}
Expand Down Expand Up @@ -461,7 +488,38 @@ func (h *handler) setBundleDeploymentStatus(gitrepo *fleet.GitRepo, status fleet
maxState = ""
}

bundles, err := h.bundleCache.List(gitrepo.Namespace, labels.SelectorFromSet(labels.Set{
fleet.RepoLabel: gitrepo.Name,
}))
if err != nil {
return status, err
}

sort.Slice(bundles, func(i, j int) bool {
return bundles[i].Name < bundles[j].Name
})

var (
clustersDesiredReady int
clustersReady = -1
)

for _, bundle := range bundles {
if bundle.Status.Summary.DesiredReady > 0 {
clustersDesiredReady = bundle.Status.Summary.DesiredReady
if clustersReady < 0 || bundle.Status.Summary.Ready < clustersReady {
clustersReady = bundle.Status.Summary.Ready
}
}
}

if clustersReady < 0 {
clustersReady = 0
}

status.Display.State = string(maxState)
status.DesiredReadyClusters = clustersDesiredReady
status.ReadyClusters = clustersReady
summary.SetReadyConditions(&status, "Bundle", status.Summary)
return status, nil
}
14 changes: 10 additions & 4 deletions pkg/display/gitrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type key struct {
Name string
}

func (b *Factory) Render(namespace, name string, isNSed func(schema.GroupVersionKind) bool) ([]fleet.GitRepoResource, []string) {
func (b *Factory) Render(namespace, name string, waitApplied bool, isNSed func(schema.GroupVersionKind) bool) ([]fleet.GitRepoResource, []string) {
var (
resources []fleet.GitRepoResource
errors []string
Expand Down Expand Up @@ -64,7 +64,7 @@ func (b *Factory) Render(namespace, name string, isNSed func(schema.GroupVersion
}

for k, state := range bundleResources {
resource := toResourceState(k, state, incomplete)
resource := toResourceState(k, state, incomplete, waitApplied)
resources = append(resources, resource)
}
}
Expand All @@ -77,7 +77,7 @@ func (b *Factory) Render(namespace, name string, isNSed func(schema.GroupVersion
return resources, errors
}

func toResourceState(k key, state []fleet.ResourcePerClusterState, incomplete bool) fleet.GitRepoResource {
func toResourceState(k key, state []fleet.ResourcePerClusterState, incomplete, waitApplied bool) fleet.GitRepoResource {
resource := fleet.GitRepoResource{
APIVersion: k.APIVersion,
Kind: k.Kind,
Expand All @@ -98,7 +98,13 @@ func toResourceState(k key, state []fleet.ResourcePerClusterState, incomplete bo

if resource.State == "" {
if resource.IncompleteState {
resource.State = "Unknown"
if waitApplied {
resource.State = "WaitApplied"
} else {
resource.State = "Unknown"
}
} else if waitApplied {
resource.State = "WaitApplied"
} else {
resource.State = "Ready"
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ func Increment(left *fleet.BundleSummary, right fleet.BundleSummary) {
}
}

func IncrementResourceCounts(left *fleet.GitRepoResourceCounts, right fleet.GitRepoResourceCounts) {
left.Ready += right.Ready
left.DesiredReady += right.DesiredReady
left.WaitApplied += right.WaitApplied
left.Modified += right.Modified
left.Orphaned += right.Orphaned
left.Missing += right.Missing
left.Unknown += right.Unknown
left.NotReady += right.NotReady
}

func GetSummaryState(summary fleet.BundleSummary) fleet.BundleState {
var state fleet.BundleState
for _, nonReady := range summary.NonReadyResources {
if fleet.StateRank[nonReady.State] > fleet.StateRank[state] {
state = nonReady.State
}
}
return state
}

func GetDeploymentState(bundleDeployment *fleet.BundleDeployment) fleet.BundleState {
switch {
case bundleDeployment.Status.AppliedDeploymentID != bundleDeployment.Spec.DeploymentID:
Expand Down

0 comments on commit 6925825

Please sign in to comment.