Skip to content

Commit

Permalink
Upgrading terraform code to support v0.12.3
Browse files Browse the repository at this point in the history
  • Loading branch information
balaguduru committed Jul 18, 2019
1 parent 125a7e1 commit 6f2e12f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 101 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ metadata:
spec:
containers:
- name: ${containerName}
image: gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${env.CONTAINER_VERSION}
image: gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${env.JENKINS_CONTAINER_VERSION}
command: ['cat']
tty: true
volumeMounts:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ In order to use the code in this demo you will need access to the following tool
* Access to an existing Google Cloud project with the
[Kubernetes Engine v1.10.0 or later](https://cloud.google.com/kubernetes-engine/docs/quickstart#before-you-begin) service enabled
* If you do not have a Google Cloud Platform account you can sign up [here](https://cloud.google.com) and get 300 dollars of free credit on your new account.
* [Google Cloud SDK (200.0.0 or later)](https://cloud.google.com/sdk/downloads)
* [Google Cloud SDK version >= 253.0.0](https://cloud.google.com/sdk/docs/downloads-versioned-archives)
* [ApacheBench](https://httpd.apache.org/docs/2.4/programs/ab.html)
* [HashiCorp Terraform (>= v0.11.7)](https://www.terraform.io/downloads.html)
* [Hashicorp Terraform >= 0.12.3](https://www.terraform.io/downloads.html)
* [gcloud](https://cloud.google.com/sdk/gcloud/)
* [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)

Expand Down
166 changes: 82 additions & 84 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
// Provides access to available Google Container Engine versions in a zone for a given project.
// https://www.terraform.io/docs/providers/google/d/google_container_engine_versions.html
data "google_container_engine_versions" "on-prem" {
zone = "${var.zone}"
project = "${var.project}"
zone = var.zone
project = var.project
}

// https://www.terraform.io/docs/providers/template/index.html
Expand Down Expand Up @@ -77,32 +77,34 @@ data "template_file" "startup_script" {
- systemctl start flaskservice.service
EOF

vars {
project = "${var.project}"
version = "${var.ver}"

vars = {
project = var.project
version = var.ver
}
}

// https://www.terraform.io/docs/providers/google/r/compute_instance.html
// The ContainerOS deployment instance definition which will
// run the container instead of as the interpreted python code.
resource "google_compute_instance" "container_server" {
name = "cos-vm"
machine_type = "${var.machine_type}"
zone = "${var.zone}"
project = "${var.project}"
name = "cos-vm"
machine_type = var.machine_type
zone = var.zone
project = var.project

tags = ["flask-web"]
tags = ["flask-web"]

boot_disk {
initialize_params {
image = "cos-cloud/cos-stable"
}
}

metadata {
user-data = "${data.template_file.startup_script.rendered}"
metadata = {
user-data = data.template_file.startup_script.rendered
}

//metadata_startup_script = "${data.template_file.startup_script.rendered}"
network_interface {
network = "default"
Expand All @@ -115,17 +117,16 @@ resource "google_compute_instance" "container_server" {
service_account {
scopes = ["storage-ro"]
}

}

// The Kubernetes Engine cluster used to deploy the application
// https://www.terraform.io/docs/providers/google/r/container_cluster.html
resource "google_container_cluster" "prime_cluster" {
name = "${var.cluster_name}"
zone = "${var.zone}"
project = "${var.project}"
min_master_version = "${data.google_container_engine_versions.on-prem.latest_master_version}"
initial_node_count = 2
name = var.cluster_name
zone = var.zone
project = var.project
min_master_version = data.google_container_engine_versions.on-prem.latest_master_version
initial_node_count = 2
}

// Create a deployment manifest with the appropriate values
Expand Down Expand Up @@ -167,107 +168,104 @@ data "template_file" "deployment_manifest" {
initialDelaySeconds: 10
EOF

vars {
project = "${var.project}"
version = "${var.ver}"
replicas = "${var.replicas}"
}

vars = {
project = var.project
version = var.ver
replicas = var.replicas
}
}

// Render the deployment manifest on the local filesystem using a null resource
// https://www.terraform.io/docs/provisioners/null_resource.html
resource "null_resource" "deployment_manifest" {
triggers {
template = "${data.template_file.deployment_manifest.rendered}"
}

provisioner "local-exec" {
command = "echo \"${data.template_file.deployment_manifest.rendered}\" > ${path.module}/manifests/prime-server-deployment.yaml"
}
triggers = {
template = data.template_file.deployment_manifest.rendered
}

provisioner "local-exec" {
command = "echo \"${data.template_file.deployment_manifest.rendered}\" > ${path.module}/manifests/prime-server-deployment.yaml"
}
}

resource "null_resource" "local_config" {

provisioner "local-exec" {
command = "gcloud container clusters get-credentials prime-server-cluster --project ${var.project}"
}
depends_on = [
"google_container_cluster.prime_cluster"
]
provisioner "local-exec" {
command = "gcloud container clusters get-credentials prime-server-cluster --project ${var.project}"
}
depends_on = [google_container_cluster.prime_cluster]
}

// This bucket will hold the deployment artifact, the tar file containing the
// prime-server
//
resource "google_storage_bucket" "artifact_store" {
name = "${var.project}-vm-artifacts"
project = "${var.project}"
# force_destroy = true
name = "${var.project}-vm-artifacts"
project = var.project
# force_destroy = true
}

// https://www.terraform.io/docs/providers/google/r/storage_bucket_object.html
resource "google_storage_bucket_object" "artifact" {
name = "${var.ver}/flask-prime.tgz"
source = "../build/flask-prime.tgz"
bucket = "${google_storage_bucket.artifact_store.name}"
// TODO: ignore lifecycle something so old versions don't get deleted
name = "${var.ver}/flask-prime.tgz"
source = "../build/flask-prime.tgz"
bucket = google_storage_bucket.artifact_store.name
// TODO: ignore lifecycle something so old versions don't get deleted
}

data "template_file" "web_init" {
template = "${file("${path.module}/web-init.sh.tmpl")}"
vars {
bucket = "${var.project}-vm-artifacts"
version = "${var.ver}"
}
template = file("${path.module}/web-init.sh.tmpl")
vars = {
bucket = "${var.project}-vm-artifacts"
version = var.ver
}
}

// https://www.terraform.io/docs/providers/google/r/compute_instance.html
resource "google_compute_instance" "web_server" {
project = "${var.project}"
name = "vm-webserver"
machine_type = "${var.machine_type}"
zone = "${var.zone}"
project = var.project
name = "vm-webserver"
machine_type = var.machine_type
zone = var.zone

tags = ["flask-web"]
tags = ["flask-web"]

boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}

network_interface {
network = "default"
access_config {
// leave this block empty to get an automatically generated ephemeral
// external IP
}
}
network_interface {
network = "default"
access_config {
// leave this block empty to get an automatically generated ephemeral
// external IP
}
}

// install pip and flask
metadata_startup_script = "${data.template_file.web_init.rendered}"
// install pip and flask
metadata_startup_script = data.template_file.web_init.rendered

service_account {
scopes = ["storage-ro"]
}
service_account {
scopes = ["storage-ro"]
}

depends_on = [
"google_storage_bucket.artifact_store",
"google_storage_bucket_object.artifact"
]
depends_on = [
google_storage_bucket.artifact_store,
google_storage_bucket_object.artifact,
]
}

// https://www.terraform.io/docs/providers/google/r/compute_firewall.html
resource "google_compute_firewall" "flask_web" {
name = "flask-web"
network = "default"
project = "${var.project}"
allow {
protocol = "tcp"
ports = ["8080"]
}
name = "flask-web"
network = "default"
project = var.project
allow {
protocol = "tcp"
ports = ["8080"]
}

source_ranges = ["0.0.0.0/0"]
source_tags = ["flask-web"]
source_ranges = ["0.0.0.0/0"]
source_tags = ["flask-web"]
}
13 changes: 7 additions & 6 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
output "prime_web_server" {
value = "http://${google_compute_instance.web_server.network_interface.0.access_config.0.nat_ip}:8080/prime"
value = "http://${google_compute_instance.web_server.network_interface[0].access_config[0].nat_ip}:8080/prime"
}

output "factorial_web_server" {
value = "http://${google_compute_instance.web_server.network_interface.0.access_config.0.nat_ip}:8080/factorial"
value = "http://${google_compute_instance.web_server.network_interface[0].access_config[0].nat_ip}:8080/factorial"
}

output "web_server_address" {
value = "http://${google_compute_instance.web_server.network_interface.0.access_config.0.nat_ip}:8080"
value = "http://${google_compute_instance.web_server.network_interface[0].access_config[0].nat_ip}:8080"
}

output "prime_cos_server" {
value = "http://${google_compute_instance.container_server.network_interface.0.access_config.0.nat_ip}:8080/prime"
value = "http://${google_compute_instance.container_server.network_interface[0].access_config[0].nat_ip}:8080/prime"
}

output "factorial_cos_server" {
value = "http://${google_compute_instance.container_server.network_interface.0.access_config.0.nat_ip}:8080/factorial"
value = "http://${google_compute_instance.container_server.network_interface[0].access_config[0].nat_ip}:8080/factorial"
}

output "cos_server_address" {
value = "http://${google_compute_instance.container_server.network_interface.0.access_config.0.nat_ip}:8080"
value = "http://${google_compute_instance.container_server.network_interface[0].access_config[0].nat_ip}:8080"
}
3 changes: 1 addition & 2 deletions terraform/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ limitations under the License.
// specify the version of the Google Cloud Provider that was used to
// develop this example.


provider "google" {
version = "~> v2.9.1"
version = "~> v2.11.0"
}

provider "null" {
Expand Down
12 changes: 6 additions & 6 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ limitations under the License.
// variables.tf - this is where all variables are defined. The user must
// provide these for any invocation of `terraform plan`, `apply`, or `destroy`.


variable "cluster_name" {
description = "The Kubernetes Engine cluster name"
default = "prime-server-cluster"
default = "prime-server-cluster"
}

variable "machine_type" {
default = "f1-micro"
}

variable "project" {
type = "string"
type = string
}

variable "replicas" {
description = "Number of prime server replicas to create"
default = "1"
default = "1"
}

variable "ver" {
type = "string"
type = string
}

variable "zone" {
type = "string"
type = string
}
19 changes: 19 additions & 0 deletions terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

terraform {
required_version = ">= 0.12"
}

0 comments on commit 6f2e12f

Please sign in to comment.