From 76544857e288d58ee0b991f405baf395ac439cc4 Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Tue, 28 Jul 2020 14:09:13 -0400 Subject: [PATCH 1/2] reenable HA for mtping --- cmd/mtping/main.go | 1 + pkg/adapter/v2/main.go | 30 +++++++++++++++++++++++++----- pkg/adapter/v2/main_message.go | 3 --- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cmd/mtping/main.go b/cmd/mtping/main.go index d171ed3ae39..c9a964fb174 100644 --- a/cmd/mtping/main.go +++ b/cmd/mtping/main.go @@ -31,5 +31,6 @@ func main() { ctx := signals.NewContext() ctx = adapter.WithConfigMapWatcherEnabled(ctx) ctx = adapter.WithInjectorEnabled(ctx) + ctx = adapter.WithHAEnabled(ctx) adapter.MainWithContext(ctx, component, mtping.NewEnvConfig, mtping.NewAdapter) } diff --git a/pkg/adapter/v2/main.go b/pkg/adapter/v2/main.go index c0f7347d66d..d445d8897c7 100644 --- a/pkg/adapter/v2/main.go +++ b/pkg/adapter/v2/main.go @@ -62,6 +62,19 @@ func IsHAEnabled(ctx context.Context) bool { return val != nil } +type haDisabledKey struct{} + +// withHADisabled signals to MainWithConfig that it should not set up an appropriate leader elector for this component. +func withHADisabled(ctx context.Context) context.Context { + return context.WithValue(ctx, haDisabledKey{}, struct{}{}) +} + +// isHADisabled checks the context for the desired to disable leader elector. +func isHADisabled(ctx context.Context) bool { + val := ctx.Value(haEnabledKey{}) + return val != nil +} + type injectorEnabledKey struct{} // WithInjectorEnabled signals to MainWithInjectors that it should try to run injectors. @@ -77,10 +90,6 @@ func IsInjectorEnabled(ctx context.Context) bool { func Main(component string, ector EnvConfigConstructor, ctor AdapterConstructor) { ctx := signals.NewContext() - - // TODO(mattmoor): expose a flag that gates this? - // ctx = WithHAEnabled(ctx) - MainWithContext(ctx, component, ector, ctor) } @@ -89,6 +98,8 @@ func MainWithContext(ctx context.Context, component string, ector EnvConfigConst } func MainWithEnv(ctx context.Context, component string, env EnvConfigAccessor, ctor AdapterConstructor) { + disableHighAvailability := flag.Bool("disable-ha", false, "Whether to disable high-availability functionality for this component.") + if IsInjectorEnabled(ctx) { ictx, informers := SetupInformers(ctx, env.GetLogger()) if informers != nil { @@ -96,6 +107,15 @@ func MainWithEnv(ctx context.Context, component string, env EnvConfigAccessor, c } ctx = ictx } + + if !flag.Parsed() { + flag.Parse() + } + + if *disableHighAvailability { + ctx = withHADisabled(ctx) + } + MainWithInformers(ctx, component, env, ctor) } @@ -186,7 +206,7 @@ func MainWithInformers(ctx context.Context, component string, env EnvConfigAcces logger.Error("Error loading the leader election configuration", zap.Error(err)) } - if IsHAEnabled(ctx) { + if !isHADisabled(ctx) && IsHAEnabled(ctx) { // Signal that we are executing in a context with leader election. ctx = leaderelection.WithStandardLeaderElectorBuilder(ctx, kubeclient.Get(ctx), *leConfig) } diff --git a/pkg/adapter/v2/main_message.go b/pkg/adapter/v2/main_message.go index 693c70ec256..e34acfe101d 100644 --- a/pkg/adapter/v2/main_message.go +++ b/pkg/adapter/v2/main_message.go @@ -46,9 +46,6 @@ type MessageAdapterConstructor func(ctx context.Context, env EnvConfigAccessor, func MainMessageAdapter(component string, ector EnvConfigConstructor, ctor MessageAdapterConstructor) { ctx := signals.NewContext() - // TODO(mattmoor): expose a flag that gates this? - // ctx = WithHAEnabled(ctx) - MainMessageAdapterWithContext(ctx, component, ector, ctor) } From 035769ff912eddbcaa10e6244051ceaacfd00e39 Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Tue, 28 Jul 2020 14:48:39 -0400 Subject: [PATCH 2/2] fix ut --- pkg/adapter/v2/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/adapter/v2/main.go b/pkg/adapter/v2/main.go index d445d8897c7..9c028c71542 100644 --- a/pkg/adapter/v2/main.go +++ b/pkg/adapter/v2/main.go @@ -22,6 +22,7 @@ import ( "fmt" "log" "net/http" + "strconv" "time" cloudevents "github.com/cloudevents/sdk-go/v2" @@ -98,7 +99,9 @@ func MainWithContext(ctx context.Context, component string, ector EnvConfigConst } func MainWithEnv(ctx context.Context, component string, env EnvConfigAccessor, ctor AdapterConstructor) { - disableHighAvailability := flag.Bool("disable-ha", false, "Whether to disable high-availability functionality for this component.") + if flag.Lookup("disable-ha") == nil { + flag.Bool("disable-ha", false, "Whether to disable high-availability functionality for this component.") + } if IsInjectorEnabled(ctx) { ictx, informers := SetupInformers(ctx, env.GetLogger()) @@ -112,7 +115,8 @@ func MainWithEnv(ctx context.Context, component string, env EnvConfigAccessor, c flag.Parse() } - if *disableHighAvailability { + b, err := strconv.ParseBool(flag.Lookup("disable-ha").Value.String()) + if err != nil || b { ctx = withHADisabled(ctx) }