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
This commit is contained in:
Eric Lake 2019-10-29 02:02:42 -05:00 committed by Kubernetes Prow Robot
parent 94d4ce5a6f
commit 108a6297e9
6 changed files with 57 additions and 43 deletions

View file

@ -10,9 +10,9 @@
- cp ansible.cfg ~/.ansible.cfg - cp ansible.cfg ~/.ansible.cfg
# Prepare inventory # Prepare inventory
- if [ "$PROVIDER" == "openstack" ]; then VARIABLEFILE="cluster.tfvars"; else VARIABLEFILE="cluster.tf"; fi - 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 - ln -s contrib/terraform/$PROVIDER/hosts
- terraform init contrib/terraform/$PROVIDER - terraform init contrib/terraform/$PROVIDER
- cp contrib/terraform/$PROVIDER/sample-inventory/$VARIABLEFILE .
# Copy SSH keypair # Copy SSH keypair
- mkdir -p ~/.ssh - mkdir -p ~/.ssh
- echo "$PACKET_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa - echo "$PACKET_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa
@ -55,7 +55,7 @@ tf-validate-openstack:
tf-validate-packet: tf-validate-packet:
extends: .terraform_validate extends: .terraform_validate
variables: variables:
TF_VERSION: 0.11.11 TF_VERSION: 0.12.12
PROVIDER: packet PROVIDER: packet
CLUSTER: $CI_COMMIT_REF_NAME CLUSTER: $CI_COMMIT_REF_NAME

View file

@ -4,59 +4,60 @@ provider "packet" {
} }
resource "packet_ssh_key" "k8s" { resource "packet_ssh_key" "k8s" {
count = "${var.public_key_path != "" ? 1 : 0}" count = var.public_key_path != "" ? 1 : 0
name = "kubernetes-${var.cluster_name}" 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" { resource "packet_device" "k8s_master" {
depends_on = ["packet_ssh_key.k8s"] depends_on = [packet_ssh_key.k8s]
count = "${var.number_of_k8s_masters}" count = var.number_of_k8s_masters
hostname = "${var.cluster_name}-k8s-master-${count.index+1}" hostname = "${var.cluster_name}-k8s-master-${count.index + 1}"
plan = "${var.plan_k8s_masters}" plan = var.plan_k8s_masters
facilities = ["${var.facility}"] facilities = [var.facility]
operating_system = "${var.operating_system}" operating_system = var.operating_system
billing_cycle = "${var.billing_cycle}" billing_cycle = var.billing_cycle
project_id = "${var.packet_project_id}" project_id = var.packet_project_id
tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-master", "etcd", "kube-node"] tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-master", "etcd", "kube-node"]
} }
resource "packet_device" "k8s_master_no_etcd" { resource "packet_device" "k8s_master_no_etcd" {
depends_on = ["packet_ssh_key.k8s"] depends_on = [packet_ssh_key.k8s]
count = "${var.number_of_k8s_masters_no_etcd}" count = var.number_of_k8s_masters_no_etcd
hostname = "${var.cluster_name}-k8s-master-${count.index+1}" hostname = "${var.cluster_name}-k8s-master-${count.index + 1}"
plan = "${var.plan_k8s_masters_no_etcd}" plan = var.plan_k8s_masters_no_etcd
facilities = ["${var.facility}"] facilities = [var.facility]
operating_system = "${var.operating_system}" operating_system = var.operating_system
billing_cycle = "${var.billing_cycle}" billing_cycle = var.billing_cycle
project_id = "${var.packet_project_id}" project_id = var.packet_project_id
tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-master"] tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-master"]
} }
resource "packet_device" "k8s_etcd" { resource "packet_device" "k8s_etcd" {
depends_on = ["packet_ssh_key.k8s"] depends_on = [packet_ssh_key.k8s]
count = "${var.number_of_etcd}" count = var.number_of_etcd
hostname = "${var.cluster_name}-etcd-${count.index+1}" hostname = "${var.cluster_name}-etcd-${count.index + 1}"
plan = "${var.plan_etcd}" plan = var.plan_etcd
facilities = ["${var.facility}"] facilities = [var.facility]
operating_system = "${var.operating_system}" operating_system = var.operating_system
billing_cycle = "${var.billing_cycle}" billing_cycle = var.billing_cycle
project_id = "${var.packet_project_id}" project_id = var.packet_project_id
tags = ["cluster-${var.cluster_name}", "etcd"] tags = ["cluster-${var.cluster_name}", "etcd"]
} }
resource "packet_device" "k8s_node" { resource "packet_device" "k8s_node" {
depends_on = ["packet_ssh_key.k8s"] depends_on = [packet_ssh_key.k8s]
count = "${var.number_of_k8s_nodes}" count = var.number_of_k8s_nodes
hostname = "${var.cluster_name}-k8s-node-${count.index+1}" hostname = "${var.cluster_name}-k8s-node-${count.index + 1}"
plan = "${var.plan_k8s_nodes}" plan = var.plan_k8s_nodes
facilities = ["${var.facility}"] facilities = [var.facility]
operating_system = "${var.operating_system}" operating_system = var.operating_system
billing_cycle = "${var.billing_cycle}" billing_cycle = var.billing_cycle
project_id = "${var.packet_project_id}" project_id = var.packet_project_id
tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-node"] tags = ["cluster-${var.cluster_name}", "k8s-cluster", "kube-node"]
} }

View file

@ -1,15 +1,16 @@
output "k8s_masters" { output "k8s_masters" {
value = "${packet_device.k8s_master.*.access_public_ipv4}" value = packet_device.k8s_master.*.access_public_ipv4
} }
output "k8s_masters_no_etc" { 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" { output "k8s_etcds" {
value = "${packet_device.k8s_etcd.*.access_public_ipv4}" value = packet_device.k8s_etcd.*.access_public_ipv4
} }
output "k8s_nodes" { output "k8s_nodes" {
value = "${packet_device.k8s_node.*.access_public_ipv4}" value = packet_device.k8s_node.*.access_public_ipv4
} }

View file

@ -54,3 +54,4 @@ variable "number_of_etcd" {
variable "number_of_k8s_nodes" { variable "number_of_k8s_nodes" {
default = 0 default = 0
} }

View file

@ -0,0 +1,4 @@
terraform {
required_version = ">= 0.12"
}

View file

@ -73,7 +73,7 @@ def iterresources(filenames):
# In version 4 the structure changes so we need to iterate # In version 4 the structure changes so we need to iterate
# each instance inside the resource branch. # each instance inside the resource branch.
for resource in state['resources']: for resource in state['resources']:
name = resource['module'].split('.')[-1] name = resource['provider'].split('.')[-1]
for instance in resource['instances']: for instance in resource['instances']:
key = "{}.{}".format(resource['type'], resource['name']) key = "{}.{}".format(resource['type'], resource['name'])
if 'index_key' in instance: if 'index_key' in instance:
@ -182,6 +182,9 @@ def parse_list(source, prefix, sep='.'):
def parse_bool(string_form): def parse_bool(string_form):
if type(string_form) is bool:
return string_form
token = string_form.lower()[0] token = string_form.lower()[0]
if token == 't': if token == 't':
@ -210,7 +213,7 @@ def packet_device(resource, tfvars=None):
'state': raw_attrs['state'], 'state': raw_attrs['state'],
# ansible # ansible
'ansible_ssh_host': raw_attrs['network.0.address'], '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 # generic
'ipv4_address': raw_attrs['network.0.address'], 'ipv4_address': raw_attrs['network.0.address'],
'public_ipv4': raw_attrs['network.0.address'], 'public_ipv4': raw_attrs['network.0.address'],
@ -220,6 +223,10 @@ def packet_device(resource, tfvars=None):
'provider': 'packet', '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 # add groups based on attrs
groups.append('packet_operating_system=' + attrs['operating_system']) groups.append('packet_operating_system=' + attrs['operating_system'])
groups.append('packet_locked=%s' % attrs['locked']) 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'] use_access_ip = host[1]['metadata']['use_access_ip']
if host_id in ips: if host_id in ips:
ip = ips[host_id] ip = ips[host_id]
host[1].update({ host[1].update({
'access_ip_v4': ip, 'access_ip_v4': ip,
'access_ip': ip, 'access_ip': ip,