From 90413f40641ac17f516259c4f00b425a5791ad74 Mon Sep 17 00:00:00 2001 From: l1b0k Date: Tue, 6 Aug 2024 19:29:54 +0800 Subject: [PATCH] add lease config Signed-off-by: l1b0k --- .../terway-controlplane.go | 120 +++++++++++------- .../terway-controlplane_test.go | 34 +++++ types/controlplane/config_default.go | 8 +- 3 files changed, 113 insertions(+), 49 deletions(-) diff --git a/cmd/terway-controlplane/terway-controlplane.go b/cmd/terway-controlplane/terway-controlplane.go index 3139255a..175327c7 100644 --- a/cmd/terway-controlplane/terway-controlplane.go +++ b/cmd/terway-controlplane/terway-controlplane.go @@ -137,53 +137,7 @@ func main() { os.Exit(1) } - ws := wh.NewServer(wh.Options{ - Port: cfg.WebhookPort, - CertDir: cfg.CertDir, - }) - options := ctrl.Options{ - Scheme: scheme, - HealthProbeBindAddress: cfg.HealthzBindAddress, - WebhookServer: ws, - LeaderElection: cfg.LeaderElection, - LeaderElectionID: cfg.ControllerName, - LeaderElectionNamespace: cfg.ControllerNamespace, - LeaderElectionResourceLock: "leases", - MetricsBindAddress: cfg.MetricsBindAddress, - Cache: cache.Options{ - ByObject: map[client.Object]cache.ByObject{ - &corev1.Node{}: { - Transform: func(i interface{}) (interface{}, error) { - if node, ok := i.(*corev1.Node); ok { - node.Status.Images = nil - node.Status.VolumesInUse = nil - node.Status.VolumesAttached = nil - return node, nil - } - return nil, fmt.Errorf("unexpected type %T", i) - }, - }, - &corev1.Pod{}: { - Transform: func(i interface{}) (interface{}, error) { - if pod, ok := i.(*corev1.Pod); ok { - pod.Spec.Volumes = nil - pod.Spec.EphemeralContainers = nil - pod.Spec.SecurityContext = nil - pod.Spec.ImagePullSecrets = nil - pod.Spec.Tolerations = nil - pod.Spec.ReadinessGates = nil - pod.Spec.PreemptionPolicy = nil - pod.Status.InitContainerStatuses = nil - pod.Status.ContainerStatuses = nil - pod.Status.EphemeralContainerStatuses = nil - return pod, nil - } - return nil, fmt.Errorf("unexpected type %T", i) - }, - }, - }, - }, - } + options := newOption(cfg) if !cfg.DisableWebhook { err = cert.SyncCert(ctx, directClient, cfg.ControllerNamespace, cfg.ControllerName, cfg.ClusterDomain, cfg.CertDir) @@ -283,6 +237,78 @@ func main() { wg.Wait() } +func newOption(cfg *controlplane.Config) ctrl.Options { + ws := wh.NewServer(wh.Options{ + Port: cfg.WebhookPort, + CertDir: cfg.CertDir, + }) + options := ctrl.Options{ + Scheme: scheme, + HealthProbeBindAddress: cfg.HealthzBindAddress, + WebhookServer: ws, + LeaderElection: cfg.LeaderElection, + LeaderElectionID: cfg.ControllerName, + LeaderElectionNamespace: cfg.ControllerNamespace, + LeaderElectionResourceLock: "leases", + MetricsBindAddress: cfg.MetricsBindAddress, + Cache: cache.Options{ + ByObject: map[client.Object]cache.ByObject{ + &corev1.Node{}: { + Transform: func(i interface{}) (interface{}, error) { + if node, ok := i.(*corev1.Node); ok { + node.Status.Images = nil + node.Status.VolumesInUse = nil + node.Status.VolumesAttached = nil + return node, nil + } + return nil, fmt.Errorf("unexpected type %T", i) + }, + }, + &corev1.Pod{}: { + Transform: func(i interface{}) (interface{}, error) { + if pod, ok := i.(*corev1.Pod); ok { + pod.Spec.Volumes = nil + pod.Spec.EphemeralContainers = nil + pod.Spec.SecurityContext = nil + pod.Spec.ImagePullSecrets = nil + pod.Spec.Tolerations = nil + pod.Spec.ReadinessGates = nil + pod.Spec.PreemptionPolicy = nil + pod.Status.InitContainerStatuses = nil + pod.Status.ContainerStatuses = nil + pod.Status.EphemeralContainerStatuses = nil + return pod, nil + } + return nil, fmt.Errorf("unexpected type %T", i) + }, + }, + }, + }, + } + + if cfg.LeaseDuration != "" { + d, err := time.ParseDuration(cfg.LeaseDuration) + if err == nil { + options.LeaseDuration = &d + } + } + + if cfg.RenewDeadline != "" { + d, err := time.ParseDuration(cfg.RenewDeadline) + if err == nil { + options.RenewDeadline = &d + } + } + + if cfg.RetryPeriod != "" { + d, err := time.ParseDuration(cfg.RetryPeriod) + if err == nil { + options.RetryPeriod = &d + } + } + return options +} + // initOpenTelemetry bootstraps the OpenTelemetry pipeline. // If it does not return an error, make sure to call shutdown for proper cleanup. func initOpenTelemetry(ctx context.Context, serviceName, serviceVersion string, cfg *controlplane.Config) (*trace.TracerProvider, error) { diff --git a/cmd/terway-controlplane/terway-controlplane_test.go b/cmd/terway-controlplane/terway-controlplane_test.go index 1374cc97..08ce857e 100644 --- a/cmd/terway-controlplane/terway-controlplane_test.go +++ b/cmd/terway-controlplane/terway-controlplane_test.go @@ -3,11 +3,13 @@ package main import ( "context" "testing" + "time" "github.com/samber/lo" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -122,3 +124,35 @@ func Test_detectMultiIP(t *testing.T) { }) } } + +func Test_newOption(t *testing.T) { + type args struct { + cfg *controlplane.Config + } + tests := []struct { + name string + args args + checkFunc func(t *testing.T, opt ctrl.Options) + }{ + { + name: "new cfg", + args: args{ + cfg: &controlplane.Config{ + LeaseDuration: "2m", + RenewDeadline: "2m", + RetryPeriod: "2m", + }, + }, + checkFunc: func(t *testing.T, opt ctrl.Options) { + assert.Equal(t, 2*time.Minute, *opt.LeaseDuration) + assert.Equal(t, 2*time.Minute, *opt.RenewDeadline) + assert.Equal(t, 2*time.Minute, *opt.RetryPeriod) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.checkFunc(t, newOption(tt.args.cfg)) + }) + } +} diff --git a/types/controlplane/config_default.go b/types/controlplane/config_default.go index 8e9b88a7..fdca2d97 100644 --- a/types/controlplane/config_default.go +++ b/types/controlplane/config_default.go @@ -26,8 +26,12 @@ import ( type Config struct { // controller config - LeaseLockName string `json:"leaseLockName" validate:"required" mod:"default=terway-controller-lock"` - LeaseLockNamespace string `json:"leaseLockNamespace" validate:"required" mod:"default=kube-system"` + LeaseLockName string `json:"leaseLockName" validate:"required" mod:"default=terway-controller-lock"` + LeaseLockNamespace string `json:"leaseLockNamespace" validate:"required" mod:"default=kube-system"` + LeaseDuration string `json:"leaseDuration"` + RenewDeadline string `json:"renewDeadline"` + RetryPeriod string `json:"retryPeriod"` + ControllerNamespace string `json:"controllerNamespace" validate:"required" mod:"default=kube-system"` ControllerName string `json:"controllerName" validate:"required" mod:"default=terway-controlplane"`