/// <summary>
        ///     Add a <see cref="KubeApiClient"/> to the service collection.
        /// </summary>
        /// <param name="services">
        ///     The service collection to configure.
        /// </param>
        /// <param name="options">
        ///     <see cref="KubeClientOptions"/> containing the client configuration to use.
        /// </param>
        /// <returns>
        ///     The configured service collection.
        /// </returns>
        public static IServiceCollection AddKubeClient(this IServiceCollection services, KubeClientOptions options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.EnsureValid();

            KubeApiClient ResolveWithOptions(IServiceProvider serviceProvider)
            {
                return(KubeApiClient.Create(options,
                                            loggerFactory: serviceProvider.GetService <ILoggerFactory>()
                                            ));
            }

            services.AddScoped <KubeApiClient>(ResolveWithOptions);
            services.AddScoped <IKubeApiClient>(ResolveWithOptions);

            return(services);
        }
Exemplo n.º 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();

            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));
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Create a new <see cref="KubeApiClient"/> (for testing purposes only).
        /// </summary>
        /// <param name="httpClient">
        ///     The <see cref="HttpClient"/> used to communicate with the Kubernetes API.
        /// </param>
        /// <param name="options">
        ///     The <see cref="KubeClientOptions"/> used to configure the <see cref="KubeApiClient"/>.
        /// </param>
        /// <param name="loggerFactory">
        ///     An optional <see cref="ILoggerFactory"/> used to create loggers for client components.
        /// </param>
        /// <returns>
        ///     The configured <see cref="KubeApiClient"/>.
        /// </returns>
        internal static KubeApiClient Create(HttpClient httpClient, KubeClientOptions options, ILoggerFactory loggerFactory = null)
        {
            if (httpClient == null)
            {
                throw new ArgumentNullException(nameof(httpClient));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (httpClient.BaseAddress == null)
            {
                throw new ArgumentException("The KubeApiClient's underlying HttpClient must specify a base address.", nameof(httpClient));
            }

            options.EnsureValid();

            return(new KubeApiClient(httpClient, options, loggerFactory));
        }
Exemplo n.º 4
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));
        }