Skip to content

Commit

Permalink
adding post config infra code
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofeteira committed Nov 9, 2022
1 parent c8fdfc9 commit 0f45daf
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 37 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ override.tf.json

# End of https://www.gitignore.io/api/terraform,visualstudiocode

plan
# Other files files
**/key.json
.vscode
.kube*
kubeconfig.yaml
.vagrant
*.log

# Project Specific
ansible_kind/hosts
infra/keys/nephio
10 changes: 10 additions & 0 deletions ansible_kind/hosts.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[servers]
%{ for compute_instances in hosts ~}
%{ for ips in compute_instances ~}
${ips}
%{ endfor ~}
%{ endfor ~}

[all:vars]
ansible_ssh_common_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_user=${user}
82 changes: 70 additions & 12 deletions infra/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,79 @@
# kind-cluster-gce

Terraform code to provision Kind clusters on top of GCE Instances.
For now it creates a number (that can be defined) of Kind K8S Clusters (based on the scripts folder) for central and edge clusters.
We can even set multiple interfaces for each VM in order to have additional NICs for Multus for instance.

## todo

- automate the #Post section of the scripts
Terraform code to provision Kind clusters on top of GCE Instances

## requirements

- [terraform 1.3.2](https://www.terraform.io/downloads.html)
- [Google Cloud SDK](https://cloud.google.com/sdk/docs/install)
- [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html)

## usage

To run the terraform code locally change the variable project_id in general.auto.tfvars and:
- Generate the SSH Key on your local machine:

```bash
ssh-keygen -t rsa -f ~/.ssh/nephio.pub -C nephio -b 2048
```

- Fill in the required parameters in compute_instances.tf file:

```bash
# Compute Instances parameters definition
ssh_public_key_path = "xxxxx"
ssh_private_key_path = "xxxxx"
num_vms = NUMBER
user = "ubuntu" - must be this one unless we autogenerate the kind_setup.yaml as it is performed on the ansible hosts inventory
```

- Choose if you want to configure the VMs through the script method or through and ansible role by uncommenting the specific section in compute_instances.tf file:

```bash
# # VM configuration through bash script
# Needs some reworking if using more than "nephio-poc" object in locals
# resource "null_resource" "config_vm" {
# count = local.num_vms
# connection {
# type = "ssh"
# user = local.user
# private_key = file(local.ssh_private_key_path)
# host = module.compute_instances["nephio-poc"].instances_details[count.index].*.network_interface[0].*.access_config[0].*.nat_ip[0]
# }

# provisioner "remote-exec" {
# script = "../scripts/startup.sh"
# }
# }
```

OR

```bash
# # VM configuration through ansible playbooks
# resource "local_file" "ansible_inventory" {
# content = templatefile("../ansible_kind/hosts.tftpl", { hosts = { for k, vm in module.compute_instances : k => vm.instances_details[*].*.network_interface[0].*.access_config[0].*.nat_ip[0] }, user = local.user })
# filename = "../ansible_kind/hosts"
# depends_on = [module.compute_instances]
# }

# resource "null_resource" "config_vm" {
# provisioner "local-exec" {
# command = "ansible-playbook -i '../ansible_kind/hosts' --private-key ${local.ssh_private_key_path} ../ansible_kind/kind_setup.yaml"
# }
# depends_on = [local_file.ansible_inventory]
# }
```

- Change the parameters in the general.auto.tfvars file:

```bash
# General Settings
project_id = "xxxxx"
region = "xxxxx"
zone = "xxxxx"
```

- To run the terraform code locally run:

```bash
gcloud auth login
Expand All @@ -26,10 +84,10 @@ terraform plan
terraform apply
```

To access the VM after creation:
## VM Access

To access the VM after creation run:

```bash
gcloud compute ssh --zone ZONE VM_NAME --project "XXXX" --tunnel-through-iap
ssh USER@IP -i PRIVATEKEYPATH
```

(one can get the VM name from the terraform outputs or via gcloud compute instances list)
43 changes: 42 additions & 1 deletion infra/compute_instances.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
# compute_instances.tf

locals {
# Compute Instances parameters definition
ssh_public_key_path = "xxxxx"
ssh_private_key_path = "xxxxx"
num_vms = 2
user = "ubuntu"
# Compute Instances definition
compute_instances = {
"nephio-poc" = { # Prefix for each Compute Instance
name = "nephio-poc"
region = var.region
zone = var.zone
num_instances = 1
num_instances = local.num_vms
instance_template = module.instance_templates["cluster"].self_link
deletion_protection = false # Protect the instance from deletion
}
}
}

# Public Key File Generation
resource "local_file" "public_key" {
content = templatefile("keys/nephio.tftpl", { pub_key = file(local.ssh_public_key_path), user = local.user })
filename = "keys/nephio"
}

# Compute Instances Creation
module "compute_instances" {
for_each = { for compute_instances in local.compute_instances : compute_instances.name => compute_instances }
Expand All @@ -28,3 +39,33 @@ module "compute_instances" {
deletion_protection = each.value.deletion_protection
depends_on = [module.subnets, module.service_accounts, resource.google_compute_project_metadata.ssh_keys]
}

# # VM configuration through bash script
# Needs some reworking if using more than "nephio-poc" object in locals
# resource "null_resource" "config_vm" {
# count = local.num_vms
# connection {
# type = "ssh"
# user = local.user
# private_key = file(local.ssh_private_key_path)
# host = module.compute_instances["nephio-poc"].instances_details[count.index].*.network_interface[0].*.access_config[0].*.nat_ip[0]
# }

# provisioner "remote-exec" {
# script = "../scripts/startup.sh"
# }
# }

# # VM configuration through ansible playbooks
# resource "local_file" "ansible_inventory" {
# content = templatefile("../ansible_kind/hosts.tftpl", { hosts = { for k, vm in module.compute_instances : k => vm.instances_details[*].*.network_interface[0].*.access_config[0].*.nat_ip[0] }, user = local.user })
# filename = "../ansible_kind/hosts"
# depends_on = [module.compute_instances]
# }

# resource "null_resource" "config_vm" {
# provisioner "local-exec" {
# command = "ansible-playbook -i '../ansible_kind/hosts' --private-key ${local.ssh_private_key_path} ../ansible_kind/kind_setup.yaml"
# }
# depends_on = [local_file.ansible_inventory]
# }
2 changes: 1 addition & 1 deletion infra/general.auto.tfvars
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# General Settings
project_id = "xxxx"
project_id = "xxxxx"
region = "europe-west1"
zone = "europe-west1-b"
2 changes: 0 additions & 2 deletions infra/instance_templates.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ locals {
# network_tier = null
# }]
# }]
startup_script = file("scripts/startup.sh")
service_account = {
email = "compute-general@${var.project_id}.iam.gserviceaccount.com"
scopes = []
Expand Down Expand Up @@ -64,7 +63,6 @@ module "instance_templates" {
access_config = each.value.access_config
# additional_networks = each.value.additional_networks
subnetwork_project = var.project_id
startup_script = each.value.startup_script
service_account = each.value.service_account
depends_on = [module.subnets, module.service_accounts]
}
1 change: 0 additions & 1 deletion infra/keys/nephio

This file was deleted.

1 change: 1 addition & 0 deletions infra/keys/nephio.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${user}:${pub_key}
7 changes: 3 additions & 4 deletions infra/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# outputs.tf

output "name" {
output "vm_names" {
description = "VM Name"
value = { for k, vm in module.compute_instances : k => vm.*.instances_details[0].*.name }
}

output "ip" {
output "vm_external_ips" {
description = "VM External IP"
value = { for k, vm in module.compute_instances : k => vm.*.instances_details[0].*.network_interface[0].*.access_config[0].*.nat_ip[0] }
value = { for k, vm in module.compute_instances : k => vm.instances_details[*].*.network_interface[0].*.access_config[0].*.nat_ip[0] }
}

9 changes: 0 additions & 9 deletions infra/scripts/startup.sh

This file was deleted.

4 changes: 1 addition & 3 deletions infra/ssh_keys.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
resource "google_compute_project_metadata" "ssh_keys" {
metadata = {
ssh-keys = file("keys/nephio")
ssh-keys = local_file.public_key.content
}
}

#to genereate the key: ssh-keygen -t rsa -f ~/.ssh/nephio.pub -C nephio -b 2048 and then edit keys/nephio and change it to the format username:ssh-rsa xxxxx username
6 changes: 6 additions & 0 deletions infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ variable "zone" {
type = string
default = null
}

variable "ssh_private_key_path" {
description = "Private SSH Key Path to be provided on terraform apply run"
type = string
default = null
}

0 comments on commit 0f45daf

Please sign in to comment.