Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed May 5, 2022
1 parent e123452 commit 8101eed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// Cluster represents configuration of a kubernetes cluster.
// Cluster represents configuration of a Kubernetes cluster.
type Cluster struct {
restConfig *rest.Config
cluster, namespace string
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Kcount counts Kubernetes objects across clusters.
package main

import (
Expand Down Expand Up @@ -28,10 +29,10 @@ func main() {
for {
counts := CountObjectsAcrossClusters(clusters, flags)
for _, count := range counts {
objectsCount.WithLabelValues(count.cluster, count.namespace, count.labelSelector, count.kind).Set(float64(count.count))
objectsCount.WithLabelValues(count.Cluster, count.Namespace, count.LabelSelector, count.Kind).Set(float64(count.Count))
if flags.age {
objectsNewest.WithLabelValues(count.cluster, count.namespace, count.labelSelector, count.kind).Set(float64(count.newest.Unix()))
objectsOldest.WithLabelValues(count.cluster, count.namespace, count.labelSelector, count.kind).Set(float64(count.oldest.Unix()))
objectsNewest.WithLabelValues(count.Cluster, count.Namespace, count.LabelSelector, count.Kind).Set(float64(count.Newest.Unix()))
objectsOldest.WithLabelValues(count.Cluster, count.Namespace, count.LabelSelector, count.Kind).Set(float64(count.Oldest.Unix()))
}
}
time.Sleep(2 * time.Second)
Expand Down
60 changes: 31 additions & 29 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import (

const timeout = 5 // cluster API call timeout in seconds

// Count represents count and age of Kubernetes objects. The objects are of
// given kind, in given cluster and namespace and matching given label selector.
// Count represents count and age of Kubernetes objects. The objects are inside
// a given cluster and namespace, of given kind and matching given label
// selector.
type Count struct {
cluster string
namespace string
kind string
labelSelector string
count int
newest objectTime
oldest objectTime
Cluster string
Namespace string
Kind string
LabelSelector string
Count int
Newest objectTime
Oldest objectTime
}

// CountObjectsAcrossClusters counts objects across all clusters concurrently.
Expand Down Expand Up @@ -85,13 +86,13 @@ func countObjects(cluster Cluster, kind, labelSelector string) (Count, error) {
}

return Count{
cluster: cluster.cluster,
namespace: cluster.namespace,
kind: kind,
labelSelector: labelSelector,
count: n,
newest: objectTime(newest),
oldest: objectTime(oldest),
Cluster: cluster.cluster,
Namespace: cluster.namespace,
Kind: kind,
LabelSelector: labelSelector,
Count: n,
Newest: objectTime(newest),
Oldest: objectTime(oldest),
}, nil
}

Expand All @@ -101,23 +102,24 @@ type Counts []Count
// name.
func (c Counts) Sort() {
sort.Slice(c, func(i, j int) bool {
if c[i].count != c[j].count {
return c[i].count > c[j].count
if c[i].Count != c[j].Count {
return c[i].Count > c[j].Count
}
if c[i].kind != c[j].kind {
return c[i].kind < c[j].kind
if c[i].Kind != c[j].Kind {
return c[i].Kind < c[j].Kind
}
if c[i].cluster != c[j].cluster {
return c[i].cluster < c[j].cluster
if c[i].Cluster != c[j].Cluster {
return c[i].Cluster < c[j].Cluster
}
if c[i].namespace != c[j].namespace {
return c[i].namespace < c[j].namespace
if c[i].Namespace != c[j].Namespace {
return c[i].Namespace < c[j].Namespace
}
return false
})
}

// Print prints a table with Kubernetes objects.
// Print prints a table with Kubernetes objects. The table can optionally
// contain the age of the objects.
func (c Counts) Print(age bool) {
if len(c) == 0 {
return
Expand All @@ -131,8 +133,8 @@ func (c Counts) Print(age bool) {
fmt.Fprintf(tw, format, "Cluster", "Namespace", "Label selector", "Kind", "Count", "Newest", "Oldest")
fmt.Fprintf(tw, format, "-------", "---------", "--------------", "----", "-----", "------", "------")
for _, o := range c {
total += o.count
fmt.Fprintf(tw, format, o.cluster, o.namespace, o.labelSelector, o.kind, o.count, o.newest, o.oldest)
total += o.Count
fmt.Fprintf(tw, format, o.Cluster, o.Namespace, o.LabelSelector, o.Kind, o.Count, o.Newest, o.Oldest)
}
fmt.Fprintf(tw, format, "", "", "", "", "-----", "", "")
fmt.Fprintf(tw, format, "", "", "", "", total, "", "")
Expand All @@ -141,8 +143,8 @@ func (c Counts) Print(age bool) {
fmt.Fprintf(tw, format, "Cluster", "Namespace", "Label selector", "Kind", "Count")
fmt.Fprintf(tw, format, "-------", "---------", "--------------", "----", "-----")
for _, o := range c {
total += o.count
fmt.Fprintf(tw, format, o.cluster, o.namespace, o.labelSelector, o.kind, o.count)
total += o.Count
fmt.Fprintf(tw, format, o.Cluster, o.Namespace, o.LabelSelector, o.Kind, o.Count)
}
fmt.Fprintf(tw, format, "", "", "", "", "-----")
fmt.Fprintf(tw, format, "", "", "", "", total)
Expand Down

0 comments on commit 8101eed

Please sign in to comment.