Skip to content

Commit

Permalink
add stop and more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sthwang-metal committed Jul 8, 2024
1 parent a3f78e4 commit f0b85a7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
22 changes: 18 additions & 4 deletions cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func process(ctx context.Context, logger *zap.SugaredLogger) error {
}

cx, cancel := context.WithCancel(ctx)
defer cancel()

eSrv, err := echox.NewServer(
logger.Desugar(),
Expand Down Expand Up @@ -194,6 +195,16 @@ func process(ctx context.Context, logger *zap.SugaredLogger) error {
logger.Fatalw("failed to initialize tracer", "error", err)
}

go func() {
defer cancel()
err := server.ReconcileTimer(ctx, config.AppConfig.ReconcilerInterval)

Check failure on line 200 in cmd/process.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)
if err != nil {

Check failure on line 201 in cmd/process.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
logger.Error("error reconciling load balancers", zap.Error(err))
}

logger.Warn("reconciler stopped")
}()

if err := server.Run(cx); err != nil {
logger.Fatalw("failed starting server", "error", err)
cancel()
Expand All @@ -202,10 +213,13 @@ func process(ctx context.Context, logger *zap.SugaredLogger) error {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)

recvSig := <-sigCh
signal.Stop(sigCh)
cancel()
logger.Infof("exiting. Performing necessary cleanup", recvSig)
select {
case <-server.Context.Done():
logger.Info("context done signal received")
case recvSig := <-sigCh:
signal.Stop(sigCh)
logger.Infof("exiting. Performing necessary cleanup", recvSig)
}

if err := server.Shutdown(); err != nil {
logger.Errorw("failed to shutdown server", zap.Error(err))
Expand Down
19 changes: 10 additions & 9 deletions internal/srv/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ var (
// ErrPortsRequired is returned when a healthcheck port has not been provided
ErrPortsRequired = errors.New("no ports provided")

errInvalidObjectNameLength = errors.New("object name must be less than 53 characters")
errSubscriptionCreate = errors.New("unable to subscribe to topic")
errInvalidHelmClient = errors.New("unable to create helm client")
errInvalidNamespace = errors.New("unable to create namespace")
errInvalidRoleBinding = errors.New("unable to create namespace role binding")
errInvalidHelmValues = errors.New("unable to create helm values")
errLoadBalancerInit = errors.New("unable to initialize loadbalancer data")
errNotMyMessage = errors.New("message not for this location")
errMissingLocation = errors.New("at least one location is required")
errInvalidObjectNameLength = errors.New("object name must be less than 53 characters")
errSubscriptionCreate = errors.New("unable to subscribe to topic")
errInvalidHelmClient = errors.New("unable to create helm client")
errInvalidNamespace = errors.New("unable to create namespace")
errInvalidRoleBinding = errors.New("unable to create namespace role binding")
errInvalidHelmValues = errors.New("unable to create helm values")
errLoadBalancerInit = errors.New("unable to initialize loadbalancer data")
errNotMyMessage = errors.New("message not for this location")
errMissingLocation = errors.New("at least one location is required")
errMissingReconcilerInterval = errors.New("reconciler interval not set")
)
29 changes: 27 additions & 2 deletions internal/srv/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"errors"
"time"

lbapi "go.infratographer.com/load-balancer-api/pkg/client"
"go.infratographer.com/x/events"
Expand All @@ -17,6 +18,28 @@ import (
// OperatorManagedLabelKey is the label key for the operator managed namespaces
var OperatorManagedLabelKey = "com.infratographer.lb-operator/managed"

func (s *Server) ReconcileTimer(ctx context.Context, interval time.Duration) error {
if interval <= 0 {
return errMissingReconcilerInterval
}

ticker := time.NewTicker(interval)

for {
select {
case <-ctx.Done():
s.Logger.Info("reconciler done")

return nil
case <-ticker.C:
s.Logger.Info("starting reconciler run")
if err := s.Reconcile(ctx); err != nil {

Check failure on line 36 in internal/srv/reconciler.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
return err
}
}
}
}

// Reconcile will reconcile out of sync load balancers
func (s *Server) Reconcile(ctx context.Context) error {
s.Logger.Info("starting reconciler")
Expand Down Expand Up @@ -70,13 +93,15 @@ func (s *Server) Reconcile(ctx context.Context) error {

s.Logger.Debugf("actual lbs: %+v\n", actualLBs)

// add load balancers
toAddLBs := difference(expectedLBs, actualLBs)
s.Logger.Infof("LBs to add: %+v", toAddLBs)

if err = s.addLBs(ctx, toAddLBs); err != nil {
s.Logger.Error("Error reconciling (adding) missing lbs:", err)
}

// remove load balancers
toRemoveLBs := difference(actualLBs, expectedLBs)
s.Logger.Infof("LBs to remove: %+v", toRemoveLBs)

Expand Down Expand Up @@ -161,7 +186,7 @@ func (s *Server) addLBs(ctx context.Context, lbs []string) error {
lb, err := s.getLoadBalancer(ctx, lbID, evt)

if err != nil {
s.Logger.Debug("Error getting lb from api: ", err)
s.Logger.Warn("Error getting lb from api: ", err)
e = errors.Join(e, err)

continue
Expand All @@ -177,7 +202,7 @@ func (s *Server) addLBs(ctx context.Context, lbs []string) error {
s.Runner.writer <- task
}

return nil
return e
}

func (s *Server) getLoadBalancer(ctx context.Context, lbID string, evt events.ChangeType) (*loadBalancer, error) {
Expand Down
25 changes: 0 additions & 25 deletions internal/srv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package srv

import (
"context"
"time"

"github.com/lestrrat-go/backoff/v2"
lbapi "go.infratographer.com/load-balancer-api/pkg/client"
Expand All @@ -14,8 +13,6 @@ import (
"k8s.io/client-go/rest"

"go.infratographer.com/ipam-api/pkg/ipamclient"

"go.infratographer.com/load-balancer-operator/internal/config"
)

// instrumentationName is a unique package name used for tracing
Expand Down Expand Up @@ -59,28 +56,6 @@ func (s *Server) Run(ctx context.Context) error {
}
}()

go func() {
if config.AppConfig.ReconcilerInterval <= 0 {
s.Logger.Warn("reconciler interval not set")
return
}

ticker := time.NewTicker(config.AppConfig.ReconcilerInterval)

for {
select {
case <-ctx.Done():
s.Logger.Info("reconciler done")

return
case <-ticker.C:
if err := s.Reconcile(ctx); err != nil {
s.Logger.Error("error reconciling load balancers", zap.Error(err))
}
}
}
}()

s.Logger.Infow("starting subscribers")

if err := s.configureSubscribers(ctx); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/srv/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type runner struct {
quit chan struct{}
buffer []*lbTask
taskRunner func(*lbTask)
ctx context.Context
}

type lbTask struct {
Expand All @@ -40,6 +41,8 @@ func (r *runner) run() {

for {
select {
case <-r.ctx.Done():
return
case <-r.quit:
return
default:
Expand Down Expand Up @@ -72,6 +75,7 @@ func NewRunner(ctx context.Context, tr taskRunner) *runner {
buffer: make([]*lbTask, 0),
quit: make(chan struct{}),
taskRunner: tr,
ctx: ctx,
}

go r.run()
Expand Down

0 comments on commit f0b85a7

Please sign in to comment.