Skip to content

Commit 902814f

Browse files
authored
fix/assing-no-public (#138)
* try to assign static public IP if node missing ephmeral IP address * keep daemonset running; avoid CrashLoopBackOff
1 parent d222017 commit 902814f

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

cmd/main.go

+35-18
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,22 @@ func assignAddress(c context.Context, log *logrus.Entry, assigner address.Assign
8888
defer ticker.Stop()
8989

9090
for retryCounter := 0; retryCounter <= cfg.RetryAttempts; retryCounter++ {
91+
log.WithFields(logrus.Fields{
92+
"node": node.Name,
93+
"instance": node.Instance,
94+
"filter": cfg.Filter,
95+
"retry-counter": retryCounter,
96+
"retry-attempts": cfg.RetryAttempts,
97+
}).Debug("assigning static public IP address to node")
9198
err := assigner.Assign(ctx, node.Instance, node.Zone, cfg.Filter, cfg.OrderBy)
9299
if err == nil || errors.Is(err, address.ErrStaticIPAlreadyAssigned) {
93100
return nil
94101
}
95102

96-
log.WithError(err).Errorf("failed to assign static public IP address to node %s", node.Name)
103+
log.WithError(err).WithFields(logrus.Fields{
104+
"node": node.Name,
105+
"instance": node.Instance,
106+
}).Error("failed to assign static public IP address to node")
97107
log.Infof("retrying after %v", cfg.RetryInterval)
98108

99109
select {
@@ -148,26 +158,33 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
148158
}
149159
}()
150160

151-
select {
152-
case err = <-errorCh:
153-
if err != nil {
154-
return errors.Wrap(err, "assigning static public IP address")
155-
}
156-
case <-ctx.Done():
157-
log.Infof("kubeip agent gracefully stopped")
158-
if cfg.ReleaseOnExit {
159-
log.Infof("releasing static public IP address")
160-
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), unassignTimeout) // release the static public IP address within 5 minutes
161-
defer releaseCancel()
162-
// use a different context for releasing the static public IP address since the main context is canceled
163-
if err = assigner.Unassign(releaseCtx, n.Instance, n.Zone); err != nil { //nolint:contextcheck
164-
return errors.Wrap(err, "failed to release static public IP address")
161+
for {
162+
select {
163+
case err = <-errorCh:
164+
if err != nil {
165+
return errors.Wrap(err, "assigning static public IP address")
166+
}
167+
case <-ctx.Done():
168+
log.Infof("kubeip agent gracefully stopped")
169+
if cfg.ReleaseOnExit {
170+
log.Infof("releasing static public IP address")
171+
err = func() error {
172+
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), unassignTimeout) // release the static public IP address within 5 minutes
173+
defer releaseCancel()
174+
// use a different context for releasing the static public IP address since the main context is canceled
175+
if err = assigner.Unassign(releaseCtx, n.Instance, n.Zone); err != nil {
176+
return errors.Wrap(err, "failed to release static public IP address")
177+
}
178+
return nil
179+
}()
180+
if err != nil {
181+
return err //nolint:wrapcheck
182+
}
183+
log.Infof("static public IP address released")
165184
}
166-
log.Infof("static public IP address released")
185+
return nil
167186
}
168187
}
169-
170-
return nil
171188
}
172189

173190
func runCmd(c *cli.Context) error {

internal/address/gcp.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const (
2828
maxRetries = 10 // number of retries for assigning ephemeral public IP address
2929
)
3030

31+
var (
32+
ErrNoPublicIPAssigned = errors.New("no public IP address assigned to the instance")
33+
)
34+
3135
type internalAssigner interface {
3236
CheckAddressAssigned(region, addressName string) (bool, error)
3337
AddInstanceAddress(ctx context.Context, instance *compute.Instance, zone string, address *compute.Address) error
@@ -233,7 +237,7 @@ func (a *gcpAssigner) Assign(ctx context.Context, instanceID, zone string, filte
233237
a.logger.WithField("addresses", ips).Debugf("found %d available addresses", len(addresses))
234238

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

@@ -359,12 +363,12 @@ func (a *gcpAssigner) Unassign(ctx context.Context, instanceID, zone string) err
359363
func getAccessConfig(networkInterface *compute.NetworkInterface, ipv6 bool) (*compute.AccessConfig, error) {
360364
if ipv6 {
361365
if len(networkInterface.Ipv6AccessConfigs) == 0 {
362-
return nil, errors.New("instance network interface has no IPv6 access configs")
366+
return nil, ErrNoPublicIPAssigned
363367
}
364368
return networkInterface.Ipv6AccessConfigs[0], nil
365369
}
366370
if len(networkInterface.AccessConfigs) == 0 {
367-
return nil, errors.New("instance network interface has no access configs")
371+
return nil, ErrNoPublicIPAssigned
368372
}
369373
return networkInterface.AccessConfigs[0], nil
370374
}

0 commit comments

Comments
 (0)