Skip to content

Commit

Permalink
better naming still + methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed May 5, 2022
1 parent 07a3099 commit e123452
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
1 change: 1 addition & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// Flags are command line flags.
type Flags struct {
allNamespaces bool
age bool
Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func main() {
if flags.daemon {
go func() {
for {
objects := CountObjectsAcrossClusters(clusters, flags)
for _, obj := range objects {
objectsCount.WithLabelValues(obj.cluster, obj.namespace, obj.labelSelector, obj.kind).Set(float64(obj.count))
counts := CountObjectsAcrossClusters(clusters, flags)
for _, count := range counts {
objectsCount.WithLabelValues(count.cluster, count.namespace, count.labelSelector, count.kind).Set(float64(count.count))
if flags.age {
objectsNewest.WithLabelValues(obj.cluster, obj.namespace, obj.labelSelector, obj.kind).Set(float64(obj.newest.Unix()))
objectsOldest.WithLabelValues(obj.cluster, obj.namespace, obj.labelSelector, obj.kind).Set(float64(obj.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 All @@ -41,8 +41,8 @@ func main() {
log.Printf("exposing Prometheus metrics at %s%s", addr, urlPath)
log.Fatal(exposeMetrics(addr, urlPath))
} else { // running as CLI app
objects := CountObjectsAcrossClusters(clusters, flags)
SortObjects(objects)
PrintObjects(objects, flags.age)
counts := CountObjectsAcrossClusters(clusters, flags)
counts.Sort()
counts.Print(flags.age)
}
}
82 changes: 35 additions & 47 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"k8s.io/client-go/kubernetes"
)

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.
type Count struct {
Expand All @@ -26,17 +28,15 @@ type Count struct {
oldest objectTime
}

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

// CountObjectsAcrossClusters counts objects across all clusters concurrently.
func CountObjectsAcrossClusters(clusters []Cluster, flags Flags) []Count {
var objects []Count
func CountObjectsAcrossClusters(clusters []Cluster, flags Flags) Counts {
var counts []Count
ch := make(chan Count)

for _, cluster := range clusters {
for _, kind := range flags.kind {
go func(cluster Cluster, kind string) {
obj, err := CountObjects(cluster, kind, flags.labelSelector)
obj, err := countObjects(cluster, kind, flags.labelSelector)
if err != nil {
log.Printf("counting objects in cluster %s: %v", cluster.cluster, err)
}
Expand All @@ -49,16 +49,16 @@ func CountObjectsAcrossClusters(clusters []Cluster, flags Flags) []Count {
for range flags.kind {
obj := <-ch
if obj != (Count{}) { // check obj is not "empty"
objects = append(objects, obj)
counts = append(counts, obj)
}
}
}

return objects
return counts
}

// CountObjects counts objects of kind within a cluster.
func CountObjects(cluster Cluster, kind, labelSelector string) (Count, error) {
// countObjects counts objects of kind within a cluster.
func countObjects(cluster Cluster, kind, labelSelector string) (Count, error) {
clientSet, err := kubernetes.NewForConfig(cluster.restConfig)
if err != nil {
return Count{}, fmt.Errorf("generating clientSet: %v", err)
Expand Down Expand Up @@ -95,23 +95,31 @@ func CountObjects(cluster Cluster, kind, labelSelector string) (Count, error) {
}, nil
}

// func countsEqual(objects []Object) bool {
// var countFirst int
// for i, o := range objects {
// if i == 0 {
// countFirst = o.count
// continue
// }
// if countFirst != o.count {
// return false
// }
// }
// return true
// }
type Counts []Count

// Sort sorts objects by count and then by cluster name and namespace
// 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].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].namespace != c[j].namespace {
return c[i].namespace < c[j].namespace
}
return false
})
}

// PrintObjects prints a table with Kubernetes objects.
func PrintObjects(objects []Count, age bool) {
if len(objects) == 0 {
// Print prints a table with Kubernetes objects.
func (c Counts) Print(age bool) {
if len(c) == 0 {
return
}
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
Expand All @@ -122,7 +130,7 @@ func PrintObjects(objects []Count, age bool) {
const format = "%v\t%v\t%v\t%v\t%v\t%s\t%s\n"
fmt.Fprintf(tw, format, "Cluster", "Namespace", "Label selector", "Kind", "Count", "Newest", "Oldest")
fmt.Fprintf(tw, format, "-------", "---------", "--------------", "----", "-----", "------", "------")
for _, o := range objects {
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)
}
Expand All @@ -132,7 +140,7 @@ func PrintObjects(objects []Count, age bool) {
const format = "%v\t%v\t%v\t%v\t%v\n"
fmt.Fprintf(tw, format, "Cluster", "Namespace", "Label selector", "Kind", "Count")
fmt.Fprintf(tw, format, "-------", "---------", "--------------", "----", "-----")
for _, o := range objects {
for _, o := range c {
total += o.count
fmt.Fprintf(tw, format, o.cluster, o.namespace, o.labelSelector, o.kind, o.count)
}
Expand All @@ -143,26 +151,6 @@ func PrintObjects(objects []Count, age bool) {
tw.Flush()
}

// SortObjects sorts objects by count and then by cluster name and namespace
// name.
func SortObjects(objects []Count) {
sort.Slice(objects, func(i, j int) bool {
if objects[i].count != objects[j].count {
return objects[i].count > objects[j].count
}
if objects[i].kind != objects[j].kind {
return objects[i].kind < objects[j].kind
}
if objects[i].cluster != objects[j].cluster {
return objects[i].cluster < objects[j].cluster
}
if objects[i].namespace != objects[j].namespace {
return objects[i].namespace < objects[j].namespace
}
return false
})
}

func countDeployments(clientset *kubernetes.Clientset, namespace string, labelSelector string, timeoutSeconds int64) (int, metav1.Time, metav1.Time, error) {
deployments, err := clientset.AppsV1().Deployments(namespace).List(
context.TODO(),
Expand Down

0 comments on commit e123452

Please sign in to comment.