Skip to content

Commit cc87190

Browse files
committed
addressing review
Signed-off-by: Imran Pochi <[email protected]>
1 parent 2dfa6b7 commit cc87190

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

cmd/server/app/options/options.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (o *ProxyRunOptions) Flags() *pflag.FlagSet {
157157
flags.BoolVar(&o.EnableLeaseController, "enable-lease-controller", o.EnableLeaseController, "Enable lease controller to publish and garbage collect proxy server leases.")
158158
flags.StringVar(&o.LeaseNamespace, "lease-namespace", o.LeaseNamespace, "The namespace where lease objects are managed by the controller.")
159159
flags.StringVar(&o.LeaseLabel, "lease-label", o.LeaseLabel, "The labels on which the lease objects are managed.")
160-
flags.DurationVar(&o.GracefulShutdownTimeout, "graceful-shutdown-timeout", o.GracefulShutdownTimeout, "Timeout duration for graceful shutdown of the server. The server will wait for active connections to close before forcefully terminating.")
160+
flags.DurationVar(&o.GracefulShutdownTimeout, "graceful-shutdown-timeout", o.GracefulShutdownTimeout, "Timeout duration for graceful shutdown of the server. The server will wait for active connections to close before forcefully terminating. Set to 0 to disable graceful shutdown (default: 0).")
161161
flags.Bool("warn-on-channel-limit", true, "This behavior is now thread safe and always on. This flag will be removed in a future release.")
162162
flags.MarkDeprecated("warn-on-channel-limit", "This behavior is now thread safe and always on. This flag will be removed in a future release.")
163163

@@ -343,6 +343,11 @@ func (o *ProxyRunOptions) Validate() error {
343343
}
344344
}
345345

346+
// Validate graceful shutdown timeout
347+
if o.GracefulShutdownTimeout < 0 {
348+
return fmt.Errorf("graceful-shutdown-timeout must be >= 0, got %v", o.GracefulShutdownTimeout)
349+
}
350+
346351
o.NeedsKubernetesClient = usingServiceAccountAuth || o.EnableLeaseController
347352

348353
return nil
@@ -386,7 +391,7 @@ func NewProxyRunOptions() *ProxyRunOptions {
386391
EnableLeaseController: false,
387392
LeaseNamespace: "kube-system",
388393
LeaseLabel: "k8s-app=konnectivity-server",
389-
GracefulShutdownTimeout: 15 * time.Second,
394+
GracefulShutdownTimeout: 0,
390395
}
391396
return &o
392397
}

cmd/server/app/options/options_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestDefaultServerOptions(t *testing.T) {
6363
assertDefaultValue(t, "CipherSuites", defaultServerOptions.CipherSuites, make([]string, 0))
6464
assertDefaultValue(t, "XfrChannelSize", defaultServerOptions.XfrChannelSize, 10)
6565
assertDefaultValue(t, "APIContentType", defaultServerOptions.APIContentType, "application/vnd.kubernetes.protobuf")
66+
assertDefaultValue(t, "GracefulShutdownTimeout", defaultServerOptions.GracefulShutdownTimeout, 0*time.Second)
6667

6768
}
6869

@@ -168,6 +169,21 @@ func TestValidate(t *testing.T) {
168169
value: -10,
169170
expected: fmt.Errorf("channel size -10 must be greater than 0"),
170171
},
172+
"NegativeGracefulShutdownTimeout": {
173+
field: "GracefulShutdownTimeout",
174+
value: -1 * time.Second,
175+
expected: fmt.Errorf("graceful-shutdown-timeout must be >= 0, got -1s"),
176+
},
177+
"ZeroGracefulShutdownTimeout": {
178+
field: "GracefulShutdownTimeout",
179+
value: 0 * time.Second,
180+
expected: nil,
181+
},
182+
"PositiveGracefulShutdownTimeout": {
183+
field: "GracefulShutdownTimeout",
184+
value: 30 * time.Second,
185+
expected: nil,
186+
},
171187
} {
172188
t.Run(desc, func(t *testing.T) {
173189
testServerOptions := NewProxyRunOptions()
@@ -184,6 +200,10 @@ func TestValidate(t *testing.T) {
184200
case reflect.Int:
185201
ivalue := tc.value.(int)
186202
fv.SetInt(int64(ivalue))
203+
case reflect.Int64:
204+
if duration, ok := tc.value.(time.Duration); ok {
205+
fv.SetInt(int64(duration))
206+
}
187207
}
188208
}
189209
actual := testServerOptions.Validate()

cmd/server/app/server.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,28 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
186186
}
187187

188188
<-stopCh
189-
klog.V(1).Infoln("Received shutdown signal, initiating graceful shutdown.")
189+
klog.V(1).Infoln("Shutting down server.")
190+
191+
// If graceful shutdown timeout is 0, use the old behavior (immediate shutdown)
192+
if o.GracefulShutdownTimeout == 0 {
193+
if frontendStop != nil {
194+
if err := frontendStop(context.Background()); err != nil {
195+
klog.ErrorS(err, "failed to stop frontend server")
196+
}
197+
}
198+
if p.agentServer != nil {
199+
p.agentServer.Stop()
200+
}
201+
if p.adminServer != nil {
202+
p.adminServer.Close()
203+
}
204+
if p.healthServer != nil {
205+
p.healthServer.Close()
206+
}
207+
return nil
208+
}
209+
210+
klog.V(1).Infoln("Initiating graceful shutdown.")
190211

191212
// Start graceful shutdown with timeout
192213
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), o.GracefulShutdownTimeout)
@@ -195,9 +216,21 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
195216
// Create a WaitGroup to track shutdown of all components
196217
var wg sync.WaitGroup
197218

198-
// Shutdown frontend server gracefully (if available)
219+
// Add all workers to WaitGroup upfront
199220
if frontendStop != nil {
200221
wg.Add(1)
222+
}
223+
wg.Add(3) // agent, admin, health servers
224+
225+
// Create completion channel before starting goroutines
226+
shutdownComplete := make(chan struct{})
227+
go func() {
228+
wg.Wait()
229+
close(shutdownComplete)
230+
}()
231+
232+
// Shutdown frontend server gracefully (if available)
233+
if frontendStop != nil {
201234
go func() {
202235
defer wg.Done()
203236
klog.V(1).Infoln("Gracefully stopping frontend server...")
@@ -210,7 +243,6 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
210243
}
211244

212245
// Shutdown agent server gracefully
213-
wg.Add(1)
214246
go func() {
215247
defer wg.Done()
216248
klog.V(1).Infoln("Gracefully stopping agent server...")
@@ -219,20 +251,17 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
219251
}()
220252

221253
// Shutdown admin server gracefully
222-
wg.Add(1)
223254
go func() {
224255
defer wg.Done()
225256
klog.V(1).Infoln("Gracefully stopping admin server...")
226257
if err := p.adminServer.Shutdown(shutdownCtx); err != nil {
227258
klog.ErrorS(err, "failed to shut down admin server")
228-
229259
} else {
230260
klog.V(1).Infoln("admin server stopped.")
231261
}
232262
}()
233263

234264
// Shutdown health server gracefully
235-
wg.Add(1)
236265
go func() {
237266
defer wg.Done()
238267
klog.V(1).Infoln("Gracefully stopping health server...")
@@ -244,12 +273,6 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
244273
}()
245274

246275
// Wait for all servers to shutdown or timeout
247-
shutdownComplete := make(chan struct{})
248-
go func() {
249-
wg.Wait()
250-
close(shutdownComplete)
251-
}()
252-
253276
select {
254277
case <-shutdownComplete:
255278
klog.V(1).Infoln("Graceful shutdown completed successfully.")
@@ -268,8 +291,6 @@ func (p *Proxy) Run(o *options.ProxyRunOptions, stopCh <-chan struct{}) error {
268291
// frontend server's force-stop is handled by its StopFunc
269292
}
270293

271-
klog.V(1).Infoln("Server shutdown complete.")
272-
273294
return nil
274295
}
275296

0 commit comments

Comments
 (0)