How to create a Kubernetes Managed Master programmatically

Issue

  • How to create a Kubernetes Managed Master programmatically.

Environment

Resolution

Create Using Groovy

The easiest approach would be by using a Groovy script.

See createManagedMasterK8s.groovy. This script can be modified as needed.

If you need access to the source code then please see Custom Plugins: APIs and Javadocs of CloudBees Jenkins Enterprise plugins

The classes used in the provided script are coming from the operations-center-server plugin.

You can find your version of the plugin by looking at the Plugin Manager on the CJOC instance and searching for operations-center-server under the Installed tab.

Create via the Script Console

You can execute the groovy script using the Script Console under Managed Jenkins > Script Console.

Create via the Jenkins CLI

You can execute a groovy script via the groovy command and passing the script to the standard input:

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    groovy = < createManagedMasterK8s.groovy

Create via the REST API

You can execute a groovy script via the /scriptText endpoint and pass the script via a bash command:

curl -v -XPOST \
    -u $USER:$TOKEN \
    --data-urlencode "script=$(<./createManagedMasterK8s.groovy)" \
    "$CJOC_URL/scriptText"

See Script Console - Remote Access for more details.

Create remotely using config files

You can also create a Managed Master remotely, by either using the REST API, or by using the Jenkins CLI to create the job.

This approach is a bit cumbersome, as some fields are auto-generated (id, idName, grantId, identity, encodedName) and must be unique. This means that several calls must be made to adjust a master configuration.

You would need to do the following:

  • Create a job
  • Get the job config file
  • Update the config file (preserve auto-generated fields)
  • Post the updated config file

Create via the Jenkins CLI

You need to create a config.xml to pass to the create-job command. The following is an example:

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    create-job $MASTER_NAME < my-k8s-managed-master.xml

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    k8s-managed-master-provision-and-start $MASTER_NAME

Create via the REST API

You need to pass a config.xml to the /createItem endpoint. The following is an example:

curl -v -XPOST \
    -u $USER:$TOKEN \
    -H "Content-Type:text/xml" \
    --data-binary @my-k8s-managed-master.xml \
    "$CJOC_URL/createItem?name=$MASTER_NAME"

curl -v -XPOST \
    -u $USER:$TOKEN \
    "$CJOC_URL/job/$MASTER_NAME/provisionAndStartAction"

Have more questions?

0 Comments

Please sign in to leave a comment.