Class BaseCmdKubeClient<K extends BaseCmdKubeClient<K>>

java.lang.Object
io.skodjob.testframe.clients.cmdClient.BaseCmdKubeClient<K>
Type Parameters:
K - The subtype of KubeClient, for fluency.
All Implemented Interfaces:
KubeCmdClient<K>
Direct Known Subclasses:
Kubectl, Oc

public abstract class BaseCmdKubeClient<K extends BaseCmdKubeClient<K>> extends Object implements KubeCmdClient<K>
Abstract class representing a base Kubernetes command-line client.
  • Method Details

    • cmd

      public abstract String cmd()
      Abstract method to retrieve the command.
      Specified by:
      cmd in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Returns:
      The command string.
    • deleteByName

      public K deleteByName(String resourceType, String resourceName)
      Deletes a resource by its name.
      Specified by:
      deleteByName in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      resourceName - The name of the resource.
      Returns:
      The instance of the client.
    • get

      public String get(String resource, String resourceName)
      Retrieves the YAML representation of a resource.
      Specified by:
      get in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resource - The type of the resource.
      resourceName - The name of the resource.
      Returns:
      The YAML representation of the resource.
    • getEvents

      public String getEvents()
      Retrieves the events from the cluster.
      Specified by:
      getEvents in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Returns:
      The events as a string.
    • create

      public K create(File... files)
      Creates resources from files.
      Specified by:
      create in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      files - The files representing the resources.
      Returns:
      The instance of the client.
    • apply

      public K apply(File... files)
      Applies resources from files.
      Specified by:
      apply in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      files - The files representing the resources.
      Returns:
      The instance of the client.
    • delete

      public K delete(File... files)
      Deletes resources from files.
      Specified by:
      delete in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      files - The files representing the resources.
      Returns:
      The instance of the client.
    • replace

      public K replace(File... files)
      Replaces resources from files.
      Specified by:
      replace in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      files - The files representing the resources.
      Returns:
      The instance of the client.
    • applyContent

      public K applyContent(String yamlContent)
      Applies YAML content.
      Specified by:
      applyContent in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      yamlContent - The YAML content.
      Returns:
      The instance of the client.
    • deleteContent

      public K deleteContent(String yamlContent)
      Deletes YAML content.
      Specified by:
      deleteContent in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      yamlContent - The YAML content.
      Returns:
      The instance of the client.
    • createNamespace

      public K createNamespace(String name)
      Creates a namespace.
      Specified by:
      createNamespace in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      name - The name of the namespace.
      Returns:
      The instance of the client.
    • deleteNamespace

      public K deleteNamespace(String name)
      Deletes a namespace.
      Specified by:
      deleteNamespace in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      name - The name of the namespace.
      Returns:
      The instance of the client.
    • scaleByName

      public K scaleByName(String kind, String name, int replicas)
      Scales resources by name.
      Specified by:
      scaleByName in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      kind - The kind of the resource.
      name - The name of the resource.
      replicas - The number of replicas.
      Returns:
      The instance of the client.
    • execInPod

      public ExecResult execInPod(String pod, String... command)
      Executes a command in a pod.
      Specified by:
      execInPod in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      pod - The name of the pod.
      command - The command to execute.
      Returns:
      The execution result.
    • execInPodContainer

      public ExecResult execInPodContainer(String pod, String container, String... command)
      Executes a command in a pod container.
      Specified by:
      execInPodContainer in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      pod - The name of the pod.
      container - The name of the container.
      command - The command to execute.
      Returns:
      The execution result.
    • execInPodContainer

      public ExecResult execInPodContainer(boolean logToOutput, String pod, String container, String... command)
      Executes a command in a pod container with the option to log to output.
      Specified by:
      execInPodContainer in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      logToOutput - Whether to log the output.
      pod - The name of the pod.
      container - The name of the container.
      command - The command to execute.
      Returns:
      The execution result.
    • exec

      public ExecResult exec(String... command)
      Executes a command.
      Specified by:
      exec in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      command - The command to execute.
      Returns:
      The execution result.
    • exec

      public ExecResult exec(boolean throwError, String... command)
      Executes a command with the option to throw errors.
      Specified by:
      exec in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      throwError - Whether to throw errors.
      command - The command to execute.
      Returns:
      The execution result.
    • exec

      public ExecResult exec(boolean throwError, boolean logToOutput, String... command)
      Executes a command with the option to throw errors and log to output.
      Specified by:
      exec in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      throwError - Whether to throw errors.
      logToOutput - Whether to log the output.
      command - The command to execute.
      Returns:
      The execution result.
    • exec

      public ExecResult exec(boolean throwError, boolean logToOutput, int timeout, String... command)
      Executes a command with the option to throw errors and log to output.
      Specified by:
      exec in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      throwError - Whether to throw errors.
      logToOutput - Whether to log the output.
      timeout - timeout in milis
      command - The command to execute.
      Returns:
      The execution result.
    • toString

      public String toString()
      Converts the object to a string representation.
      Overrides:
      toString in class Object
      Returns:
      The string representation of the object.
    • list

      public List<String> list(String resourceType)
      Lists resources of a certain type.
      Specified by:
      list in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      Returns:
      A list of resource names.
    • getResourceAsJson

      public String getResourceAsJson(String resourceType, String resourceName)
      Retrieves a resource as JSON.
      Specified by:
      getResourceAsJson in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      resourceName - The name of the resource.
      Returns:
      The resource as JSON.
    • getResourceAsYaml

      public String getResourceAsYaml(String resourceType, String resourceName)
      Retrieves a resource as YAML.
      Specified by:
      getResourceAsYaml in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      resourceName - The name of the resource.
      Returns:
      The resource as YAML.
    • getResourcesAsYaml

      public String getResourcesAsYaml(String resourceType)
      Retrieves resources as YAML.
      Specified by:
      getResourcesAsYaml in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      Returns:
      The resources as YAML.
    • createResourceAndApply

      public void createResourceAndApply(String template, Map<String,String> params)
      Creates a resource from a template and applies it.
      Specified by:
      createResourceAndApply in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      template - The template file.
      params - The parameters for the template.
    • describe

      public String describe(String resourceType, String resourceName)
      Retrieves the description of a resource.
      Specified by:
      describe in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      resourceName - The name of the resource.
      Returns:
      The description of the resource.
    • logs

      public String logs(String pod, String container)
      Retrieves logs of a pod/container.
      Specified by:
      logs in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      pod - The name of the pod.
      container - The name of the container.
      Returns:
      The logs as a string.
    • searchInLog

      public String searchInLog(String resourceType, String resourceName, long sinceSeconds, String... grepPattern)
      Searches for patterns in logs.
      Specified by:
      searchInLog in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      resourceName - The name of the resource.
      sinceSeconds - The duration since the logs should be searched.
      grepPattern - The patterns to search for.
      Returns:
      The matching lines from logs.
    • searchInLog

      public String searchInLog(String resourceType, String resourceName, String resourceContainer, long sinceSeconds, String... grepPattern)
      Searches for patterns in logs of a specific container.
      Specified by:
      searchInLog in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      resourceName - The name of the resource.
      resourceContainer - The name of the container.
      sinceSeconds - The duration since the logs should be searched.
      grepPattern - The patterns to search for.
      Returns:
      The matching lines from logs.
    • listResourcesByLabel

      public List<String> listResourcesByLabel(String resourceType, String label)
      Lists resources by label.
      Specified by:
      listResourcesByLabel in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      resourceType - The type of the resource.
      label - The label.
      Returns:
      A list of resource names.
    • process

      public K process(Map<String,String> parameters, String file, Consumer<String> c)
      Processes a template file with parameters.
      Specified by:
      process in interface KubeCmdClient<K extends BaseCmdKubeClient<K>>
      Parameters:
      parameters - The parameters for the template.
      file - The template file.
      c - The consumer to process the output.
      Returns:
      The instance of the client.