generated from replicatedhq/krew-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
splits out resources into separate command
- Loading branch information
1 parent
6931f20
commit 08c3a44
Showing
6 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |