Skip to content

Commit

Permalink
Merge pull request #383 from kubenow/feature/azure-alternative-bootimage
Browse files Browse the repository at this point in the history
 Azure alternative boot image and some other things (1)
  • Loading branch information
andersla authored May 14, 2018
2 parents 14fad84 + 6290758 commit 52a5d6e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 58 deletions.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ RUN apt-get update -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Install json2hcl
RUN curl -L "https://github.com/kvz/json2hcl/releases/download/v0.0.6/json2hcl_v0.0.6_linux_amd64" > \
"/bin/json2hcl" && \
chmod a+rx /bin/json2hcl

# Install Terraform
RUN curl "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" > \
"terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<span class="badge-slack"><a href="https://kubenow-slackin.herokuapp.com" title="Invite yourself to our Slack team"><img src="https://kubenow-slackin.herokuapp.com/badge.svg" alt="Slackin button"/></a></span>
<span class="badge-patreon"><a href="https://patreon.com/kubenow" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span>


KubeNow is a cloud agnostic platform for microservices, based on Docker and Kubernetes. Other than lighting-fast Kubernetes operations, KubeNow helps you in lifting your final application configuring DNS records and distributed storage. Once you have defined your application as a Helm package, lifting it is as simple as:

```bash
Expand Down
96 changes: 67 additions & 29 deletions azure/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# Cluster settings
variable cluster_prefix {}

variable boot_image {}
# This var is for KubeNow boot image version
# leave empty if public image is specified
variable boot_image {
default = ""
}

# This var is for any Azure boot image
# leave empty if KubeNow image is specified
variable boot_image_public {
type = "map"

default = {
publisher = ""
offer = ""
sku = ""
version = ""
}
}

variable bootstrap_script {
default = "bootstrap/bootstrap-default.sh"
Expand Down Expand Up @@ -120,13 +137,30 @@ provider "azurerm" {
# Data-lookup, subscriptin_id etc.
data "azurerm_client_config" "current" {}

# This is for KubeNow boot image only
# Generates image resource group name by adding location as suffix (without spaces)
data "null_data_source" "image_rg" {
inputs = {
name = "${var.image_resource_group_prefix}-${replace(var.location, " " , "")}"
}
}

# This is for KubeNow boot image only
# Generates KubeNow image_id
data "null_data_source" "private_img" {
inputs = {
image_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${data.null_data_source.image_rg.inputs.name}/providers/Microsoft.Compute/images/${var.boot_image}"
}
}

# This is for KubeNow boot image only
# Sets image_id to KubeNow image id if KubeNow image is specified otherwise ""
data "null_data_source" "boot_image_private_id_lookup" {
inputs = {
image_id = "${var.boot_image == "" ? "" : data.null_data_source.private_img.inputs.image_id}"
}
}

# Resource-group
resource "azurerm_resource_group" "rg" {
name = "${var.cluster_prefix}-rg"
Expand All @@ -151,13 +185,14 @@ module "network" {

module "master" {
# Core settings
source = "./node"
count = "${var.master_count}"
name_prefix = "${var.cluster_prefix}-master"
vm_size = "${var.master_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
image_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${data.null_data_source.image_rg.inputs.name}/providers/Microsoft.Compute/images/${var.boot_image}"
location = "${var.location}"
source = "./node"
count = "${var.master_count}"
name_prefix = "${var.cluster_prefix}-master"
vm_size = "${var.master_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
boot_image_public = "${var.boot_image_public}"
boot_image_private_id = "${data.null_data_source.boot_image_private_id_lookup.inputs.image_id}"
location = "${var.location}"

# SSH settings
ssh_user = "${var.ssh_user}"
Expand All @@ -178,13 +213,14 @@ module "master" {

module "node" {
# Core settings
source = "./node"
count = "${var.node_count}"
name_prefix = "${var.cluster_prefix}-node"
vm_size = "${var.node_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
image_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${data.null_data_source.image_rg.inputs.name}/providers/Microsoft.Compute/images/${var.boot_image}"
location = "${var.location}"
source = "./node"
count = "${var.node_count}"
name_prefix = "${var.cluster_prefix}-node"
vm_size = "${var.node_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
boot_image_public = "${var.boot_image_public}"
boot_image_private_id = "${data.null_data_source.boot_image_private_id_lookup.inputs.image_id}"
location = "${var.location}"

# SSH settings
ssh_user = "${var.ssh_user}"
Expand All @@ -205,13 +241,14 @@ module "node" {

module "edge" {
# Core settings
source = "./node"
count = "${var.edge_count}"
name_prefix = "${var.cluster_prefix}-edge"
vm_size = "${var.node_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
image_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${data.null_data_source.image_rg.inputs.name}/providers/Microsoft.Compute/images/${var.boot_image}"
location = "${var.location}"
source = "./node"
count = "${var.edge_count}"
name_prefix = "${var.cluster_prefix}-edge"
vm_size = "${var.node_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
boot_image_public = "${var.boot_image_public}"
boot_image_private_id = "${data.null_data_source.boot_image_private_id_lookup.inputs.image_id}"
location = "${var.location}"

# SSH settings
ssh_user = "${var.ssh_user}"
Expand All @@ -232,13 +269,14 @@ module "edge" {

module "glusternode" {
# Core settings
source = "./node-extra-disk"
count = "${var.glusternode_count}"
name_prefix = "${var.cluster_prefix}-glusternode"
vm_size = "${var.glusternode_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
image_id = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourceGroups/${data.null_data_source.image_rg.inputs.name}/providers/Microsoft.Compute/images/${var.boot_image}"
location = "${var.location}"
source = "./node-extra-disk"
count = "${var.glusternode_count}"
name_prefix = "${var.cluster_prefix}-glusternode"
vm_size = "${var.glusternode_vm_size}"
resource_group_name = "${azurerm_resource_group.rg.name}"
boot_image_public = "${var.boot_image_public}"
boot_image_private_id = "${data.null_data_source.boot_image_private_id_lookup.inputs.image_id}"
location = "${var.location}"

# SSH settings
ssh_user = "${var.ssh_user}"
Expand Down
19 changes: 17 additions & 2 deletions azure/node-extra-disk/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ variable name_prefix {}
variable resource_group_name {}
variable location {}
variable vm_size {}
variable image_id {}

# This var is for KubeNow boot image version
# will be empty if public image is specified
variable boot_image_private_id {}

# This var is for any Azure boot image
# will be empty if KubeNow image is specified
variable boot_image_public {
type = "map"
}

# SSH settings
variable ssh_user {}
Expand Down Expand Up @@ -107,8 +116,14 @@ resource "azurerm_virtual_machine" "vm" {
vm_size = "${var.vm_size}"
network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]

# if id specified, other params will be empty
# if id is not specified, other params will be set
storage_image_reference {
id = "${var.image_id}"
id = "${var.boot_image_private_id}"
publisher = "${var.boot_image_public["publisher"]}"
offer = "${var.boot_image_public["offer"]}"
sku = "${var.boot_image_public["sku"]}"
version = "${var.boot_image_public["version"]}"
}

storage_os_disk {
Expand Down
19 changes: 17 additions & 2 deletions azure/node/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ variable name_prefix {}
variable resource_group_name {}
variable location {}
variable vm_size {}
variable image_id {}

# This var is for KubeNow boot image version
# will be empty if public image is specified
variable boot_image_private_id {}

# This var is for any Azure boot image
# will be empty if KubeNow image is specified
variable boot_image_public {
type = "map"
}

# SSH settings
variable ssh_user {}
Expand Down Expand Up @@ -94,8 +103,14 @@ resource "azurerm_virtual_machine" "vm" {
vm_size = "${var.vm_size}"
network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]

# if id specified, other params will be empty
# if id is not specified, other params will be set
storage_image_reference {
id = "${var.image_id}"
id = "${var.boot_image_private_id}"
publisher = "${var.boot_image_public["publisher"]}"
offer = "${var.boot_image_public["offer"]}"
sku = "${var.boot_image_public["sku"]}"
version = "${var.boot_image_public["version"]}"
}

storage_os_disk {
Expand Down
22 changes: 9 additions & 13 deletions bin/image-create-azure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ if [ -z "${TF_VARS_FILE}" ]; then
fi

# Get vars from tfvars-file
arm_client_id=$(grep "client_id" "$TF_VARS_FILE" |
arm_client_id=$(grep "^client_id" "$TF_VARS_FILE" |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')

arm_client_secret=$(grep "client_secret" "$TF_VARS_FILE" |
arm_client_secret=$(grep "^client_secret" "$TF_VARS_FILE" |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')

arm_tenant_id=$(grep "tenant_id" "$TF_VARS_FILE" |
arm_tenant_id=$(grep "^tenant_id" "$TF_VARS_FILE" |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')

arm_location=$(grep "location" "$TF_VARS_FILE" |
arm_location=$(grep "^location" "$TF_VARS_FILE" |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')

Expand Down Expand Up @@ -133,21 +133,17 @@ if [ -z "$image_details" ]; then

done_bytes=$(echo "$progress" | cut -d '/' -f 1)
total_bytes=$(echo "$progress" | cut -d '/' -f 2)
# The total bytes show actual file size, but since only about 3.5GB of 30GB is being
# used, the last 26.5GB is zero-filled and copied instantly, then file copy progress displayed
# to user would be very skewed if not adjusted down to display used bytes)
ACTUAL_IMAGE_FILE_SIZE=3450000000
percent=$(bc -l <<<"($done_bytes/$ACTUAL_IMAGE_FILE_SIZE)*100")
# Never more than 99.99% (this could happen when ACTUAL_IMAGE_FILE_SIZE is set to small)
percent=$(bc -l <<<"if ($percent > 100) 99.99 else $percent")
percent=$(bc -l <<<"($done_bytes/$total_bytes)*100")
done_mbytes=$(bc -l <<<"($done_bytes/1000000)")
total_mbytes=$(bc -l <<<"($total_bytes/1000000)")
fi

# Every time print status message with an updated spinner symbol
printf '\r %s Image copy progress: %.2f%%' "${spin:$n:1}" "$percent"
printf '\r %s Image copy progress: %.2f%% %.2f / %.2f MB (sometimes the copy process is much faster after ca 10%%)' "${spin:$n:1}" "$percent" "$done_mbytes" "$total_mbytes"

# Break when finished
if [[ "$done_bytes" == "$total_bytes" ]]; then
printf '\rDone copy image \n'
printf '\rDone copy image \n'
break
fi

Expand Down
2 changes: 1 addition & 1 deletion bin/kn
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function docker_run() {
--env-file <(env | grep GOOGLE_) \
--env-file <(env | grep AWS_) \
--env-file <(env | grep ARM_) \
--env-file <(env | grep KUBENOW_) \
--env-file <(env | grep KN_) \
"$container" \
"/opt/KubeNow/bin/docker-entrypoint" ${@:3}
}
Expand Down
14 changes: 7 additions & 7 deletions bin/kn-apply
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# Exit immediately if a command exits with a non-zero status
set -e

host_cloud=$(grep -w "provider" config.tfvars |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')
# Read config file into variable as as json
kn_config=$(json2hcl -reverse <config.tfvars)

image_name=$(grep -w "boot_image" config.tfvars |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')
# Set variables from json-config
host_cloud=$(jq -r '.provider' <<<"$kn_config")
image_name=$(jq -r '.boot_image' <<<"$kn_config")
skip_image_import=$(jq -r '.skip_image_import' <<<"$kn_config")

# Check for recognized cloud provider
if ! grep -qw "$host_cloud" <<<"openstack gce azure aws"; then
Expand All @@ -18,7 +18,7 @@ if ! grep -qw "$host_cloud" <<<"openstack gce azure aws"; then
fi

# Import image (AWS doesn't need it)
if [ "$host_cloud" = 'openstack' ] || [ "$host_cloud" = 'gce' ] || [ "$host_cloud" = 'azure' ]; then
if [[ "$skip_image_import" != "true" ]] && [[ "$host_cloud" != 'aws' ]]; then
export KN_GCE_ACCOUNT_FILE_PATH="$PWD/service-account.json"
export TF_VARS_FILE="$PWD/config.tfvars"
export KN_IMAGE_NAME="$image_name"
Expand Down
11 changes: 8 additions & 3 deletions bin/kn-destroy
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
# Exit immediately if a command exits with a non-zero status
set -e

host_cloud=$(grep -w "provider" config.tfvars |
cut -d "=" -f 2- |
awk -F\" '{print $(NF-1)}')
# Run pre-destroy hook
/KubeNow_root/bin/pre-destroy

# Read config file into variable as as json
kn_config=$(json2hcl -reverse <config.tfvars)

# Set variables from json-config
host_cloud=$(jq -r '.provider' <<<"$kn_config")

# Check for recognized cloud provider
if ! grep -qw "$host_cloud" <<<"openstack gce azure aws"; then
Expand Down

0 comments on commit 52a5d6e

Please sign in to comment.