Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/GitHub AKS Provisioning #160

Open
wants to merge 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
318f1f8
updated with AKS provision workflow in github action
prathibhapadma Jul 1, 2022
069c94a
updated with AKS provision workflow in github action
prathibhapadma Jul 1, 2022
7d28403
automatic commit dor docs
prathibhapadma Jul 1, 2022
5ea4d24
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 1, 2022
f70b423
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 1, 2022
ec02564
automatic commit dor docs
prathibhapadma Jul 1, 2022
81cb809
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 1, 2022
412c2a7
Merge branch 'master' into feature/github_aks_provision
prathibhapadma Jul 1, 2022
9008087
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 1, 2022
be22db0
Automatic generation of documentation
prathibhapadma Jul 1, 2022
d81e5ab
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 1, 2022
22f0704
Automatic generation of documentation
prathibhapadma Jul 1, 2022
5c30817
Create setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 1, 2022
74f6b58
Delete documentation/src directory
prathibhapadma Jul 1, 2022
2488464
Merge branch 'devonfw:master' into feature/github_aks_provision
prathibhapadma Jul 5, 2022
8fc8de7
updating aks provision document
prathibhapadma Jul 5, 2022
62f4add
Automatic generation of documentation
prathibhapadma Jul 5, 2022
1a486b6
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
2df5177
Automatic generation of documentation
prathibhapadma Jul 5, 2022
4002c10
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
9927dd8
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
6008fd0
Automatic generation of documentation
prathibhapadma Jul 5, 2022
b87f5e7
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
43d0a9c
Automatic generation of documentation
prathibhapadma Jul 5, 2022
dd01418
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
895e452
Automatic generation of documentation
prathibhapadma Jul 5, 2022
4ac04d0
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
1250980
Automatic generation of documentation
prathibhapadma Jul 5, 2022
24d98ed
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 5, 2022
c5554a1
Automatic generation of documentation
prathibhapadma Jul 5, 2022
e95872b
Merge branch 'devonfw:master' into feature/github_aks_provision
prathibhapadma Jul 6, 2022
e27a753
updated cluster name in azure devops pipeline
prathibhapadma Jul 7, 2022
5a20455
Automatic generation of documentation
prathibhapadma Jul 7, 2022
f2b19b1
Update aks-provisioning.yml.template
prathibhapadma Jul 7, 2022
26b457f
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 7, 2022
e62c914
Automatic generation of documentation
prathibhapadma Jul 7, 2022
21868f9
Update terraform.tfvars
prathibhapadma Jul 7, 2022
821c228
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Jul 7, 2022
42b78f3
Automatic generation of documentation
prathibhapadma Jul 7, 2022
be8d44d
Merge branch 'devonfw:master' into feature/github_aks_provision
prathibhapadma Jul 7, 2022
ae6d73a
Update aks-provisioning.yml.template
prathibhapadma Jul 9, 2022
aa8a072
Update aks-provisioning.yml.template
prathibhapadma Jul 12, 2022
efbd4df
Merge branch 'master' into feature/github_aks_provision
prathibhapadma Jul 12, 2022
c05d1a2
updated with common variable group
prathibhapadma Jul 18, 2022
30f9517
Update aks-provisioning.yml.template
prathibhapadma Jul 18, 2022
89cd9b5
fixed end of the lines
prathibhapadma Jul 20, 2022
58929fd
cluster name in help for aks
ultymatom Aug 4, 2022
15e22f9
Merge branch 'master' into feature/github_aks_provision
ultymatom Aug 4, 2022
8f5029a
Update aks-provisioning.yml.template
prathibhapadma Sep 8, 2022
d97e86a
Update setup-aks-provisioning-pipeline.asciidoc
prathibhapadma Sep 8, 2022
3843096
Automatic generation of documentation
prathibhapadma Sep 8, 2022
8d551bb
Merge branch 'devonfw:master' into feature/github_aks_provision
prathibhapadma Sep 23, 2022
6e8cb73
Merge branch 'master' into feature/github_aks_provision
prathibhapadma Oct 18, 2022
1f8e2e0
Automatic generation of documentation
prathibhapadma Oct 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,89 +1,8 @@
= Setting up a Azure AKS provisioning pipeline on Azure DevOps

In this section we will create a pipeline which will provision an Azure AKS cluster. This pipeline will be configured to be manually triggered by the user. As part of AKS cluster provisioning, a NGINX Ingress controller is deployed and a variable group with the name `aks-variables` is created, which contains, among others, the DNS name of the Ingress controller, that you you will need to add as CNAME record on the domains used in your application Ingress manifest files. Refer to the appendix to retrieve the DNS name of the Ingress controller independently.

The creation of the pipeline will follow the project workflow, so a new branch named `feature/aks-provisioning` will be created, the YAML file for the pipeline and the terraform files for creating the cluster will be pushed to it.

Then, a Pull Request (PR) will be created in order to merge the new branch into the appropiate branch (provided in `-b` flag). The PR will be automatically merged if the repository policies are met. If the merge is not possible, either the PR URL will be shown as output, or it will be opened in your web browser if using `-w` flag.

The script located at `/scripts/pipelines/azure-devops/pipeline_generator.sh` will automatically create this new branch, create the AKS provisioning pipeline based on the YAML template, create the Pull Request and, if it is possible, merge this new branch into the specified branch.

== Prerequisites

* Install the https://marketplace.visualstudio.com/items?itemName=ms-devlabs.custom-terraform-tasks[Terraform extension] for Azure DevOps.
* Create a https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#create-a-service-connection[service connection] to Azure Resource Manager and name it `aks-connection`. If you already have a service connection available or you need a specific connection name, please update `aks-pipeline.cfg` accordingly.
* An Azure resource group in the desired cluster location (e.g. `westeurope`). You can use an existing one or create a new one with the following command:

```
az group create -n <resource group name> -l <location>
```

* An Azure storage account within the previous resource group. You can use an existing one or create a new one with the following command:

```
az storage account create -n <storage account name> -g <resource group name> -l <location>
```

* An Azure storage container in Azure within the previous storage account. You can use an existing one or create a new one with the following command:

```
az storage container create -n <storage container name> --account-name <storage account name>
```

* This script will commit and push the corresponding YAML template into your repository, so please be sure your local repository is up-to-date (i.e you have pulled the latest changes with `git pull`).

== Creating the pipeline using provided script

Before executing the script you will need to customize some input variables about the environment. To do so, you can either edit `terraform.tfvars` file or take advantage of the `set-terraform-variables.sh` script located at `/scripts/environment-provisioning/azure/aks`, which allows you to create or update values for the required variables, passing them as flags. As a full example:

```
./set-terraform-variables.sh --cluster_name <cluster name> --location <location> --resource_group_name <resource group name> --instance_type <worker instance type> --worker_node_count <number of worker nodes> --dns_prefix <dns prefix>
```

=== Usage
```
pipeline_generator.sh \
-c <config file path> \
-n <pipeline name> \
-d <project local path> \
--resource-group <resource group name> \
--storage-account <storage account name> \
--storage-container <storage container name> \
[-b <branch>] \
[-w]
```

NOTE: The config file for the AKS provisioning pipeline is located at `/scripts/pipelines/azure-devops/templates/aks/aks-pipeline.cfg`.

=== Flags
```
-c, --config-file [Required] Configuration file containing pipeline definition.
-n, --pipeline-name [Required] Name that will be set to the pipeline.
-d, --local-directory [Required] Local directory of your project (the path should always be using '/' and not '\').
--resource-group [Required] Name of the resource group for the cluster.
--storage--account [Required] Name of the storage account for the cluster.
--storage-container [Required] Name of the storage container where the Terraform state of the cluster will be stored.
-b, --target-branch Name of the branch to which the Pull Request will target. PR is not created if the flag is not provided.
-w Open the Pull Request on the web browser if it cannot be automatically merged. Requires -b flag.
```

=== Example

```
./pipeline_generator.sh -c ./templates/aks/aks-pipeline.cfg -n aks-provisioning -d C:/Users/$USERNAME/Desktop/quarkus-project --resource-group devonfw --storage-account hangar --storage-container aks-state -b develop -w
```

=== Appendix: Interacting with the cluster

NOTE: Make sure you have https://kubernetes.io/docs/tasks/tools/#kubectl[kubectl] installed.

In order to interact with your cluster you will need to download the artifact `kubeconfig` generated by the cluster provisioning pipeline on the location it is expected by default (`~/.kube/config`) or either:

```
# via environment variable (you can add this on your profile)
export KUBECONFIG=<path to downloaded kubeconfig>
kubectl <command>

# via command-line flag
kubectl <command> --kubeconfig=<path to downloaded kubeconfig>
```
:provider: Azure Devops
:pipeline_type: Pipeline
:trigger_sentence: * Install the https://marketplace.visualstudio.com/items?itemName=ms-devlabs.custom-terraform-tasks[Terraform extension] for Azure DevOps. * Create a https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#create-a-service-connection[service connection] to Azure Resource Manager and name it `aks-connection`. If you already have a service connection available or you need a specific connection name, please update `aks-pipeline.cfg` accordingly.
:pipeline_type2: pipeline
:path_provider: azure-devops
:cmd_add: ./pipeline_generator.sh -c ./templates/aks/aks-pipeline.cfg -n aks-provisioning -d C:/Users/$USERNAME/Desktop/quarkus-project --resource-group devonfw --storage-account hangar --storage-container aks-state --rancher -b develop -w
:aks_variables: Azure DevOps > Pipelines > Library > `aks-variables`
Unresolved directive in setup-aks-provisioning-pipeline.asciidoc - include::../common_templates/setup-build-pipeline.asciidoc[]
8 changes: 8 additions & 0 deletions documentation/github/setup-aks-provisioning-pipeline.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:provider: Github
:pipeline_type: workflow
:trigger_sentence: * Add AZURE credentials as https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository[Github Secrets] in your repository and name it `AZURE_USERNAME`,`AZURE_PASSWORD`. If you already have a available credentials or you need a specific credentials connection, please update `aks-provisioning.yml` accordingly.
:pipeline_type2: Github action
:path_provider: github
:cmd_add: ./pipeline_generator.sh -c ./templates/aks/aks-pipeline.cfg -n aks-provisioning -d C:/Users/$USERNAME/Desktop/quarkus-project --cluster-name devon-hangar --resource-group devonfw --storage-account hangar --storage-container aks-state --rancher -b develop -w
:aks_variables: .github > vars > `aks-variables.env`
Unresolved directive in setup-aks-provisioning-pipeline.asciidoc - include::../common_templates/setup-build-pipeline.asciidoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:provider: Azure Devops
:pipeline_type: Pipeline
:trigger_sentence: * Install the https://marketplace.visualstudio.com/items?itemName=ms-devlabs.custom-terraform-tasks[Terraform extension] for Azure DevOps. * Create a https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#create-a-service-connection[service connection] to Azure Resource Manager and name it `aks-connection`. If you already have a service connection available or you need a specific connection name, please update `aks-pipeline.cfg` accordingly.
:pipeline_type2: pipeline
:path_provider: azure-devops
:cmd_add: ./pipeline_generator.sh -c ./templates/aks/aks-pipeline.cfg -n aks-provisioning -d C:/Users/$USERNAME/Desktop/quarkus-project --resource-group devonfw --storage-account hangar --storage-container aks-state --rancher -b develop -w
:aks_variables: Azure DevOps > Pipelines > Library > `aks-variables`
include::../common_templates/setup-build-pipeline.asciidoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
= Setting up a Azure AKS provisioning {pipeline_type} on {provider}

In this section we will create a {pipeline_type} which will provision an Azure AKS cluster. This pipeline will be configured to be manually triggered by the user. As part of AKS cluster provisioning, a NGINX Ingress controller is deployed and a variable group with the name `aks-variables` is created, which contains, among others, the DNS name of the Ingress controller, that you you will need to add as CNAME record on the domains used in your application Ingress manifest files. Refer to the appendix for more details.

The creation of the pipeline will follow the project workflow, so a new branch named `feature/aks-provisioning` will be created, the YAML file for the {pipeline_type} and the terraform files for creating the cluster will be pushed to it.

Then, a Pull Request (PR) will be created in order to merge the new branch into the appropiate branch (provided in `-b` flag). The PR will be automatically merged if the repository policies are met. If the merge is not possible, either the PR URL will be shown as output, or it will be opened in your web browser if using `-w` flag.

The script located at `/scripts/pipelines/{path_provider}/pipeline_generator.sh` will automatically create this new branch, create the AKS provisioning pipeline based on the YAML template, create the Pull Request and, if it is possible, merge this new branch into the specified branch.

== Prerequisites

{trigger_sentence}

* An Azure resource group in the desired cluster location (e.g. `westeurope`). You can use an existing one or create a new one with the following command:

```
az group create -n <resource group name> -l <location>
```

* An Azure storage account within the previous resource group. You can use an existing one or create a new one with the following command:

```
az storage account create -n <storage account name> -g <resource group name> -l <location>
```

* An Azure storage container in Azure within the previous storage account. You can use an existing one or create a new one with the following command:

```
az storage container create -n <storage container name> --account-name <storage account name>
```

* This script will commit and push the corresponding YAML template into your repository, so please be sure your local repository is up-to-date (i.e you have pulled the latest changes with `git pull`).

== Creating the {pipeline_type} using provided script

Before executing the script you will need to customize some input variables about the environment. To do so, you can either edit `terraform.tfvars` file or take advantage of the `set-terraform-variables.sh` script located at `/scripts/environment-provisioning/azure/aks`, which allows you to create or update values for the required variables, passing them as flags. As a full example:

```
./set-terraform-variables.sh --cluster_name <cluster name> --location <location> --resource_group_name <resource group name> --instance_type <worker instance type> --worker_node_count <number of worker nodes> --dns_prefix <dns prefix>
```

=== Usage
```
pipeline_generator.sh \
-c <config file path> \
-n <pipeline name> \
-d <project local path> \
--resource-group <resource group name> \
--storage-account <storage account name> \
--storage-container <storage container name> \
[--rancher] \
[-b <branch>] \
[-w]
```

NOTE: The config file for the AKS provisioning pipeline is located at `/scripts/pipelines/{path_provider}/templates/aks/aks-pipeline.cfg`.

=== Flags
```
-c, --config-file [Required] Configuration file containing pipeline definition.
-n, --pipeline-name [Required] Name that will be set to the pipeline.
-d, --local-directory [Required] Local directory of your project (the path should always be using '/' and not '\').
--resource-group [Required] Name of the resource group for the cluster.
--storage--account [Required] Name of the storage account for the cluster.
--storage-container [Required] Name of the storage container where the Terraform state of the cluster will be stored.
--rancher Install Rancher to manage the cluster.
-b, --target-branch Name of the branch to which the Pull Request will target. PR is not created if the flag is not provided.
-w Open the Pull Request on the web browser if it cannot be automatically merged. Requires -b flag.
```

=== Example

```
{cmd_add}
```

NOTE: Rancher is installed on the cluster after provisioning when using the above command.

=== Appendix: Interacting with the cluster

NOTE: Make sure you have https://kubernetes.io/docs/tasks/tools/#kubectl[kubectl] installed.

In order to interact with your cluster you will need to download the artifact `kubeconfig` generated by the cluster provisioning pipeline on the location it is expected by default (`~/.kube/config`) or either:

```
# via environment variable (you can add this on your profile)
export KUBECONFIG=<path to downloaded kubeconfig>
kubectl <command>

# via command-line flag
kubectl <command> --kubeconfig=<path to downloaded kubeconfig>
```

To get the DNS name of the NGINX Ingress controller on the AKS cluster, go into {aks_variables}.

Rancher, if installed, will be available on `https://<ingress controller domain>/dashboard`. You will be asked for an initial password, which can be retrieved with:

```
kubectl get secret --namespace cattle-system bootstrap-secret -o go-template='{{.data.bootstrapPassword|base64decode}}{{"\n"}}'
```

=== Appendix: Destroying the cluster

To destroy the provisioned resources, set `operation` pipeline variable value to `destroy` and run the pipeline.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:provider: Github
:pipeline_type: workflow
:trigger_sentence: * Add AZURE credentials as https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository[Github Secrets] in your repository and name it `AZURE_USERNAME`,`AZURE_PASSWORD`. If you already have a available credentials or you need a specific credentials connection, please update `aks-provisioning.yml` accordingly.
:pipeline_type2: Github action
:path_provider: github
:cmd_add: ./pipeline_generator.sh -c ./templates/aks/aks-pipeline.cfg -n aks-provisioning -d C:/Users/$USERNAME/Desktop/quarkus-project --cluster-name devon-hangar --resource-group devonfw --storage-account hangar --storage-container aks-state --rancher -b develop -w
:aks_variables: .github > vars > `aks-variables.env`
include::../common_templates/setup-build-pipeline.asciidoc[]
4 changes: 2 additions & 2 deletions scripts/environment-provisioning/azure/aks/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cluster_name = "hangar-k8s"
#cluster_name = "hangar-k8s"

location = "westeurope"

resource_group_name = "hangar-aks-rg"
resource_group_name = "az-devops-rg"

instance_type = "Standard_DS2_v2"

Expand Down
3 changes: 2 additions & 1 deletion scripts/pipelines/github/pipeline_generator.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
set -e
FLAGS=$(getopt -a --options c:n:d:a:b:l:i:u:p:hw --long "config-file:,pipeline-name:,local-directory:,artifact-path:,target-branch:,language:,build-pipeline-name:,sonar-url:,sonar-token:,image-name:,registry-user:,registry-password:,resource-group:,storage-account:,storage-container:,cluster-name:,s3-bucket:,s3-key-path:,quality-pipeline-name:,dockerfile:,test-pipeline-name:,aws-access-key:,aws-secret-access-key:,aws-region:,help" -- "$@")
FLAGS=$(getopt -a --options c:n:d:a:b:l:i:u:p:hw --long "config-file:,pipeline-name:,local-directory:,artifact-path:,target-branch:,language:,build-pipeline-name:,sonar-url:,sonar-token:,image-name:,registry-user:,registry-password:,resource-group:,storage-account:,storage-container:,cluster-name:,s3-bucket:,s3-key-path:,quality-pipeline-name:,dockerfile:,test-pipeline-name:,aws-access-key:,aws-secret-access-key:,aws-region:,help,rancher" -- "$@")

eval set -- "$FLAGS"
while true; do
Expand All @@ -20,6 +20,7 @@ while true; do
--resource-group) resourceGroupName=$2; shift 2;;
--storage-account) storageAccountName=$2; shift 2;;
--storage-container) storageContainerName=$2; shift 2;;
--rancher) installRancher="true"; shift 1;;
prathibhapadma marked this conversation as resolved.
Show resolved Hide resolved
prathibhapadma marked this conversation as resolved.
Show resolved Hide resolved
--cluster-name) clusterName=$2; shift 2;;
--s3-bucket) s3Bucket=$2; shift 2;;
--s3-key-path) s3KeyPath=$2; shift 2;;
Expand Down
61 changes: 61 additions & 0 deletions scripts/pipelines/github/templates/aks/aks-pipeline.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Mandatory flags.
mandatoryFalgs="$pipelineName,$localDirectory,$clusterName,$resourceGroupName,$storageAccountName,$storageContainerName,"
# Path to the templates.
templatesPath="scripts/pipelines/github/templates/aks"
#Path to common kubernetes templates.
commonKubernetesPath="scripts/pipelines/github/templates/common/kubernetes"
# YAML file name.
yamlFile="aks-provisioning.yml"
# Source branch.
sourceBranch="feature/aks-provisioning"
# Path to terraform templates.
terraformTemplatesPath="scripts/environment-provisioning/azure/aks"
# Path to terraform scripts.
terraformPath=".terraform/aks"
# Default cluster operation.
operation="create"
# Install Rancher on AKS cluster.
if test -z $installRancher
then
installRancher=false
fi

# Function that copies the necessary scripts into the directory.
function copyScript {
# Create .terraform/aks folder if it does not exist.
mkdir -p "${localDirectory}/${terraformPath}"

# Copy the terraform files.
cd "${hangarPath}/${terraformTemplatesPath}"
cp * "${localDirectory}/${terraformPath}"

# Copy the script for the DNS name into the directory.
cp "${hangarPath}/${templatesPath}/obtain-dns.sh" "${localDirectory}/${scriptFilePath}/obtain-dns.sh"

# Copy the common files for kubernetes
cp "${hangarPath}/${commonKubernetesPath}"/*.sh "${localDirectory}/${scriptFilePath}"
}

function commitFiles {
# Add the terraform files.
git add .terraform -f

# Changing all files to be executable.
find .terraform -type f -name '*.sh' -exec git update-index --chmod=+x {} \;

# Git commit and push it into the repository.
git commit -m "Adding the terraform files"
git push -u origin ${sourceBranch}
}

# Function that adds the variables to be used in the pipeline.
function addPipelineVariables {

export clusterName
export resourceGroupName
export storageAccountName
export storageContainerName
export installRancher
export operation
specificEnvSubstList='${clusterName} ${resourceGroupName} ${storageAccountName} ${storageContainerName} ${installRancher} ${operation}'
}
Loading