Skip to content

Commit

Permalink
cmd/assignAddress: return assignedAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
huwcbjones committed Oct 24, 2024
1 parent cb26e29 commit e43c567
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
21 changes: 11 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func prepareLogger(level string, json bool) *logrus.Entry {
return log
}

func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Interface, assigner address.Assigner, node *types.Node, cfg *config.Config) error {
func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Interface, assigner address.Assigner, node *types.Node, cfg *config.Config) (string, error) {
ctx, cancel := context.WithCancel(c)
defer cancel()

Expand All @@ -101,22 +101,23 @@ func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Inter
"retry-counter": retryCounter,
"retry-attempts": cfg.RetryAttempts,
}).Debug("assigning static public IP address to node")
err := func(ctx context.Context) error {
assignedAddress, err := func(ctx context.Context) (string, error) {
if err := lock.Lock(ctx); err != nil {
return errors.Wrap(err, "failed to acquire lock")
return "", errors.Wrap(err, "failed to acquire lock")
}
log.Debug("lock acquired")
defer func() {
lock.Unlock(ctx) //nolint:errcheck
log.Debug("lock released")
}()
if _, err := assigner.Assign(ctx, node.Instance, node.Zone, cfg.Filter, cfg.OrderBy); err != nil {
return err //nolint:wrapcheck
assignedAddress, err := assigner.Assign(ctx, node.Instance, node.Zone, cfg.Filter, cfg.OrderBy)
if err != nil {
return "", err //nolint:wrapcheck
}
return nil
return assignedAddress, nil
}(c)
if err == nil || errors.Is(err, address.ErrStaticIPAlreadyAssigned) {
return nil
return assignedAddress, nil
}

log.WithError(err).WithFields(logrus.Fields{
Expand All @@ -130,10 +131,10 @@ func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Inter
continue
case <-ctx.Done():
// If the context is done, return an error indicating that the operation was cancelled
return errors.Wrap(ctx.Err(), "context cancelled while assigning addresses")
return "", errors.Wrap(ctx.Err(), "context cancelled while assigning addresses")
}
}
return errors.New("reached maximum number of retries")
return "", errors.New("reached maximum number of retries")
}

func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
Expand Down Expand Up @@ -169,7 +170,7 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
return errors.Wrap(err, "initializing assigner")
}

err = assignAddress(ctx, log, clientset, assigner, n, cfg)
_, err = assignAddress(ctx, log, clientset, assigner, n, cfg)
if err != nil {
return errors.Wrap(err, "assigning static public IP address")
}
Expand Down
26 changes: 16 additions & 10 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ func Test_assignAddress(t *testing.T) {
tests := []struct {
name string
args args
address string
wantErr bool
}{
{
name: "assign address successfully",
name: "assign address successfully",
address: "1.1.1.1",
args: args{
c: context.Background(),
assignerFn: func(t *testing.T) address.Assigner {
mock := mocks.NewAssigner(t)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(nil)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("1.1.1.1", nil)
return mock
},
node: &types.Node{
Expand All @@ -51,14 +53,15 @@ func Test_assignAddress(t *testing.T) {
},
},
{
name: "assign address after a few retries",
name: "assign address after a few retries",
address: "1.1.1.1",
args: args{
c: context.Background(),
assignerFn: func(t *testing.T) address.Assigner {
mock := mocks.NewAssigner(t)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(errors.New("first error")).Once()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(errors.New("second error")).Once()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(nil).Once()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("", errors.New("first error")).Once()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("", errors.New("second error")).Once()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("1.1.1.1", nil).Once()
return mock
},
node: &types.Node{
Expand All @@ -82,7 +85,7 @@ func Test_assignAddress(t *testing.T) {
c: context.Background(),
assignerFn: func(t *testing.T) address.Assigner {
mock := mocks.NewAssigner(t)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(errors.New("error")).Times(4)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("", errors.New("error")).Times(4)
return mock
},
node: &types.Node{
Expand Down Expand Up @@ -115,7 +118,7 @@ func Test_assignAddress(t *testing.T) {
}(),
assignerFn: func(t *testing.T) address.Assigner {
mock := mocks.NewAssigner(t)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(errors.New("error")).Maybe()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("", errors.New("error")).Maybe()
return mock
},
node: &types.Node{
Expand Down Expand Up @@ -143,7 +146,7 @@ func Test_assignAddress(t *testing.T) {
}(),
assignerFn: func(t *testing.T) address.Assigner {
mock := mocks.NewAssigner(t)
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return(errors.New("error")).Maybe()
mock.EXPECT().Assign(tmock.Anything, "test-instance", "test-zone", []string{"test-filter"}, "test-order-by").Return("", errors.New("error")).Maybe()
return mock
},
node: &types.Node{
Expand All @@ -168,8 +171,11 @@ func Test_assignAddress(t *testing.T) {
log := prepareLogger("debug", false)
assigner := tt.args.assignerFn(t)
client := fake.NewSimpleClientset()
if err := assignAddress(tt.args.c, log, client, assigner, tt.args.node, tt.args.cfg); (err != nil) != tt.wantErr {
assignedAddress, err := assignAddress(tt.args.c, log, client, assigner, tt.args.node, tt.args.cfg)
if err != nil != tt.wantErr {
t.Errorf("assignAddress() error = %v, wantErr %v", err, tt.wantErr)
} else if assignedAddress != tt.address {
t.Fatalf("assignAddress() = %v, want %v", assignedAddress, tt.address)
}
})
}
Expand Down

0 comments on commit e43c567

Please sign in to comment.