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