From ad1471a4a724dd109f3e717233241dc96f3c35a1 Mon Sep 17 00:00:00 2001 From: zryfish Date: Tue, 23 Jun 2020 11:48:50 +0800 Subject: [PATCH] only run virtualservice and destinationrule controller when servicemesh is enabled (#2243) --- cmd/controller-manager/app/controllers.go | 32 +++++++++++-------- cmd/controller-manager/app/options/options.go | 12 +++---- cmd/controller-manager/app/server.go | 5 ++- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/cmd/controller-manager/app/controllers.go b/cmd/controller-manager/app/controllers.go index c6026046a..0f125a554 100644 --- a/cmd/controller-manager/app/controllers.go +++ b/cmd/controller-manager/app/controllers.go @@ -63,6 +63,7 @@ func addControllers( openpitrixClient openpitrix.Client, multiClusterEnabled bool, networkPolicyEnabled bool, + serviceMeshEnabled bool, stopCh <-chan struct{}) error { kubernetesInformer := informerFactory.KubernetesSharedInformerFactory() @@ -70,21 +71,24 @@ func addControllers( kubesphereInformer := informerFactory.KubeSphereSharedInformerFactory() applicationInformer := informerFactory.ApplicationSharedInformerFactory() - vsController := virtualservice.NewVirtualServiceController(kubernetesInformer.Core().V1().Services(), - istioInformer.Networking().V1alpha3().VirtualServices(), - istioInformer.Networking().V1alpha3().DestinationRules(), - kubesphereInformer.Servicemesh().V1alpha2().Strategies(), - client.Kubernetes(), - client.Istio(), - client.KubeSphere()) + var vsController, drController manager.Runnable + if serviceMeshEnabled { + vsController = virtualservice.NewVirtualServiceController(kubernetesInformer.Core().V1().Services(), + istioInformer.Networking().V1alpha3().VirtualServices(), + istioInformer.Networking().V1alpha3().DestinationRules(), + kubesphereInformer.Servicemesh().V1alpha2().Strategies(), + client.Kubernetes(), + client.Istio(), + client.KubeSphere()) - drController := destinationrule.NewDestinationRuleController(kubernetesInformer.Apps().V1().Deployments(), - istioInformer.Networking().V1alpha3().DestinationRules(), - kubernetesInformer.Core().V1().Services(), - kubesphereInformer.Servicemesh().V1alpha2().ServicePolicies(), - client.Kubernetes(), - client.Istio(), - client.KubeSphere()) + drController = destinationrule.NewDestinationRuleController(kubernetesInformer.Apps().V1().Deployments(), + istioInformer.Networking().V1alpha3().DestinationRules(), + kubernetesInformer.Core().V1().Services(), + kubesphereInformer.Servicemesh().V1alpha2().ServicePolicies(), + client.Kubernetes(), + client.Istio(), + client.KubeSphere()) + } apController := application.NewApplicationController(kubernetesInformer.Core().V1().Services(), kubernetesInformer.Apps().V1().Deployments(), diff --git a/cmd/controller-manager/app/options/options.go b/cmd/controller-manager/app/options/options.go index ccaede7fd..4bb1ee157 100644 --- a/cmd/controller-manager/app/options/options.go +++ b/cmd/controller-manager/app/options/options.go @@ -6,13 +6,13 @@ import ( "k8s.io/client-go/tools/leaderelection" cliflag "k8s.io/component-base/cli/flag" "k8s.io/klog" - kubesphereconfig "kubesphere.io/kubesphere/pkg/apiserver/config" "kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins" "kubesphere.io/kubesphere/pkg/simple/client/k8s" "kubesphere.io/kubesphere/pkg/simple/client/multicluster" "kubesphere.io/kubesphere/pkg/simple/client/network" "kubesphere.io/kubesphere/pkg/simple/client/openpitrix" "kubesphere.io/kubesphere/pkg/simple/client/s3" + "kubesphere.io/kubesphere/pkg/simple/client/servicemesh" "strings" "time" ) @@ -24,6 +24,7 @@ type KubeSphereControllerManagerOptions struct { OpenPitrixOptions *openpitrix.Options NetworkOptions *network.Options MultiClusterOptions *multicluster.Options + ServiceMeshOptions *servicemesh.Options LeaderElect bool LeaderElection *leaderelection.LeaderElectionConfig WebhookCertDir string @@ -37,6 +38,7 @@ func NewKubeSphereControllerManagerOptions() *KubeSphereControllerManagerOptions OpenPitrixOptions: openpitrix.NewOptions(), NetworkOptions: network.NewNetworkOptions(), MultiClusterOptions: multicluster.NewOptions(), + ServiceMeshOptions: servicemesh.NewServiceMeshOptions(), LeaderElection: &leaderelection.LeaderElectionConfig{ LeaseDuration: 30 * time.Second, RenewDeadline: 15 * time.Second, @@ -49,13 +51,6 @@ func NewKubeSphereControllerManagerOptions() *KubeSphereControllerManagerOptions return s } -func (s *KubeSphereControllerManagerOptions) ApplyTo(conf *kubesphereconfig.Config) { - s.S3Options.ApplyTo(conf.S3Options) - s.KubernetesOptions.ApplyTo(conf.KubernetesOptions) - s.DevopsOptions.ApplyTo(conf.DevopsOptions) - s.OpenPitrixOptions.ApplyTo(conf.OpenPitrixOptions) -} - func (s *KubeSphereControllerManagerOptions) Flags() cliflag.NamedFlagSets { fss := cliflag.NamedFlagSets{} @@ -65,6 +60,7 @@ func (s *KubeSphereControllerManagerOptions) Flags() cliflag.NamedFlagSets { s.OpenPitrixOptions.AddFlags(fss.FlagSet("openpitrix"), s.OpenPitrixOptions) s.NetworkOptions.AddFlags(fss.FlagSet("network"), s.NetworkOptions) s.MultiClusterOptions.AddFlags(fss.FlagSet("multicluster"), s.MultiClusterOptions) + s.ServiceMeshOptions.AddFlags(fss.FlagSet("servicemesh"), s.ServiceMeshOptions) fs := fss.FlagSet("leaderelection") s.bindLeaderElectionFlags(s.LeaderElection, fs) diff --git a/cmd/controller-manager/app/server.go b/cmd/controller-manager/app/server.go index 6fbba6627..40a0ffba9 100644 --- a/cmd/controller-manager/app/server.go +++ b/cmd/controller-manager/app/server.go @@ -61,6 +61,7 @@ func NewControllerManagerCommand() *cobra.Command { OpenPitrixOptions: conf.OpenPitrixOptions, NetworkOptions: conf.NetworkOptions, MultiClusterOptions: conf.MultiClusterOptions, + ServiceMeshOptions: conf.ServiceMeshOptions, LeaderElection: s.LeaderElection, LeaderElect: s.LeaderElect, } @@ -157,7 +158,9 @@ func Run(s *options.KubeSphereControllerManagerOptions, stopCh <-chan struct{}) klog.Fatal("Unable to create namespace controller") } - if err := addControllers(mgr, kubernetesClient, informerFactory, devopsClient, s3Client, openpitrixClient, s.MultiClusterOptions.Enable, s.NetworkOptions.EnableNetworkPolicy, stopCh); err != nil { + // TODO(jeff): refactor config with CRD + servicemeshEnabled := s.ServiceMeshOptions != nil && len(s.ServiceMeshOptions.IstioPilotHost) != 0 + if err = addControllers(mgr, kubernetesClient, informerFactory, devopsClient, s3Client, openpitrixClient, s.MultiClusterOptions.Enable, s.NetworkOptions.EnableNetworkPolicy, servicemeshEnabled, stopCh); err != nil { klog.Fatalf("unable to register controllers to the manager: %v", err) }