Skip to content

Commit d2d28f5

Browse files
authored
Merge pull request #2 from KarmaComputing/1-github-runner-manages-remote
GitHub runner manages remote kubernetes cluster
2 parents cbd14a2 + c5b97bb commit d2d28f5

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

.github/workflows/kubectl.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Run kubectl against remote cluster
2+
3+
on:
4+
workflow_dispatch: # Allows manual start of workflows
5+
push:
6+
branches:
7+
- "1-github-runner-manages-remote"
8+
paths-ignore:
9+
- *.md
10+
- *.example
11+
12+
jobs:
13+
deploy:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Install kubectl
17+
run: |
18+
mkdir $HOME/bin
19+
curl -Lf "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o $HOME/bin/kubectl
20+
chmod +x $HOME/bin/kubectl
21+
echo "$HOME/bin" >> $GITHUB_PATH
22+
23+
- name: Check kubectl is available on PATH
24+
run: kubectl version --client
25+
26+
- name: Checkout repo
27+
uses: actions/checkout@v4
28+
29+
- name: Set kubeconfig with kubectl
30+
run: |
31+
kubectl config set-cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --server "${{ secrets.KUBE_API_SERVER_ADDR }}"
32+
kubectl config set-credentials "${{ vars.KUBE_REMOTE_USER }}" --token "${{ secrets.KUBE_JWT_AUTH_TOKEN }}"
33+
kubectl config set-context "${{ vars.KUBE_REMOTE_CONTEXT }}" --cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --user "${{ vars.KUBE_REMOTE_USER }}"
34+
kubectl config use-context "${{ vars.KUBE_REMOTE_CONTEXT }}"
35+
36+
- name: Run kubectl command against remote API
37+
run: kubectl get namespaces
38+
39+
- name: kubectl apply with a file
40+
run: kubectl apply -f "${GITHUB_WORKSPACE}/manifests/nginx-test.yml"

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
kube-config
22
*.crt
33
*.key
4-
kubectl

kubectl-gh-actions.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
- Assuming you followed [manual-remote-access.md](./manual-remote-access.md), you should now have remote access to your minikube cluster set up
2+
3+
# Steps
4+
5+
1. Add secrets for the server address and JWT token, and other variables
6+
7+
- Note that generated JWT tokens are relatively short-lived, but you can extend their validity by passing `--duration=<timespan>` to `kubectl create token`
8+
- e.g. `kubectl create-token remote-dev --duration=12h` for a token valid for 12 hours
9+
- We probably don't want to use these in production, your kubernetes provider (e.g. EKS) may offer a better means of authentication
10+
11+
- On the webpage for your repo:
12+
- Settings -> Secrets and Variables -> Actions -> New Repository Secret
13+
- Set the name to `KUBE_JWT_AUTH_TOKEN`
14+
- Set the value to the JWT token you generated
15+
- Add another secret called `KUBE_API_SERVER_ADDR` with the value of your public-facing API server address
16+
17+
- We'll also add some variables for the cluster, remote username, and remote context
18+
- On the webpage for your repo:
19+
- Settings -> Secrets and Variables -> Actions -> Variables -> New Repository Variable
20+
- Add three variables with the names and values:
21+
- KUBE_REMOTE_CLUSTER = minikube
22+
- KUBE_REMOTE_USER = remote-dev
23+
- KUBE_REMOTE_CONTEXT = remote-context
24+
25+
2. Access the secrets and variables in the action
26+
27+
- Github actions can access repository secrets using the syntax `${{ secrets.<secret> }}` and variables with `${{ vars.<variable> }}`
28+
- We'll create a step in our action that sets the correct kubeconfig
29+
30+
```yaml
31+
# Other steps... #
32+
33+
- name: Set kubeconfig with kubectl
34+
run: |
35+
kubectl config set-cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --server "${{ secrets.KUBE_API_SERVER_ADDR }}"
36+
kubectl config set-credentials "${{ vars.KUBE_REMOTE_USER }}" --token "${{ secrets.KUBE_JWT_AUTH_TOKEN }}"
37+
kubectl config set-context "${{ vars.KUBE_REMOTE_CONTEXT }}" --cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --user "${{ vars.KUBE_REMOTE_USER }}"
38+
kubectl config use-context "${{ vars.KUBE_REMOTE_CONTEXT }}"
39+
40+
# kubectl command steps ... #
41+
```
42+
43+
- Using these variables and secrets makes it easier to update them in the future, without modifying the workflow file directly
44+
45+
3. Create the full workflow
46+
47+
- So we need to:
48+
1. Make sure the `kubectl` binary is available
49+
2. Checkout the repo
50+
3. Configure authentication with kubectl
51+
4. Run `kubectl` commands against the remote API
52+
53+
```yaml
54+
# File: .github/workflows/kubectl.yaml
55+
56+
name: Run kubectl against remote cluster
57+
on:
58+
workflow_dispatch: # Allows manual start of workflows
59+
push:
60+
branches:
61+
- "main"
62+
jobs:
63+
deploy:
64+
runs-on: ubuntu-latest
65+
steps:
66+
- name: Install kubectl
67+
run: |
68+
mkdir "$HOME/bin"
69+
curl -Lf "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o "$HOME/bin/kubectl"
70+
chmod +x $HOME/bin/kubectl
71+
echo "$HOME/bin" >> $GITHUB_PATH
72+
73+
- name: Check kubectl is available on PATH
74+
run: kubectl version --client
75+
76+
- name: Checkout repo
77+
uses: actions/checkout@v4
78+
79+
- name: Set kubeconfig with kubectl
80+
run: |
81+
kubectl config set-cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --server "${{ secrets.KUBE_API_SERVER_ADDR }}"
82+
kubectl config set-credentials "${{ vars.KUBE_REMOTE_USER }}" --token "${{ secrets.KUBE_JWT_AUTH_TOKEN }}"
83+
kubectl config set-context "${{ vars.KUBE_REMOTE_CONTEXT }}" --cluster "${{ vars.KUBE_REMOTE_CLUSTER }}" --user "${{ vars.KUBE_REMOTE_USER }}"
84+
kubectl config use-context "${{ vars.KUBE_REMOTE_CONTEXT }}"
85+
86+
- name: Run kubectl command against remote API
87+
run: kubectl get namespaces
88+
```
89+
90+
- With this, we have remote access to the API in according with the RBAC rules we created earlier
91+
- If you wanted to `kubectl apply -f` in this action, you could do so like below:
92+
93+
```yaml
94+
# previous setup steps #
95+
96+
- name: kubectl apply with a file
97+
run: kubectl apply -f "${GITHUB_WORKSPACE}/manifests/nginx-test.yml"
98+
```

manifests/nginx-test.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Create an nginx pod
2+
3+
apiVersion: v1
4+
kind: Pod
5+
metadata:
6+
name: nginx-pod
7+
labels:
8+
app: nginx
9+
spec:
10+
containers:
11+
- name: nginx-container
12+
image: nginx:latest
13+
ports:
14+
- containerPort: 80

0 commit comments

Comments
 (0)