From 108a6297e9b0d2dde17289ce4ce8bdec97e445d3 Mon Sep 17 00:00:00 2001 From: Eric Lake <ericlake@gmail.com> Date: Tue, 29 Oct 2019 02:02:42 -0500 Subject: [PATCH] Terraform dynamic inventory 0.12.12 (#5298) * Update parsing of terraform state file for 0.12.12 * Resource does not seem to have a module element but instead has provider * Return the boolean right way if it is already a bool since a bool does not have an lower method * Remove the setting of ansible_ssh_user to root for all Packet Not all servers in packet are accessed as root by default. CoreOS systems use the `core` user. Removing this allows the user to specify the remote user with an extra_var or in an ansible.cfg file. * Default to root user for packet devices except on CoreOS * Update TF_VERSION for packet in tf-validate-packet Update TV_VERSION to 0.12.12 for gitlab-ci tf-validate-packet tests * convert packet terraform files to TV_VERSION 4 * initalize terraform before copying the variable file to the top level dir --- .gitlab-ci/terraform.yml | 4 +- contrib/terraform/packet/kubespray.tf | 77 ++++++++++++++------------- contrib/terraform/packet/output.tf | 9 ++-- contrib/terraform/packet/variables.tf | 1 + contrib/terraform/packet/versions.tf | 4 ++ contrib/terraform/terraform.py | 13 +++-- 6 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 contrib/terraform/packet/versions.tf diff --git a/.gitlab-ci/terraform.yml b/.gitlab-ci/terraform.yml index 5bdb33ef7..22b20812c 100644 --- a/.gitlab-ci/terraform.yml +++ b/.gitlab-ci/terraform.yml @@ -10,9 +10,9 @@ - cp ansible.cfg ~/.ansible.cfg # Prepare inventory - if [ "$PROVIDER" == "openstack" ]; then VARIABLEFILE="cluster.tfvars"; else VARIABLEFILE="cluster.tf"; fi - - cp contrib/terraform/$PROVIDER/sample-inventory/$VARIABLEFILE . - ln -s contrib/terraform/$PROVIDER/hosts - terraform init contrib/terraform/$PROVIDER + - cp contrib/terraform/$PROVIDER/sample-inventory/$VARIABLEFILE . # Copy SSH keypair - mkdir -p ~/.ssh - echo "$PACKET_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa @@ -55,7 +55,7 @@ tf-validate-openstack: tf-validate-packet: extends: .terraform_validate variables: - TF_VERSION: 0.11.11 + TF_VERSION: 0.12.12 PROVIDER: packet CLUSTER: $CI_COMMIT_REF_NAME diff --git a/contrib/terraform/packet/kubespray.tf b/contrib/terraform/packet/kubespray.tf index e3b6bfc2f..568db0dd7 100644 --- a/contrib/terraform/packet/kubespray.tf +++ b/contrib/terraform/packet/kubespray.tf @@ -4,59 +4,60 @@ provider "packet" { } resource "packet_ssh_key" "k8s" { - count = "${var.public_key_path != "" ? 1 : 0}" + count = var.public_key_path != "" ? 1 : 0 name = "kubernetes-${var.cluster_name}" - public_key = "${chomp(file(var.public_key_path))}" + public_key = chomp(file(var.public_key_path)) } resource "packet_device" "k8s_master" { - depends_on = ["packet_ssh_key.k8s"] - - count = "${var.number_of_k8s_masters}" - hostname = "${var.cluster_name}-k8s-master-${count.index+1}" - plan = "${var.plan_k8s_masters}" - facilities = ["${var.facility}"] - operating_system = "${var.operating_system}" - billing_cycle = "${var.billing_cycle}" - project_id = "${var.packet_project_id}" + depends_on = [packet_ssh_key.k8s] + + count = var.number_of_k8s_masters + hostname = "${var.cluster_name}-k8s-master-${count.index + 1}" + plan = var.plan_k8s_masters + facilities = [var.facility] + operating_system = var.operating_system + billing_cycle = var.billing_cycle + project_id = var.packet_project_id tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-master", "etcd", "kube-node"] } resource "packet_device" "k8s_master_no_etcd" { - depends_on = ["packet_ssh_key.k8s"] - - count = "${var.number_of_k8s_masters_no_etcd}" - hostname = "${var.cluster_name}-k8s-master-${count.index+1}" - plan = "${var.plan_k8s_masters_no_etcd}" - facilities = ["${var.facility}"] - operating_system = "${var.operating_system}" - billing_cycle = "${var.billing_cycle}" - project_id = "${var.packet_project_id}" + depends_on = [packet_ssh_key.k8s] + + count = var.number_of_k8s_masters_no_etcd + hostname = "${var.cluster_name}-k8s-master-${count.index + 1}" + plan = var.plan_k8s_masters_no_etcd + facilities = [var.facility] + operating_system = var.operating_system + billing_cycle = var.billing_cycle + project_id = var.packet_project_id tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-master"] } resource "packet_device" "k8s_etcd" { - depends_on = ["packet_ssh_key.k8s"] - - count = "${var.number_of_etcd}" - hostname = "${var.cluster_name}-etcd-${count.index+1}" - plan = "${var.plan_etcd}" - facilities = ["${var.facility}"] - operating_system = "${var.operating_system}" - billing_cycle = "${var.billing_cycle}" - project_id = "${var.packet_project_id}" + depends_on = [packet_ssh_key.k8s] + + count = var.number_of_etcd + hostname = "${var.cluster_name}-etcd-${count.index + 1}" + plan = var.plan_etcd + facilities = [var.facility] + operating_system = var.operating_system + billing_cycle = var.billing_cycle + project_id = var.packet_project_id tags = ["cluster-${var.cluster_name}", "etcd"] } resource "packet_device" "k8s_node" { - depends_on = ["packet_ssh_key.k8s"] - - count = "${var.number_of_k8s_nodes}" - hostname = "${var.cluster_name}-k8s-node-${count.index+1}" - plan = "${var.plan_k8s_nodes}" - facilities = ["${var.facility}"] - operating_system = "${var.operating_system}" - billing_cycle = "${var.billing_cycle}" - project_id = "${var.packet_project_id}" + depends_on = [packet_ssh_key.k8s] + + count = var.number_of_k8s_nodes + hostname = "${var.cluster_name}-k8s-node-${count.index + 1}" + plan = var.plan_k8s_nodes + facilities = [var.facility] + operating_system = var.operating_system + billing_cycle = var.billing_cycle + project_id = var.packet_project_id tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-node"] } + diff --git a/contrib/terraform/packet/output.tf b/contrib/terraform/packet/output.tf index 2f9633dea..c27b9b915 100644 --- a/contrib/terraform/packet/output.tf +++ b/contrib/terraform/packet/output.tf @@ -1,15 +1,16 @@ output "k8s_masters" { - value = "${packet_device.k8s_master.*.access_public_ipv4}" + value = packet_device.k8s_master.*.access_public_ipv4 } output "k8s_masters_no_etc" { - value = "${packet_device.k8s_master_no_etcd.*.access_public_ipv4}" + value = packet_device.k8s_master_no_etcd.*.access_public_ipv4 } output "k8s_etcds" { - value = "${packet_device.k8s_etcd.*.access_public_ipv4}" + value = packet_device.k8s_etcd.*.access_public_ipv4 } output "k8s_nodes" { - value = "${packet_device.k8s_node.*.access_public_ipv4}" + value = packet_device.k8s_node.*.access_public_ipv4 } + diff --git a/contrib/terraform/packet/variables.tf b/contrib/terraform/packet/variables.tf index e71b78bbf..83bdb5d03 100644 --- a/contrib/terraform/packet/variables.tf +++ b/contrib/terraform/packet/variables.tf @@ -54,3 +54,4 @@ variable "number_of_etcd" { variable "number_of_k8s_nodes" { default = 0 } + diff --git a/contrib/terraform/packet/versions.tf b/contrib/terraform/packet/versions.tf new file mode 100644 index 000000000..ac97c6ac8 --- /dev/null +++ b/contrib/terraform/packet/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +} diff --git a/contrib/terraform/terraform.py b/contrib/terraform/terraform.py index fa490d816..b9f50db48 100755 --- a/contrib/terraform/terraform.py +++ b/contrib/terraform/terraform.py @@ -73,7 +73,7 @@ def iterresources(filenames): # In version 4 the structure changes so we need to iterate # each instance inside the resource branch. for resource in state['resources']: - name = resource['module'].split('.')[-1] + name = resource['provider'].split('.')[-1] for instance in resource['instances']: key = "{}.{}".format(resource['type'], resource['name']) if 'index_key' in instance: @@ -182,6 +182,9 @@ def parse_list(source, prefix, sep='.'): def parse_bool(string_form): + if type(string_form) is bool: + return string_form + token = string_form.lower()[0] if token == 't': @@ -210,7 +213,7 @@ def packet_device(resource, tfvars=None): 'state': raw_attrs['state'], # ansible 'ansible_ssh_host': raw_attrs['network.0.address'], - 'ansible_ssh_user': 'root', # it's always "root" on Packet + 'ansible_ssh_user': 'root', # Use root by default in packet # generic 'ipv4_address': raw_attrs['network.0.address'], 'public_ipv4': raw_attrs['network.0.address'], @@ -220,6 +223,10 @@ def packet_device(resource, tfvars=None): 'provider': 'packet', } + if raw_attrs['operating_system'] == 'coreos_stable': + # For CoreOS set the ssh_user to core + attrs.update({'ansible_ssh_user': 'core'}) + # add groups based on attrs groups.append('packet_operating_system=' + attrs['operating_system']) groups.append('packet_locked=%s' % attrs['locked']) @@ -342,7 +349,7 @@ def iter_host_ips(hosts, ips): use_access_ip = host[1]['metadata']['use_access_ip'] if host_id in ips: ip = ips[host_id] - + host[1].update({ 'access_ip_v4': ip, 'access_ip': ip, -- GitLab