Skip to main content

Automate Harness Kubernetes Delegate Setup

You can use a simple script to support GitOps scenarios where you want to name, configure, and install a Harness Kubernetes Delegate from a repo.

Developers often need to create Delegates in multiple cluster in their environments (DEV, UAT, SIT, STAGE, PROD, etc). This script method gives developers a quick alternative to using the manual process in the Harness Manager.

In this topic:

Before You Begin

Step 1: Download the Base Delegate

You will use one Harness Kubernetes Delegate as the base for all of the Delegates you generate automatically.

  1. In Harness, click Setup, and then click Harness Delegates.
  2. Click Download Delegate, and then click Kubernetes YAML. The Delegate settings appear.
  3. Enter a name for the Delegate, select a Profile (the default is named Primary), and click Download.The name is limited to ^[a-z0-9-]+$ because it is a StatefulSet name. Remember this when generating Delegates automatically.The harness-delegate-kubernetes.tar.gz file is downloaded.
  4. Extract the file by double-clicking it or using the command tar -zxvf harness-delegate-kubernetes.tar.gz.

The harness-delegate-kubernetes folder is where you will save your script. The folder contains the harness-delegate.yaml file for the Delegate. Your script will use this file to create additional Delegate YAML files.

Step 2: Create the Script

The script you will create uses harness-delegate.yaml to generate a new Delegate YAML file.

Each time you run the script, you will provide a new Delegate name.

Let's create the script:

  1. In the harness-delegate-kubernetes folder, create a file and paste in the following script:

    if [ $# -lt 1 ]; then
    echo "usage <name>"
    exit 0
    if [ -f $DST_FILE ]; then
    echo "File $DST_FILE exists. Exiting..."
    exit 1
    if [ ! -f ${TMP_FILE} ]; then
    echo "creating $TMP_FILE"
    dname=$(sed -n "1,/^.*^.* ??p" $TMP_FILE)
    sed -i -e "s/$dname/DELEGATENAME/" $TMP_FILE

    echo "creating $DST_FILE"
  2. Save the file and name it

  3. Open a Terminal and navigate to the harness-delegate-kubernetes folder where you saved your script.

  4. Enter the following command to make the file executable:

    chmod a+x

Now the script is ready to run.

Step 3: Create a New Delegate

When you use the script to generate a new Delegate YAML file, you will run the following command:

./ <name>

You will supply a name for the <name> parameter passed to the script.

There are many different conventions for naming the Delegate. Many customers use a combination of the following to create a unique Delegate name:

  • environment prefix
  • application name
  • namespace

The name is limited to ^[a-z0-9-]+$ because it is a StatefulSet name.

Here is an example using the name mydelegate-dev with

./ mydelegate-dev

The output is:

creating harness-delegate.tmp  
creating harness-mydelegate-dev.yaml

If you look in the harness-delegate-kubernetes folder you will see the new YAML file created using the name you provided: harness-mydelegate-dev.yaml.

If you open the file you will see that the Delegate spec uses the name you provided:

apiVersion: apps/v1
kind: StatefulSet
labels: harness-delegate lnfzrf mydelegate-dev
# Name must contain the six letter account identifier: lnfzrf
name: mydelegate-dev-lnfzrf
namespace: harness-delegate
replicas: 1
matchLabels: harness-delegate lnfzrf mydelegate-dev

A suffix such as -lnfzrf is a Harness convention for identifying the Harness account. Do not remove it.

Step 4: Deploy the Delegate

To install the new Delegate you created with the script, do the following:

  1. Log into your Kubernetes cluster.
  2. Use kubectl apply as shown below:
kubectl apply -f harness-mydelegate-dev.yaml

The output is:

namespace/harness-delegate created created
secret/mydelegate-dev-proxy created
statefulset.apps/mydelegate-dev-sdqvgx created

Step 5: Add the Script and File to Your Repo

You can now add the harness-delegate-kubernetes folder containing the script and harness-delegate.yaml to your repo.

Now your developers do not need to use the Harness Manager to create Delegates. They can use the script in your repo to create new Delegates and then install them in their target cluster.

Option 1: Deploy using the Script

If you want to use the script to both create and deploy the new Delegate, you can add the following line to the end the script:

kubectl apply -f $DST_FILE

Add this line before making the script executable, and then save the script and run chmod a+x to make it executable.

Now you can copy the harness-delegate-kubernetes folder containing the script and harness-delegate.yaml to your cluster and simply run script using the new Delegate name:

./ <name>

The new Delegate is created and deployed in the cluster.


  • Selectors and Scopes — Harness does not support a way to automatically add Selectors or Delegate Scope for Delegates. Currently, you must add these attributes to Delegate manually in the Harness Manager.
  • Delete a Delegate — To delete a Harness Delegate from your Kubernetes cluster, delete the StatefulSet for the Delegate. Deleting the pod without deleting the StatefulSet will result in the pod being recreated.
    For example, if you have the Delegate pod name mydelegate-vutpmk-0, you can delete the StatefulSet with the following command:
    $ kubectl delete statefulset -n harness-delegate mydelegate-vutpmk
    Note that the -0 suffix in the pod name is removed for the StatefulSet name.

Next Steps