controllers.go 5.2 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*

 Copyright 2019 The KubeSphere Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

*/
J
Jeff 已提交
18 19 20
package app

import (
J
Jeff 已提交
21
	"k8s.io/client-go/informers"
J
Jeff 已提交
22 23
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
J
Jeff 已提交
24
	"kubesphere.io/kubesphere/pkg/controller/application"
J
Jeff 已提交
25
	"kubesphere.io/kubesphere/pkg/controller/destinationrule"
J
Jeff 已提交
26
	"kubesphere.io/kubesphere/pkg/controller/job"
R
runzexia 已提交
27 28 29
	"kubesphere.io/kubesphere/pkg/controller/s2ibinary"

	"kubesphere.io/kubesphere/pkg/controller/s2irun"
J
Jeff 已提交
30

J
Jeff 已提交
31
	//"kubesphere.io/kubesphere/pkg/controller/job"
J
Jeff 已提交
32 33 34 35 36 37 38 39
	"kubesphere.io/kubesphere/pkg/controller/virtualservice"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	"time"

	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

	istioclientset "github.com/knative/pkg/client/clientset/versioned"
	istioinformers "github.com/knative/pkg/client/informers/externalversions"
J
Jeff 已提交
40 41
	applicationclientset "github.com/kubernetes-sigs/application/pkg/client/clientset/versioned"
	applicationinformers "github.com/kubernetes-sigs/application/pkg/client/informers/externalversions"
R
runzexia 已提交
42 43 44 45
	s2iclientset "github.com/kubesphere/s2ioperator/pkg/client/clientset/versioned"
	s2iinformers "github.com/kubesphere/s2ioperator/pkg/client/informers/externalversions"
	kubesphereclientset "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
	kubesphereinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
J
Jeff 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
)

const defaultResync = 600 * time.Second

var log = logf.Log.WithName("controller-manager")

func AddControllers(mgr manager.Manager, cfg *rest.Config, stopCh <-chan struct{}) error {

	kubeClient, err := kubernetes.NewForConfig(cfg)
	if err != nil {
		log.Error(err, "building kubernetes client failed")
	}

	istioclient, err := istioclientset.NewForConfig(cfg)
	if err != nil {
		log.Error(err, "create istio client failed")
		return err
	}

J
Jeff 已提交
65 66 67 68 69
	applicationClient, err := applicationclientset.NewForConfig(cfg)
	if err != nil {
		log.Error(err, "create application client failed")
		return err
	}
R
runzexia 已提交
70 71 72 73 74 75 76 77 78 79
	s2iclient, err := s2iclientset.NewForConfig(cfg)
	if err != nil {
		log.Error(err, "create s2i client failed")
		return err
	}
	kubesphereclient, err := kubesphereclientset.NewForConfig(cfg)
	if err != nil {
		log.Error(err, "create kubesphere client failed")
		return err
	}
J
Jeff 已提交
80

J
Jeff 已提交
81
	informerFactory := informers.NewSharedInformerFactory(kubeClient, defaultResync)
J
Jeff 已提交
82
	istioInformer := istioinformers.NewSharedInformerFactory(istioclient, defaultResync)
J
Jeff 已提交
83
	applicationInformer := applicationinformers.NewSharedInformerFactory(applicationClient, defaultResync)
R
runzexia 已提交
84
	s2iInformer := s2iinformers.NewSharedInformerFactory(s2iclient, defaultResync)
J
Jeff 已提交
85

R
runzexia 已提交
86
	kubesphereInformer := kubesphereinformers.NewSharedInformerFactory(kubesphereclient, defaultResync)
J
Jeff 已提交
87 88 89 90

	vsController := virtualservice.NewVirtualServiceController(informerFactory.Core().V1().Services(),
		istioInformer.Networking().V1alpha3().VirtualServices(),
		istioInformer.Networking().V1alpha3().DestinationRules(),
R
runzexia 已提交
91
		kubesphereInformer.Servicemesh().V1alpha2().Strategies(),
J
Jeff 已提交
92
		kubeClient,
J
fix bug  
Jeff 已提交
93
		istioclient,
R
runzexia 已提交
94
		kubesphereclient)
J
Jeff 已提交
95 96 97 98

	drController := destinationrule.NewDestinationRuleController(informerFactory.Apps().V1().Deployments(),
		istioInformer.Networking().V1alpha3().DestinationRules(),
		informerFactory.Core().V1().Services(),
R
runzexia 已提交
99
		kubesphereInformer.Servicemesh().V1alpha2().ServicePolicies(),
J
Jeff 已提交
100
		kubeClient,
101
		istioclient,
R
runzexia 已提交
102
		kubesphereclient)
J
Jeff 已提交
103

J
Jeff 已提交
104 105 106
	apController := application.NewApplicationController(informerFactory.Core().V1().Services(),
		informerFactory.Apps().V1().Deployments(),
		informerFactory.Apps().V1().StatefulSets(),
R
runzexia 已提交
107 108
		kubesphereInformer.Servicemesh().V1alpha2().Strategies(),
		kubesphereInformer.Servicemesh().V1alpha2().ServicePolicies(),
J
Jeff 已提交
109 110 111 112
		applicationInformer.App().V1beta1().Applications(),
		kubeClient,
		applicationClient)

J
Jeff 已提交
113
	jobController := job.NewJobController(informerFactory.Batch().V1().Jobs(), kubeClient)
H
hongming 已提交
114

R
runzexia 已提交
115 116 117 118 119 120 121
	s2iBinaryController := s2ibinary.NewController(kubesphereclient, kubeClient, kubesphereInformer.Devops().V1alpha1().S2iBinaries())

	s2iRunController := s2irun.NewController(kubesphereclient, s2iclient, kubeClient,
		kubesphereInformer.Devops().V1alpha1().S2iBinaries(),
		s2iInformer.Devops().V1alpha1().S2iRuns())

	kubesphereInformer.Start(stopCh)
J
Jeff 已提交
122 123
	istioInformer.Start(stopCh)
	informerFactory.Start(stopCh)
J
Jeff 已提交
124
	applicationInformer.Start(stopCh)
R
runzexia 已提交
125
	s2iInformer.Start(stopCh)
J
Jeff 已提交
126 127 128 129

	controllers := map[string]manager.Runnable{
		"virtualservice-controller":  vsController,
		"destinationrule-controller": drController,
J
Jeff 已提交
130
		"application-controller":     apController,
J
Jeff 已提交
131
		"job-controller":             jobController,
R
runzexia 已提交
132 133
		"s2ibinary-controller":       s2iBinaryController,
		"s2irun-controller":          s2iRunController,
J
Jeff 已提交
134 135 136 137 138 139 140 141 142 143 144 145
	}

	for name, ctrl := range controllers {
		err = mgr.Add(ctrl)
		if err != nil {
			log.Error(err, "add controller to manager failed", "name", name)
			return err
		}
	}

	return nil
}