@@ -23,7 +23,8 @@ import (
23
23
type contextKey string
24
24
25
25
const (
26
- developModeKey contextKey = "develop-mode"
26
+ developModeKey contextKey = "develop-mode"
27
+ unassignTimeout = 5 * time .Minute
27
28
)
28
29
29
30
var (
@@ -81,36 +82,29 @@ func prepareLogger(level string, json bool) *logrus.Entry {
81
82
func assignAddress (c context.Context , log * logrus.Entry , assigner address.Assigner , node * types.Node , cfg * config.Config ) error {
82
83
ctx , cancel := context .WithCancel (c )
83
84
defer cancel ()
84
- // retry counter
85
- retryCounter := 0
85
+
86
86
// ticker for retry interval
87
87
ticker := time .NewTicker (cfg .RetryInterval )
88
88
defer ticker .Stop ()
89
89
90
- for {
90
+ for retryCounter := 0 ; retryCounter <= cfg . RetryAttempts ; retryCounter ++ {
91
91
err := assigner .Assign (ctx , node .Instance , node .Zone , cfg .Filter , cfg .OrderBy )
92
- if err != nil && errors .Is (err , address .ErrStaticIPAlreadyAssigned ) {
93
- log .Infof ("static public IP address already assigned to node instance %s" , node .Instance )
92
+ if err == nil || errors .Is (err , address .ErrStaticIPAlreadyAssigned ) {
94
93
return nil
95
94
}
96
- if err != nil {
97
- log .WithError (err ).Errorf ("failed to assign static public IP address to node %s" , node .Name )
98
- if retryCounter < cfg .RetryAttempts {
99
- retryCounter ++
100
- log .Infof ("retrying after %v" , cfg .RetryInterval )
101
- } else {
102
- log .Infof ("reached maximum number of retries (%d)" , cfg .RetryAttempts )
103
- return errors .Wrap (err , "reached maximum number of retries" )
104
- }
105
- select {
106
- case <- ticker .C :
107
- continue
108
- case <- ctx .Done ():
109
- return errors .Wrap (err , "context is done" )
110
- }
95
+
96
+ log .WithError (err ).Errorf ("failed to assign static public IP address to node %s" , node .Name )
97
+ log .Infof ("retrying after %v" , cfg .RetryInterval )
98
+
99
+ select {
100
+ case <- ticker .C :
101
+ continue
102
+ case <- ctx .Done ():
103
+ // If the context is done, return an error indicating that the operation was cancelled
104
+ return errors .Wrap (ctx .Err (), "context cancelled while assigning addresses" )
111
105
}
112
- return nil
113
106
}
107
+ return errors .New ("reached maximum number of retries" )
114
108
}
115
109
116
110
func run (c context.Context , log * logrus.Entry , cfg * config.Config ) error {
@@ -145,8 +139,9 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
145
139
return errors .Wrap (err , "initializing assigner" )
146
140
}
147
141
// assign static public IP address
148
- errorCh := make (chan error )
142
+ errorCh := make (chan error , 1 ) // buffered channel to avoid goroutine leak
149
143
go func () {
144
+ defer close (errorCh ) // close the channel when the goroutine exits to avoid goroutine leak
150
145
e := assignAddress (ctx , log , assigner , n , cfg )
151
146
if e != nil {
152
147
errorCh <- e
@@ -159,13 +154,16 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
159
154
return errors .Wrap (err , "assigning static public IP address" )
160
155
}
161
156
case <- ctx .Done ():
162
- log .Infof ("kubeip agent stopped" )
157
+ log .Infof ("kubeip agent gracefully stopped" )
163
158
if cfg .ReleaseOnExit {
164
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 ()
165
162
// use a different context for releasing the static public IP address since the main context is canceled
166
- if err = assigner .Unassign (context . Background () , n .Instance , n .Zone ); err != nil { //nolint:contextcheck
167
- return errors .Wrap (err , "releasing static public IP address" )
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" )
168
165
}
166
+ log .Infof ("static public IP address released" )
169
167
}
170
168
}
171
169
0 commit comments