2020-10-21 12:49:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Ceph-CSI 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
|
|
|
|
|
2023-06-02 09:49:22 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2020-10-21 12:49:45 +00:00
|
|
|
|
|
|
|
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 controller
|
|
|
|
|
|
|
|
import (
|
2021-05-11 09:28:56 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2020-10-21 12:49:45 +00:00
|
|
|
|
2024-09-25 10:15:28 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/client-go/rest"
|
2022-04-11 08:00:08 +00:00
|
|
|
"k8s.io/client-go/tools/leaderelection/resourcelock"
|
2020-10-21 12:49:45 +00:00
|
|
|
clientConfig "sigs.k8s.io/controller-runtime/pkg/client/config"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/manager"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
|
2023-08-29 07:01:01 +00:00
|
|
|
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
|
2020-10-21 12:49:45 +00:00
|
|
|
)
|
|
|
|
|
2021-08-18 04:16:11 +00:00
|
|
|
// Manager is the interface that will wrap Add function.
|
2020-10-21 12:49:45 +00:00
|
|
|
// The New controllers which gets added, as to implement Add function to get
|
|
|
|
// started by the manager.
|
2021-08-18 04:16:11 +00:00
|
|
|
type Manager interface {
|
2024-04-04 08:56:33 +00:00
|
|
|
Add(mgr manager.Manager, cfg Config) error
|
2020-10-21 12:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds the drivername and namespace name.
|
|
|
|
type Config struct {
|
2022-04-11 04:27:29 +00:00
|
|
|
DriverName string
|
|
|
|
Namespace string
|
|
|
|
ClusterName string
|
2024-08-02 13:19:02 +00:00
|
|
|
InstanceID string
|
2022-04-12 04:03:00 +00:00
|
|
|
SetMetadata bool
|
2020-10-21 12:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ControllerList holds the list of managers need to be started.
|
2021-08-18 04:16:11 +00:00
|
|
|
var ControllerList []Manager
|
2020-10-21 12:49:45 +00:00
|
|
|
|
|
|
|
// addToManager calls the registered managers Add method.
|
|
|
|
func addToManager(mgr manager.Manager, config Config) error {
|
|
|
|
for _, c := range ControllerList {
|
|
|
|
err := c.Add(mgr, config)
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to add: %w", err)
|
2020-10-21 12:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start will start all the registered managers.
|
|
|
|
func Start(config Config) error {
|
|
|
|
electionID := config.DriverName + "-" + config.Namespace
|
|
|
|
opts := manager.Options{
|
|
|
|
LeaderElection: true,
|
|
|
|
// disable metrics
|
2023-08-29 07:01:01 +00:00
|
|
|
Metrics: metricsserver.Options{BindAddress: "0"},
|
2022-04-11 08:00:08 +00:00
|
|
|
LeaderElectionNamespace: config.Namespace,
|
|
|
|
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
|
|
|
|
LeaderElectionID: electionID,
|
2020-10-21 12:49:45 +00:00
|
|
|
}
|
2024-09-25 10:15:28 +00:00
|
|
|
|
|
|
|
kubeConfig := clientConfig.GetConfigOrDie()
|
|
|
|
coreKubeConfig := rest.CopyConfig(kubeConfig)
|
|
|
|
coreKubeConfig.ContentType = runtime.ContentTypeProtobuf
|
|
|
|
mgr, err := manager.New(coreKubeConfig, opts)
|
2020-10-21 12:49:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLogMsg("failed to create manager %s", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = addToManager(mgr, config)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLogMsg("failed to add manager %s", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = mgr.Start(signals.SetupSignalHandler())
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLogMsg("failed to start manager %s", err)
|
2020-10-21 12:49:45 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-21 12:49:45 +00:00
|
|
|
return err
|
|
|
|
}
|