github.com/verrazzano/verrazzano@v1.7.0/tools/vz/cmd/helpers/cmd_context.go (about) 1 // Copyright (c) 2022, 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package helpers 5 6 import ( 7 "fmt" 8 "io" 9 "k8s.io/client-go/discovery" 10 "net/http" 11 12 "k8s.io/client-go/dynamic" 13 "k8s.io/client-go/rest" 14 15 "github.com/spf13/cobra" 16 "github.com/verrazzano/verrazzano/pkg/k8sutil" 17 "github.com/verrazzano/verrazzano/tools/vz/pkg/constants" 18 "github.com/verrazzano/verrazzano/tools/vz/pkg/helpers" 19 "k8s.io/cli-runtime/pkg/genericclioptions" 20 "k8s.io/client-go/kubernetes" 21 "sigs.k8s.io/controller-runtime/pkg/client" 22 ) 23 24 type RootCmdContext struct { 25 genericclioptions.IOStreams 26 } 27 28 // GetOutputStream - return the output stream 29 func (rc *RootCmdContext) GetOutputStream() io.Writer { 30 return rc.IOStreams.Out 31 } 32 33 // GetErrorStream - return the error stream 34 func (rc *RootCmdContext) GetErrorStream() io.Writer { 35 return rc.IOStreams.ErrOut 36 } 37 38 // GetInputStream - return the input stream 39 func (rc *RootCmdContext) GetInputStream() io.Reader { 40 return rc.IOStreams.In 41 } 42 43 // GetClient - return a Kubernetes controller runtime client that supports the schemes used by the CLI 44 func (rc *RootCmdContext) GetClient(cmd *cobra.Command) (client.Client, error) { 45 config, err := getKubeConfigGivenCommand(cmd) 46 if err != nil { 47 return nil, err 48 } 49 // Set warning handler to output warnings to the error stream 50 config.WarningHandler = getWarningHandler(rc.GetErrorStream()) 51 return client.New(config, client.Options{Scheme: helpers.NewScheme()}) 52 } 53 54 // GetKubeClient - return a Kubernetes clientset for use with the go-client 55 func (rc *RootCmdContext) GetKubeClient(cmd *cobra.Command) (kubernetes.Interface, error) { 56 config, err := getKubeConfigGivenCommand(cmd) 57 if err != nil { 58 return nil, err 59 } 60 61 return kubernetes.NewForConfig(config) 62 } 63 64 // GetDynamicClient - return a dynamic clientset for use with the go-client 65 func (rc *RootCmdContext) GetDynamicClient(cmd *cobra.Command) (dynamic.Interface, error) { 66 config, err := getKubeConfigGivenCommand(cmd) 67 if err != nil { 68 return nil, err 69 } 70 return dynamic.NewForConfig(config) 71 } 72 73 func getKubeConfigGivenCommand(cmd *cobra.Command) (*rest.Config, error) { 74 // Get command line value of --kubeconfig 75 kubeConfigLoc, err := cmd.Flags().GetString(constants.GlobalFlagKubeConfig) 76 if err != nil { 77 return nil, err 78 } 79 80 // Get command line value of --context 81 context, err := cmd.Flags().GetString(constants.GlobalFlagContext) 82 if err != nil { 83 return nil, err 84 } 85 86 config, err := k8sutil.GetKubeConfigGivenPathAndContext(kubeConfigLoc, context) 87 if err != nil { 88 return nil, err 89 } 90 return config, err 91 } 92 93 // GetHTTPClient - return an HTTP client 94 func (rc *RootCmdContext) GetHTTPClient() *http.Client { 95 return &http.Client{} 96 } 97 98 // NewRootCmdContext - create the root command context object 99 func NewRootCmdContext(streams genericclioptions.IOStreams) *RootCmdContext { 100 return &RootCmdContext{ 101 IOStreams: streams, 102 } 103 } 104 105 func (rc *RootCmdContext) GetDiscoveryClient(cmd *cobra.Command) (discovery.DiscoveryInterface, error) { 106 client, _ := rc.GetKubeClient(cmd) 107 discoveryClient, ok := client.Discovery().(*discovery.DiscoveryClient) 108 if !ok { 109 return nil, fmt.Errorf("DiscoveryClient was not successfully created") 110 } 111 return discoveryClient, nil 112 } 113 114 // VerifyCLIArgsNil checks that command args are not set at the creation of the command 115 func (rc *RootCmdContext) VerifyCLIArgsNil(cmd *cobra.Command) error { 116 cmd.Args = func(cmd *cobra.Command, args []string) error { 117 if len(args) != 0 { 118 return fmt.Errorf("invalid arguments specified: %s", args) 119 } 120 return nil 121 } 122 return nil 123 }