Skip to content

Commit bc11d3a

Browse files
authored
httpclient to support different timeout for watch and regular api (#725)
* api for httpclient timeout * testcases for global timeout * update timeout to honor token * generated files
1 parent 78387dc commit bc11d3a

File tree

5 files changed

+3435
-2
lines changed

5 files changed

+3435
-2
lines changed

gen/KubernetesGenerator/Kubernetes.cs.template

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ namespace k8s
319319
Dictionary<string, List<string>> customHeaders = null,
320320
CancellationToken cancellationToken = default(CancellationToken))
321321
{
322+
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
323+
cts.CancelAfter(HttpClientTimeout);
324+
{{#IfParamCotains operation "watch"}}
325+
if (watch == true)
326+
{
327+
cts.CancelAfter(Timeout.InfiniteTimeSpan);
328+
}
329+
{{/IfParamCotains operation "watch"}}
330+
cancellationToken = cts.Token;
331+
322332
{{#operation.parameters}}
323333
{{#isRequired}}
324334
if ({{GetDotNetName name}} == null)

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Reflection;
99
using System.Runtime.InteropServices;
1010
using System.Security.Cryptography.X509Certificates;
11+
using System.Threading;
1112
using k8s.Exceptions;
1213
using k8s.Models;
1314
using Microsoft.Rest;
@@ -16,6 +17,13 @@ namespace k8s
1617
{
1718
public partial class Kubernetes
1819
{
20+
/// <summary>
21+
/// Timeout of REST calls to Kubernetes server
22+
/// Does not apply to watch related api
23+
/// </summary>
24+
/// <value>timeout</value>
25+
public TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
26+
1927
/// <summary>
2028
/// Initializes a new instance of the <see cref="Kubernetes" /> class.
2129
/// </summary>
@@ -69,6 +77,7 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
6977
SkipTlsVerify = config.SkipTlsVerify;
7078
CreateHttpClient(handlers, config);
7179
InitializeFromConfig(config);
80+
HttpClientTimeout = config.HttpClientTimeout;
7281
}
7382

7483
private void ValidateConfig(KubernetesClientConfiguration config)
@@ -247,7 +256,10 @@ private void CreateHttpClient(DelegatingHandler[] handlers, KubernetesClientConf
247256
}
248257
}
249258

250-
HttpClient = new HttpClient(FirstMessageHandler, false);
259+
HttpClient = new HttpClient(FirstMessageHandler, false)
260+
{
261+
Timeout = Timeout.InfiniteTimeSpan,
262+
};
251263
}
252264

253265
/// <summary>

src/KubernetesClient/KubernetesClientConfiguration.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Runtime.InteropServices;
34
using System.Security.Cryptography.X509Certificates;
@@ -79,8 +80,25 @@ public partial class KubernetesClientConfiguration
7980
/// <value>The access token.</value>
8081
public string AccessToken { get; set; }
8182

83+
/// <summary>
84+
/// Gets or sets the TokenProvider for authentication.
85+
/// </summary>
86+
/// <value>The access token.</value>
8287
public ITokenProvider TokenProvider { get; set; }
8388

89+
/// <summary>
90+
/// Set true to enable tcp keep alive
91+
/// You have to set https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html as well
92+
/// </summary>
93+
/// <value>true or false</value>
8494
public bool TcpKeepAlive { get; set; } = true;
95+
96+
97+
/// <summary>
98+
/// Timeout of REST calls to Kubernetes server
99+
/// Does not apply to watch related api
100+
/// </summary>
101+
/// <value>timeout</value>
102+
public TimeSpan HttpClientTimeout { get; set; } = TimeSpan.FromSeconds(100);
85103
}
86104
}

0 commit comments

Comments
 (0)