Skip to content

Commit

Permalink
style: rename ubuntu jammy client to account for different archs
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta committed Dec 13, 2024
1 parent a1a02a5 commit 09d88bc
Show file tree
Hide file tree
Showing 26 changed files with 544 additions and 37 deletions.
2 changes: 1 addition & 1 deletion e2e/terraform/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CONSUL_LICENSE_PATH ?=
custom.tfvars:
echo 'nomad_local_binary = "$(PKG_PATH)"' > custom.tfvars
echo 'volumes = false' >> custom.tfvars
echo 'client_count_ubuntu_jammy_amd64 = 3' >> custom.tfvars
echo 'client_count_linux = 3' >> custom.tfvars
echo 'client_count_windows_2016_amd64 = 0' >> custom.tfvars
echo 'consul_license = "$(shell cat $(CONSUL_LICENSE_PATH))"' >> custom.tfvars
echo 'nomad_license = "$(shell cat $(NOMAD_LICENSE_PATH))"' >> custom.tfvars
Expand Down
2 changes: 1 addition & 1 deletion e2e/terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Linux clients or Windows clients.
region = "us-east-1"
instance_type = "t2.medium"
server_count = "3"
client_count_ubuntu_jammy_amd64 = "4"
client_count_linux = "4"
client_count_windows_2016_amd64 = "1"
```

Expand Down
32 changes: 28 additions & 4 deletions e2e/terraform/compute.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

locals {
ami_prefix = "nomad-e2e-v3"
ubuntu_instance_name = "ubuntu-jammy-${var.instance_architecture}"
}

resource "aws_instance" "server" {
Expand All @@ -22,18 +23,18 @@ resource "aws_instance" "server" {
}
}

resource "aws_instance" "client_ubuntu_jammy_amd64" {
ami = data.aws_ami.ubuntu_jammy_amd64.image_id
resource "aws_instance" "client_ubuntu_jammy" {
ami = data.aws_ami.ubuntu_jammy.image_id
instance_type = var.instance_type
key_name = module.keys.key_name
vpc_security_group_ids = [aws_security_group.clients.id] # see also the secondary ENI
count = var.client_count_ubuntu_jammy_amd64
count = var.client_count_linux
iam_instance_profile = data.aws_iam_instance_profile.nomad_e2e_cluster.name
availability_zone = var.availability_zone

# Instance tags
tags = {
Name = "${local.random_name}-client-ubuntu-jammy-amd64-${count.index}"
Name = "${local.random_name}-client-ubuntu-jammy-${count.index}"
ConsulAutoJoin = "auto-join-${local.random_name}"
User = data.aws_caller_identity.current.arn
}
Expand Down Expand Up @@ -102,6 +103,29 @@ data "aws_ami" "ubuntu_jammy_amd64" {
name = "tag:BuilderSha"
values = [data.external.packer_sha.result["sha"]]
}
<<<<<<< Updated upstream
=======
}

data "aws_ami" "ubuntu_jammy" {
most_recent = true
owners = ["self"]

filter {
name = "name"
values = ["${local.ami_prefix}-${ubuntu_instance_name}-*"]
}

filter {
name = "tag:OS"
values = ["Ubuntu"]
}

filter {
name = "tag:BuilderSha"
values = [data.external.packer_sha.result["sha"]]
}
>>>>>>> Stashed changes
}

data "aws_ami" "windows_2016_amd64" {
Expand Down
4 changes: 2 additions & 2 deletions e2e/terraform/network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ resource "aws_network_interface" "clients_secondary" {
subnet_id = data.aws_subnet.secondary.id
security_groups = [aws_security_group.clients_secondary.id]

count = var.client_count_ubuntu_jammy_amd64
count = var.client_count_linux
attachment {
instance = aws_instance.client_ubuntu_jammy_amd64[count.index].id
instance = aws_instance.client_ubuntu_jammy[count.index].id
device_index = 1
}
}
Expand Down
8 changes: 4 additions & 4 deletions e2e/terraform/nomad.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ module "nomad_server" {

# TODO: split out the different Linux targets (ubuntu, centos, arm, etc.) when
# they're available
module "nomad_client_ubuntu_jammy_amd64" {
module "nomad_client_ubuntu_jammy" {
source = "./provision-nomad"
depends_on = [aws_instance.client_ubuntu_jammy_amd64]
count = var.client_count_ubuntu_jammy_amd64
depends_on = [aws_instance.client_ubuntu_jammy]
count = var.client_count_linux

platform = "linux"
arch = "linux_amd64"
role = "client"
index = count.index
instance = aws_instance.client_ubuntu_jammy_amd64[count.index]
instance = aws_instance.client_ubuntu_jammy[count.index]

nomad_local_binary = count.index < length(var.nomad_local_binary_client_ubuntu_jammy_amd64) ? var.nomad_local_binary_client_ubuntu_jammy_amd64[count.index] : var.nomad_local_binary

Expand Down
4 changes: 2 additions & 2 deletions e2e/terraform/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ output "servers" {
}

output "linux_clients" {
value = aws_instance.client_ubuntu_jammy_amd64.*.public_ip
value = aws_instance.client_ubuntu_jammy.*.public_ip
}

output "windows_clients" {
Expand All @@ -31,7 +31,7 @@ ssh into servers with:
ssh into clients with:
%{for ip in aws_instance.client_ubuntu_jammy_amd64.*.public_ip~}
%{for ip in aws_instance.client_ubuntu_jammy.*.public_ip~}
ssh -i keys/${local.random_name}.pem ubuntu@${ip}
%{endfor~}
%{for ip in aws_instance.client_windows_2016_amd64.*.public_ip~}
Expand Down
59 changes: 59 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

variable "build_sha" {
type = string
description = "the revision of the packer scripts building this image"
}

locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
distro = "ubuntu-jammy-22.04-arm64-server-*"
version = "v3"
}

source "amazon-ebs" "latest_ubuntu_jammy" {
ami_name = "nomad-e2e-${local.version}-ubuntu-jammy-arm64-${local.timestamp}"
iam_instance_profile = "packer_build" // defined in nomad-e2e repo
instance_type = "m7a.large"
region = "us-east-1"
ssh_username = "ubuntu"
ssh_interface = "public_ip"

source_ami_filter {
filters = {
architecture = "arm64"
"block-device-mapping.volume-type" = "gp2"
name = "ubuntu/images/hvm-ssd/${local.distro}"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"] // Canonical
}

tags = {
OS = "Ubuntu"
Version = "Jammy"
BuilderSha = var.build_sha
}
}

build {
sources = ["source.amazon-ebs.latest_ubuntu_jammy"]

provisioner "file" {
destination = "/tmp/linux"
source = "./ubuntu-jammy-arm64"
}

// cloud-init modifies the apt sources, so we need to wait
// before running our setup
provisioner "shell-local" {
inline = ["sleep 30"]
}

provisioner "shell" {
script = "./ubuntu-jammy-arm64/setup.sh"
}
}
9 changes: 9 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/cni/cni_args.conflist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cniVersion": "1.0.0",
"name": "cni_args",
"plugins": [
{
"type": "cni_args.sh"
}
]
}
70 changes: 70 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/cni/cni_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash

# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

set -euo pipefail

# things are prefixed with "Fancy*" because this is a fancy plugin.
# CNI_ARGS='IgnoreUnknown=true;FancyTaskDir=/tmp/cni_args;FancyMessage=hiiii;Another=whatever'
# what we need to do:
# 1. read CNI_ARGS environment variable
# * write to a file named $FancyTaskDir/victory
# 2. write CNI-spec json to stdout for Nomad to read

# https://github.com/containernetworking/cni/blob/main/SPEC.md#version-success
function version() {
cat <<EOF
{
"cniVersion": "1.0.0",
"supportedVersions": [ "0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0" ]
}
EOF
}

# https://github.com/containernetworking/cni/blob/main/SPEC.md#add-success
function add() {
# get our task dir out of the env var
task_dir="$(echo "$CNI_ARGS" | tr ';' '\n' | awk -F= '/^FancyTaskDir=/ {print$2}')"
message="$(echo "$CNI_ARGS" | tr ';' '\n' | awk -F= '/^FancyMessage=/ {print$2}')"
1>&2 echo "got task dir: $task_dir; message: $message"

mkdir -p "$task_dir"
# and write something to a file we can check in the test.
echo "$message" > "$task_dir/victory"
}

# run the appropriate CNI command
case "$CNI_COMMAND" in
VERSION) version ; exit ;;
ADD) add ;;
esac

# bogus reply so nomad doesn't error
cat <<EOF
{
"cniVersion" : "1.0.0",
"ips": [
{
"address": "10.1.0.5/16",
"gateway": "10.1.0.1",
"interface": 0
}
],
"routes": [
{
"dst": "0.0.0.0/0"
}
],
"interfaces": [
{
"name": "cni0",
"mac": "00:11:22:33:44:55"
}
],
"dns": {
"nameservers": [ "10.1.0.1" ]
}
}
EOF

5 changes: 5 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/cni/loopback.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cniVersion": "1.0.0",
"name": "test-loopback-conf",
"type": "loopback"
}
7 changes: 7 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/cni/loopback.conflist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cniVersion": "1.0.0",
"name": "test-loopback-conflist",
"plugins": [{
"type": "loopback"
}]
}
5 changes: 5 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/cni/loopback.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cniVersion": "1.0.0",
"name": "test-loopback-json",
"type": "loopback"
}
16 changes: 16 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/consul.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Consul Agent
Requires=network-online.target
After=network-online.target

[Service]
Restart=on-failure
Environment=CONSUL_ALLOW_PRIVILEGED_PORTS=true
ExecStart=/usr/local/bin/consul agent -config-dir="/etc/consul.d"
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM
User=root
Group=root

[Install]
WantedBy=multi-user.target
55 changes: 55 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/dnsconfig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

set -e

# These tasks can't be executed during AMI builds because they rely on
# instance-specific data.

mkdir -p /var/run/dnsmasq
mkdir -p /etc/dnsmasq.d

# Add hostname to /etc/hosts
echo "127.0.0.1 $(hostname)" | tee --append /etc/hosts

# this script should run after docker.service but we can't guarantee
# it's created docker0 yet, so wait to make sure
while ! (ip link | grep -q docker0)
do
sleep 1
done

# Use dnsmasq first and then docker bridge network for DNS resolution
DOCKER_BRIDGE_IP_ADDRESS=$(docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}')
cat <<EOF > /tmp/resolv.conf
nameserver 127.0.0.1
nameserver $DOCKER_BRIDGE_IP_ADDRESS
EOF
cp /tmp/resolv.conf /etc/resolv.conf

# need to get the interface for dnsmasq config so that we can
# accomodate both "predictable" and old-style interface names
IFACE=$(ip route | grep default | awk '{print $5}')

cat <<EOF > /tmp/dnsmasq
port=53
resolv-file=/var/run/dnsmasq/resolv.conf
bind-interfaces
interface=docker0
interface=lo
interface=$IFACE
listen-address=127.0.0.1
server=/consul/127.0.0.1#8600
EOF
cp /tmp/dnsmasq /etc/dnsmasq.d/default

# need to get the AWS DNS address from the VPC...
# this is pretty hacky but will work for any typical case
MAC=$(curl -s --fail http://169.254.169.254/latest/meta-data/mac)
CIDR_BLOCK=$(curl -s --fail "http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/vpc-ipv4-cidr-block")
VPC_DNS_ROOT=$(echo "$CIDR_BLOCK" | cut -d'.' -f1-3)
echo "nameserver ${VPC_DNS_ROOT}.2" > /tmp/dnsmasq-resolv.conf
cp /tmp/dnsmasq-resolv.conf /var/run/dnsmasq/resolv.conf

/usr/sbin/dnsmasq --test
8 changes: 8 additions & 0 deletions e2e/terraform/packer/ubuntu-jammy-arm64/dnsmasq
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
port=53
resolv-file=/var/run/dnsmasq/resolv.conf
bind-interfaces
interface=docker0
interface=lo
interface=eth0
listen-address=127.0.0.1
server=/consul/127.0.0.1#8600
Loading

0 comments on commit 09d88bc

Please sign in to comment.