How do I create a new Kubernetes deployment target using the API?

I have an automated process for provisioning an Azure AKS Kubernetes cluster. After the cluster is provisioned, I want to register the cluster as a deployment target in Octopus. I can’t find any documentation on creating this type of deployment target from the API. How would I do this?

Hi @erick-thompson,

Thanks for getting in touch!

I have just done some testing here and have a very simple script to create a new Kubernetes cluster via the API. I say simple because it’s just a demonstration with a simple dummy configuration to get it past the line and into Octopus. You will want to make changes and may need to define some different resources. For example, you may want to provide a certificate, where I have not.

This should all be fairly easy to explore and set up with the below template I have created. I suggest you manually create one in Octopus and get the object in Powershell to see exactly what the different objects you need to provide will look like.

$repository.Machines.FindByName("KubeCluster") I made a template on my server called KubeCluster to get an idea of exactly what I’ll need to provide to get this into Octopus via the API.

Here is the full script I made, again, it’s a very basic example just to give you an idea:

Add-Type -Path 'C:\MyScripts\Octopus.Client.dll' 

$apikey = 'API-KEY' # Get this from your profile
$octopusURI = 'http://OctopusServer/' # Your server address

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

## These two lines will let you run this script against a different space based on the display name of said space.
#$space = $client.ForSystem().Spaces.FindByName("Second")
#$spaceRepo = $client.ForSpace($space)

$accountID = "usernamepassword-user-spaces-1" # define accountID you want to use to authenticate.
$clusterURL = "http://kubernetes.example.com" # Cluster URL

# Create an object for the authentication, this object is nested inside the below endpoint object
$AuthenticationEndpoint = New-Object Octopus.Client.Model.Endpoints.KubernetesStandardAccountAuthenticationResource
$AuthenticationEndpoint.AccountId = $accountID

# Endpoint object is created, and you add in the auth object alongside some other info you need.
$targetEndpoint = New-Object Octopus.Client.Model.Endpoints.KubernetesEndpointResource
$targetEndpoint.Authentication = $AuthenticationEndpoint
$targetEndpoint.ClusterUrl = $clusterURL
$targetEndpoint.SkipTlsVerification = "True"
$targetEndpoint.DefaultWorkerPoolId = "WorkerPools-1"

# Now to create the actual target in Octopus. Provide the endpoint object and the below at a minimum. You do not need to provide an ID (Octopus will automatically do this)
$kubeTarget = New-Object Octopus.Client.Model.MachineResource
$kubeTarget.Endpoint = $targetEndpoint
$kubeTarget.EnvironmentIds = "Environments-1"
$kubeTarget.Roles = "web"
$kubeTarget.Name = "NewKubeCluster"
$kubeTarget.MachinePolicyId = "MachinePolicies-1"

# Create and your very basic Kubernetes Cluster will appear in Octopus.
$repository.machines.Create($kubeTarget)

Hopefully this will give you an idea of the basic requirements for creating a target in Octopus. I think the best way to get it working is as I mentioned above, create a target as a template, call it in Powershell and look at the info you need to pass into it.

From there you can do the rest with whatever variables you need.

I hope this gets you started in the right direction.

If you have any questions here, or run into any issues, please don’t hesitate to let me know.

Best regards,
Daniel

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.