Skip to content

Commit

Permalink
cilium-dbg: make remote clusters output logic reusable
Browse files Browse the repository at this point in the history
Extract this logic into a separate function, so that it can be reused
for the kvstoremesh-dbg command as well. Similarly, let's also slightly
refactor and export the NumReadyClusters helper function.

Signed-off-by: Marco Iorio <[email protected]>
  • Loading branch information
giorio94 authored and youngnick committed May 3, 2024
1 parent f7bd2b4 commit e670ca6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
62 changes: 33 additions & 29 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ func clusterReadiness(cluster *models.RemoteCluster) string {
return "ready"
}

func numReadyClusters(clustermesh *models.ClusterMeshStatus) int {
func NumReadyClusters(clusters []*models.RemoteCluster) int {
numReady := 0
for _, cluster := range clustermesh.Clusters {
for _, cluster := range clusters {
if cluster.Ready {
numReady++
}
Expand Down Expand Up @@ -425,34 +425,9 @@ func FormatStatusResponse(w io.Writer, sr *models.StatusResponse, sd StatusDetai

if sr.ClusterMesh != nil {
fmt.Fprintf(w, "ClusterMesh:\t%d/%d clusters ready, %d global-services\n",
numReadyClusters(sr.ClusterMesh), len(sr.ClusterMesh.Clusters), sr.ClusterMesh.NumGlobalServices)

for _, cluster := range sr.ClusterMesh.Clusters {
if sd.AllClusters || !cluster.Ready {
fmt.Fprintf(w, " %s: %s, %d nodes, %d endpoints, %d identities, %d services, %d failures (last: %s)\n",
cluster.Name, clusterReadiness(cluster), cluster.NumNodes,
cluster.NumEndpoints, cluster.NumIdentities, cluster.NumSharedServices,
cluster.NumFailures, timeSince(time.Time(cluster.LastFailure)))
fmt.Fprintf(w, " └ %s\n", cluster.Status)

fmt.Fprint(w, " └ remote configuration: ")
if cluster.Config != nil {
fmt.Fprintf(w, "expected=%t, retrieved=%t", cluster.Config.Required, cluster.Config.Retrieved)
if cluster.Config.Retrieved {
fmt.Fprintf(w, ", cluster-id=%d, kvstoremesh=%t, sync-canaries=%t",
cluster.Config.ClusterID, cluster.Config.Kvstoremesh, cluster.Config.SyncCanaries)
}
} else {
fmt.Fprint(w, "expected=unknown, retrieved=unknown")
}
fmt.Fprint(w, "\n")
NumReadyClusters(sr.ClusterMesh.Clusters), len(sr.ClusterMesh.Clusters), sr.ClusterMesh.NumGlobalServices)

if cluster.Synced != nil {
fmt.Fprintf(w, " └ synchronization status: nodes=%v, endpoints=%v, identities=%v, services=%v\n",
cluster.Synced.Nodes, cluster.Synced.Endpoints, cluster.Synced.Identities, cluster.Synced.Services)
}
}
}
FormatStatusResponseRemoteClusters(w, sr.ClusterMesh.Clusters, sd.AllClusters)
}

if sr.IPV4BigTCP != nil {
Expand Down Expand Up @@ -808,3 +783,32 @@ func FormatStatusResponse(w io.Writer, sr *models.StatusResponse, sd StatusDetai
fmt.Fprintf(w, "Encryption:\t%s\t%s\n", sr.Encryption.Mode, strings.Join(fields, ", "))
}
}

func FormatStatusResponseRemoteClusters(w io.Writer, clusters []*models.RemoteCluster, verbose bool) {
for _, cluster := range clusters {
if verbose || !cluster.Ready {
fmt.Fprintf(w, " %s: %s, %d nodes, %d endpoints, %d identities, %d services, %d failures (last: %s)\n",
cluster.Name, clusterReadiness(cluster), cluster.NumNodes,
cluster.NumEndpoints, cluster.NumIdentities, cluster.NumSharedServices,
cluster.NumFailures, timeSince(time.Time(cluster.LastFailure)))
fmt.Fprintf(w, " └ %s\n", cluster.Status)

fmt.Fprint(w, " └ remote configuration: ")
if cluster.Config != nil {
fmt.Fprintf(w, "expected=%t, retrieved=%t", cluster.Config.Required, cluster.Config.Retrieved)
if cluster.Config.Retrieved {
fmt.Fprintf(w, ", cluster-id=%d, kvstoremesh=%t, sync-canaries=%t",
cluster.Config.ClusterID, cluster.Config.Kvstoremesh, cluster.Config.SyncCanaries)
}
} else {
fmt.Fprint(w, "expected=unknown, retrieved=unknown")
}
fmt.Fprint(w, "\n")

if cluster.Synced != nil {
fmt.Fprintf(w, " └ synchronization status: nodes=%v, endpoints=%v, identities=%v, services=%v\n",
cluster.Synced.Nodes, cluster.Synced.Endpoints, cluster.Synced.Identities, cluster.Synced.Services)
}
}
}
}
8 changes: 4 additions & 4 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (cs *ClientTestSuite) TestClusterReadiness(c *C) {
}

func (cs *ClientTestSuite) TestNumReadyClusters(c *C) {
c.Assert(numReadyClusters(&models.ClusterMeshStatus{}), Equals, 0)
c.Assert(numReadyClusters(&models.ClusterMeshStatus{
Clusters: []*models.RemoteCluster{{Ready: true}, {Ready: true}, {Ready: false}},
}), Equals, 2)
c.Assert(NumReadyClusters(nil), Equals, 0)
c.Assert(NumReadyClusters(
[]*models.RemoteCluster{{Ready: true}, {Ready: true}, {Ready: false}},
), Equals, 2)
}

0 comments on commit e670ca6

Please sign in to comment.