-
Notifications
You must be signed in to change notification settings - Fork 327
166 lines (162 loc) · 6.4 KB
/
aks-cost-optimization.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
name: aks-cost-optimization
on:
workflow_dispatch:
env:
ACR_NAME: contosotradersacr
AKS_CLUSTER_NAME: contoso-traders-aks
AKS_CPU_LIMIT: 250m
AKS_MEMORY_LIMIT: 256Mi
AKS_REPLICAS: "1"
AKS_SECRET_NAME_ACR_PASSWORD: contoso-traders-acr-password
KV_NAME: contosotraderskv
LOAD_TEST_SERVICE_NAME: contoso-traders-loadtest
PRODUCTS_ACR_REPOSITORY_NAME: contosotradersapiproducts
RESOURCE_GROUP_NAME: contoso-traders-rg
jobs:
aks-cost-optimization:
strategy:
fail-fast: false
matrix:
AKS_CPU_LIMIT: ["250m", "100m"]
AKS_MEMORY_LIMIT: ["256Mi", "128Mi"]
max-parallel: 1
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v4
- name: azure login
uses: azure/login@v1
with:
creds: ${{ secrets.SERVICEPRINCIPAL }}
- name: extract acr password
uses: azure/CLI@v1
id: extract-acr-password
with:
inlineScript: |
acrPassword=$(az acr credential show -n ${{ env.ACR_NAME }}${{ vars.SUFFIX }} -g ${{ env.RESOURCE_GROUP_NAME }}${{ vars.SUFFIX }} --query "passwords[0].value" --output tsv)
echo "::add-mask::$acrPassword"
echo acrPassword=$acrPassword >> $GITHUB_OUTPUT
- name: azure container registry login
uses: azure/docker-login@v1
with:
login-server: ${{ env.ACR_NAME }}${{ vars.SUFFIX }}.azurecr.io
username: ${{ env.ACR_NAME }}${{ vars.SUFFIX }}
password: ${{ steps.extract-acr-password.outputs.acrPassword }}
- name: set aks context
uses: azure/aks-set-context@v3
with:
resource-group: ${{ env.RESOURCE_GROUP_NAME }}${{ vars.SUFFIX }}
cluster-name: ${{ env.AKS_CLUSTER_NAME }}${{ vars.SUFFIX }}
- name: get products api endpoint
uses: azure/CLI@v1
id: get-productsApiEndpoint
with:
inlineScript: echo "productsApiEndpoint"="$(az keyvault secret show --vault-name ${{ env.KV_NAME }}${{ vars.SUFFIX }} --name productsApiEndpoint --query value -o tsv)" >> $GITHUB_OUTPUT
- name: substitute tokens in deployment manifest
uses: cschleiden/replace-tokens@v1.2
with:
tokenPrefix: "{"
tokenSuffix: "}"
files: ./src/ContosoTraders.Api.Products/Manifests/Deployment.yaml
env:
SUFFIX: ${{ vars.SUFFIX }}
AKS_REPLICAS: ${{ env.AKS_REPLICAS }}
AKS_CPU_LIMIT: ${{ matrix.AKS_CPU_LIMIT }}
AKS_MEMORY_LIMIT: ${{ matrix.AKS_MEMORY_LIMIT }}
- name: apply deployment manifest
uses: Azure/k8s-deploy@v4
with:
manifests: ./src/ContosoTraders.Api.Products/Manifests/Deployment.yaml
images: ${{ env.ACR_NAME }}${{ vars.SUFFIX }}.azurecr.io/${{ env.PRODUCTS_ACR_REPOSITORY_NAME }}:latest
imagepullsecrets: ${{ env.AKS_SECRET_NAME_ACR_PASSWORD }}
force: true
- name: sleep for 30 seconds
run: sleep 30s
shell: bash
- name: load test (products API)
uses: Azure/load-testing@v1.1.19
with:
# Path of the YAML file. Should be fully qualified path or relative to the default working directory
loadtestConfigFile: ./loadtests/contoso-traders-products.yaml
loadtestResource: ${{ env.LOAD_TEST_SERVICE_NAME }}${{ vars.SUFFIX }}
resourceGroup: ${{ env.RESOURCE_GROUP_NAME }}${{ vars.SUFFIX }}
env: |
[
{
"name": "domain",
"value": "${{ steps.get-productsApiEndpoint.outputs.productsApiEndpoint }}"
},
{
"name": "protocol",
"value": "https"
},
{
"name": "path",
"value": "v1/Products/1"
},
{
"name": "threads_per_engine",
"value": "25"
},
{
"name": "ramp_up_time",
"value": "0"
},
{
"name": "duration_in_sec",
"value": "45"
}
]
reset-aks:
runs-on: ubuntu-latest
needs: [aks-cost-optimization]
if: always()
steps:
- name: checkout code
uses: actions/checkout@v4
- name: azure login
uses: azure/login@v1
with:
creds: ${{ secrets.SERVICEPRINCIPAL }}
- name: extract acr password
uses: azure/CLI@v1
id: extract-acr-password
with:
inlineScript: |
acrPassword=$(az acr credential show -n ${{ env.ACR_NAME }}${{ vars.SUFFIX }} -g ${{ env.RESOURCE_GROUP_NAME }}${{ vars.SUFFIX }} --query "passwords[0].value" --output tsv)
echo "::add-mask::$acrPassword"
echo acrPassword=$acrPassword >> $GITHUB_OUTPUT
- name: azure container registry login
uses: azure/docker-login@v1
with:
login-server: ${{ env.ACR_NAME }}${{ vars.SUFFIX }}.azurecr.io
username: ${{ env.ACR_NAME }}${{ vars.SUFFIX }}
password: ${{ steps.extract-acr-password.outputs.acrPassword }}
- name: set aks context
uses: azure/aks-set-context@v3
with:
resource-group: ${{ env.RESOURCE_GROUP_NAME }}${{ vars.SUFFIX }}
cluster-name: ${{ env.AKS_CLUSTER_NAME }}${{ vars.SUFFIX }}
- name: get products api endpoint
uses: azure/CLI@v1
id: get-productsApiEndpoint
with:
inlineScript: echo "productsApiEndpoint"="$(az keyvault secret show --vault-name ${{ env.KV_NAME }}${{ vars.SUFFIX }} --name productsApiEndpoint --query value -o tsv)" >> $GITHUB_OUTPUT
- name: substitute tokens in deployment manifest
uses: cschleiden/replace-tokens@v1.2
with:
tokenPrefix: "{"
tokenSuffix: "}"
files: ./src/ContosoTraders.Api.Products/Manifests/Deployment.yaml
env:
SUFFIX: ${{ vars.SUFFIX }}
AKS_REPLICAS: ${{ env.AKS_REPLICAS }}
AKS_CPU_LIMIT: ${{ env.AKS_CPU_LIMIT }}
AKS_MEMORY_LIMIT: ${{ env.AKS_MEMORY_LIMIT }}
- name: apply deployment manifest
uses: Azure/k8s-deploy@v4
with:
manifests: ./src/ContosoTraders.Api.Products/Manifests/Deployment.yaml
images: ${{ env.ACR_NAME }}${{ vars.SUFFIX }}.azurecr.io/${{ env.PRODUCTS_ACR_REPOSITORY_NAME }}:latest
imagepullsecrets: ${{ env.AKS_SECRET_NAME_ACR_PASSWORD }}
force: true