some improvements

This commit is contained in:
ston1th 2021-12-08 22:12:44 +01:00
commit 312dcd577c
10 changed files with 367 additions and 379 deletions

View file

@ -52,10 +52,10 @@ func cloudInitializer(config *config.CompletedConfig) cloudprovider.Interface {
// initialize cloud provider with the cloud provider name and config file provided
cloud, err := cloudprovider.InitCloudProvider(providerName, cloudConfig.CloudConfigFile)
if err != nil {
klog.Fatalf("Cloud provider could not be initialized: %v", err)
klog.Fatalf("cloud provider could not be initialized: %v", err)
}
if cloud == nil {
klog.Fatalf("Cloud provider is nil")
klog.Fatalf("cloud provider is nil")
}
if !cloud.HasClusterID() {

View file

@ -22,66 +22,45 @@ import (
pve "git.giftfish.de/ston1th/pve-go"
)
type config struct {
PVE pve.Config `json:"pve"`
}
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
leaderElect bool
healthAddr string
cloudConfig string
csrConcurrency int
syncPeriod time.Duration
)
const name = "pve-csr-approver-manager"
func init() {
_ = clientgoscheme.AddToScheme(scheme)
}
type config struct {
PVE pve.Config `json:"pve"`
}
func main() {
rand.Seed(time.Now().UnixNano())
klog.InitFlags(nil)
var (
leaderElect bool
healthAddr string
cloudConfig string
csrConcurrency int
syncPeriod time.Duration
)
flag.BoolVar(&leaderElect, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&cloudConfig,
"cloud-config",
"",
"The path to the cloud provider configuration file. Empty string for no configuration file.",
)
flag.StringVar(&healthAddr,
"health-addr",
":9449",
"The address the health endpoint binds to.",
)
flag.IntVar(&csrConcurrency,
"csr-concurrency",
1,
"Number of CSRs to process simultaneously",
)
flag.DurationVar(&syncPeriod,
"sync-period",
10*time.Minute,
"The minimum interval at which watched resources are reconciled (e.g. 15m)",
)
flag.Parse()
ctrl.SetLogger(klogr.New())
initFlags()
flag.Parse()
rcfg := ctrl.GetConfigOrDie()
rcfg.UserAgent = name
mgr, err := ctrl.NewManager(rcfg, ctrl.Options{
Scheme: scheme,
LeaderElection: leaderElect,
LeaderElectionID: "pve-csr-approver-manager",
HealthProbeBindAddress: healthAddr,
MetricsBindAddress: "0",
SyncPeriod: &syncPeriod,
Scheme: scheme,
LeaderElection: leaderElect,
LeaderElectionID: name,
LeaderElectionResourceLock: "leases",
HealthProbeBindAddress: healthAddr,
MetricsBindAddress: "0",
SyncPeriod: &syncPeriod,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
@ -120,7 +99,7 @@ func main() {
if err = (&approver.CSRReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("approver"),
Log: ctrl.Log.WithName("controllers").WithName("approver"),
Scheme: mgr.GetScheme(),
PVEClient: pveClient,
CSRClient: typedcertificatesv1.NewForConfigOrDie(rcfg),
@ -143,82 +122,30 @@ func main() {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
/*
cfg, err := rest.InClusterConfig()
if err != nil {
log.Error(err, "could not configure kubernetes client")
os.Exit(1)
}
cl, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Error(err, "could not create kubernetes client")
os.Exit(1)
}
id, err := os.Hostname()
if err != nil {
log.Error(err, "could not get hostname")
os.Exit(1)
}
lock := &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: csrLock,
Namespace: namespace,
},
Client: cl.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: id,
},
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: time.Duration(leaseDuration) * time.Second,
RenewDeadline: time.Duration(renewDeadline) * time.Second,
RetryPeriod: time.Duration(retryPeriod) * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
log.Info("Leader election successful")
factory := informers.NewSharedInformerFactory(cl, time.Second*30)
certInformerV1beta1 := factory.Certificates().V1beta1().CertificateSigningRequests().Informer()
fV1beta1 := func(obj interface{}) {
if req, ok := obj.(*certificatesv1beta1.CertificateSigningRequest); ok {
if err := approver.ApproveV1beta1(log, pveClient, cl.CertificatesV1beta1().CertificateSigningRequests(), req); err != nil {
log.Error(err, "csr approval failed", "name", req.ObjectMeta.Name)
return
}
}
}
certInformerV1beta1.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fV1beta1(obj)
},
UpdateFunc: func(_, obj interface{}) {
fV1beta1(obj)
},
})
stop := make(chan struct{})
factory.Start(stop)
<-ctx.Done()
close(stop)
},
OnStoppedLeading: func() {
cancel()
log.Info("Leader election lost")
},
}})
log.Info("csr approver started")
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
cancel()
log.Info("csr approver stopped")
*/
}
func initFlags() {
flag.BoolVar(&leaderElect, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&cloudConfig,
"cloud-config",
"",
"The path to the cloud provider configuration file. Empty string for no configuration file.",
)
flag.StringVar(&healthAddr,
"health-addr",
":9449",
"The address the health endpoint binds to.",
)
flag.IntVar(&csrConcurrency,
"csr-concurrency",
1,
"Number of CSRs to process simultaneously",
)
flag.DurationVar(&syncPeriod,
"sync-period",
10*time.Minute,
"The minimum interval at which watched resources are reconciled (e.g. 15m)",
)
}