Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a possibility for NSE to Unregister itself on termination #552

Merged
merged 1 commit into from
Dec 20, 2023
Merged
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
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Config struct {
CidrPrefix cidr.Groups `default:"169.254.0.0/16" desc:"List of CIDR Prefix to assign IPv4 and IPv6 addresses from" split_words:"true"`
IdleTimeout time.Duration `default:"0" desc:"timeout for automatic shutdown when there were no requests for specified time. Set 0 to disable auto-shutdown." split_words:"true"`
RegisterService bool `default:"true" desc:"if true then registers network service on startup" split_words:"true"`
UnregisterItself bool `default:"false" desc:"if true then NSE unregister itself when it completes working" split_words:"true"`
PBRConfigPath string `default:"/etc/policy-based-routing/config.yaml" desc:"Path to policy based routing config file" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint"`
Expand All @@ -112,15 +113,17 @@ func main() {
// ********************************************************************************
// setup context to catch signals
// ********************************************************************************
ctx, cancel := signal.NotifyContext(
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

signalCtx, _ := signal.NotifyContext(
context.Background(),
os.Interrupt,
// More Linux signals here
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGQUIT,
)
defer cancel()

// ********************************************************************************
// setup logging
Expand Down Expand Up @@ -298,12 +301,20 @@ func main() {
log.FromContext(ctx).Fatalf("unable to register nse %+v", err)
}

if config.UnregisterItself {
defer func() {
_, err = nseRegistryClient.Unregister(context.Background(), nse)
if err != nil {
log.FromContext(ctx).Errorf("nse failed to unregister itself on termination: %s", err.Error())
}
}()
}
// ********************************************************************************
log.FromContext(ctx).Infof("startup completed in %v", time.Since(starttime))
// ********************************************************************************

// wait for server to exit
<-ctx.Done()
<-signalCtx.Done()
}

func getNseEndpoint(config *Config, listenOn fmt.Stringer) *registryapi.NetworkServiceEndpoint {
Expand Down
Loading