kubekey/feature/pkg/manager/controller_manager.go
github-actions[bot] fe55978234
Some checks failed
CI / Build (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
Sync feature branch to master/feature directory (#2400)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2024-09-11 09:48:38 +08:00

72 lines
2.0 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"
"fmt"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
"github.com/kubesphere/kubekey/v4/pkg/controllers"
"github.com/kubesphere/kubekey/v4/pkg/proxy"
)
type controllerManager struct {
MaxConcurrentReconciles int
LeaderElection bool
}
// Run controllerManager, run controller in kubernetes
func (c controllerManager) Run(ctx context.Context) error {
ctrl.SetLogger(klog.NewKlogr())
restconfig, err := ctrl.GetConfig()
if err != nil {
klog.Infof("kubeconfig in empty, store resources local")
restconfig = &rest.Config{}
}
restconfig, err = proxy.NewConfig(restconfig)
if err != nil {
return fmt.Errorf("could not get rest config: %w", err)
}
mgr, err := ctrl.NewManager(restconfig, ctrl.Options{
Scheme: _const.Scheme,
LeaderElection: c.LeaderElection,
LeaderElectionID: "controller-leader-election-kk",
})
if err != nil {
return fmt.Errorf("could not create controller manager: %w", err)
}
if err := (&controllers.PipelineReconciler{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor("pipeline"),
Scheme: mgr.GetScheme(),
MaxConcurrentReconciles: c.MaxConcurrentReconciles,
}).SetupWithManager(mgr); err != nil {
klog.ErrorS(err, "create pipeline controller error")
return err
}
return mgr.Start(ctx)
}