/// <summary> /// Add a <see cref="KubeApiClient"/> to the service collection. /// </summary> /// <param name="services"> /// The service collection to configure. /// </param> /// <param name="usePodServiceAccount"> /// Configure the client to use the service account for the current Pod? /// </param> public static void AddKubeClient(this IServiceCollection services, bool usePodServiceAccount = false) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (usePodServiceAccount) { // When running inside Kubernetes, use pod-level service account (e.g. access token from mounted Secret). KubeApiClient ResolveWithPodServiceAccount(IServiceProvider serviceProvider) { return(KubeApiClient.CreateFromPodServiceAccount( loggerFactory: serviceProvider.GetService <ILoggerFactory>() )); } services.AddScoped <KubeApiClient>(ResolveWithPodServiceAccount); services.AddScoped <IKubeApiClient>(ResolveWithPodServiceAccount); } else { KubeApiClient ResolveWithOptions(IServiceProvider serviceProvider) { KubeClientOptions options = serviceProvider.GetRequiredService <IOptions <KubeClientOptions> >().Value; return(KubeApiClient.Create(options, loggerFactory: serviceProvider.GetService <ILoggerFactory>() )); } services.AddScoped <KubeApiClient>(ResolveWithOptions); services.AddScoped <IKubeApiClient>(ResolveWithOptions); } }
/// <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); }
/// <summary> /// Resolve the Kubernetes API client with the specified name. /// </summary> /// <param name="name"> /// The client name. /// </param> /// <returns> /// The resolved <see cref="KubeApiClient"/>. /// </returns> public IKubeApiClient Get(string name) { if (String.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'name'.", nameof(name)); } KubeClientOptions clientOptions = ServiceProvider.GetRequiredService <IOptionsMonitor <KubeClientOptions> >().Get(name); if (clientOptions == null) { throw new InvalidOperationException($"Cannot resolve a {nameof(KubeClientOptions)} instance named '{name}'."); } clientOptions.LoggerFactory = ServiceProvider.GetService <ILoggerFactory>(); return(KubeApiClient.Create(clientOptions)); }