From 7193936e4078ae199e20771fee40297610e8c983 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Tue, 7 Nov 2017 20:23:27 +0800 Subject: [PATCH] watcher handler will auto patch to all ctor path --- ...netes.Auth.cs => Kubernetes.ConfigInit.cs} | 99 +++++++++++-------- 1 file changed, 60 insertions(+), 39 deletions(-) rename src/{Kubernetes.Auth.cs => Kubernetes.ConfigInit.cs} (57%) diff --git a/src/Kubernetes.Auth.cs b/src/Kubernetes.ConfigInit.cs similarity index 57% rename from src/Kubernetes.Auth.cs rename to src/Kubernetes.ConfigInit.cs index 987efad..cb01062 100644 --- a/src/Kubernetes.Auth.cs +++ b/src/Kubernetes.ConfigInit.cs @@ -1,38 +1,36 @@ using k8s.Models; +using System; +using System.Diagnostics.CodeAnalysis; +using System.Net.Http; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using k8s.Exceptions; +using Microsoft.Rest; namespace k8s { - using System; - using System.Diagnostics.CodeAnalysis; - using System.Net.Http; - using System.Net.Security; - using System.Security.Cryptography.X509Certificates; - using System.Threading.Tasks; - using k8s.Exceptions; - using Microsoft.Rest; - - public partial class Kubernetes : ServiceClient, IKubernetes + public partial class Kubernetes { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// - /// Optional. The delegating handlers to add to the http client pipeline. + /// Optional. The delegating handlers to add to the http client pipeline. /// - public Kubernetes(KubernetesClientConfiguration config) + /// + /// Optional. The delegating handlers to add to the http client pipeline. + /// + public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler[] handlers) : this(handlers) { - this.Initialize(); - - this.CaCert = config.SslCaCert; - this.BaseUri = new Uri(config.Host); - - var handler = new HttpClientHandler(); + CaCert = config.SslCaCert; + BaseUri = new Uri(config.Host); if (BaseUri.Scheme == "https") { if (config.SkipTlsVerify) { - handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + HttpClientHandler.ServerCertificateCustomValidationCallback = + (sender, certificate, chain, sslPolicyErrors) => true; } else { @@ -41,21 +39,47 @@ namespace k8s throw new KubeConfigException("a CA must be set when SkipTlsVerify === false"); } - handler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack; + HttpClientHandler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack; } } // set credentails for the kubernernet client - this.SetCredentials(config, handler); - this.InitializeHttpClient(handler, new DelegatingHandler[]{new WatcherDelegatingHandler()}); - - DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter()); + SetCredentials(config, HttpClientHandler); } - private X509Certificate2 CaCert { get; set; } + private X509Certificate2 CaCert { get; } + + partial void CustomInitialize() + { + AppendDelegatingHandler(); + DeserializationSettings.Converters.Add(new V1Status.V1StatusObjectViewConverter()); + } + + private void AppendDelegatingHandler() 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; + } + } /// - /// Set credentials for the Client + /// Set credentials for the Client /// /// k8s client configuration /// http client handler for the rest client @@ -88,7 +112,7 @@ namespace k8s } /// - /// SSl Cert Validation Callback + /// SSl Cert Validation Callback /// /// sender /// client certificate @@ -97,10 +121,10 @@ namespace k8s /// true if valid cert [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "Unused by design")] private bool CertificateValidationCallBack( - object sender, - X509Certificate certificate, - X509Chain chain, - SslPolicyErrors sslPolicyErrors) + object sender, + X509Certificate certificate, + X509Chain chain, + SslPolicyErrors sslPolicyErrors) { // If the certificate is a valid, signed certificate, return true. if (sslPolicyErrors == SslPolicyErrors.None) @@ -114,16 +138,13 @@ namespace k8s chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; // add all your extra certificate chain - chain.ChainPolicy.ExtraStore.Add(this.CaCert); + chain.ChainPolicy.ExtraStore.Add(CaCert); chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority; - var isValid = chain.Build((X509Certificate2)certificate); + var isValid = chain.Build((X509Certificate2) certificate); return isValid; } - else - { - // In all other cases, return false. - return false; - } + // In all other cases, return false. + return false; } } }