-
Notifications
You must be signed in to change notification settings - Fork 8
330 lines (318 loc) · 11.4 KB
/
kubectl-aks.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
name: Azure Kubernetes Service kubectl plugin CI
env:
GO_VERSION: 1.18
AZURE_PREFIX: kubectl-aks-ci
AZURE_NODE_COUNT: 3 # multiple nodes are needed to allow running parallel 'run-command' against the same cluster
concurrency:
# Only one workflow can run at a time unless
# we create a new AKS cluster per github_ref (branch)
group: kubectl-aks-ci
on:
pull_request:
push:
branches:
- main
tags:
- 'v*'
jobs:
build:
name: Build kubectl-aks
runs-on: ubuntu-latest
strategy:
matrix:
os: [ linux, darwin, windows ]
arch: [ amd64, arm64 ]
exclude:
- os: windows
arch: arm64
steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
- name: Cache Go
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Check out code
uses: actions/checkout@v3
- name: Build and generate tarball
run: |
target=kubectl-aks-${{ matrix.os }}-${{ matrix.arch }}
make $target
binary_name=kubectl-aks
if [ ${{ matrix.os }} = "windows" ]; then
binary_name=kubectl-aks.exe
fi
# Prepare binary as artifact, it will be used by other jobs
mv $target $binary_name
tar --sort=name --owner=root:0 --group=root:0 \
-czf ${target}.tar.gz \
$binary_name LICENSE
- name: Add kubectl-aks-${{ matrix.os }}-${{ matrix.arch }}.tar.gz as artifact
uses: actions/upload-artifact@v3
with:
name: kubectl-aks-${{ matrix.os }}-${{ matrix.arch }}-tar-gz
path: kubectl-aks-${{ matrix.os }}-${{ matrix.arch }}.tar.gz
lint:
name: Run linter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
- name: Lint
uses: golangci/[email protected]
with:
# This version number must be kept in sync with Makefile lint one.
version: v1.53.2
working-directory: /home/runner/work/kubectl-aks/kubectl-aks
# Workaround to display the output:
# https://github.com/golangci/golangci-lint-action/issues/119#issuecomment-981090648
args: "--out-${NO_FUTURE}format colored-line-number"
unit-tests:
name: Run unit tests
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
- name: Check out code
uses: actions/checkout@v3
- name: Cache Go
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
run: make unit-test
create-aks-cluster:
name: Create AKS cluster
needs: [ build, lint, unit-tests ]
runs-on: ubuntu-latest
outputs:
nodes: ${{ steps.nodes.outputs.result }}
strategy:
fail-fast: false
matrix:
arch: [ amd64 ]
environment: aks
permissions:
# This is needed to use federated credentials:
# https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux#set-up-azure-login-with-openid-connect-authentication
id-token: write
contents: read
steps:
- name: Login to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_AKS_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_AKS_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_AKS_SUBSCRIPTION_ID }}
- name: Create AKS cluster ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster
shell: bash
run: |
az aks create \
--resource-group ${{ env.AZURE_PREFIX }}-rg \
--name ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster \
--node-count ${{ env.AZURE_NODE_COUNT }} \
--no-ssh-key
- uses: azure/aks-set-context@v3
name: Set AKS cluster ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster context
with:
cluster-name: ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster
resource-group: ${{ env.AZURE_PREFIX }}-rg
admin: false
- name: Prepare list of nodes
id: nodes
shell: bash
run: |
echo "result=$(kubectl get nodes -o jsonpath={.items[*].metadata.name} | jq -R -s -c 'split(" ")')" >> $GITHUB_OUTPUT
delete-aks-cluster:
name: Delete AKS cluster
if: always()
needs: [ integration-tests ]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
arch: [ amd64 ]
environment: aks
permissions:
# This is needed to use federated credentials:
# https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux#set-up-azure-login-with-openid-connect-authentication
id-token: write
contents: read
steps:
- name: Login to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_AKS_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_AKS_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_AKS_SUBSCRIPTION_ID }}
- name: Delete AKS cluster ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster
shell: bash
run: |
az aks delete \
--resource-group ${{ env.AZURE_PREFIX }}-rg \
--name ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster \
--yes
integration-tests:
name: Run integration tests
needs: [ build, unit-tests , create-aks-cluster ]
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, macOS-latest, windows-latest ]
arch: [ amd64 ] # TODO: Support ARM
environment: aks
permissions:
# This is needed to use federated credentials:
# https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux#set-up-azure-login-with-openid-connect-authentication
id-token: write
contents: read
steps:
- name: Ensure enough nodes are available to run parallel tests
shell: bash
run: |
if [ $(echo '${{ needs.create-aks-cluster.outputs.nodes }}' | jq -r '. | length') -lt ${{ strategy.job-total }} ]; then
echo "Not enough nodes to run parallel tests"
exit 1
fi
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
- name: Check out code
uses: actions/checkout@v3
- name: Set environment variables
shell: bash
run: |
case ${{ matrix.os }} in
ubuntu-latest)
echo "os=linux" >> $GITHUB_ENV
;;
macOS-latest)
echo "os=darwin" >> $GITHUB_ENV
;;
windows-latest)
echo "os=windows" >> $GITHUB_ENV
;;
*)
echo "Not supported OS: ${{ matrix.os }}"
exit 1
;;
esac
- name: Get kubectl-aks from artifact
uses: actions/download-artifact@v3
with:
name: kubectl-aks-${{ env.os }}-${{ matrix.arch }}-tar-gz
- name: Prepare kubectl-aks binary
shell: bash
run: |
tar zxvf kubectl-aks-${{ env.os }}-${{ matrix.arch }}.tar.gz
chmod +x kubectl-aks
ls -la
- name: Login to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_AKS_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_AKS_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_AKS_SUBSCRIPTION_ID }}
- name: Set AKS cluster context
uses: azure/aks-set-context@v3
with:
cluster-name: ${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster
resource-group: ${{ env.AZURE_PREFIX }}-rg
admin: false
- if: matrix.os != 'ubuntu-latest'
# kubectl is already installed in Linux runners
uses: azure/setup-kubectl@v3
# setup-kubectl doesn't support v1.28.2+ macOS (amd64) yet
# https://github.com/Azure/setup-kubectl/issues/88
with:
version: 'v1.28.2'
- name: Run integration tests
shell: bash
run: |
export AZURE_RESOURCE_GROUP=${{ env.AZURE_PREFIX }}-rg
export AZURE_CLUSTER_NAME=${{ env.AZURE_PREFIX }}-${{ matrix.arch }}-cluster
export AZURE_SUBSCRIPTION_ID=${{ secrets.AZURE_AKS_SUBSCRIPTION_ID }}
export AZURE_NODE_NAME=$(echo '${{ needs.create-aks-cluster.outputs.nodes }}' | jq -r ".[${{ strategy.job-index }}]")
make integration-test -o kubectl-aks
documentation-test:
name: Run documentation tests
runs-on: ubuntu-latest
needs: [ build, unit-tests ]
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v4
with:
go-version: '>=1.20.0' # ie requires go 1.20+
check-latest: true
# Use official krew installation guide:
# https://krew.sigs.k8s.io/docs/user-guide/setup/install/
- name: Install krew
run: |
set -x; cd "$(mktemp -d)" && \
OS="$(uname | tr '[:upper:]' '[:lower:]')" && \
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" && \
KREW="krew-${OS}_${ARCH}" && \
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" && \
tar zxvf "${KREW}.tar.gz" && \
./"${KREW}" install krew && \
cp ~/.krew/bin/kubectl-krew /usr/local/bin/kubectl-krew
echo "$HOME/.krew/bin" >> $GITHUB_PATH
- name: Run Documentation tests
run: make documentation-test -o kubectl-aks
release:
name: Release
needs: [ integration-tests ]
runs-on: ubuntu-latest
permissions:
contents: write
if: startsWith(github.ref, 'refs/tags/v')
steps:
# Checkout the repo to get the .krew.yaml file
- uses: actions/checkout@v3
- name: Get all artifacts.
uses: actions/download-artifact@v3
- name: Rename all artifacts to kubectl-aks-${{ github.ref_name }}.tar.gz
shell: bash
run: |
for i in kubectl-aks-*-*-tar-gz/kubectl-aks-*-*.tar.gz; do
mv $i $(dirname $i)/$(basename $i .tar.gz)-${{ github.ref_name }}.tar.gz
done
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
name: Release ${{ github.ref }}
- name: Upload kubectl-aks binaries to the release
uses: csexton/release-asset-action@v2
with:
pattern: "kubectl-aks-*-*-tar-gz/kubectl-aks-*-*.tar.gz"
github-token: ${{ secrets.GITHUB_TOKEN }}
release-url: ${{ steps.create_release.outputs.upload_url }}
- name: Update new version in krew-index
if: github.repository == 'azure/kubectl-aks'
uses: rajatjindal/[email protected]