Moved everything from k8s-nodes to the top-level.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
locals {
|
||||
amis = {
|
||||
amzn2 = {
|
||||
owner-id = "137112412989"
|
||||
name = "amzn2-ami-hvm-2*x86_64-gp2"
|
||||
},
|
||||
ubuntu = {
|
||||
owner-id = "099720109477"
|
||||
name = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
|
||||
},
|
||||
centos7 = {
|
||||
owner-id = "125523088429"
|
||||
name = "CentOS 7.*x86_64"
|
||||
},
|
||||
centos8 = {
|
||||
owner-id = "125523088429"
|
||||
name = "CentOS 8.*x86_64"
|
||||
},
|
||||
arch = {
|
||||
owner-id = "093273469852"
|
||||
name = "arch-linux-lts-hvm*x86_64-ebs"
|
||||
},
|
||||
rhel7 = {
|
||||
owner-id = "309956199498"
|
||||
name = "RHEL-7.*HVM*x86_64*GP2"
|
||||
},
|
||||
rhel8 = {
|
||||
owner-id = "309956199498"
|
||||
name = "RHEL-8.*HVM*x86_64*GP2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data "aws_ami" "amis" {
|
||||
for_each = local.amis
|
||||
most_recent = true
|
||||
owners = [each.value.owner-id]
|
||||
|
||||
filter {
|
||||
name = "name"
|
||||
values = [each.value.name]
|
||||
}
|
||||
|
||||
filter {
|
||||
name = "virtualization-type"
|
||||
values = ["hvm"]
|
||||
}
|
||||
|
||||
filter {
|
||||
name = "architecture"
|
||||
values = ["x86_64"]
|
||||
}
|
||||
|
||||
filter {
|
||||
name = "root-device-type"
|
||||
values = ["ebs"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
output "amis" {
|
||||
value = tomap({ for type, ami in data.aws_ami.amis : type => ami.id })
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
locals {
|
||||
az-to-subnets = {
|
||||
for s in data.aws_subnet.subnets : s.availability_zone => s.id...
|
||||
}
|
||||
}
|
||||
|
||||
data "aws_vpc" "default" {
|
||||
tags = {
|
||||
Name = var.default-vpc-name
|
||||
}
|
||||
}
|
||||
|
||||
data "aws_subnets" "subnet-ids" {
|
||||
filter {
|
||||
name = "vpc-id"
|
||||
values = [data.aws_vpc.default.id]
|
||||
}
|
||||
}
|
||||
|
||||
data "aws_subnet" "subnets" {
|
||||
for_each = toset(data.aws_subnets.subnet-ids.ids)
|
||||
id = each.key
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
output "default-vpc" {
|
||||
value = data.aws_vpc.default
|
||||
}
|
||||
|
||||
output "subnets" {
|
||||
description = "An array of all subnets in default-vpc."
|
||||
value = data.aws_subnet.subnets
|
||||
}
|
||||
|
||||
output "k8s-subnets" {
|
||||
description = "An array of subnets to be used for k8s VMs. These subnets were chosen by selecting a single subnet from each availability_zone."
|
||||
value = [for k,v in local.az-to-subnets : v[0]]
|
||||
}
|
||||
|
||||
output "az-to-subnets" {
|
||||
description = "A map of availability zone to array of subnets that are in thet availability zone."
|
||||
value = local.az-to-subnets
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
variable "default-vpc-name" {
|
||||
description = "The name of the existing default VPC. This module will query AWS for a VPC with this name,"
|
||||
default = "Managed VPC"
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
resource "aws_vpc" "vpc" {
|
||||
cidr_block = var.vpc-cidr-block
|
||||
tags = {
|
||||
Name = "${var.name-prefix}-vpc"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_subnet" "subnet" {
|
||||
vpc_id = aws_vpc.vpc.id
|
||||
cidr_block = var.subnet-cidr-block
|
||||
# availability_zone = var.avail_zone
|
||||
tags = {
|
||||
Name = "${var.name-prefix}-subnet"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_default_security_group" "sg" {
|
||||
vpc_id = aws_vpc.vpc.id
|
||||
|
||||
ingress {
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
protocol = "tcp"
|
||||
cidr_blocks = var.admin-ips
|
||||
}
|
||||
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
prefix_list_ids = []
|
||||
}
|
||||
|
||||
tags = {
|
||||
Name = "${var.name-prefix}-ssh-from-admins-sg"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_internet_gateway" "igw" {
|
||||
vpc_id = aws_vpc.vpc.id
|
||||
tags = {
|
||||
Name = "${var.name-prefix}-igw"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_default_route_table" "route-table" {
|
||||
default_route_table_id = aws_vpc.vpc.main_route_table_id
|
||||
|
||||
route {
|
||||
cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.igw.id
|
||||
}
|
||||
|
||||
# default route, mapping VPC CIDR block to "local", created implicitly and
|
||||
# cannot be specified.
|
||||
|
||||
tags = {
|
||||
Name = "${var.name-prefix}-route-table"
|
||||
}
|
||||
}
|
||||
|
||||
# Associate subnet with Route Table
|
||||
resource "aws_route_table_association" "a-rtb-subnet" {
|
||||
subnet_id = aws_subnet.subnet.id
|
||||
route_table_id = aws_default_route_table.route-table.id
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
output "vpc" {
|
||||
value = aws_vpc.vpc
|
||||
}
|
||||
|
||||
output "subnet" {
|
||||
value = aws_subnet.subnet
|
||||
}
|
||||
|
||||
output "default-security-group" {
|
||||
value = aws_default_security_group.sg
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
variable "admin-ips" {
|
||||
description = "A list of ips or cidr blocks that are allowed to connect to the nodes."
|
||||
type = list(string)
|
||||
}
|
||||
|
||||
variable "name-prefix" {
|
||||
default = "tf"
|
||||
description = "This prefix will be used in all the names of the resources creates in our AWS network."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "subnet-cidr-block" {
|
||||
default = "10.0.1.0/24"
|
||||
description = "The address space to be used for this subnet."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "vpc-cidr-block" {
|
||||
default = "10.0.0.0/16"
|
||||
description = "The address space to be used for out networks VPC."
|
||||
type = string
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
resource "aws_instance" "nodes" {
|
||||
ami = var.ami
|
||||
instance_type = var.ec2-instance-type
|
||||
# TODO REM double check this key.
|
||||
# key_name = aws_key_pair.debug1.key_name
|
||||
associate_public_ip_address = true
|
||||
subnet_id = var.subnet-id
|
||||
vpc_security_group_ids = var.security-group-ids
|
||||
user_data = element(var.user-datas.*.rendered, count.index)
|
||||
count = var.num-nodes
|
||||
|
||||
tags = {
|
||||
Name = "${var.name-prefix}-${count.index}"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
output "ips" {
|
||||
value = aws_instance.nodes.*.public_ip
|
||||
}
|
||||
|
||||
output "names" {
|
||||
value = aws_instance.nodes.*.tags.Name
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
variable "ami" {
|
||||
description = "The AWS AMI to be used for all the nodes"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "ec2-instance-type" {
|
||||
default = "t2.micro"
|
||||
description = "The AWS instance type to use for all nodes."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "name-prefix" {
|
||||
default = "tf-node"
|
||||
description = "This prefix will be applied to all names created by this module."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "num-nodes" {
|
||||
default = 1
|
||||
description = "The number of nodes to create from the given input parameters."
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "user-datas" {
|
||||
description = "A list of cloud-init configs that get applied to their corresponding node."
|
||||
}
|
||||
|
||||
variable "subnet-id" {
|
||||
description = "The ID of the subnet that all the nodes will be added to."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "security-group-ids" {
|
||||
description = "A list of security group IDs to be applied to all the nodes."
|
||||
type = list(string)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
data "template_file" "user-datas" {
|
||||
template = file("${var.cloud-init-template}")
|
||||
vars = {
|
||||
admin-passwd = "${var.root-admin-passwd}"
|
||||
admin-pub-key = "${var.root-admin-pub-key}"
|
||||
hostname = "${var.hostname-prefix}-${count.index}"
|
||||
install-qemu-agent = var.install-qemu-agent
|
||||
}
|
||||
count = var.num
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
output "user-datas" {
|
||||
value = data.template_file.user-datas
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
variable "cloud-init-template" {
|
||||
default = "../../cloud_init.cfg"
|
||||
description = "The path to the cloud-init config template."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "hostname-prefix" {
|
||||
description = "This prefix wil be applied as a prefix for the hostnames."
|
||||
}
|
||||
|
||||
variable "install-qemu-agent" {
|
||||
default = false
|
||||
description = "This flag determines whether or not qemu-agent is installed."
|
||||
type = bool
|
||||
}
|
||||
|
||||
variable "num" {
|
||||
description = "The number of user-datas to create with these parameters."
|
||||
}
|
||||
|
||||
variable "root-admin-passwd" {
|
||||
description = "This value will be substituted for any occurence of 'admin-password' in the cloud-init config template."
|
||||
}
|
||||
|
||||
variable "root-admin-pub-key" {
|
||||
description = "This value will be substituted for any occurence of 'admin-pub-key' in the cloud-init config template."
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
terraform {
|
||||
required_version = ">= 0.13"
|
||||
required_providers {
|
||||
libvirt = {
|
||||
source = "dmacvicar/libvirt"
|
||||
version = "0.6.11"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "libvirt_volume" "node-images" {
|
||||
name = "${var.name-prefix}-base"
|
||||
pool = var.pool-name
|
||||
source = var.base-image
|
||||
format = "qcow2"
|
||||
}
|
||||
|
||||
resource "libvirt_volume" "node-images-resized" {
|
||||
name = "${var.name-prefix}-${count.index}-resized"
|
||||
pool = var.pool-name
|
||||
base_volume_id = libvirt_volume.node-images.id
|
||||
count = var.num-nodes
|
||||
size = var.node-disk-size
|
||||
}
|
||||
|
||||
data "template_file" "network-config" {
|
||||
template = file("${path.module}/network_config.cfg")
|
||||
}
|
||||
|
||||
resource "libvirt_cloudinit_disk" "node-inits" {
|
||||
name = "${var.name-prefix}-${count.index}-init"
|
||||
user_data = element(var.user-datas.*.rendered, count.index)
|
||||
network_config = data.template_file.network-config.rendered
|
||||
pool = var.pool-name
|
||||
count = var.num-nodes
|
||||
}
|
||||
|
||||
resource "libvirt_domain" "nodes" {
|
||||
count = var.num-nodes
|
||||
name = "${var.name-prefix}-${count.index}"
|
||||
memory = var.node-memory
|
||||
vcpu = var.node-vcpus
|
||||
|
||||
cloudinit = element(libvirt_cloudinit_disk.node-inits.*.id, count.index)
|
||||
|
||||
network_interface {
|
||||
network_name = var.network-name
|
||||
hostname = "${var.name-prefix}-${count.index}"
|
||||
# wait_for_lease = true
|
||||
}
|
||||
|
||||
# IMPORTANT: this is a known bug on cloud images, since they expect a console
|
||||
# we need to pass it
|
||||
# https://bugs.launchpad.net/cloud-images/+bug/1573095
|
||||
console {
|
||||
type = "pty"
|
||||
target_port = "0"
|
||||
target_type = "serial"
|
||||
}
|
||||
|
||||
console {
|
||||
type = "pty"
|
||||
target_type = "virtio"
|
||||
target_port = "1"
|
||||
}
|
||||
|
||||
disk {
|
||||
volume_id = element(libvirt_volume.node-images-resized.*.id, count.index)
|
||||
}
|
||||
|
||||
graphics {
|
||||
type = "spice"
|
||||
listen_type = "address"
|
||||
autoport = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
version: 2
|
||||
ethernets:
|
||||
ens3:
|
||||
dhcp4: true
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
# This only works on the default network. They will not work using the bridged
|
||||
# network.
|
||||
# output "ips" {
|
||||
# value = libvirt_domain.nodes.*.network_interface.0.addresses.0
|
||||
# }
|
||||
@@ -0,0 +1,56 @@
|
||||
variable "base-image" {
|
||||
default = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
|
||||
description = "The base image to be used for all nodes."
|
||||
}
|
||||
|
||||
variable "libvirt-connection-url" {
|
||||
description = "The libvirt connection URI, ie. qemu+ssh://<user>@<host>/system"
|
||||
}
|
||||
|
||||
variable "name-prefix" {
|
||||
default = "k8s-node"
|
||||
description = "This will be a prefix for all resource names, ie. domains will be created suck as \"k8s-node-2\"."
|
||||
}
|
||||
|
||||
variable "network-name" {
|
||||
default = "default"
|
||||
description = "The name of a pre-existing virtual-network."
|
||||
}
|
||||
|
||||
variable "node-disk-size" {
|
||||
default = 4294967296
|
||||
description = "The size of the disk to be used for libvirt nodes. (in bytes)"
|
||||
}
|
||||
|
||||
variable "node-memory" {
|
||||
default = "2048"
|
||||
description = "The amount of memory to be used for all the nodes."
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "node-vcpus" {
|
||||
default = "2"
|
||||
description = "The amount of vcpus to be used for all the nodes."
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "user-datas" {
|
||||
description = "A list of cloud-init configs that get applied to their corresponding node."
|
||||
}
|
||||
|
||||
variable "num-nodes" {
|
||||
description = "The number of nodes to create with this config."
|
||||
}
|
||||
|
||||
variable "pool-name" {
|
||||
default = "default"
|
||||
description = "The name of the pool to put all disk images in."
|
||||
}
|
||||
|
||||
variable "root-admin-passwd" {
|
||||
description = "This will be the password for the root and admin user. The format of this can by any format accepted by cloud-init's chpasswd module."
|
||||
}
|
||||
|
||||
variable "root-admin-pub-key" {
|
||||
description = "The public key to be added to authorized_keys for the root and admin accounts."
|
||||
}
|
||||
Reference in New Issue
Block a user