istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/serviceregistry/kube/controller/ambient/meshconfig.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  // nolint: gocritic
    16  package ambient
    17  
    18  import (
    19  	"google.golang.org/protobuf/proto"
    20  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/types"
    22  
    23  	meshapi "istio.io/api/mesh/v1alpha1"
    24  	"istio.io/istio/pilot/pkg/features"
    25  	"istio.io/istio/pkg/config/mesh"
    26  	"istio.io/istio/pkg/kube/krt"
    27  	"istio.io/istio/pkg/log"
    28  )
    29  
    30  type MeshConfig struct {
    31  	*meshapi.MeshConfig
    32  }
    33  
    34  func (m MeshConfig) ResourceName() string { return " " }
    35  
    36  func (m MeshConfig) Equals(other MeshConfig) bool { return proto.Equal(m.MeshConfig, other.MeshConfig) }
    37  
    38  func MeshConfigCollection(ConfigMaps krt.Collection[*v1.ConfigMap], options Options) krt.Singleton[MeshConfig] {
    39  	cmName := "istio"
    40  	if options.Revision != "" && options.Revision != "default" {
    41  		cmName = cmName + "-" + options.Revision
    42  	}
    43  	return krt.NewSingleton[MeshConfig](
    44  		func(ctx krt.HandlerContext) *MeshConfig {
    45  			meshCfg := mesh.DefaultMeshConfig()
    46  			cms := []*v1.ConfigMap{}
    47  			if features.SharedMeshConfig != "" {
    48  				cms = AppendNonNil(cms, krt.FetchOne(ctx, ConfigMaps,
    49  					krt.FilterObjectName(types.NamespacedName{Name: features.SharedMeshConfig, Namespace: options.SystemNamespace})))
    50  			}
    51  			cms = AppendNonNil(cms, krt.FetchOne(ctx, ConfigMaps,
    52  				krt.FilterObjectName(types.NamespacedName{Name: cmName, Namespace: options.SystemNamespace})))
    53  
    54  			for _, c := range cms {
    55  				n, err := mesh.ApplyMeshConfig(meshConfigMapData(c), meshCfg)
    56  				if err != nil {
    57  					log.Error(err)
    58  					continue
    59  				}
    60  				meshCfg = n
    61  			}
    62  			return &MeshConfig{meshCfg}
    63  		}, krt.WithName("MeshConfig"),
    64  	)
    65  }
    66  
    67  func meshConfigMapData(cm *v1.ConfigMap) string {
    68  	if cm == nil {
    69  		return ""
    70  	}
    71  
    72  	cfgYaml, exists := cm.Data["mesh"]
    73  	if !exists {
    74  		return ""
    75  	}
    76  
    77  	return cfgYaml
    78  }