Skip to content

Commit

Permalink
adds namespaces overview to fleet command
Browse files Browse the repository at this point in the history
  • Loading branch information
mhausenblas committed Nov 28, 2019
1 parent 3b48515 commit 476ef0f
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions pkg/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,47 @@ package fleet

import (
"fmt"
"os"
"text/tabwriter"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"os"
"text/tabwriter"
)

// RunFleetCommand runs the top-level fleet command
// RunFleetCommand executes the top-level fleet command
func RunFleetCommand(configFlags *genericclioptions.ConfigFlags) error {
clientcfg := configFlags.ToRawKubeConfigLoader()
cfg, err := clientcfg.RawConfig()
if err != nil {
return errors.Wrap(err, "Can't assemble raw config")
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintln(w, "CLUSTER\tNODES\tAPI")
fmt.Fprintln(w, "CLUSTER\tNODES\tNAMESPACES\tAPI")
for name, context := range cfg.Contexts {
cluster := cfg.Clusters[context.Cluster]
apiServerEndpoint := "?"
if cluster != nil {
apiServerEndpoint = cluster.Server
}
ninfo, err := nodesOverview(cfg, name)
noinfo, err := nodesOverview(cfg, name)
if err != nil {
noinfo = "?"
}
nsinfo, err := nsOverview(cfg, name)
if err != nil {
ninfo = "?"
nsinfo = "?"
}
fmt.Fprintln(w, fmt.Sprintf("%v\t%v\t%v", context.Cluster, ninfo, apiServerEndpoint))
fmt.Fprintln(w, fmt.Sprintf("%v\t%v\t%v\t%v", context.Cluster, noinfo, nsinfo, apiServerEndpoint))
}
w.Flush()
return nil
}

// nodesOverview provides an overview of the cluster's worker nodes in the context
// nodesOverview returns the cluster's worker nodes overview in given context
func nodesOverview(cfg api.Config, context string) (string, error) {
cs, err := csForContext(cfg, context)
if err != nil {
Expand All @@ -51,6 +56,20 @@ func nodesOverview(cfg api.Config, context string) (string, error) {
return noverview, nil
}

// nsOverview returns the cluster's namespaces overview in given context
func nsOverview(cfg api.Config, context string) (string, error) {
cs, err := csForContext(cfg, context)
if err != nil {
return "", errors.Wrap(err, "Can't create a clientset based on config provided")
}
ns, err := cs.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return "", errors.Wrap(err, "Can't get nodes in cluster")
}
nsverview := fmt.Sprintf("%v", len(ns.Items))
return nsverview, nil
}

// csForContext returns a client for a given context
func csForContext(cfg api.Config, context string) (*kubernetes.Clientset, error) {
config, err := clientcmd.NewNonInteractiveClientConfig(
Expand Down

0 comments on commit 476ef0f

Please sign in to comment.