All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.kubernetes.client.KubernetesClientBuilder Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.fabric8.kubernetes.client;

import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;
import io.fabric8.kubernetes.client.utils.Utils;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * If no {@link Executor} or {@link ExecutorSupplier} is specified, a default {@link ExecutorSupplier} will
 * be used which creates an unbounded cached thread pool per client.
 */
public class KubernetesClientBuilder {

  @FunctionalInterface
  public interface ExecutorSupplier extends Supplier {

    default void onClose(Executor executor) {

    }

  }

  private Config config;
  private HttpClient.Factory factory;
  private Class clazz;
  private ExecutorSupplier executorSupplier;
  private Consumer builderConsumer;
  private KubernetesSerialization kubernetesSerialization = new KubernetesSerialization();

  public KubernetesClientBuilder() {
    // basically the same logic as in KubernetesResourceUtil for finding list types
    String className = "io.fabric8.kubernetes.client.impl.KubernetesClientImpl";
    try {
      clazz = (Class) Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (ClassNotFoundException | ClassCastException | NullPointerException e) {
      try {
        clazz = (Class) KubernetesClient.class.getClassLoader().loadClass(className);
      } catch (Exception ex) {
        throw KubernetesClientException.launderThrowable(ex);
      }
    }
  }

  KubernetesClientBuilder(Class clazz) {
    this.clazz = clazz;
  }

  public KubernetesClient build() {
    if (config == null) {
      config = new ConfigBuilder().build();
    }
    try {
      if (factory == null) {
        this.factory = HttpClientUtils.getHttpClientFactory();
      }
      HttpClient client = getHttpClient();
      return clazz.getConstructor(HttpClient.class, Config.class, ExecutorSupplier.class, KubernetesSerialization.class)
          .newInstance(client, config,
              executorSupplier, kubernetesSerialization);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
        | NoSuchMethodException | SecurityException e) {
      throw KubernetesClientException.launderThrowable(e);
    }
  }

  HttpClient getHttpClient() {
    HttpClient.Builder builder = factory.newBuilder(config);
    if (this.builderConsumer != null) {
      this.builderConsumer.accept(builder);
    }
    return builder.build();
  }

  public KubernetesClientBuilder withConfig(Config config) {
    this.config = config;
    return this;
  }

  public KubernetesClientBuilder withKubernetesSerialization(KubernetesSerialization kubernetesSerialization) {
    this.kubernetesSerialization = Utils.checkNotNull(kubernetesSerialization, "kubernetesSerialization must not be null");
    return this;
  }

  public KubernetesClientBuilder withConfig(String config) {
    this.config = kubernetesSerialization.unmarshal(config, Config.class);
    return this;
  }

  public KubernetesClientBuilder withConfig(InputStream config) {
    this.config = kubernetesSerialization.unmarshal(config, Config.class);
    return this;
  }

  public KubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory) {
    this.factory = factory;
    return this;
  }

  /**
   * Configure the client to use the given executor for async tasks, such as {@link ResourceEventHandler}
   * calls and writing to streams.
   * 

* Only override if you need more control over the number of task threads used by the kubernetes client. * * @return this builder */ public KubernetesClientBuilder withTaskExecutor(Executor executor) { this.executorSupplier = () -> executor; return this; } /** * Configure the client to use the given {@link ExecutorSupplier} for async tasks, such as {@link ResourceEventHandler} * calls and writing to streams. *

* There will be a call to {@link ExecutorSupplier#onClose(Executor)} when a client is closed. *

* Only override if you need more control over the number of task threads used by the kubernetes client. * * @return this builder */ public KubernetesClientBuilder withTaskExecutorSupplier(ExecutorSupplier executorSupplier) { this.executorSupplier = executorSupplier; return this; } /** * Provide additional configuration for the {@link HttpClient} that is created for this {@link KubernetesClient}. * * @param consumer to modify the {@link HttpClient.Builder} * @return this builder */ public KubernetesClientBuilder withHttpClientBuilderConsumer(Consumer consumer) { this.builderConsumer = consumer; return this; } public class ConfigNested extends ConfigFluent { private ConfigBuilder builder; private ConfigNested() { this.builder = new ConfigBuilder(this, config); } public KubernetesClientBuilder endConfig() { config = this.builder.build(); return KubernetesClientBuilder.this; } } public ConfigNested editOrNewConfig() { return new ConfigNested(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy