Пример #1
0
        /// <summary>
        ///     Create a new <see cref="Startup"/>.
        /// </summary>
        /// <param name="configuration">
        ///     The application configuration.
        /// </param>
        public Startup(IConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            Configuration     = configuration;
            DatabaseOptions   = DatabaseOptions.From(Configuration);
            KubernetesOptions = KubernetesOptions.From(Configuration);
        }
Пример #2
0
        /// <summary>
        ///     Add a <see cref="KubeApiClient"/> to the service collection.
        /// </summary>
        /// <param name="services">
        ///     The service collection.
        /// </param>
        public static void AddKubeClient(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (IsKubernetes)
            {
                // When running inside Kubernetes, use pod-level service account (e.g. access token from mounted Secret).
                services.AddScoped <KubeClient.KubeApiClient>(
                    serviceProvider => KubeClient.KubeApiClient.CreateFromPodServiceAccount()
                    );
            }
            else
            {
                // For debugging purposes only.
                services.AddScoped <KubeClient.KubeApiClient>(serviceProvider =>
                {
                    KubernetesOptions kubeOptions = serviceProvider.GetRequiredService <IOptions <KubernetesOptions> >().Value;

                    if (String.IsNullOrWhiteSpace(kubeOptions.ApiEndPoint))
                    {
                        throw new InvalidOperationException("Application configuration is missing Kubernetes API end-point.");
                    }

                    if (String.IsNullOrWhiteSpace(kubeOptions.Token))
                    {
                        throw new InvalidOperationException("Application configuration is missing Kubernetes API token.");
                    }

                    return(KubeClient.KubeApiClient.Create(
                               endPointUri: new Uri(kubeOptions.ApiEndPoint),
                               accessToken: kubeOptions.Token
                               ));
                });
            }
        }