Skip to content

Commit

Permalink
splits out resources into separate command
Browse files Browse the repository at this point in the history
  • Loading branch information
mhausenblas committed Nov 28, 2019
1 parent 6931f20 commit 08c3a44
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
25 changes: 25 additions & 0 deletions cmd/fleet/cli/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

import (
"github.com/mhausenblas/kcf/pkg/fleet"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// ResourcesCmd runs the fleet resources command
func ResourcesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "resources",
Short: "Details about the resources of a cluster in the fleet",
Long: `.`,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := fleet.Resources(KubernetesConfigFlags, args); err != nil {
return errors.Cause(err)
}
return nil
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/fleet/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func RootCmd() *cobra.Command {
func InitAndExecute() {
rootCmd := RootCmd()
rootCmd.AddCommand(DetailsCmd())
rootCmd.AddCommand(ResourcesCmd())
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
10 changes: 10 additions & 0 deletions pkg/fleet/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ func csForContext(cfg api.Config, context string) (*kubernetes.Clientset, error)
}
return cs, nil
}

// contextOf returns the context name of a given cluster
func contextOf(cfg api.Config, clusterID string) string {
for name, context := range cfg.Contexts {
if clusterID == context.Cluster {
return name
}
}
return ""
}
7 changes: 4 additions & 3 deletions pkg/fleet/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

// Details creates detailed information on a particular clusters in a fleet
// Details creates a detailed report on a particular clusters in a fleet
func Details(configFlags *genericclioptions.ConfigFlags, args []string) error {
clientcfg := configFlags.ToRawKubeConfigLoader()
cfg, err := clientcfg.RawConfig()
Expand All @@ -17,8 +17,9 @@ func Details(configFlags *genericclioptions.ConfigFlags, args []string) error {
if len(args) < 1 {
return errors.New("need a cluster to operate on, please provide the cluster name")
}
_ = cfg
clusterID := args[0]
fmt.Println(clusterID)
cluster := cfg.Clusters[clusterID]
fmt.Printf("API server endpoint: %v\n", cluster.Server)
// context := contextOf(cfg, clusterID)
return nil
}
1 change: 0 additions & 1 deletion pkg/fleet/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func Overview(configFlags *genericclioptions.ConfigFlags) error {
if cluster != nil {
apiServerEndpoint = cluster.Server
}

fmt.Fprintln(w, fmt.Sprintf("%v\t%v\t%v\t%v\t%v", context.Cluster, clusterVersion, noinfo, nsinfo, apiServerEndpoint))
}
w.Flush()
Expand Down
55 changes: 55 additions & 0 deletions pkg/fleet/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fleet

import (
"fmt"
"strings"

"github.com/pkg/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/tools/clientcmd/api"
)

// Resources creates a detailed report of the resources in
// a particular cluster in a fleet
func Resources(configFlags *genericclioptions.ConfigFlags, args []string) error {
clientcfg := configFlags.ToRawKubeConfigLoader()
cfg, err := clientcfg.RawConfig()
if err != nil {
return errors.Wrap(err, "Can't assemble raw config")
}
if len(args) < 1 {
return errors.New("need a cluster to operate on, please provide the cluster name")
}
clusterID := args[0]
context := contextOf(cfg, clusterID)
err = resourceDetails(cfg, context)
if err != nil {
return err
}
return nil
}

// resourceDetails prints the supported resources in the cluster
func resourceDetails(cfg api.Config, context string) error {
cs, err := csForContext(cfg, context)
if err != nil {
return errors.Wrap(err, "Can't create a clientset based on config provided")
}
_, reslist, err := cs.Discovery().ServerGroupsAndResources()
if err != nil {
return errors.Wrap(err, "Can't get cluster server version")
}
fmt.Println("Resources supported in this cluster:")
for _, res := range reslist {
fmt.Println(strings.Repeat("-", 80))
fmt.Printf("%v:\n ", res.GroupVersion)
for _, r := range res.APIResources {
if !strings.Contains(r.Name, "/") {
fmt.Printf("%v (namespaced: %v) ", r.Name, r.Namespaced)
}
}
fmt.Printf("\n")
}
fmt.Println(strings.Repeat("*", 80))
return nil
}

0 comments on commit 08c3a44

Please sign in to comment.