Skip to content

Commit c641a21

Browse files
authored
Merge pull request #12554 from LingyanCao/dev/lingyancao/timeout
🐛 Fix timeout handling in GetAPIServerCertificateExpiry and DialContext
2 parents 4541443 + b2aca38 commit c641a21

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

controlplane/kubeadm/internal/proxy/dial.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,30 @@ func (d *Dialer) DialContextWithAddr(ctx context.Context, addr string) (net.Conn
8383
}
8484

8585
// DialContext creates proxied port-forwarded connections.
86-
// ctx is currently unused, but fulfils the type signature used by GRPC.
87-
func (d *Dialer) DialContext(_ context.Context, _ string, addr string) (net.Conn, error) {
86+
func (d *Dialer) DialContext(ctx context.Context, _ string, addr string) (net.Conn, error) {
87+
// Check if context is already cancelled or timed out
88+
select {
89+
case <-ctx.Done():
90+
return nil, errors.Wrap(ctx.Err(), "context cancelled before establishing connection")
91+
default:
92+
}
93+
8894
req := d.clientset.CoreV1().RESTClient().
8995
Post().
9096
Resource(d.proxy.Kind).
9197
Namespace(d.proxy.Namespace).
9298
Name(addr).
9399
SubResource("portforward")
94100

95-
dialer := spdy.NewDialer(d.upgrader, &http.Client{Transport: d.proxyTransport}, "POST", req.URL())
101+
httpClient := &http.Client{
102+
Transport: d.proxyTransport,
103+
}
104+
105+
if deadline, ok := ctx.Deadline(); ok {
106+
httpClient.Timeout = time.Until(deadline)
107+
}
108+
109+
dialer := spdy.NewDialer(d.upgrader, httpClient, "POST", req.URL())
96110

97111
// Create a new connection from the dialer.
98112
//

controlplane/kubeadm/internal/workload_cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ func (w *Workload) ClusterStatus(ctx context.Context) (ClusterStatus, error) {
318318

319319
// GetAPIServerCertificateExpiry returns the certificate expiry of the apiserver on the given node.
320320
func (w *Workload) GetAPIServerCertificateExpiry(ctx context.Context, kubeadmConfig *bootstrapv1.KubeadmConfig, nodeName string) (*time.Time, error) {
321+
// Create a context with 15 second timeout
322+
ctx, cancel := context.WithTimeoutCause(ctx, 15*time.Second, errors.New("timeout getting API server certificate expiry"))
323+
defer cancel()
324+
321325
// Create a proxy.
322326
p := proxy.Proxy{
323327
Kind: "pods",

0 commit comments

Comments
 (0)