aks-baseline-automation/.github/workflows/IaC-bicep-AKS.yml

179 строки
8.4 KiB
YAML
Исходник Постоянная ссылка Обычный вид История

2022-09-14 03:07:44 +03:00
name: 'IaC Deploy CARML based AKS Cluster'
2022-03-30 13:39:18 +03:00
2022-03-11 19:20:08 +03:00
on:
workflow_dispatch:
inputs:
ENVIRONMENT:
description: 'A GitHub Environment to pull action secrets from'
required: true
type: environment
REGION:
description: 'The Azure region to deploy to'
required: true
2022-06-23 11:04:16 +03:00
default: westus2
2022-05-06 00:54:17 +03:00
clusterAdminAadGroupObjectId:
description: 'K8S Admin Azure AAD Group ObjectID'
required: true
type: string
2022-05-06 00:54:17 +03:00
a0008NamespaceReaderAadGroupObjectId:
description: 'K8S Reader Azure AAD Group ObjectID'
required: true
type: string
2022-06-23 11:04:16 +03:00
2022-03-11 19:20:08 +03:00
env:
event_sha: +refs/pull/${{ github.event.issue.number }}/merge
2022-03-30 13:43:45 +03:00
permissions:
id-token: write
contents: read
2022-03-11 19:20:08 +03:00
jobs:
2022-03-23 14:15:58 +03:00
prereqs:
runs-on: ubuntu-latest
2022-03-30 13:49:26 +03:00
environment: ${{ github.event.inputs.ENVIRONMENT }}
2022-03-23 14:21:31 +03:00
name: Prerequisite Checks
2022-03-23 14:15:58 +03:00
steps:
- name: "Checkout"
uses: actions/checkout@v2
with:
fetch-depth: 0
2022-03-30 13:51:34 +03:00
- name: "Parameter Check"
run: |
echo "Environment : ${{ github.event.inputs.ENVIRONMENT }}"
echo "REGION : ${{ github.event.inputs.REGION }}"
2022-03-30 13:43:45 +03:00
- name: Azure Login
uses: Azure/login@v1.4.3
2022-03-23 14:15:58 +03:00
with:
2022-03-30 13:43:45 +03:00
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
2022-03-23 14:15:58 +03:00
- name: "Check Preview Features"
shell: pwsh
run: |
2022-03-28 17:27:54 +03:00
write-output "Verifying required Resource Providers Features are registered"
$aksfeatures = az feature list --query "[?contains(name, 'Microsoft.ContainerService')]" | ConvertFrom-Json
$featureName='AKS-ExtensionManager'
write-output "-- $featureName"
$feature = $aksfeatures | Where-Object {$_.name -like "*$featureName"}
$feature.properties.state
if ($feature.properties.state -ne 'Registered') {
Write-Output $feature
Write-Error "$featureName NOT registered"
}
2022-03-23 14:21:31 +03:00
$featureName='EnableOIDCIssuerPreview'
write-output "-- $featureName"
$feature = $aksfeatures | Where-Object {$_.name -like "*$featureName"}
2022-03-23 14:21:31 +03:00
$feature.properties.state
if ($feature.properties.state -ne 'Registered') {
Write-Output $feature
Write-Error "$featureName NOT registered"
}
2022-03-23 14:21:31 +03:00
$featureName='AKS-AzureDefender'
write-output "-- $featureName"
$feature = $aksfeatures | Where-Object {$_.name -like "*$featureName"}
2022-03-23 14:21:31 +03:00
$feature.properties.state
if ($feature.properties.state -ne 'Registered') {
Write-Output $feature
Write-Error "$featureName NOT registered"
}
2022-03-23 14:15:58 +03:00
2022-03-11 19:20:08 +03:00
deployment:
runs-on: ubuntu-latest
2022-03-30 13:49:26 +03:00
environment: ${{ github.event.inputs.ENVIRONMENT }}
2022-03-28 17:28:37 +03:00
name: Deployment
2022-03-23 14:15:58 +03:00
needs: [prereqs]
2022-03-11 19:20:08 +03:00
steps:
- name: "Checkout"
uses: actions/checkout@v2
with:
fetch-depth: 0
2022-03-23 13:16:53 +03:00
2022-08-27 05:30:08 +03:00
# This step is just used for convenience as you won't be using self-signed certificate in your production environment.
# The certificate used in your environment will likely be generated and imported into KeyVault by your security team
- name: "Cert Generation"
2022-07-29 14:52:42 +03:00
id: cert
run: |
2022-08-27 05:30:08 +03:00
export DOMAIN_NAME_AKS_BASELINE="contoso.com"
export CN="bicycle"
2022-08-26 04:27:44 +03:00
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -out appgw.crt -keyout appgw.key -subj "/CN=${CN}.${DOMAIN_NAME_AKS_BASELINE}/O=Contoso Bicycle" -addext "subjectAltName = DNS:${CN}.${DOMAIN_NAME_AKS_BASELINE}" -addext "keyUsage = digitalSignature" -addext "extendedKeyUsage = serverAuth"
openssl pkcs12 -export -out appgw.pfx -in appgw.crt -inkey appgw.key -passout pass:
export APP_GATEWAY_LISTENER_CERTIFICATE_AKS_BASELINE=$(cat appgw.pfx | base64 | tr -d '\n')
2022-07-29 18:09:33 +03:00
echo "APP_GATEWAY_LISTENER_CERTIFICATE=$APP_GATEWAY_LISTENER_CERTIFICATE_AKS_BASELINE" >> $GITHUB_ENV
2022-08-25 22:41:51 +03:00
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -out traefik-ingress-internal-aks-ingress-tls.crt -keyout traefik-ingress-internal-aks-ingress-tls.key -subj "/CN=*.aks-ingress.${DOMAIN_NAME_AKS_BASELINE}/O=Contoso AKS Ingress"
export AKS_INGRESS_CONTROLLER_CERTIFICATE_BASE64_AKS_BASELINE=$(cat traefik-ingress-internal-aks-ingress-tls.crt | base64 | tr -d '\n')
2022-07-29 18:09:33 +03:00
echo "AKS_INGRESS_CONTROLLER_CERTIFICATE=$AKS_INGRESS_CONTROLLER_CERTIFICATE_BASE64_AKS_BASELINE" >> $GITHUB_ENV
2022-03-30 13:43:45 +03:00
- name: Azure Login
uses: Azure/login@v1.4.3
2022-03-11 19:20:08 +03:00
with:
2022-03-30 13:43:45 +03:00
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
2022-03-23 13:16:53 +03:00
- name: "Deploy AKS Landingzone"
id: akslz
2022-08-25 17:17:11 +03:00
uses: azure/arm-deploy@v1
with:
subscriptionId: ${{ secrets.SUBSCRIPTION_ID }}
region: ${{ github.event.inputs.REGION }}
scope: subscription
template: ./IaC/bicep/main.bicep
parameters: ./IaC/bicep/main.parameters.json clusterAdminAadGroupObjectId=${{ github.event.inputs.clusterAdminAadGroupObjectId }} a0008NamespaceReaderAadGroupObjectId=${{ github.event.inputs.a0008NamespaceReaderAadGroupObjectId }} appGatewayListenerCertificate=${{ env.APP_GATEWAY_LISTENER_CERTIFICATE }} aksIngressControllerCertificate=${{ env.AKS_INGRESS_CONTROLLER_CERTIFICATE }}
2022-08-25 17:17:11 +03:00
failOnStdErr: false
deploymentName: carml-aks-landingzone-${{ github.event.inputs.REGION }}
2022-08-25 12:20:25 +03:00
2022-08-28 00:17:50 +03:00
# Import core images hosted in public container registries to be used during bootstrapping
2022-08-28 03:29:07 +03:00
- name: "Import Images into ACR for flux"
id: image_import
run: |
az acr import --source docker.io/weaveworks/kured:1.10.1 -n ${{ steps.akslz.outputs.containerRegistryName }} --force
az acr import --source docker.io/library/traefik:v2.8.1 -n ${{ steps.akslz.outputs.containerRegistryName }} --force
2022-08-25 23:05:03 +03:00
2022-08-27 00:20:12 +03:00
# Temporary workaround until we figure out why steps.cluster.outputs.keyVaultName is blank
2022-08-27 22:54:11 +03:00
- name: "Get KeyVault Name"
2022-08-25 23:01:38 +03:00
id: akv_name
run: |
export AKV_NAME=$(az keyvault list -g rg-bu0001a0008 -o table | grep "kv-aks" | awk '{print $2}')
2022-08-26 05:26:01 +03:00
echo "AKV_NAME=${AKV_NAME}" >> $GITHUB_ENV
echo "AKV Name from bicep output is ${{ steps.akslz.outputs.keyVaultName }}"
echo "aksIngressControllerPodManagedIdentityResourceId from bicep output is ${{ steps.akslz.outputs.aksIngressControllerPodManagedIdentityResourceId }}"
echo "To prove that this should work: hubVnetId is ${{ steps.akslz.outputs.hubVnetId }}"
2022-08-25 22:31:15 +03:00
2022-08-28 03:29:07 +03:00
# Re-authentication is required for the commands that follow
- name: Azure Login
uses: Azure/login@v1.4.3
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
2022-08-26 05:26:01 +03:00
2022-08-27 05:30:08 +03:00
# This step is just used for convenience as the certificate used in your environment will likely be generated and imported into KeyVault by your security team
2022-08-28 00:17:50 +03:00
# This step imports into KeyVault the same backend cert that was generated above for the App Gateway. This cert will be used by the Ingress Controller Traefik.
2022-08-27 05:30:08 +03:00
- name: "Cert Import into Key Vault for Traefik"
2022-08-25 12:13:57 +03:00
id: cert_import
run: |
ASSIGNEE_OBJ_ID=$(az ad sp show --id ${{ secrets.AZURE_CLIENT_ID }} --query id -o tsv)
2022-08-26 04:24:53 +03:00
AKV_RESOURCE_ID=$(az keyvault show -n ${{ env.AKV_NAME }} --query id -o tsv)
2022-08-25 12:13:57 +03:00
TEMP_ROLEASSIGNMENT_TO_UPLOAD_CERT=$(az role assignment create --role a4417e6f-fecd-4de8-b567-7b0420556985 --assignee-principal-type serviceprincipal --assignee-object-id $ASSIGNEE_OBJ_ID --scope $AKV_RESOURCE_ID | jq '.id' )
sleep 60
CURRENT_IP_ADDRESS=$(curl -s -4 https://ifconfig.io)
2022-08-26 02:54:08 +03:00
az keyvault network-rule add -n ${{ env.AKV_NAME }} --ip-address ${CURRENT_IP_ADDRESS}
2022-08-25 12:13:57 +03:00
sleep 30
cat traefik-ingress-internal-aks-ingress-tls.crt traefik-ingress-internal-aks-ingress-tls.key > traefik-ingress-internal-aks-ingress-tls.pem
2022-08-26 02:54:08 +03:00
az keyvault certificate import -f traefik-ingress-internal-aks-ingress-tls.pem -n traefik-ingress-internal-aks-ingress-tls --vault-name ${{ env.AKV_NAME }}
2022-08-25 00:51:02 +03:00
2022-08-27 05:30:08 +03:00
az keyvault network-rule remove -n ${{ env.AKV_NAME }} --ip-address ${CURRENT_IP_ADDRESS}
2022-08-28 04:20:33 +03:00
az role assignment delete --role a4417e6f-fecd-4de8-b567-7b0420556985 --assignee $ASSIGNEE_OBJ_ID --scope $AKV_RESOURCE_ID
2022-08-27 05:30:08 +03:00