Skip to content

Commit

Permalink
terraform version upgrade and code reorg (#39)
Browse files Browse the repository at this point in the history
terraform version upgrade and code reorg
  • Loading branch information
yupwei68 authored Feb 7, 2020
1 parent d365197 commit e6b0bff
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 378 deletions.
40 changes: 31 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
# Pull the base image with given version.
ARG BUILD_TERRAFORM_VERSION=0.11.7
FROM microsoft/terraform-test:${BUILD_TERRAFORM_VERSION}
ARG BUILD_TERRAFORM_VERSION="0.12.10"
FROM mcr.microsoft.com/terraform-test:${BUILD_TERRAFORM_VERSION}

ARG MODULE_NAME="terraform-azurerm-aks"

# Set work directory
RUN mkdir -p /go/src/${MODULE_NAME}
RUN mkdir -p /go/bin
# Declare default build configurations for terraform.
ARG BUILD_ARM_SUBSCRIPTION_ID=""
ARG BUILD_ARM_CLIENT_ID=""
ARG BUILD_ARM_CLIENT_SECRET=""
ARG BUILD_ARM_TENANT_ID=""
ARG BUILD_ARM_TEST_LOCATION="WestEurope"
ARG BUILD_ARM_TEST_LOCATION_ALT="WestUS"

# Set environment variables for terraform runtime.
ENV ARM_SUBSCRIPTION_ID=${BUILD_ARM_SUBSCRIPTION_ID}
ENV ARM_CLIENT_ID=${BUILD_ARM_CLIENT_ID}
ENV ARM_CLIENT_SECRET=${BUILD_ARM_CLIENT_SECRET}
ENV ARM_TENANT_ID=${BUILD_ARM_TENANT_ID}
ENV ARM_TEST_LOCATION=${BUILD_ARM_TEST_LOCATION}
ENV ARM_TEST_LOCATION_ALT=${BUILD_ARM_TEST_LOCATION_ALT}

# Set environment variables for variables used in AKS.
ENV TF_VAR_client_id=${BUILD_ARM_CLIENT_ID}
ENV TF_VAR_client_secret=${BUILD_ARM_CLIENT_SECRET}

# Set work directory.
RUN mkdir /go
RUN mkdir /go/bin
RUN mkdir /go/src
RUN mkdir /go/src/${MODULE_NAME}
COPY . /go/src/${MODULE_NAME}
WORKDIR /go/src/${MODULE_NAME}

# Install required go packages using dep ensure
# Install dep.
ENV GOPATH /go
ENV PATH $GOPATH/bin:$PATH
ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
RUN /bin/bash -c "curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh"

COPY . /go/src/${MODULE_NAME}
RUN chmod 744 test.sh
RUN ["bundle", "install", "--gemfile", "./Gemfile"]
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ruby "~> 2.3.0"

source 'https://rubygems.org/'

group :test do
git 'https://github.com/Azure/terramodtest.git' do
gem 'terramodtest', :tag => 'v0.3.0'
end
end
144 changes: 87 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,87 +6,117 @@ This Terraform module deploys a Kubernetes cluster on Azure using AKS (Azure Kub
## Usage

```hcl
module "aks" {
source = "Azure/aks/azurerm"
version = "2.0.0"
resource "azurerm_resource_group" "example" {
name = "ask-resource-group"
location = "eastus"
}
CLIENT_ID = "your-service-principal-client-appid"
CLIENT_SECRET = "your-service-principal-client-password"
prefix = "your-custom-resource-prefix"
module "aks" {
source = "Azure/aks/azurerm"
resource_group_name = azurerm_resource_group.example.name
client_id = "your-service-principal-client-appid"
client_secret = "your-service-principal-client-password"
prefix = "prefix"
}
```

This module is configured through variables. Make sure to select an [Azure location that supports AKS](https://azure.microsoft.com/en-us/global-infrastructure/services/?products=kubernetes-service) and to [have a Service Principal created](https://www.terraform.io/docs/providers/azurerm/authenticating_via_service_principal.html). If no public ssh key is set through variables, a newly generated public key will be used and the private key will be saved in a *private_ssh_key* file.

See below for the default variable values.
The module supports some outputs that may be used to configure a kubernetes
provider after deploying an AKS cluster.

```hcl
variable "prefix" {
description = "The prefix for the resources created in the specified Azure Resource Group"
provider "kubernetes" {
host = "${module.aks.host}"
client_certificate = "${base64decode(module.aks.client_certificate)}"
client_key = "${base64decode(module.aks.client_key)}"
cluster_ca_certificate = "${base64decode(module.aks.cluster_ca_certificate)}"
}
```

variable "location" {
default = "eastus"
description = "The location for the AKS deployment"
}
## Test

variable "CLIENT_ID" {
description = "The Client ID (appId) for the Service Principal used for the AKS deployment"
}
### Configurations

variable "CLIENT_SECRET" {
description = "The Client Secret (password) for the Service Principal used for the AKS deployment"
}
- [Configure Terraform for Azure](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure)

variable "admin_username" {
default = "azureuser"
description = "The username of the local administrator to be created on the Kubernetes cluster"
}
We provide 2 ways to build, run, and test the module on a local development machine. [Native (Mac/Linux)](#native-maclinux) or [Docker](#docker).

variable "agents_size" {
default = "Standard_F2"
description = "The default virtual machine size for the Kubernetes agents"
}
### Native (Mac/Linux)

variable "log_analytics_workspace_sku" {
description = "The SKU (pricing level) of the Log Analytics workspace. For new subscriptions the SKU should be set to PerGB2018"
default = "PerGB2018"
}
#### Prerequisites

variable "log_retention_in_days" {
description = "The retention period for the logs in days"
default = 30
}
- [Ruby **(~> 2.3)**](https://www.ruby-lang.org/en/downloads/)
- [Bundler **(~> 1.15)**](https://bundler.io/)
- [Terraform **(~> 0.11.7)**](https://www.terraform.io/downloads.html)
- [Golang **(~> 1.10.3)**](https://golang.org/dl/)

variable "agents_count" {
description = "The number of Agents that should exist in the Agent Pool"
default = 2
}
#### Environment setup

variable "kubernetes_version" {
description = "Version of Kubernetes to install"
default = "1.14.5"
}
We provide simple script to quickly set up module development environment:

variable "public_ssh_key" {
description = "A custom ssh key to control access to the AKS cluster"
default = ""
}
```sh
$ curl -sSL https://raw.githubusercontent.com/Azure/terramodtest/master/tool/env_setup.sh | sudo bash
```

The module supports some outputs that may be used to configure a kubernetes
provider after deploying an AKS cluster.
#### Run test

Then simply run it in local shell:

```sh
$ cd $GOPATH/src/{directory_name}/
$ dep ensure

# set service principal
$ export ARM_CLIENT_ID="service-principal-client-id"
$ export ARM_CLIENT_SECRET="service-principal-client-secret"
$ export ARM_SUBSCRIPTION_ID="subscription-id"
$ export ARM_TENANT_ID="tenant-id"
$ export ARM_TEST_LOCATION="eastus"
$ export ARM_TEST_LOCATION_ALT="eastus2"
$ export ARM_TEST_LOCATION_ALT2="westus"

# set aks variables
$ export TF_VAR_client_id="service-principal-client-id"
$ export TF_VAR_client_secret="service-principal-client-secret"

# run test
$ go test -v ./test/ -timeout 45m
```
provider "kubernetes" {
host = "${module.aks.host}"

client_certificate = "${base64decode(module.aks.client_certificate)}"
client_key = "${base64decode(module.aks.client_key)}"
cluster_ca_certificate = "${base64decode(module.aks.cluster_ca_certificate)}"
}
### Docker

We provide a Dockerfile to build a new image based `FROM` the `mcr.microsoft.com/terraform-test` Docker hub image which adds additional tools / packages specific for this module (see Custom Image section). Alternatively use only the `microsoft/terraform-test` Docker hub image [by using these instructions](https://github.com/Azure/terraform-test).

#### Prerequisites

- [Docker](https://www.docker.com/community-edition#/download)

#### Custom Image

This builds the custom image:

```sh
$ docker build --build-arg BUILD_ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID --build-arg BUILD_ARM_CLIENT_ID=$ARM_CLIENT_ID --build-arg BUILD_ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET --build-arg BUILD_ARM_TENANT_ID=$ARM_TENANT_ID -t azure-aks .
```

This runs the build and unit tests:

```sh
$ docker run --rm azure-aks /bin/bash -c "bundle install && rake build"
```

This runs the end to end tests:

```sh
$ docker run --rm azure-aks /bin/bash -c "bundle install && rake e2e"
```

This runs the full tests:

```sh
$ docker run --rm azure-aks /bin/bash -c "bundle install && rake full"
```


## Authors

Originally created by [Damien Caro](http://github.com/dcaro) and [Malte Lantin](http://github.com/n01d)
Expand Down
58 changes: 58 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Official gems.
require 'colorize'
require 'rspec/core/rake_task'

# Git repo gems.
require 'bundler/setup'
require 'terramodtest'

namespace :presteps do
task :ensure do
puts "Using dep ensure to install required go packages.\n"
success = system ("dep ensure")
if not success
raise "ERROR: Dep ensure failed!\n".red
end
end
end

namespace :static do
task :style do
style_tf
end
task :lint do
success = system ("terraform init")
if not success
raise "ERROR: terraform init failed!\n".red
end
lint_tf
end
task :format do
format_tf
end
end

namespace :integration do
task :test do
success = system ("go test -v ./test/ -timeout 45m")
if not success
raise "ERROR: Go test failed!\n".red
end
end
end

task :prereqs => [ 'presteps:ensure' ]

task :validate => [ 'static:style', 'static:lint' ]

task :format => [ 'static:format' ]

task :build => [ 'prereqs', 'validate' ]

task :unit => []

task :e2e => [ 'integration:test' ]

task :default => [ 'build' ]

task :full => [ 'build', 'unit', 'e2e' ]
18 changes: 11 additions & 7 deletions azure-pipelines.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Azure pipeline for Terraform AKS module

pool:
vmImage: 'Ubuntu 16.04'
vmImage: 'ubuntu-latest'

variables:
imagename: terraform-azurerm-aks:$(build.buildId)
image_name: terraform-azurerm-aks:$(build.buildId)
terraform_version: 0.12.10

steps:
- script: docker build -f Dockerfile -t $(imageName) .
displayName: 'docker build'
- script: docker build --build-arg BUILD_TERRAFORM_VERSION=${TERRAFORM_VERSION} -t ${IMAGE_NAME} .
displayName: 'docker build'

- script: docker run ${IMAGE_NAME} rake build
displayName: 'validate'

- script: docker run -e "ARM_SUBSCRIPTION_ID=$AZURE_SUBSCRIPTION_ID" -e "ARM_CLIENT_ID=$AZURE_CLIENT_ID" -e "ARM_CLIENT_SECRET=$AZURE_CLIENT_SECRET" -e "ARM_TENANT_ID=$AZURE_TENANT_ID" -e "ARM_TEST_LOCATION=WestUS2" -e "ARM_TEST_LOCATION_ALT=EastUS" --rm $(imageName) bash -c "./test.sh validate"
displayName: 'docker run'
- script: docker run $(IMAGE_NAME) rake full
displayName: 'full build'
condition: and(succeeded(),eq(variables['build.sourceBranch'], 'refs/heads/master'))
Loading

0 comments on commit e6b0bff

Please sign in to comment.