Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform make targets: Switch from the plane structure to module terraform config #1375

Merged
merged 4 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions build/includes/terraform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,65 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# The GKE development cluster name
GCP_TF_CLUSTER_NAME ?= agones-tf-cluster

### Deploy cluster with Terraform
terraform-init:
terraform-init: $(ensure-build-image)
docker run --rm -it $(common_mounts) $(DOCKER_RUN_ARGS) $(build_tag) bash -c '\
cd $(mount_path)/install/terraform && terraform init && gcloud auth application-default login'
cd $(mount_path)/build/terraform/gke && terraform init && gcloud auth application-default login'

terraform-clean:
rm -r ../install/terraform/.terraform
rm ../install/terraform/terraform.tfstate*
rm -r ../build/terraform/gke/.terraform || true
rm ../build/terraform/gke/terraform.tfstate* || true

# Creates a cluster and install release version of Agones controller
# Version could be specified by AGONES_VERSION
gcloud-terraform-cluster: GCP_CLUSTER_NODEPOOL_INITIALNODECOUNT ?= 4
gcloud-terraform-cluster: GCP_CLUSTER_NODEPOOL_MACHINETYPE ?= n1-standard-4
gcloud-terraform-cluster: AGONES_VERSION ?= ''
gcloud-terraform-cluster: GCP_TF_CLUSTER_NAME ?= agones-tf-cluster
gcloud-terraform-cluster: $(ensure-build-image)
gcloud-terraform-cluster:
ifndef GCP_PROJECT
$(eval GCP_PROJECT=$(shell sh -c "gcloud config get-value project 2> /dev/null"))
endif
$(DOCKER_RUN) bash -c 'export TF_VAR_agones_version=$(AGONES_VERSION) && \
cd $(mount_path)/install/terraform && terraform apply -auto-approve -var values_file="" \
-var chart="agones" \
-var "cluster={name=\"$(GCP_CLUSTER_NAME)\", machineType=\"$(GCP_CLUSTER_NODEPOOL_MACHINETYPE)\", \
zone=\"$(GCP_CLUSTER_ZONE)\", project=\"$(GCP_PROJECT)\", \
initialNodeCount=\"$(GCP_CLUSTER_NODEPOOL_INITIALNODECOUNT)\"}"'
$(MAKE) gcloud-auth-cluster
$(DOCKER_RUN) bash -c 'cd $(mount_path)/build/terraform/gke && \
terraform apply -auto-approve -var agones_version="$(AGONES_VERSION)" \
-var name=$(GCP_TF_CLUSTER_NAME) -var machine_type="$(GCP_CLUSTER_NODEPOOL_MACHINETYPE)" \
-var values_file="" \
-var zone="$(GCP_CLUSTER_ZONE)" -var project="$(GCP_PROJECT)" \
-var node_count=$(GCP_CLUSTER_NODEPOOL_INITIALNODECOUNT)'
GCP_CLUSTER_NAME=$(GCP_TF_CLUSTER_NAME) $(MAKE) gcloud-auth-cluster

# Creates a cluster and install current version of Agones controller
# Set all necessary variables as `make install` does
# Unifies previous `make gcloud-test-cluster` and `make install` targets
markmandel marked this conversation as resolved.
Show resolved Hide resolved
gcloud-terraform-install: GCP_CLUSTER_NODEPOOL_INITIALNODECOUNT ?= 4
gcloud-terraform-install: GCP_CLUSTER_NODEPOOL_MACHINETYPE ?= n1-standard-4
gcloud-terraform-install: ALWAYS_PULL_SIDECAR := true
gcloud-terraform-install: IMAGE_PULL_POLICY := "Always"
gcloud-terraform-install: PING_SERVICE_TYPE := "LoadBalancer"
gcloud-terraform-install: CRD_CLEANUP := true
gcloud-terraform-install: GCP_TF_CLUSTER_NAME ?= agones-tf-cluster
gcloud-terraform-install:
ifndef GCP_PROJECT
$(eval GCP_PROJECT=$(shell sh -c "gcloud config get-value project 2> /dev/null"))
endif
$(DOCKER_RUN) bash -c ' \
cd $(mount_path)/install/terraform && terraform apply -auto-approve -var agones_version="$(VERSION)" -var image_registry="$(REGISTRY)" \
cd $(mount_path)/build/terraform/gke && terraform apply -auto-approve -var agones_version="$(VERSION)" -var image_registry="$(REGISTRY)" \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this target install the latest compiled version, like Make install ?

Copy link
Collaborator Author

@aLekSer aLekSer Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it already is doing that. Latest compiled version got set using
terraform apply [...] -var agones_version="$(VERSION)" this line

GCP_CLUSTER_NAME=test-cl2 make gcloud-terraform-install
docker run --rm -v /Users/alekser/go/src/github.com/agones/agones/build//.config/gcloud:/root/.config/gcloud -v ~/.kube/:/root/.kube -v ~/.helm:/root/.helm -v /Users/alekser/go/src
/github.com/agones/agones:/go/src/agones.dev/agones -v /Users/alekser/go/src/github.com/agones/agones/build//.gomod:/go/pkg/mod -v /Users/alekser/go/src/github.com/agones/agones/bu
ild//.gocache:/root/.cache/go-build -e "KUBECONFIG=/root/.kube/config" -e "GO111MODULE=on" -w /go/src/agones.dev/agones  agones-build:8f473e7ff2 bash -c ' \                                             
        cd /go/src/agones.dev/agones/build/terraform/gke && terraform apply -auto-approve -var agones_version="1.5.0-39911ef" -var image_registry="gcr.io/agones-images" \                               
                -var pull_policy=""Always"" \
                -var always_pull_sidecar="true" 

-var agones_version="1.5.0-39911ef" which is current short hash of the latest commit.

Copy link
Collaborator Author

@aLekSer aLekSer Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After that we are doing the same as in make install

# Use terraform of the latest >=0.12 version
values = [
"${length(var.values_file) == 0 ? "" : file("${var.values_file}")}",
]
set {
name = "crds.CleanupOnDelete"
value = var.crd_cleanup
}
set {
name = local.tag_name
value = var.agones_version
}

which is setting helm parameter "agones.image.tag" to be an up to date $(VERSION)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this changes and the resulted controller pod uses correct image:

Controlled By:        ReplicaSet/agones-controller-55d9bdd8f7                                                                                                                                     [50/706]
Containers:
  agones-controller:
    Container ID:   docker://0a986a93103382805c304f837c68449719d47ac4390058effd1052e47719d466
    Image:          gcr.io/agones-images/agones-controller:1.5.0-39911ef

Registry could be changed by a REGISTRY make variable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome - thanks for the explanation!

-var pull_policy="$(IMAGE_PULL_POLICY)" \
-var always_pull_sidecar="$(ALWAYS_PULL_SIDECAR)" \
-var image_pull_secret="$(IMAGE_PULL_SECRET)" \
-var ping_service_type="$(PING_SERVICE_TYPE)" \
-var crd_cleanup="$(CRD_CLEANUP)" \
-var "cluster={name=\"$(GCP_CLUSTER_NAME)\", machineType=\"$(GCP_CLUSTER_NODEPOOL_MACHINETYPE)\", \
zone=\"$(GCP_CLUSTER_ZONE)\", project=\"$(GCP_PROJECT)\", \
initialNodeCount=\"$(GCP_CLUSTER_NODEPOOL_INITIALNODECOUNT)\"}"'
$(MAKE) gcloud-auth-cluster
-var chart="../../../install/helm/agones/" \
-var name=$(GCP_TF_CLUSTER_NAME) -var machine_type="$(GCP_CLUSTER_NODEPOOL_MACHINETYPE)" \
-var zone=$(GCP_CLUSTER_ZONE) -var project=$(GCP_PROJECT) \
-var node_count=$(GCP_CLUSTER_NODEPOOL_INITIALNODECOUNT)'
GCP_CLUSTER_NAME=$(GCP_TF_CLUSTER_NAME) $(MAKE) gcloud-auth-cluster

gcloud-terraform-destroy-cluster:
$(DOCKER_RUN) bash -c 'cd $(mount_path)/install/terraform && \
terraform destroy -auto-approve'
$(DOCKER_RUN) bash -c 'cd $(mount_path)/build/terraform/gke && \
terraform destroy -target module.helm_agones.helm_release.agones -auto-approve && sleep 60 && terraform destroy -auto-approve'
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ variable "project" {
default = ""
}

variable "name" {
default = "agones-terraform-example"
}

// Install latest version of agones
variable "agones_version" {
default = ""
Expand All @@ -41,11 +37,30 @@ variable "machine_type" {
default = "n1-standard-4"
}

variable "name" {
default = "agones-tf-cluster"
}

variable "values_file" {
default = "../../../install/helm/agones/values.yaml"
}

// Note: This is the number of gameserver nodes. The Agones module will automatically create an additional
// two node pools with 1 node each for "agones-system" and "agones-metrics".
variable "node_count" {
default = "4"
}
variable "chart" {
default = "agones"
}

variable "crd_cleanup" {
default = "true"
}

variable "ping_service_type" {
default = "LoadBalancer"
}

variable "zone" {
default = "us-west1-c"
Expand All @@ -57,6 +72,23 @@ variable "network" {
description = "The name of the VPC network to attach the cluster and firewall rule to"
}

variable "pull_policy" {
default = "Always"
}

variable "image_registry" {
default = "gcr.io/agones-images"
}

variable "always_pull_sidecar" {
default = "true"
}

variable "image_pull_secret" {
default = ""
}


module "gke_cluster" {
source = "../../../install/terraform/modules/gke"

Expand All @@ -74,11 +106,15 @@ module "helm_agones" {
source = "../../../install/terraform/modules/helm"

agones_version = var.agones_version
values_file = ""
chart = "agones"
values_file = var.values_file
chart = var.chart
host = module.gke_cluster.host
token = module.gke_cluster.token
cluster_ca_certificate = module.gke_cluster.cluster_ca_certificate
image_registry = var.image_registry
image_pull_secret = var.image_pull_secret
crd_cleanup = var.crd_cleanup
ping_service_type = var.ping_service_type
}

output "host" {
Expand Down
2 changes: 1 addition & 1 deletion install/terraform/modules/gke/cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ resource "null_resource" "test-setting-variables" {
EOT
}
}

resource "google_container_cluster" "primary" {
name = var.cluster["name"]
location = var.cluster["zone"]
Expand Down Expand Up @@ -139,6 +140,5 @@ resource "google_compute_firewall" "default" {
protocol = "udp"
ports = [var.ports]
}

target_tags = ["game-server"]
}
2 changes: 1 addition & 1 deletion install/terraform/modules/helm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ variable "ping_service_type" {
}

variable "values_file" {
default = "../../../helm/agones/values.yaml"
default = ""
}