istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/config/config.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 config 16 17 import ( 18 "fmt" 19 "io" 20 "sort" 21 "text/tabwriter" 22 23 "github.com/spf13/cobra" 24 "github.com/spf13/viper" 25 26 "istio.io/istio/istioctl/pkg/root" 27 "istio.io/istio/pkg/config/constants" 28 "istio.io/istio/pkg/env" 29 ) 30 31 // settableFlags are the flags used to istioctl 32 var settableFlags = map[string]env.VariableInfo{ 33 "istioNamespace": env.Register("ISTIOCTL_ISTIONAMESPACE", constants.IstioSystemNamespace, "The istioctl --istioNamespace override"), 34 "xds-address": env.Register("ISTIOCTL_XDS_ADDRESS", "", "The istioctl --xds-address override"), 35 "xds-port": env.Register("ISTIOCTL_XDS_PORT", 15012, "The istioctl --xds-port override"), 36 "authority": env.Register("ISTIOCTL_AUTHORITY", "", "The istioctl --authority override"), 37 "cert-dir": env.Register("ISTIOCTL_CERT_DIR", "", "The istioctl --cert-dir override"), 38 "insecure": env.Register("ISTIOCTL_INSECURE", false, "The istioctl --insecure override"), 39 "prefer-experimental": env.Register("ISTIOCTL_PREFER_EXPERIMENTAL", false, "The istioctl should use experimental subcommand variants"), 40 "plaintext": env.Register("ISTIOCTL_PLAINTEXT", false, "The istioctl --plaintext override"), 41 } 42 43 // Cmd represents the config subcommand command 44 func Cmd() *cobra.Command { 45 configCmd := &cobra.Command{ 46 Use: "config SUBCOMMAND", 47 Short: "Configure istioctl defaults", 48 Args: cobra.NoArgs, 49 Example: ` # list configuration parameters 50 istioctl experimental config list`, 51 } 52 configCmd.AddCommand(listCommand()) 53 return configCmd 54 } 55 56 func listCommand() *cobra.Command { 57 listCmd := &cobra.Command{ 58 Use: "list", 59 Short: "List istio configurable defaults", 60 Args: cobra.ExactArgs(0), 61 RunE: func(c *cobra.Command, _ []string) error { 62 root.Scope.Debugf("Config file %q", root.IstioConfig) 63 return runList(c.OutOrStdout()) 64 }, 65 } 66 return listCmd 67 } 68 69 func runList(writer io.Writer) error { 70 // Sort flag names 71 keys := make([]string, len(settableFlags)) 72 i := 0 73 for key := range settableFlags { 74 keys[i] = key 75 i++ 76 } 77 sort.Strings(keys) 78 w := new(tabwriter.Writer).Init(writer, 0, 8, 5, ' ', 0) 79 fmt.Fprintf(w, "FLAG\tVALUE\tFROM\n") 80 for _, flag := range keys { 81 v := settableFlags[flag] 82 fmt.Fprintf(w, "%s\t%s\t%v\n", flag, viper.GetString(flag), configSource(flag, v)) 83 } 84 return w.Flush() 85 } 86 87 func configSource(flag string, v env.VariableInfo) string { 88 // Environment variables have high precedence in Viper 89 if v.IsSet() { 90 return "$" + v.GetName() 91 } 92 93 if viper.InConfig(flag) { 94 return root.IstioConfig 95 } 96 97 return "default" 98 }