How to create a Kubernetes Team controller programmatically

Article ID:360035634631
2 minute readKnowledge base

Issue

  • How to create a Kubernetes Team controller programmatically.

Resolution

Create Using Groovy

The easiest approach would be by using a Groovy script.

See createTeamcontrollerK8s.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 = < createManagedcontrollerK8s.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=$(<./createManagedcontrollerK8s.groovy)" \
    "$CJOC_URL/scriptText"

See Script Console - Remote Access for more details.

Create remotely using config files

You can also create a Team controller 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 controller configuration.

You would need to do the following:

  • Create the Team controller

  • Get the Managed controller config file (the config.xml)

  • Update the config file (preserve auto-generated fields)

  • Post the updated config file

Create via the Jenkins CLI

You need to create a config.json to pass to the teams command.

The following is an example of a JSON configuration for a team controller:

{
    "data": {
        "displayName": "Test from CLI",
        "icon": {
            "color": "#dd6669",
            "name": "hexagons"
        },
        "members": [
            {
                "id": "admin",
                "roles": [
                    "TEAM_ADMIN"
                ]
            }
        ],
        "name": "test-from-cli",
        "provisioningRecipe": "basic"
    },
    "version": "1"
}

And you can create this Team controller with the following command:

java -jar jenkins-cli.jar \
    -auth $USER:$TOKEN \
    -s $CJOC_URL/ \
    teams "test-from-cli" --put < config.json
Create via the REST API

You need to create a config.json to pass to the /blue/rest/cjoc/teams/ endpoint.

The following is an example of a JSON description for a team controller:

{
    "displayName": "Test From Rest Api",
    "icon": {
        "color": "#dd6669",
        "name": "hexagons"
    },
    "members": [
        {
            "id": "allan",
            "role": "TEAM_ADMIN"
        }
    ],
    "name": "test-from-restapi",
    "recipe": "basic"
}

And you can create this team controller with the following command:

curl -v -XPOST \
    -u $USER:$TOKEN \
    -H "Content-Type: application/json" \
    --data-binary @config.json \
    "$CJOC_URL/blue/rest/cjoc/teams/"