diff --git a/Jenkinsfile b/Jenkinsfile index 809e548..099326b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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: diff --git a/README.md b/README.md index 3d30a44..7585fa4 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/terraform/main.tf b/terraform/main.tf index 1d957a3..ea0cfba 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -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 @@ -77,9 +77,10 @@ data "template_file" "startup_script" { - systemctl start flaskservice.service EOF - vars { - project = "${var.project}" - version = "${var.ver}" + + vars = { + project = var.project + version = var.ver } } @@ -87,12 +88,12 @@ EOF // 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 { @@ -100,9 +101,10 @@ resource "google_compute_instance" "container_server" { } } - 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" @@ -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 @@ -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"] } diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 2914e56..821ef1c 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -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" } diff --git a/terraform/provider.tf b/terraform/provider.tf index fba45fb..660c8fb 100644 --- a/terraform/provider.tf +++ b/terraform/provider.tf @@ -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" { diff --git a/terraform/variables.tf b/terraform/variables.tf index 61a4e35..ac25346 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -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 } diff --git a/terraform/versions.tf b/terraform/versions.tf new file mode 100644 index 0000000..c51f32c --- /dev/null +++ b/terraform/versions.tf @@ -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" +}