Skip to content

Commit

Permalink
try to assign static public IP if node missing ephmeral IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
alexei-led committed Mar 27, 2024
1 parent d222017 commit 9ea516b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 11 additions & 1 deletion 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
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 9ea516b

Please sign in to comment.