From 07ae83608b3092c2242bfdebe1608b5ecdba7214 Mon Sep 17 00:00:00 2001 From: Reed Schalo Date: Wed, 24 Jul 2024 15:07:14 -0700 Subject: [PATCH] feat: Drop Env Vars for v1 (#1457) --- pkg/operator/operator.go | 2 +- pkg/operator/options/options.go | 32 ++++---- pkg/operator/options/suite_test.go | 122 ++++++++++++++--------------- pkg/test/options.go | 58 +++++++------- 4 files changed, 107 insertions(+), 107 deletions(-) diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 13c0263306..a036ee4528 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -150,7 +150,7 @@ func NewOperator() (context.Context, *Operator) { // Manager mgrOpts := ctrl.Options{ Logger: logging.IgnoreDebugEvents(logger), - LeaderElection: options.FromContext(ctx).EnableLeaderElection, + LeaderElection: !options.FromContext(ctx).DisableLeaderElection, LeaderElectionID: "karpenter-leader-election", LeaderElectionResourceLock: resourcelock.LeasesResourceLock, LeaderElectionNamespace: system.Namespace(), diff --git a/pkg/operator/options/options.go b/pkg/operator/options/options.go index cb98afe1a7..9aab6d6176 100644 --- a/pkg/operator/options/options.go +++ b/pkg/operator/options/options.go @@ -46,21 +46,21 @@ type FeatureGates struct { // Options contains all CLI flags / env vars for karpenter-core. It adheres to the options.Injectable interface. type Options struct { - ServiceName string - DisableWebhook bool - WebhookPort int - MetricsPort int - WebhookMetricsPort int - HealthProbePort int - KubeClientQPS int - KubeClientBurst int - EnableProfiling bool - EnableLeaderElection bool - MemoryLimit int64 - LogLevel string - BatchMaxDuration time.Duration - BatchIdleDuration time.Duration - FeatureGates FeatureGates + ServiceName string + DisableWebhook bool + WebhookPort int + MetricsPort int + WebhookMetricsPort int + HealthProbePort int + KubeClientQPS int + KubeClientBurst int + EnableProfiling bool + DisableLeaderElection bool + MemoryLimit int64 + LogLevel string + BatchMaxDuration time.Duration + BatchIdleDuration time.Duration + FeatureGates FeatureGates } type FlagSet struct { @@ -90,7 +90,7 @@ func (o *Options) AddFlags(fs *FlagSet) { fs.IntVar(&o.KubeClientQPS, "kube-client-qps", env.WithDefaultInt("KUBE_CLIENT_QPS", 200), "The smoothed rate of qps to kube-apiserver") fs.IntVar(&o.KubeClientBurst, "kube-client-burst", env.WithDefaultInt("KUBE_CLIENT_BURST", 300), "The maximum allowed burst of queries to the kube-apiserver") fs.BoolVarWithEnv(&o.EnableProfiling, "enable-profiling", "ENABLE_PROFILING", false, "Enable the profiling on the metric endpoint") - fs.BoolVarWithEnv(&o.EnableLeaderElection, "leader-elect", "LEADER_ELECT", true, "Start leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.") + fs.BoolVarWithEnv(&o.DisableLeaderElection, "disable-leader-election", "DISABLE_LEADER_ELECTION", false, "Disable the leader election client before executing the main loop. Disable when running replicated components for high availability is not desired.") fs.Int64Var(&o.MemoryLimit, "memory-limit", env.WithDefaultInt64("MEMORY_LIMIT", -1), "Memory limit on the container running the controller. The GC soft memory limit is set to 90% of this value.") fs.StringVar(&o.LogLevel, "log-level", env.WithDefaultString("LOG_LEVEL", "info"), "Log verbosity level. Can be one of 'debug', 'info', or 'error'") fs.DurationVar(&o.BatchMaxDuration, "batch-max-duration", env.WithDefaultDuration("BATCH_MAX_DURATION", 10*time.Second), "The maximum length of a batch window. The longer this is, the more pods we can consider for provisioning at one time which usually results in fewer but larger nodes.") diff --git a/pkg/operator/options/suite_test.go b/pkg/operator/options/suite_test.go index 5c8fb261a0..f818d0066b 100644 --- a/pkg/operator/options/suite_test.go +++ b/pkg/operator/options/suite_test.go @@ -54,7 +54,7 @@ var _ = Describe("Options", func() { "KUBE_CLIENT_QPS", "KUBE_CLIENT_BURST", "ENABLE_PROFILING", - "LEADER_ELECT", + "DISABLE_LEADER_ELECTION", "MEMORY_LIMIT", "LOG_LEVEL", "BATCH_MAX_DURATION", @@ -96,20 +96,20 @@ var _ = Describe("Options", func() { err := opts.Parse(fs) Expect(err).To(BeNil()) expectOptionsMatch(opts, test.Options(test.OptionsFields{ - ServiceName: lo.ToPtr(""), - DisableWebhook: lo.ToPtr(false), - WebhookPort: lo.ToPtr(8443), - MetricsPort: lo.ToPtr(8000), - WebhookMetricsPort: lo.ToPtr(8001), - HealthProbePort: lo.ToPtr(8081), - KubeClientQPS: lo.ToPtr(200), - KubeClientBurst: lo.ToPtr(300), - EnableProfiling: lo.ToPtr(false), - EnableLeaderElection: lo.ToPtr(true), - MemoryLimit: lo.ToPtr[int64](-1), - LogLevel: lo.ToPtr("info"), - BatchMaxDuration: lo.ToPtr(10 * time.Second), - BatchIdleDuration: lo.ToPtr(time.Second), + ServiceName: lo.ToPtr(""), + DisableWebhook: lo.ToPtr(false), + WebhookPort: lo.ToPtr(8443), + MetricsPort: lo.ToPtr(8000), + WebhookMetricsPort: lo.ToPtr(8001), + HealthProbePort: lo.ToPtr(8081), + KubeClientQPS: lo.ToPtr(200), + KubeClientBurst: lo.ToPtr(300), + EnableProfiling: lo.ToPtr(false), + DisableLeaderElection: lo.ToPtr(false), + MemoryLimit: lo.ToPtr[int64](-1), + LogLevel: lo.ToPtr("info"), + BatchMaxDuration: lo.ToPtr(10 * time.Second), + BatchIdleDuration: lo.ToPtr(time.Second), FeatureGates: test.FeatureGates{ SpotToSpotConsolidation: lo.ToPtr(false), }, @@ -128,7 +128,7 @@ var _ = Describe("Options", func() { "--kube-client-qps", "0", "--kube-client-burst", "0", "--enable-profiling", - "--leader-elect=false", + "--disable-leader-election=true", "--memory-limit", "0", "--log-level", "debug", "--batch-max-duration", "5s", @@ -137,20 +137,20 @@ var _ = Describe("Options", func() { ) Expect(err).To(BeNil()) expectOptionsMatch(opts, test.Options(test.OptionsFields{ - ServiceName: lo.ToPtr("cli"), - DisableWebhook: lo.ToPtr(true), - WebhookPort: lo.ToPtr(0), - MetricsPort: lo.ToPtr(0), - WebhookMetricsPort: lo.ToPtr(0), - HealthProbePort: lo.ToPtr(0), - KubeClientQPS: lo.ToPtr(0), - KubeClientBurst: lo.ToPtr(0), - EnableProfiling: lo.ToPtr(true), - EnableLeaderElection: lo.ToPtr(false), - MemoryLimit: lo.ToPtr[int64](0), - LogLevel: lo.ToPtr("debug"), - BatchMaxDuration: lo.ToPtr(5 * time.Second), - BatchIdleDuration: lo.ToPtr(5 * time.Second), + ServiceName: lo.ToPtr("cli"), + DisableWebhook: lo.ToPtr(true), + WebhookPort: lo.ToPtr(0), + MetricsPort: lo.ToPtr(0), + WebhookMetricsPort: lo.ToPtr(0), + HealthProbePort: lo.ToPtr(0), + KubeClientQPS: lo.ToPtr(0), + KubeClientBurst: lo.ToPtr(0), + EnableProfiling: lo.ToPtr(true), + DisableLeaderElection: lo.ToPtr(true), + MemoryLimit: lo.ToPtr[int64](0), + LogLevel: lo.ToPtr("debug"), + BatchMaxDuration: lo.ToPtr(5 * time.Second), + BatchIdleDuration: lo.ToPtr(5 * time.Second), FeatureGates: test.FeatureGates{ SpotToSpotConsolidation: lo.ToPtr(true), }, @@ -167,7 +167,7 @@ var _ = Describe("Options", func() { os.Setenv("KUBE_CLIENT_QPS", "0") os.Setenv("KUBE_CLIENT_BURST", "0") os.Setenv("ENABLE_PROFILING", "true") - os.Setenv("LEADER_ELECT", "false") + os.Setenv("DISABLE_LEADER_ELECTION", "true") os.Setenv("MEMORY_LIMIT", "0") os.Setenv("LOG_LEVEL", "debug") os.Setenv("BATCH_MAX_DURATION", "5s") @@ -180,20 +180,20 @@ var _ = Describe("Options", func() { err := opts.Parse(fs) Expect(err).To(BeNil()) expectOptionsMatch(opts, test.Options(test.OptionsFields{ - ServiceName: lo.ToPtr("env"), - DisableWebhook: lo.ToPtr(true), - WebhookPort: lo.ToPtr(0), - MetricsPort: lo.ToPtr(0), - WebhookMetricsPort: lo.ToPtr(0), - HealthProbePort: lo.ToPtr(0), - KubeClientQPS: lo.ToPtr(0), - KubeClientBurst: lo.ToPtr(0), - EnableProfiling: lo.ToPtr(true), - EnableLeaderElection: lo.ToPtr(false), - MemoryLimit: lo.ToPtr[int64](0), - LogLevel: lo.ToPtr("debug"), - BatchMaxDuration: lo.ToPtr(5 * time.Second), - BatchIdleDuration: lo.ToPtr(5 * time.Second), + ServiceName: lo.ToPtr("env"), + DisableWebhook: lo.ToPtr(true), + WebhookPort: lo.ToPtr(0), + MetricsPort: lo.ToPtr(0), + WebhookMetricsPort: lo.ToPtr(0), + HealthProbePort: lo.ToPtr(0), + KubeClientQPS: lo.ToPtr(0), + KubeClientBurst: lo.ToPtr(0), + EnableProfiling: lo.ToPtr(true), + DisableLeaderElection: lo.ToPtr(true), + MemoryLimit: lo.ToPtr[int64](0), + LogLevel: lo.ToPtr("debug"), + BatchMaxDuration: lo.ToPtr(5 * time.Second), + BatchIdleDuration: lo.ToPtr(5 * time.Second), FeatureGates: test.FeatureGates{ SpotToSpotConsolidation: lo.ToPtr(true), }, @@ -208,7 +208,7 @@ var _ = Describe("Options", func() { os.Setenv("KUBE_CLIENT_QPS", "0") os.Setenv("KUBE_CLIENT_BURST", "0") os.Setenv("ENABLE_PROFILING", "true") - os.Setenv("LEADER_ELECT", "false") + os.Setenv("DISABLE_LEADER_ELECTION", "true") os.Setenv("MEMORY_LIMIT", "0") os.Setenv("LOG_LEVEL", "debug") os.Setenv("BATCH_MAX_DURATION", "5s") @@ -225,20 +225,20 @@ var _ = Describe("Options", func() { ) Expect(err).To(BeNil()) expectOptionsMatch(opts, test.Options(test.OptionsFields{ - ServiceName: lo.ToPtr("cli"), - DisableWebhook: lo.ToPtr(true), - WebhookPort: lo.ToPtr(0), - MetricsPort: lo.ToPtr(0), - WebhookMetricsPort: lo.ToPtr(0), - HealthProbePort: lo.ToPtr(0), - KubeClientQPS: lo.ToPtr(0), - KubeClientBurst: lo.ToPtr(0), - EnableProfiling: lo.ToPtr(true), - EnableLeaderElection: lo.ToPtr(false), - MemoryLimit: lo.ToPtr[int64](0), - LogLevel: lo.ToPtr("debug"), - BatchMaxDuration: lo.ToPtr(5 * time.Second), - BatchIdleDuration: lo.ToPtr(5 * time.Second), + ServiceName: lo.ToPtr("cli"), + DisableWebhook: lo.ToPtr(true), + WebhookPort: lo.ToPtr(0), + MetricsPort: lo.ToPtr(0), + WebhookMetricsPort: lo.ToPtr(0), + HealthProbePort: lo.ToPtr(0), + KubeClientQPS: lo.ToPtr(0), + KubeClientBurst: lo.ToPtr(0), + EnableProfiling: lo.ToPtr(true), + DisableLeaderElection: lo.ToPtr(true), + MemoryLimit: lo.ToPtr[int64](0), + LogLevel: lo.ToPtr("debug"), + BatchMaxDuration: lo.ToPtr(5 * time.Second), + BatchIdleDuration: lo.ToPtr(5 * time.Second), FeatureGates: test.FeatureGates{ SpotToSpotConsolidation: lo.ToPtr(true), }, @@ -294,7 +294,7 @@ func expectOptionsMatch(optsA, optsB *options.Options) { Expect(optsA.KubeClientQPS).To(Equal(optsB.KubeClientQPS)) Expect(optsA.KubeClientBurst).To(Equal(optsB.KubeClientBurst)) Expect(optsA.EnableProfiling).To(Equal(optsB.EnableProfiling)) - Expect(optsA.EnableLeaderElection).To(Equal(optsB.EnableLeaderElection)) + Expect(optsA.DisableLeaderElection).To(Equal(optsB.DisableLeaderElection)) Expect(optsA.MemoryLimit).To(Equal(optsB.MemoryLimit)) Expect(optsA.LogLevel).To(Equal(optsB.LogLevel)) Expect(optsA.BatchMaxDuration).To(Equal(optsB.BatchMaxDuration)) diff --git a/pkg/test/options.go b/pkg/test/options.go index 3e3de9c041..b9b22386da 100644 --- a/pkg/test/options.go +++ b/pkg/test/options.go @@ -28,21 +28,21 @@ import ( type OptionsFields struct { // Vendor Neutral - ServiceName *string - DisableWebhook *bool - WebhookPort *int - MetricsPort *int - WebhookMetricsPort *int - HealthProbePort *int - KubeClientQPS *int - KubeClientBurst *int - EnableProfiling *bool - EnableLeaderElection *bool - MemoryLimit *int64 - LogLevel *string - BatchMaxDuration *time.Duration - BatchIdleDuration *time.Duration - FeatureGates FeatureGates + ServiceName *string + DisableWebhook *bool + WebhookPort *int + MetricsPort *int + WebhookMetricsPort *int + HealthProbePort *int + KubeClientQPS *int + KubeClientBurst *int + EnableProfiling *bool + DisableLeaderElection *bool + MemoryLimit *int64 + LogLevel *string + BatchMaxDuration *time.Duration + BatchIdleDuration *time.Duration + FeatureGates FeatureGates } type FeatureGates struct { @@ -58,20 +58,20 @@ func Options(overrides ...OptionsFields) *options.Options { } return &options.Options{ - ServiceName: lo.FromPtrOr(opts.ServiceName, ""), - DisableWebhook: lo.FromPtrOr(opts.DisableWebhook, false), - WebhookPort: lo.FromPtrOr(opts.WebhookPort, 8443), - MetricsPort: lo.FromPtrOr(opts.MetricsPort, 8000), - WebhookMetricsPort: lo.FromPtrOr(opts.WebhookMetricsPort, 8001), - HealthProbePort: lo.FromPtrOr(opts.HealthProbePort, 8081), - KubeClientQPS: lo.FromPtrOr(opts.KubeClientQPS, 200), - KubeClientBurst: lo.FromPtrOr(opts.KubeClientBurst, 300), - EnableProfiling: lo.FromPtrOr(opts.EnableProfiling, false), - EnableLeaderElection: lo.FromPtrOr(opts.EnableLeaderElection, true), - MemoryLimit: lo.FromPtrOr(opts.MemoryLimit, -1), - LogLevel: lo.FromPtrOr(opts.LogLevel, ""), - BatchMaxDuration: lo.FromPtrOr(opts.BatchMaxDuration, 10*time.Second), - BatchIdleDuration: lo.FromPtrOr(opts.BatchIdleDuration, time.Second), + ServiceName: lo.FromPtrOr(opts.ServiceName, ""), + DisableWebhook: lo.FromPtrOr(opts.DisableWebhook, false), + WebhookPort: lo.FromPtrOr(opts.WebhookPort, 8443), + MetricsPort: lo.FromPtrOr(opts.MetricsPort, 8000), + WebhookMetricsPort: lo.FromPtrOr(opts.WebhookMetricsPort, 8001), + HealthProbePort: lo.FromPtrOr(opts.HealthProbePort, 8081), + KubeClientQPS: lo.FromPtrOr(opts.KubeClientQPS, 200), + KubeClientBurst: lo.FromPtrOr(opts.KubeClientBurst, 300), + EnableProfiling: lo.FromPtrOr(opts.EnableProfiling, false), + DisableLeaderElection: lo.FromPtrOr(opts.DisableLeaderElection, false), + MemoryLimit: lo.FromPtrOr(opts.MemoryLimit, -1), + LogLevel: lo.FromPtrOr(opts.LogLevel, ""), + BatchMaxDuration: lo.FromPtrOr(opts.BatchMaxDuration, 10*time.Second), + BatchIdleDuration: lo.FromPtrOr(opts.BatchIdleDuration, time.Second), FeatureGates: options.FeatureGates{ SpotToSpotConsolidation: lo.FromPtrOr(opts.FeatureGates.SpotToSpotConsolidation, false), },