mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-27 19:52:49 +00:00
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
/*
|
|
Copyright 2023 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.
|
|
*/
|
|
|
|
package manager
|
|
|
|
import (
|
|
"context"
|
|
|
|
"k8s.io/klog/v2"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/config"
|
|
ctrlcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
|
|
|
|
_const "github.com/kubesphere/kubekey/v4/pkg/const"
|
|
"github.com/kubesphere/kubekey/v4/pkg/controllers"
|
|
"github.com/kubesphere/kubekey/v4/pkg/proxy"
|
|
"github.com/kubesphere/kubekey/v4/pkg/task"
|
|
"github.com/kubesphere/kubekey/v4/pkg/variable"
|
|
)
|
|
|
|
type controllerManager struct {
|
|
ControllerGates []string
|
|
MaxConcurrentReconciles int
|
|
LeaderElection bool
|
|
}
|
|
|
|
func (c controllerManager) Run(ctx context.Context) error {
|
|
ctrl.SetLogger(klog.NewKlogr())
|
|
|
|
restconfig := config.GetConfigOrDie()
|
|
proxyTransport, err := proxy.NewProxyTransport(false)
|
|
if err != nil {
|
|
klog.ErrorS(err, "Create proxy transport error")
|
|
return err
|
|
}
|
|
restconfig.Transport = proxyTransport
|
|
mgr, err := ctrl.NewManager(restconfig, ctrl.Options{
|
|
Scheme: _const.Scheme,
|
|
LeaderElection: c.LeaderElection,
|
|
LeaderElectionID: "controller-leader-election-kk",
|
|
})
|
|
if err != nil {
|
|
klog.ErrorS(err, "Create manager error")
|
|
return err
|
|
}
|
|
|
|
taskController, err := task.NewController(task.ControllerOptions{
|
|
Scheme: mgr.GetScheme(),
|
|
VariableCache: variable.Cache,
|
|
MaxConcurrent: c.MaxConcurrentReconciles,
|
|
Client: mgr.GetClient(),
|
|
TaskReconciler: &controllers.TaskReconciler{
|
|
Client: mgr.GetClient(),
|
|
VariableCache: variable.Cache,
|
|
},
|
|
})
|
|
if err != nil {
|
|
klog.ErrorS(err, "Create task controller error")
|
|
return err
|
|
}
|
|
|
|
// add task controller to manager
|
|
if err := mgr.Add(taskController); err != nil {
|
|
klog.ErrorS(err, "Add task controller error")
|
|
return err
|
|
}
|
|
|
|
if err := (&controllers.PipelineReconciler{
|
|
Client: mgr.GetClient(),
|
|
EventRecorder: mgr.GetEventRecorderFor("pipeline"),
|
|
TaskController: taskController,
|
|
}).SetupWithManager(ctx, mgr, controllers.Options{
|
|
ControllerGates: c.ControllerGates,
|
|
Options: ctrlcontroller.Options{
|
|
MaxConcurrentReconciles: c.MaxConcurrentReconciles,
|
|
},
|
|
}); err != nil {
|
|
klog.ErrorS(err, "create pipeline controller error")
|
|
return err
|
|
}
|
|
|
|
return mgr.Start(ctx)
|
|
}
|