Execute on Pod

The Execute on Pod type is similar to using the kubectl exec command.

Execute On Pod Configuration Properties describes the properties available for Execute on Pod.

Table 1. Execute On Pod Configuration Properties
Property Description
command The command executed on the selected pods.
containerName (Optional) The name of the container to execute on if there is more than one container in a pod and a default is not set.
fileInjection[ ].files (Optional) A map of files that are added to the pod before the command is run.
fileInjection[ ].filePath (Optional) The path where the files are added to the pod.
fileInjection[ ].removeAfterUse (Optional) When set to true, files added to the pod are removed once the command completes.
fileExtractions[ ].path (Optional) The path where any file matching the associated pattern is extracted from the pod once the command completes.
fileExtractions[ ].pattern The pattern used to match files to extract from the pod post-execution.
Note: When using Execute on Pod, the resource selector must resolve to resources of type pod.

The following example executor definition runs the id command on selected pods, using sh:

executor:
  executeOnPod:
    command:
      - "bin/sh"
      - "-c"
      - "id"

The following example executor definition runs the top command in the main container on the selected pod(s) using bash:

executor:
  executeOnPod:
    containerName: main
    command:
      - "bin/bash"
      - "-c"
      - "top" 
Note: You must ensure that the Admin Service is given permission to execute commands on pods in the target namespace. For more information, see the discussion about the RBAC Execute On Pod example.

Sometimes the scripts that you want to execute on the pod are not available and must be injected first. A command can define these files inline using the fileInjections list. Each entry in this list refers to a map of files to inject at a specified location. Within this map, the key is the file name and the value is the file contents.

Files are automatically injected into a directory defined in the filePath attribute. If you do not specify a value. it defaults to /tmp/${command.execution.id}. The directory is created and the injected files are made executable.

Files are not removed from the pod and are overwritten if they already exist. To ensure the injected files are removed, set removeAfterUse to true.

The following example adds a script to the /tmp directory of the pod and executes it:

executor:
  executeOnPod:
    command:
      - "bin/bash"
      - "-c"
      - "/tmp/${command.execution.id}/hello-world.sh" 
    fileInjections:  
      - files:
          "hello-world.sh": |
            #!/bin/bash
            echo "Hello World from $(pwd) on ${HOSTNAME}"

The following example adds a script to a specified directory of the pod and executes it:

executor:
  executeOnPod:
    command:
      - "bin/bash"
      - "-c"
      - "/admin-command/hello-world.sh"
    fileInjections:
      - filePath: /admin-command
        files:
          "hello-world.sh": |
            #!/bin/bash
            echo "Hello World from ${HOSTNAME}"
            cat /admin-command/info.txt
          "info.txt": |
            Here is some text that was added in ${command.execution.id}

The outcome of a command executed on a pod can include the creation of one or more files that need to be transferred out of the cluster. A list of fileExtractions can be defined that allow files to be extracted from the pod after the command has been executed. Each entry in the list defines the path on the pod where the files are expected and a filename pattern to use when matching. Files are matched using shell matching syntax.

The following example extracts all text files from an output directory after the command has completed:

executor:
  executeOnPod:
    command:
      - "bin/bash"
      - "-c"
      - "text-file-generator.sh"
      - "/output"
    fileExtractions:
      - path: /output
        pattern: "*.txt"