istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/bootstrap/mesh.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 bootstrap 16 17 import ( 18 "encoding/json" 19 "os" 20 21 "istio.io/istio/pilot/pkg/features" 22 "istio.io/istio/pkg/config/mesh" 23 "istio.io/istio/pkg/config/mesh/kubemesh" 24 "istio.io/istio/pkg/filewatcher" 25 "istio.io/istio/pkg/log" 26 "istio.io/istio/pkg/version" 27 ) 28 29 const ( 30 // defaultMeshConfigMapName is the default name of the ConfigMap with the mesh config 31 // The actual name can be different - use getMeshConfigMapName 32 defaultMeshConfigMapName = "istio" 33 // configMapKey should match the expected MeshConfig file name 34 configMapKey = "mesh" 35 ) 36 37 // initMeshConfiguration creates the mesh in the pilotConfig from the input arguments. 38 // Original/default behavior: 39 // - use the mounted file, if it exists. 40 // - use istio-REVISION if k8s is enabled 41 // - fallback to default 42 // 43 // If the 'SHARED_MESH_CONFIG' env is set (experimental feature in 1.10): 44 // - if a file exist, load it - will be merged 45 // - if istio-REVISION exists, will be used, even if the file is present. 46 // - the SHARED_MESH_CONFIG config map will also be loaded and merged. 47 func (s *Server) initMeshConfiguration(args *PilotArgs, fileWatcher filewatcher.FileWatcher) { 48 log.Infof("initializing mesh configuration %v", args.MeshConfigFile) 49 defer func() { 50 if s.environment.Watcher != nil { 51 log.Infof("mesh configuration: %s", mesh.PrettyFormatOfMeshConfig(s.environment.Mesh())) 52 log.Infof("version: %s", version.Info.String()) 53 argsdump, _ := json.MarshalIndent(args, "", " ") 54 log.Infof("flags: %s", argsdump) 55 } 56 }() 57 58 // Watcher will be merging more than one mesh config source? 59 multiWatch := features.SharedMeshConfig != "" 60 61 var err error 62 if _, err = os.Stat(args.MeshConfigFile); !os.IsNotExist(err) { 63 s.environment.Watcher, err = mesh.NewFileWatcher(fileWatcher, args.MeshConfigFile, multiWatch) 64 if err == nil { 65 if multiWatch && s.kubeClient != nil { 66 kubemesh.AddUserMeshConfig( 67 s.kubeClient, s.environment.Watcher, args.Namespace, configMapKey, features.SharedMeshConfig, s.internalStop) 68 } else { 69 // Normal install no longer uses this mode - testing and special installs still use this. 70 log.Warnf("Using local mesh config file %s, in cluster configs ignored", args.MeshConfigFile) 71 } 72 return 73 } 74 } 75 76 // Config file either didn't exist or failed to load. 77 if s.kubeClient == nil { 78 // Use a default mesh. 79 meshConfig := mesh.DefaultMeshConfig() 80 s.environment.Watcher = mesh.NewFixedWatcher(meshConfig) 81 log.Warnf("Using default mesh - missing file %s and no k8s client", args.MeshConfigFile) 82 return 83 } 84 85 // Watch the istio ConfigMap for mesh config changes. 86 // This may be necessary for external Istiod. 87 configMapName := getMeshConfigMapName(args.Revision) 88 multiWatcher := kubemesh.NewConfigMapWatcher( 89 s.kubeClient, args.Namespace, configMapName, configMapKey, multiWatch, s.internalStop) 90 s.environment.Watcher = multiWatcher 91 s.environment.NetworksWatcher = multiWatcher 92 log.Infof("initializing mesh networks from mesh config watcher") 93 94 if multiWatch { 95 kubemesh.AddUserMeshConfig(s.kubeClient, s.environment.Watcher, args.Namespace, configMapKey, features.SharedMeshConfig, s.internalStop) 96 } 97 } 98 99 // initMeshNetworks loads the mesh networks configuration from the file provided 100 // in the args and add a watcher for changes in this file. 101 func (s *Server) initMeshNetworks(args *PilotArgs, fileWatcher filewatcher.FileWatcher) { 102 if s.environment.NetworksWatcher != nil { 103 return 104 } 105 log.Info("initializing mesh networks") 106 if args.NetworksConfigFile != "" { 107 var err error 108 s.environment.NetworksWatcher, err = mesh.NewNetworksWatcher(fileWatcher, args.NetworksConfigFile) 109 if err != nil { 110 log.Info(err) 111 } 112 } 113 114 if s.environment.NetworksWatcher == nil { 115 log.Info("mesh networks configuration not provided") 116 s.environment.NetworksWatcher = mesh.NewFixedNetworksWatcher(nil) 117 } 118 } 119 120 func getMeshConfigMapName(revision string) string { 121 name := defaultMeshConfigMapName 122 if revision == "" || revision == "default" { 123 return name 124 } 125 return name + "-" + revision 126 }