Last active
July 29, 2020 22:56
-
-
Save mcasperson/bda9e36b84a5f2a537aefa4d83e4881b to your computer and use it in GitHub Desktop.
Create a Test K8s cluster in Octopus with Kind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use Kind (https://kind.sigs.k8s.io/docs/user/quick-start/) to create a test cluster in Octopus | |
$ErrorActionPreference = 'SilentlyContinue' | |
$ProgressPreference = 'SilentlyContinue' | |
docker version | |
if ($LastExitCode -ne 0) { | |
Write-Error "Docker needs to be installed" | |
exit 1 | |
} | |
Write-Host "Downloading Kind" | |
Invoke-WebRequest ` | |
-Uri https://github.com/kubernetes-sigs/kind/releases/download/v0.8.1/kind-windows-amd64 ` | |
-OutFile kind.exe | |
Write-Host "Downloading kubectl" | |
Invoke-WebRequest ` | |
-Uri https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/windows/amd64/kubectl.exe ` | |
-OutFile kubectl.exe | |
$env:KUBECONFIG = $env:OctopusCalamariWorkingDirectory + "/kind-config" | |
Write-Host "Create cluster" | |
./kind.exe delete cluster --name kind 2>&1 | |
./kind.exe create cluster --name kind 2>&1 | |
Get-Content $env:KUBECONFIG | |
Write-Host "Creating Service Account" | |
Set-Content -Path serviceaccount.yaml -Value @" | |
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: octopus-administrator | |
namespace: default | |
--- | |
kind: ClusterRoleBinding | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: octopus-administrator-binding | |
namespace: default | |
subjects: | |
- kind: ServiceAccount | |
name: octopus-administrator | |
namespace: default | |
apiGroup: "" | |
roleRef: | |
apiGroup: rbac.authorization.k8s.io | |
kind: ClusterRole | |
name: cluster-admin | |
"@ | |
./kubectl.exe apply -f serviceaccount.yaml | |
$data = ./kubectl get secret $(./kubectl get serviceaccount octopus-administrator -o jsonpath="{.secrets[0].name}" --namespace=default) -o jsonpath="{.data.token}" --namespace=default | |
$token = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data)) | |
New-OctopusTokenAccount -Name kind -token $token -updateIfExisting | |
$url = (./kubectl config view -o json | ConvertFrom-Json).clusters[0].cluster.server | |
New-OctopusKubernetesTarget ` | |
-name "Kind" ` | |
-clusterUrl $url ` | |
-octopusRoles "k8s" ` | |
-octopusAccountIdOrName "kind" ` | |
-namespace "default" ` | |
-updateIfExisting ` | |
-skipTlsVerification True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment