Skip to content

Commit

Permalink
fix/assing-no-public (#138)
Browse files Browse the repository at this point in the history
* try to assign static public IP if node missing ephmeral IP address

* keep daemonset running; avoid CrashLoopBackOff
  • Loading branch information
alexei-led authored Mar 27, 2024
1 parent d222017 commit 902814f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
53 changes: 35 additions & 18 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,22 @@ func assignAddress(c context.Context, log *logrus.Entry, assigner address.Assign
defer ticker.Stop()

for retryCounter := 0; retryCounter <= cfg.RetryAttempts; retryCounter++ {
log.WithFields(logrus.Fields{
"node": node.Name,
"instance": node.Instance,
"filter": cfg.Filter,
"retry-counter": retryCounter,
"retry-attempts": cfg.RetryAttempts,
}).Debug("assigning static public IP address to node")
err := assigner.Assign(ctx, node.Instance, node.Zone, cfg.Filter, cfg.OrderBy)
if err == nil || errors.Is(err, address.ErrStaticIPAlreadyAssigned) {
return nil
}

log.WithError(err).Errorf("failed to assign static public IP address to node %s", node.Name)
log.WithError(err).WithFields(logrus.Fields{
"node": node.Name,
"instance": node.Instance,
}).Error("failed to assign static public IP address to node")
log.Infof("retrying after %v", cfg.RetryInterval)

select {
Expand Down Expand Up @@ -148,26 +158,33 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
}
}()

select {
case err = <-errorCh:
if err != nil {
return errors.Wrap(err, "assigning static public IP address")
}
case <-ctx.Done():
log.Infof("kubeip agent gracefully stopped")
if cfg.ReleaseOnExit {
log.Infof("releasing static public IP address")
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), unassignTimeout) // release the static public IP address within 5 minutes
defer releaseCancel()
// use a different context for releasing the static public IP address since the main context is canceled
if err = assigner.Unassign(releaseCtx, n.Instance, n.Zone); err != nil { //nolint:contextcheck
return errors.Wrap(err, "failed to release static public IP address")
for {
select {
case err = <-errorCh:
if err != nil {
return errors.Wrap(err, "assigning static public IP address")
}
case <-ctx.Done():
log.Infof("kubeip agent gracefully stopped")
if cfg.ReleaseOnExit {
log.Infof("releasing static public IP address")
err = func() error {
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), unassignTimeout) // release the static public IP address within 5 minutes
defer releaseCancel()
// use a different context for releasing the static public IP address since the main context is canceled
if err = assigner.Unassign(releaseCtx, n.Instance, n.Zone); err != nil {
return errors.Wrap(err, "failed to release static public IP address")
}
return nil
}()
if err != nil {
return err //nolint:wrapcheck
}
log.Infof("static public IP address released")
}
log.Infof("static public IP address released")
return nil
}
}

return nil
}

func runCmd(c *cli.Context) error {
Expand Down
10 changes: 7 additions & 3 deletions internal/address/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const (
maxRetries = 10 // number of retries for assigning ephemeral public IP address
)

var (
ErrNoPublicIPAssigned = errors.New("no public IP address assigned to the instance")
)

type internalAssigner interface {
CheckAddressAssigned(region, addressName string) (bool, error)
AddInstanceAddress(ctx context.Context, instance *compute.Instance, zone string, address *compute.Address) error
Expand Down Expand Up @@ -233,7 +237,7 @@ func (a *gcpAssigner) Assign(ctx context.Context, instanceID, zone string, filte
a.logger.WithField("addresses", ips).Debugf("found %d available addresses", len(addresses))

// delete current ephemeral public IP address
if err = a.DeleteInstanceAddress(ctx, instance, zone); err != nil {
if err = a.DeleteInstanceAddress(ctx, instance, zone); err != nil && !errors.Is(err, ErrNoPublicIPAssigned) {
return errors.Wrap(err, "failed to delete current public IP address")
}

Expand Down Expand Up @@ -359,12 +363,12 @@ func (a *gcpAssigner) Unassign(ctx context.Context, instanceID, zone string) err
func getAccessConfig(networkInterface *compute.NetworkInterface, ipv6 bool) (*compute.AccessConfig, error) {
if ipv6 {
if len(networkInterface.Ipv6AccessConfigs) == 0 {
return nil, errors.New("instance network interface has no IPv6 access configs")
return nil, ErrNoPublicIPAssigned
}
return networkInterface.Ipv6AccessConfigs[0], nil
}
if len(networkInterface.AccessConfigs) == 0 {
return nil, errors.New("instance network interface has no access configs")
return nil, ErrNoPublicIPAssigned
}
return networkInterface.AccessConfigs[0], nil
}
Expand Down

0 comments on commit 902814f

Please sign in to comment.