Skip to content

Commit

Permalink
Merge pull request #11 from Pamir/master
Browse files Browse the repository at this point in the history
#2 issue custom modules sample added for aks
  • Loading branch information
Pamir authored Apr 12, 2020
2 parents 26eaba9 + 2a9d5df commit 09e379c
Show file tree
Hide file tree
Showing 22 changed files with 703 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 08-modules/02-custom-modules/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
}

#Search for warnings in tf apply
module "aks" {
source = "./modules/aks/azurerm-aks"
resource_group_name = azurerm_resource_group.example.name
client_id = var.client_id
client_secret = var.client_secret
prefix = "az"
}
50 changes: 50 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Variable files
terraform.tfvars

### https://raw.github.com/github/gitignore/abad92dac5a4306f72242dae3bca6e277bce3615/Terraform.gitignore

# Compiled files
*.tfstate
*.tfstate.backup
*.tfvars

# Terraform directory
.terraform/
terraform.tfstate.d/
logs/

# Go vendor directory
vendor/

# Files generated by terratest
.test-data/

# Terraform log file
terraform.log

### https://raw.github.com/github/gitignore/abad92dac5a4306f72242dae3bca6e277bce3615/Global/Vim.gitignore

# swap
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags

# IDE configs
.idea

# Ruby download package lock file.
Gemfile.lock

# Mac folder attribute file
.DS_Store

.terraform.tfstate.lock.info

# SSH Key
private_ssh_key
40 changes: 40 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Pull the base image with given version.
ARG BUILD_TERRAFORM_VERSION="0.12.10"
FROM mcr.microsoft.com/terraform-test:${BUILD_TERRAFORM_VERSION}

ARG MODULE_NAME="terraform-azurerm-aks"

# 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 dep.
ENV GOPATH /go
ENV PATH /usr/local/go/bin:$GOPATH/bin:$PATH
RUN /bin/bash -c "curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh"

RUN ["bundle", "install", "--gemfile", "./Gemfile"]
9 changes: 9 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/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
37 changes: 37 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true

[[constraint]]
name = "github.com/gruntwork-io/terratest"
version = "0.9.15"

[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/lib/pq"
version = "1.0.0"
21 changes: 21 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
140 changes: 140 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# terraform-azurerm-aks
## Deploys a Kubernetes cluster on AKS with monitoring support through Azure Log Analytics

This Terraform module deploys a Kubernetes cluster on Azure using AKS (Azure Kubernetes Service) and adds support for monitoring with Log Analytics.

## Usage

```hcl
resource "azurerm_resource_group" "example" {
name = "ask-resource-group"
location = "eastus"
}
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"
}
```

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

```hcl
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)}"
}
```

## Test

### Configurations

- [Configure Terraform for Azure](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure)

We provide 2 ways to build, run, and test the module on a local development machine. [Native (Mac/Linux)](#native-maclinux) or [Docker](#docker).

### Native (Mac/Linux)

#### Prerequisites

- [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/)

#### Environment setup

We provide simple script to quickly set up module development environment:

```sh
$ curl -sSL https://raw.githubusercontent.com/Azure/terramodtest/master/tool/env_setup.sh | sudo bash
```

#### 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
```

### 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)

## License

[MIT](LICENSE)

# Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
58 changes: 58 additions & 0 deletions 08-modules/02-custom-modules/modules/aks/azurerm-aks/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' ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Azure pipeline for Terraform AKS module
pool:
vmImage: 'ubuntu-latest'

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

steps:
- 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 $(IMAGE_NAME) rake full
displayName: 'full build'
condition: and(succeeded(),eq(variables['build.sourceBranch'], 'refs/heads/master'))
Loading

0 comments on commit 09e379c

Please sign in to comment.