Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/mtping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
34 changes: 29 additions & 5 deletions pkg/adapter/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"time"

cloudevents "github.com/cloudevents/sdk-go/v2"
Expand Down Expand Up @@ -62,6 +63,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.
Expand All @@ -77,10 +91,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)
}

Expand All @@ -89,13 +99,27 @@ func MainWithContext(ctx context.Context, component string, ector EnvConfigConst
}

func MainWithEnv(ctx context.Context, component string, env EnvConfigAccessor, ctor AdapterConstructor) {
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())
if informers != nil {
StartInformers(ctx, informers) // none-blocking
}
ctx = ictx
}

if !flag.Parsed() {
flag.Parse()
}

b, err := strconv.ParseBool(flag.Lookup("disable-ha").Value.String())
if err != nil || b {
ctx = withHADisabled(ctx)
}

MainWithInformers(ctx, component, env, ctor)
}

Expand Down Expand Up @@ -186,7 +210,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)
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/adapter/v2/main_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down