@@ -139,6 +139,7 @@ func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Inter
139
139
func run (c context.Context , log * logrus.Entry , cfg * config.Config ) error {
140
140
ctx , cancel := context .WithCancel (c )
141
141
defer cancel ()
142
+
142
143
// add debug mode to context
143
144
if cfg .DevelopMode {
144
145
ctx = context .WithValue (ctx , developModeKey , true )
@@ -167,43 +168,37 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
167
168
if err != nil {
168
169
return errors .Wrap (err , "initializing assigner" )
169
170
}
170
- // assign static public IP address
171
- errorCh := make (chan error , 1 ) // buffered channel to avoid goroutine leak
172
- go func () {
173
- defer close (errorCh ) // close the channel when the goroutine exits to avoid goroutine leak
174
- e := assignAddress (ctx , log , clientset , assigner , n , cfg )
175
- if e != nil {
176
- errorCh <- e
177
- }
178
- }()
179
171
180
- for {
181
- select {
182
- case err = <- errorCh :
183
- if err != nil {
184
- return errors .Wrap (err , "assigning static public IP address" )
185
- }
186
- case <- ctx .Done ():
187
- log .Infof ("kubeip agent gracefully stopped" )
188
- if cfg .ReleaseOnExit {
189
- log .Infof ("releasing static public IP address" )
190
- err = func () error {
191
- releaseCtx , releaseCancel := context .WithTimeout (context .Background (), unassignTimeout ) // release the static public IP address within 5 minutes
192
- defer releaseCancel ()
193
- // use a different context for releasing the static public IP address since the main context is canceled
194
- if err = assigner .Unassign (releaseCtx , n .Instance , n .Zone ); err != nil {
195
- return errors .Wrap (err , "failed to release static public IP address" )
196
- }
197
- return nil
198
- }()
199
- if err != nil {
200
- return err //nolint:wrapcheck
201
- }
202
- log .Infof ("static public IP address released" )
203
- }
204
- return nil
172
+ err = assignAddress (ctx , log , clientset , assigner , n , cfg )
173
+ if err != nil {
174
+ return errors .Wrap (err , "assigning static public IP address" )
175
+ }
176
+
177
+ // pause the agent to prevent it from exiting immediately after assigning the static public IP address
178
+ // wait for the context to be done: SIGTERM, SIGINT
179
+ <- ctx .Done ()
180
+ log .Infof ("shutting down kubeip agent" )
181
+
182
+ // release the static public IP address on exit
183
+ if cfg .ReleaseOnExit {
184
+ log .Infof ("releasing static public IP address" )
185
+ if releaseErr := releaseIP (assigner , n ); releaseErr != nil { //nolint:contextcheck
186
+ return releaseErr
205
187
}
188
+ log .Infof ("static public IP address released" )
206
189
}
190
+ return nil
191
+ }
192
+
193
+ func releaseIP (assigner address.Assigner , n * types.Node ) error {
194
+ releaseCtx , releaseCancel := context .WithTimeout (context .Background (), unassignTimeout )
195
+ defer releaseCancel ()
196
+
197
+ if err := assigner .Unassign (releaseCtx , n .Instance , n .Zone ); err != nil {
198
+ return errors .Wrap (err , "failed to release static public IP address" )
199
+ }
200
+
201
+ return nil
207
202
}
208
203
209
204
func runCmd (c * cli.Context ) error {
@@ -213,7 +208,8 @@ func runCmd(c *cli.Context) error {
213
208
cfg := config .NewConfig (c )
214
209
215
210
if err := run (ctx , log , cfg ); err != nil {
216
- log .Fatalf ("eks-lens agent failed: %v" , err )
211
+ log .WithError (err ).Error ("error running kubeip agent" )
212
+ return err
217
213
}
218
214
219
215
return nil
0 commit comments