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

refactor(cmd/ingress): invert signal ctx logic #2139

Merged
merged 3 commits into from
Jan 31, 2024
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
42 changes: 20 additions & 22 deletions cmd/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package ingress

import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"

Expand All @@ -42,13 +42,19 @@ func dief(template string, args ...interface{}) {
os.Exit(1)
}

func waitForSignal(stopCh chan struct{}) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)

sig := <-sigCh
log.Infof("signal %d (%s) received", sig, sig.String())
close(stopCh)
func contextWithSignalCancel(ctx context.Context, signals ...os.Signal) context.Context {
newCtx, cancel := context.WithCancel(ctx)
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, signals...)

sig := <-sigCh
log.Infof("signal %d (%s) received", sig, sig.String())
signal.Stop(sigCh)
close(sigCh)
cancel()
}()
return newCtx
}

// NewIngressCommand creates the ingress sub command for apisix-ingress-controller.
Expand Down Expand Up @@ -118,8 +124,8 @@ the apisix cluster and others are created`,
dief("failed to initialize logging: %s", err)
}
log.DefaultLogger = logger
log.Info("init apisix ingress controller")

log.Info("init apisix ingress controller")
log.Info("version:\n", version.Long())

// We should make sure that the cfg that's logged out is sanitized.
Expand All @@ -132,25 +138,17 @@ the apisix cluster and others are created`,
}
log.Info("use configuration\n", string(data))

stop := make(chan struct{})
ctx := contextWithSignalCancel(context.Background(), syscall.SIGINT, syscall.SIGTERM)

ingress, err := controller.NewController(cfg)
if err != nil {
dief("failed to create ingress controller: %s", err)
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()

log.Info("start ingress controller")

if err := ingress.Run(stop); err != nil {
dief("failed to run ingress controller: %s", err)
}
}()
if err := ingress.Run(ctx); err != nil {
dief("failed to run ingress controller: %s", err)
}

waitForSignal(stop)
wg.Wait()
log.Info("apisix ingress controller exited")
},
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/providers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,10 @@ func (c *Controller) Eventf(_ runtime.Object, eventType string, reason string, m
}

// Run launches the controller.
func (c *Controller) Run(stop chan struct{}) error {
rootCtx, rootCancel := context.WithCancel(context.Background())
func (c *Controller) Run(ctx context.Context) error {
rootCtx, rootCancel := context.WithCancel(ctx)
defer rootCancel()
go func() {
<-stop
rootCancel()
}()

c.MetricsCollector.ResetLeader(false)

go func() {
Expand Down
Loading