istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/multicluster/options.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 multicluster 16 17 import ( 18 "k8s.io/client-go/tools/clientcmd" 19 20 "istio.io/istio/istioctl/pkg/cli" 21 ) 22 23 const ( 24 clusterNameAnnotationKey = "networking.istio.io/cluster" 25 ) 26 27 // KubeOptions contains kubernetes options common to all commands. 28 type KubeOptions struct { 29 Kubeconfig string 30 Context string 31 Namespace string 32 } 33 34 // Inherit the common kubernetes flags defined in the root package. This is a bit of a hack, 35 // but it allows us to directly get the final values for each of these flags without needing 36 // to pass pointers-to-flags through all of the (sub)commands. 37 func (o *KubeOptions) prepare(ctx cli.Context) { 38 o.Namespace = ctx.Namespace() 39 if o.Namespace == "" { 40 o.Namespace = ctx.IstioNamespace() 41 42 configAccess := clientcmd.NewDefaultPathOptions() 43 configAccess.GlobalFile = o.Kubeconfig 44 if config, err := configAccess.GetStartingConfig(); err == nil { 45 if context, ok := config.Contexts[config.CurrentContext]; ok && context.Namespace != "" { 46 o.Namespace = context.Namespace 47 } 48 } 49 } 50 }