Class KubeClient

java.lang.Object
io.skodjob.testframe.clients.KubeClient

public class KubeClient extends Object
Provides functionality to interact with Kubernetes and OpenShift clusters. This includes creating clients, reading resources from files, and managing kubeconfig for authentication.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes the Kubernetes client with configuration derived from environment variables or default context.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    create(String namespace, List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
    Creates resource and apply modifier
    void
    create(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
    Creates resource and apply modifier
    void
    createOrUpdate(String ns, List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
    Create or update resources from file and apply modifier
    void
    createOrUpdate(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
    Create or update resources from file and apply modifier
    void
    delete(List<io.fabric8.kubernetes.api.model.HasMetadata> resources)
    Deletes resources
    void
    delete(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, String namespace)
    Delete resources
    io.fabric8.kubernetes.client.KubernetesClient
    Returns the Kubernetes client.
    getDeploymentNameByPrefix(String namespace, String namePrefix)
    Returns list of deployments with prefix name
    Returns the path to the kubeconfig file used for authentication.
    getLogsFromContainer(String namespaceName, String podName, String containerName)
    Return log from pods specific container
    getLogsFromPod(String namespaceName, String podName)
    Return log from pod with one container
    io.fabric8.openshift.client.OpenShiftClient
    Adapts the Kubernetes client to an OpenShift client.
    List<io.fabric8.kubernetes.api.model.Pod>
    listPods(String namespaceName)
    Get all pods from namespace
    List<io.fabric8.kubernetes.api.model.Pod>
    listPods(String namespaceName, io.fabric8.kubernetes.api.model.LabelSelector selector)
    Get all pods with prefix nanme
    List<io.fabric8.kubernetes.api.model.Pod>
    listPodsByPrefixInName(String namespaceName, String podNamePrefix)
    Returns list of pods by prefix in pod name
    boolean
    Check if namespace exists in current cluster
    List<io.fabric8.kubernetes.api.model.HasMetadata>
    Reads Kubernetes resources from an InputStream.
    List<io.fabric8.kubernetes.api.model.HasMetadata>
    Reads Kubernetes resources from a file at the specified path.
    void
    update(String namespace, List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
    Updates resources and apply modifier
    void
    update(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
    Updates resources and apply modifier

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • KubeClient

      public KubeClient()
      Initializes the Kubernetes client with configuration derived from environment variables or default context.
  • Method Details

    • getClient

      public io.fabric8.kubernetes.client.KubernetesClient getClient()
      Returns the Kubernetes client.
      Returns:
      The initialized Kubernetes client.
    • getOpenShiftClient

      public io.fabric8.openshift.client.OpenShiftClient getOpenShiftClient()
      Adapts the Kubernetes client to an OpenShift client.
      Returns:
      An instance of OpenShiftClient.
    • getKubeconfigPath

      public String getKubeconfigPath()
      Returns the path to the kubeconfig file used for authentication.
      Returns:
      The kubeconfig file path.
    • readResourcesFromFile

      public List<io.fabric8.kubernetes.api.model.HasMetadata> readResourcesFromFile(Path file) throws IOException
      Reads Kubernetes resources from a file at the specified path.
      Parameters:
      file - The path to the file containing Kubernetes resources.
      Returns:
      A list of HasMetadata resources defined in the file.
      Throws:
      IOException - If an I/O error occurs reading from the file.
    • readResourcesFromFile

      public List<io.fabric8.kubernetes.api.model.HasMetadata> readResourcesFromFile(InputStream is) throws IOException
      Reads Kubernetes resources from an InputStream.
      Parameters:
      is - The InputStream containing Kubernetes resources.
      Returns:
      A list of HasMetadata resources defined in the stream.
      Throws:
      IOException - If an I/O error occurs.
    • namespaceExists

      public boolean namespaceExists(String namespace)
      Check if namespace exists in current cluster
      Parameters:
      namespace - namespace name
      Returns:
      true if namespace exists
    • create

      public void create(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
      Creates resource and apply modifier
      Parameters:
      resources - resources
      modifier - modifier
    • update

      public void update(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
      Updates resources and apply modifier
      Parameters:
      resources - resources
      modifier - modifier
    • create

      public void create(String namespace, List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
      Creates resource and apply modifier
      Parameters:
      namespace - namespace
      resources - resources
      modifier - modifier
    • update

      public void update(String namespace, List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
      Updates resources and apply modifier
      Parameters:
      namespace - namespace
      resources - resources
      modifier - modifier
    • createOrUpdate

      public void createOrUpdate(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
      Create or update resources from file and apply modifier
      Parameters:
      resources - resources
      modifier - modifier method
    • createOrUpdate

      public void createOrUpdate(String ns, List<io.fabric8.kubernetes.api.model.HasMetadata> resources, Function<io.fabric8.kubernetes.api.model.HasMetadata,io.fabric8.kubernetes.api.model.HasMetadata> modifier)
      Create or update resources from file and apply modifier
      Parameters:
      ns - namespace
      resources - resources
      modifier - modifier method
    • delete

      public void delete(List<io.fabric8.kubernetes.api.model.HasMetadata> resources)
      Deletes resources
      Parameters:
      resources - resources
    • delete

      public void delete(List<io.fabric8.kubernetes.api.model.HasMetadata> resources, String namespace)
      Delete resources
      Parameters:
      resources - resources
      namespace - namespace
    • listPods

      public List<io.fabric8.kubernetes.api.model.Pod> listPods(String namespaceName)
      Get all pods from namespace
      Parameters:
      namespaceName - namespace
      Returns:
      list of pods
    • listPods

      public List<io.fabric8.kubernetes.api.model.Pod> listPods(String namespaceName, io.fabric8.kubernetes.api.model.LabelSelector selector)
      Get all pods with prefix nanme
      Parameters:
      namespaceName - namespace
      selector - prefix
      Returns:
      lust of pods
    • listPodsByPrefixInName

      public List<io.fabric8.kubernetes.api.model.Pod> listPodsByPrefixInName(String namespaceName, String podNamePrefix)
      Returns list of pods by prefix in pod name
      Parameters:
      namespaceName - Namespace name
      podNamePrefix - prefix with which the name should begin
      Returns:
      List of pods
    • getLogsFromPod

      public String getLogsFromPod(String namespaceName, String podName)
      Return log from pod with one container
      Parameters:
      namespaceName - namespace of the pod
      podName - pod name
      Returns:
      logs
    • getLogsFromContainer

      public String getLogsFromContainer(String namespaceName, String podName, String containerName)
      Return log from pods specific container
      Parameters:
      namespaceName - namespace of the pod
      podName - pod name
      containerName - container name
      Returns:
      logs
    • getDeploymentNameByPrefix

      public String getDeploymentNameByPrefix(String namespace, String namePrefix)
      Returns list of deployments with prefix name
      Parameters:
      namespace - namespace
      namePrefix - prefix
      Returns:
      list of deployments