/// <summary>
        ///     Create and configure a <see cref="KubeApiClient"/> using the specified options.
        /// </summary>
        /// <param name="options">
        ///     The <see cref="KubeClientOptions"/> used to configure the client.
        /// </param>
        /// <param name="loggerFactory">
        ///     An optional <see cref="ILoggerFactory"/> used to create loggers for client components.
        /// </param>
        /// <returns>
        ///     The configured <see cref="KubeApiClient"/>.
        /// </returns>
        public static KubeApiClient Create(KubeClientOptions options, ILoggerFactory loggerFactory = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.EnsureValid();

            var clientBuilder = new ClientBuilder();

            if (!String.IsNullOrWhiteSpace(options.AccessToken))
            {
                clientBuilder = clientBuilder.AddHandler(
                    () => new BearerTokenHandler(options.AccessToken)
                    );
            }

            if (options.AllowInsecure)
            {
                clientBuilder = clientBuilder.AcceptAnyServerCertificate();
            }
            else if (options.CertificationAuthorityCertificate != null)
            {
                clientBuilder = clientBuilder.WithServerCertificate(options.CertificationAuthorityCertificate);
            }

            if (options.ClientCertificate != null)
            {
                clientBuilder = clientBuilder.WithClientCertificate(options.ClientCertificate);
            }

            if (loggerFactory != null)
            {
                LogMessageComponents logComponents = LogMessageComponents.Basic;
                if (options.LogHeaders)
                {
                    logComponents |= LogMessageComponents.Headers;
                }
                if (options.LogPayloads)
                {
                    logComponents |= LogMessageComponents.Body;
                }

                clientBuilder = clientBuilder.WithLogging(
                    logger: loggerFactory.CreateLogger(
                        typeof(KubeApiClient).FullName + ".Http"
                        ),
                    requestComponents: logComponents,
                    responseComponents: logComponents
                    );
            }

            HttpClient httpClient = clientBuilder.CreateClient(options.ApiEndPoint);

            return(new KubeApiClient(httpClient, options, loggerFactory));
        }
Example #2
0
        /// <summary>
        ///     Create and configure a <see cref="KubeApiClient"/> using the specified options.
        /// </summary>
        /// <param name="options">
        ///     The <see cref="KubeClientOptions"/> used to configure the client.
        /// </param>
        /// <param name="loggerFactory">
        ///     An optional <see cref="ILoggerFactory"/> used to create loggers for client components.
        /// </param>
        /// <returns>
        ///     The configured <see cref="KubeApiClient"/>.
        /// </returns>
        public static KubeApiClient Create(KubeClientOptions options, ILoggerFactory loggerFactory = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.EnsureValid();

            var clientBuilder = new ClientBuilder();

            switch (options.AuthStrategy)
            {
            case KubeAuthStrategy.BearerToken:
            {
                clientBuilder = clientBuilder.AddHandler(
                    () => new StaticBearerTokenHandler(options.AccessToken)
                    );

                break;
            }

            case KubeAuthStrategy.BearerTokenProvider:
            {
                clientBuilder = clientBuilder.AddHandler(
                    () => new CommandBearerTokenHandler(
                        accessTokenCommand: options.AccessTokenCommand,
                        accessTokenCommandArguments: options.AccessTokenCommandArguments,
                        accessTokenSelector: options.AccessTokenSelector,
                        accessTokenExpirySelector: options.AccessTokenExpirySelector,
                        initialAccessToken: options.InitialAccessToken,
                        initialTokenExpiryUtc: options.InitialTokenExpiryUtc
                        )
                    );

                break;
            }

            case KubeAuthStrategy.ClientCertificate:
            {
                if (options.ClientCertificate == null)
                {
                    throw new KubeClientException("Cannot specify ClientCertificate authentication strategy without supplying a client certificate.");
                }

                clientBuilder = clientBuilder.WithClientCertificate(options.ClientCertificate);

                break;
            }
            }

            if (options.AllowInsecure)
            {
                clientBuilder = clientBuilder.AcceptAnyServerCertificate();
            }
            else if (options.CertificationAuthorityCertificate != null)
            {
                clientBuilder = clientBuilder.WithServerCertificate(options.CertificationAuthorityCertificate);
            }

            if (loggerFactory != null)
            {
                LogMessageComponents logComponents = LogMessageComponents.Basic;
                if (options.LogHeaders)
                {
                    logComponents |= LogMessageComponents.Headers;
                }
                if (options.LogPayloads)
                {
                    logComponents |= LogMessageComponents.Body;
                }

                clientBuilder = clientBuilder.WithLogging(
                    logger: loggerFactory.CreateLogger(
                        typeof(KubeApiClient).FullName + ".Http"
                        ),
                    requestComponents: logComponents,
                    responseComponents: logComponents
                    );
            }

            HttpClient httpClient = clientBuilder.CreateClient(options.ApiEndPoint);

            return(new KubeApiClient(httpClient, options, loggerFactory));
        }