istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/cli/option.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cli
    16  
    17  import (
    18  	"github.com/spf13/pflag"
    19  	"github.com/spf13/viper"
    20  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/client-go/tools/clientcmd"
    22  
    23  	"istio.io/istio/pkg/ptr"
    24  )
    25  
    26  const (
    27  	FlagKubeConfig     = "kubeconfig"
    28  	FlagContext        = "context"
    29  	FlagNamespace      = "namespace"
    30  	FlagIstioNamespace = "istioNamespace"
    31  )
    32  
    33  type RootFlags struct {
    34  	kubeconfig     *string
    35  	configContext  *string
    36  	namespace      *string
    37  	istioNamespace *string
    38  
    39  	defaultNamespace string
    40  }
    41  
    42  func AddRootFlags(flags *pflag.FlagSet) *RootFlags {
    43  	r := &RootFlags{
    44  		kubeconfig:     ptr.Of[string](""),
    45  		configContext:  ptr.Of[string](""),
    46  		namespace:      ptr.Of[string](""),
    47  		istioNamespace: ptr.Of[string](""),
    48  	}
    49  	flags.StringVarP(r.kubeconfig, FlagKubeConfig, "c", "",
    50  		"Kubernetes configuration file")
    51  	flags.StringVar(r.configContext, FlagContext, "",
    52  		"Kubernetes configuration context")
    53  	flags.StringVarP(r.namespace, FlagNamespace, "n", v1.NamespaceAll,
    54  		"Kubernetes namespace")
    55  	flags.StringVarP(r.istioNamespace, FlagIstioNamespace, "i", viper.GetString(FlagIstioNamespace),
    56  		"Istio system namespace")
    57  	return r
    58  }
    59  
    60  // Namespace returns the namespace flag value.
    61  func (r *RootFlags) Namespace() string {
    62  	return *r.namespace
    63  }
    64  
    65  // IstioNamespace returns the istioNamespace flag value.
    66  func (r *RootFlags) IstioNamespace() string {
    67  	return *r.istioNamespace
    68  }
    69  
    70  // DefaultNamespace returns the default namespace to use.
    71  func (r *RootFlags) DefaultNamespace() string {
    72  	if r.defaultNamespace == "" {
    73  		r.configureDefaultNamespace()
    74  	}
    75  	return r.defaultNamespace
    76  }
    77  
    78  func (r *RootFlags) configureDefaultNamespace() {
    79  	configAccess := clientcmd.NewDefaultPathOptions()
    80  
    81  	kubeconfig := *r.kubeconfig
    82  	if kubeconfig != "" {
    83  		// use specified kubeconfig file for the location of the
    84  		// config to read
    85  		configAccess.GlobalFile = kubeconfig
    86  	}
    87  
    88  	// gets existing kubeconfig or returns new empty config
    89  	config, err := configAccess.GetStartingConfig()
    90  	if err != nil {
    91  		r.defaultNamespace = v1.NamespaceDefault
    92  		return
    93  	}
    94  
    95  	// If a specific context was specified, use that. Otherwise, just use the current context from the kube config.
    96  	selectedContext := config.CurrentContext
    97  	if *r.configContext != "" {
    98  		selectedContext = *r.configContext
    99  	}
   100  
   101  	// Use the namespace associated with the selected context as default, if the context has one
   102  	context, ok := config.Contexts[selectedContext]
   103  	if !ok {
   104  		r.defaultNamespace = v1.NamespaceDefault
   105  		return
   106  	}
   107  	if context.Namespace == "" {
   108  		r.defaultNamespace = v1.NamespaceDefault
   109  		return
   110  	}
   111  	r.defaultNamespace = context.Namespace
   112  }