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

fix/assing-no-public #138

Merged
merged 2 commits into from
Mar 27, 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
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
Loading