istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/bootstrap/servicecontroller.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  	"fmt"
    19  
    20  	"istio.io/istio/pilot/pkg/serviceregistry/aggregate"
    21  	kubecontroller "istio.io/istio/pilot/pkg/serviceregistry/kube/controller"
    22  	"istio.io/istio/pilot/pkg/serviceregistry/provider"
    23  	"istio.io/istio/pilot/pkg/serviceregistry/serviceentry"
    24  	"istio.io/istio/pkg/log"
    25  	"istio.io/istio/pkg/util/sets"
    26  )
    27  
    28  func (s *Server) ServiceController() *aggregate.Controller {
    29  	return s.environment.ServiceDiscovery.(*aggregate.Controller)
    30  }
    31  
    32  // initServiceControllers creates and initializes the service controllers
    33  func (s *Server) initServiceControllers(args *PilotArgs) error {
    34  	serviceControllers := s.ServiceController()
    35  
    36  	s.serviceEntryController = serviceentry.NewController(
    37  		s.configController, s.XDSServer,
    38  		serviceentry.WithClusterID(s.clusterID),
    39  	)
    40  	serviceControllers.AddRegistry(s.serviceEntryController)
    41  
    42  	registered := sets.New[provider.ID]()
    43  	for _, r := range args.RegistryOptions.Registries {
    44  		serviceRegistry := provider.ID(r)
    45  		if registered.Contains(serviceRegistry) {
    46  			log.Warnf("%s registry specified multiple times.", r)
    47  			continue
    48  		}
    49  		registered.Insert(serviceRegistry)
    50  		log.Infof("Adding %s registry adapter", serviceRegistry)
    51  		switch serviceRegistry {
    52  		case provider.Kubernetes:
    53  			if err := s.initKubeRegistry(args); err != nil {
    54  				return err
    55  			}
    56  		default:
    57  			return fmt.Errorf("service registry %s is not supported", r)
    58  		}
    59  	}
    60  
    61  	// Defer running of the service controllers.
    62  	s.addStartFunc("service controllers", func(stop <-chan struct{}) error {
    63  		go serviceControllers.Run(stop)
    64  		return nil
    65  	})
    66  
    67  	return nil
    68  }
    69  
    70  // initKubeRegistry creates all the k8s service controllers under this pilot
    71  func (s *Server) initKubeRegistry(args *PilotArgs) (err error) {
    72  	args.RegistryOptions.KubeOptions.ClusterID = s.clusterID
    73  	args.RegistryOptions.KubeOptions.Revision = args.Revision
    74  	args.RegistryOptions.KubeOptions.Metrics = s.environment
    75  	args.RegistryOptions.KubeOptions.XDSUpdater = s.XDSServer
    76  	args.RegistryOptions.KubeOptions.MeshNetworksWatcher = s.environment.NetworksWatcher
    77  	args.RegistryOptions.KubeOptions.MeshWatcher = s.environment.Watcher
    78  	args.RegistryOptions.KubeOptions.SystemNamespace = args.Namespace
    79  	args.RegistryOptions.KubeOptions.MeshServiceController = s.ServiceController()
    80  	// pass namespace to k8s service registry
    81  	kubecontroller.NewMulticluster(args.PodName,
    82  		s.kubeClient.Kube(),
    83  		args.RegistryOptions.ClusterRegistriesNamespace,
    84  		args.RegistryOptions.KubeOptions,
    85  		s.serviceEntryController,
    86  		s.configController,
    87  		s.istiodCertBundleWatcher,
    88  		args.Revision,
    89  		s.shouldStartNsController(),
    90  		s.environment.ClusterLocal(),
    91  		s.server,
    92  		s.multiclusterController)
    93  
    94  	return
    95  }