watcher handler will auto patch to all ctor path

This commit is contained in:
Boshi Lian
2017-11-07 20:23:27 +08:00
parent 8016b6ce32
commit 7193936e40

View File

@@ -1,17 +1,15 @@
using k8s.Models; using k8s.Models;
namespace k8s
{
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Net.Http; using System.Net.Http;
using System.Net.Security; using System.Net.Security;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using k8s.Exceptions; using k8s.Exceptions;
using Microsoft.Rest; using Microsoft.Rest;
public partial class Kubernetes : ServiceClient<Kubernetes>, IKubernetes namespace k8s
{
public partial class Kubernetes
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Kubernetes" /> class. /// Initializes a new instance of the <see cref="Kubernetes" /> class.
@@ -19,20 +17,20 @@ namespace k8s
/// <param name='config'> /// <param name='config'>
/// Optional. The delegating handlers to add to the http client pipeline. /// Optional. The delegating handlers to add to the http client pipeline.
/// </param> /// </param>
public Kubernetes(KubernetesClientConfiguration config) /// <param name="handlers">
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler[] handlers) : this(handlers)
{ {
this.Initialize(); CaCert = config.SslCaCert;
BaseUri = new Uri(config.Host);
this.CaCert = config.SslCaCert;
this.BaseUri = new Uri(config.Host);
var handler = new HttpClientHandler();
if (BaseUri.Scheme == "https") if (BaseUri.Scheme == "https")
{ {
if (config.SkipTlsVerify) if (config.SkipTlsVerify)
{ {
handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; HttpClientHandler.ServerCertificateCustomValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;
} }
else else
{ {
@@ -41,18 +39,44 @@ namespace k8s
throw new KubeConfigException("a CA must be set when SkipTlsVerify === false"); throw new KubeConfigException("a CA must be set when SkipTlsVerify === false");
} }
handler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack; HttpClientHandler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack;
} }
} }
// set credentails for the kubernernet client // set credentails for the kubernernet client
this.SetCredentials(config, handler); SetCredentials(config, HttpClientHandler);
this.InitializeHttpClient(handler, new DelegatingHandler[]{new WatcherDelegatingHandler()}); }
private X509Certificate2 CaCert { get; }
partial void CustomInitialize()
{
AppendDelegatingHandler<WatcherDelegatingHandler>();
DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter()); DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter());
} }
private X509Certificate2 CaCert { get; set; } private void AppendDelegatingHandler<T>() where T : DelegatingHandler, new()
{
var cur = FirstMessageHandler as DelegatingHandler;
while (cur != null)
{
var next = cur.InnerHandler as DelegatingHandler;
if (next == null)
{
// last one
// append watcher handler between to last handler
cur.InnerHandler = new T
{
InnerHandler = cur.InnerHandler
};
break;
}
cur = next;
}
}
/// <summary> /// <summary>
/// Set credentials for the Client /// Set credentials for the Client
@@ -114,16 +138,13 @@ namespace k8s
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
// add all your extra certificate chain // add all your extra certificate chain
chain.ChainPolicy.ExtraStore.Add(this.CaCert); chain.ChainPolicy.ExtraStore.Add(CaCert);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
var isValid = chain.Build((X509Certificate2) certificate); var isValid = chain.Build((X509Certificate2) certificate);
return isValid; return isValid;
} }
else
{
// In all other cases, return false. // In all other cases, return false.
return false; return false;
} }
} }
} }
}