CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-fabric8--kubernetes-client

Java client for Kubernetes and OpenShift providing access to the full Kubernetes & OpenShift REST APIs via a fluent DSL

Pending
Overview
Eval results
Files

client-configuration.mddocs/

Client Configuration

The Fabric8 Kubernetes Client provides flexible configuration options for connecting to Kubernetes clusters. Configuration can be auto-discovered from kubeconfig files, environment variables, or explicitly set through the Config and ConfigBuilder classes.

Core Configuration Classes

Config Class

public class Config {
    // Factory methods
    public static Config empty();
    public static Config autoConfigure(String context);
    public static Config fromKubeconfig(String kubeconfigContents);
    public static Config fromKubeconfig(File kubeconfigFile);
    public static Config fromKubeconfig(String context, File kubeconfig);
    public static ConfigBuilder builder();
    
    // Connection settings
    public String getMasterUrl();
    public String getApiVersion();
    public String getNamespace();
    
    // Authentication
    public String getUsername();
    public String getPassword();
    public String getOauthToken();
    public String getCaCertFile();
    public String getCaCertData();
    public String getClientCertFile();
    public String getClientCertData();
    public String getClientKeyFile();
    public String getClientKeyData();
    public String getClientKeyAlgo();
    public String getClientKeyPassphrase();
    
    // Timeouts and limits
    public int getConnectionTimeout();
    public int getRequestTimeout();
    public int getUploadRequestTimeout();
    public int getWatchReconnectInterval();
    public int getWatchReconnectLimit();
    public long getWebsocketPingInterval();
    
    // Security settings
    public Boolean isTrustCerts();
    public Boolean isDisableHostnameVerification();
    
    // HTTP settings
    public String getHttpProxy();
    public String getHttpsProxy();
    public String[] getNoProxy();
    public String getProxyUsername();
    public String getProxyPassword();
    
    // Configuration management
    public Config refresh();
    public File getFile();
    public File getFileWithCluster();
    public File getFileWithAuthInfo();
    public List<NamedContext> getContexts();
    public NamedContext getCurrentContext();
    public void setCurrentContext(NamedContext context);
}

ConfigBuilder Class

public class ConfigBuilder {
    // Connection configuration
    public ConfigBuilder withMasterUrl(String masterUrl);
    public ConfigBuilder withApiVersion(String apiVersion);
    public ConfigBuilder withNamespace(String namespace);
    
    // Authentication
    public ConfigBuilder withUsername(String username);
    public ConfigBuilder withPassword(String password);
    public ConfigBuilder withOauthToken(String oauthToken);
    public ConfigBuilder withCaCertFile(String caCertFile);
    public ConfigBuilder withCaCertData(String caCertData);
    public ConfigBuilder withClientCertFile(String clientCertFile);
    public ConfigBuilder withClientCertData(String clientCertData);
    public ConfigBuilder withClientKeyFile(String clientKeyFile);
    public ConfigBuilder withClientKeyData(String clientKeyData);
    public ConfigBuilder withClientKeyAlgo(String clientKeyAlgo);
    public ConfigBuilder withClientKeyPassphrase(String clientKeyPassphrase);
    
    // Timeout configuration
    public ConfigBuilder withConnectionTimeout(int connectionTimeout);
    public ConfigBuilder withRequestTimeout(int requestTimeout);
    public ConfigBuilder withUploadRequestTimeout(int uploadRequestTimeout);
    public ConfigBuilder withWatchReconnectInterval(int watchReconnectInterval);
    public ConfigBuilder withWatchReconnectLimit(int watchReconnectLimit);
    public ConfigBuilder withWebsocketPingInterval(long websocketPingInterval);
    
    // Security configuration
    public ConfigBuilder withTrustCerts(Boolean trustCerts);
    public ConfigBuilder withDisableHostnameVerification(Boolean disableHostnameVerification);
    
    // HTTP proxy configuration
    public ConfigBuilder withHttpProxy(String httpProxy);
    public ConfigBuilder withHttpsProxy(String httpsProxy);
    public ConfigBuilder withNoProxy(String[] noProxy);
    public ConfigBuilder withProxyUsername(String proxyUsername);
    public ConfigBuilder withProxyPassword(String proxyPassword);
    
    // Build the configuration
    public Config build();
}

Client Builder

KubernetesClientBuilder Class

public class KubernetesClientBuilder {
    public KubernetesClientBuilder withConfig(Config config);
    public KubernetesClientBuilder withConfig(String config);
    public KubernetesClientBuilder withConfig(InputStream config);
    public KubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory);
    public KubernetesClientBuilder withTaskExecutor(Executor executor);
    public KubernetesClientBuilder withTaskExecutorSupplier(ExecutorSupplier executorSupplier);
    public KubernetesClientBuilder withHttpClientBuilderConsumer(Consumer<HttpClient.Builder> consumer);
    public KubernetesClientBuilder withKubernetesSerialization(KubernetesSerialization kubernetesSerialization);
    public KubernetesClient build();
}

Usage Examples

Auto-Configuration

The simplest way to create a client using auto-discovered configuration:

try (KubernetesClient client = new KubernetesClientBuilder().build()) {
    // Client automatically discovers configuration from:
    // 1. System properties
    // 2. Environment variables
    // 3. ~/.kube/config file
    // 4. Service account token (when running in cluster)
    
    String namespace = client.getConfiguration().getNamespace();
    System.out.println("Connected to cluster, default namespace: " + namespace);
}

Custom Configuration

Creating a client with explicit configuration:

Config config = new ConfigBuilder()
    .withMasterUrl("https://kubernetes.example.com:6443")
    .withNamespace("my-namespace")
    .withOauthToken("my-token")
    .withTrustCerts(true)
    .withConnectionTimeout(10000)
    .withRequestTimeout(30000)
    .build();

try (KubernetesClient client = new KubernetesClientBuilder()
    .withConfig(config)
    .build()) {
    
    // Use the client with custom configuration
    PodList pods = client.pods().list();
}

Configuration from Kubeconfig

Loading configuration from a specific kubeconfig file:

Config config = Config.fromKubeconfig("/path/to/kubeconfig");

try (KubernetesClient client = new KubernetesClientBuilder()
    .withConfig(config)
    .build()) {
    
    // Client uses the specified kubeconfig
    NodeList nodes = client.nodes().list();
}

Certificate-Based Authentication

Configuring client certificate authentication:

Config config = new ConfigBuilder()
    .withMasterUrl("https://kubernetes.example.com:6443")
    .withCaCertFile("/path/to/ca.crt")
    .withClientCertFile("/path/to/client.crt")
    .withClientKeyFile("/path/to/client.key")
    .withClientKeyPassphrase("key-passphrase")
    .build();

try (KubernetesClient client = new KubernetesClientBuilder()
    .withConfig(config)
    .build()) {
    
    // Client uses certificate authentication
    ServiceList services = client.services().list();
}

Proxy Configuration

Configuring HTTP proxy settings:

Config config = new ConfigBuilder()
    .withMasterUrl("https://kubernetes.example.com:6443")
    .withHttpProxy("http://proxy.example.com:8080")
    .withHttpsProxy("https://proxy.example.com:8080")
    .withNoProxy(new String[]{"localhost", "127.0.0.1", "*.local"})
    .withProxyUsername("proxy-user")
    .withProxyPassword("proxy-password")
    .build();

try (KubernetesClient client = new KubernetesClientBuilder()
    .withConfig(config)
    .build()) {
    
    // Client uses proxy settings
    NamespaceList namespaces = client.namespaces().list();
}

Timeout Configuration

Configuring various timeout settings:

Config config = new ConfigBuilder()
    .withConnectionTimeout(15000)     // 15 seconds to establish connection
    .withRequestTimeout(60000)        // 60 seconds for regular requests
    .withUploadRequestTimeout(300000) // 5 minutes for upload requests
    .withWatchReconnectInterval(1000) // 1 second between watch reconnect attempts
    .withWatchReconnectLimit(10)      // Maximum 10 reconnect attempts
    .withWebsocketPingInterval(30000) // 30 seconds between websocket pings
    .build();

try (KubernetesClient client = new KubernetesClientBuilder()
    .withConfig(config)
    .build()) {
    
    // Client uses custom timeout settings
    ConfigMapList configMaps = client.configMaps().list();
}

Configuration Sources

The client configuration is auto-discovered from multiple sources in the following order of precedence:

  1. System Properties: Java system properties starting with kubernetes.
  2. Environment Variables: Environment variables like KUBERNETES_MASTER, KUBERNETES_NAMESPACE, etc.
  3. Kubeconfig File: Default location ~/.kube/config or specified by KUBECONFIG environment variable
  4. Service Account: When running inside a Kubernetes pod, uses the mounted service account token

Environment Variables

Common environment variables for configuration:

  • KUBERNETES_MASTER - Kubernetes API server URL
  • KUBERNETES_NAMESPACE - Default namespace
  • KUBERNETES_AUTH_TOKEN - Authentication token
  • KUBERNETES_CA_CERTIFICATE_FILE - CA certificate file path
  • KUBERNETES_CLIENT_CERTIFICATE_FILE - Client certificate file path
  • KUBERNETES_CLIENT_KEY_FILE - Client key file path
  • KUBERNETES_TRUST_CERTIFICATES - Trust all certificates (true/false)
  • KUBERNETES_DISABLE_HOSTNAME_VERIFICATION - Disable hostname verification (true/false)

System Properties

Corresponding system properties use the kubernetes. prefix:

  • kubernetes.master - Kubernetes API server URL
  • kubernetes.namespace - Default namespace
  • kubernetes.auth.token - Authentication token
  • kubernetes.certs.ca.file - CA certificate file path
  • kubernetes.certs.client.file - Client certificate file path
  • kubernetes.certs.client.key.file - Client key file path
  • kubernetes.trust.certificates - Trust all certificates
  • kubernetes.disable.hostname.verification - Disable hostname verification

Install with Tessl CLI

npx tessl i tessl/maven-io-fabric8--kubernetes-client

docs

api-groups.md

client-configuration.md

core-resources.md

custom-resources.md

exception-handling.md

index.md

pod-operations.md

utilities.md

watch-informers.md

tile.json