Skip to content

Commit 7456868

Browse files
committed
feat: gcp resources, with name prefix
1 parent ec67a62 commit 7456868

21 files changed

Lines changed: 248 additions & 130 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
!infrastructure-templates
1212
infrastructure-templates/**/.terraform.d
1313
!context.tar
14+
!flake.*

Dockerfile

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RUN curl -L0 https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linu
55
WORKDIR /app
66
COPY --chown=nonroot ./terraform ./terraform
77
RUN mkdir -p infrastructure-templates
8-
# COPY --chown=nonroot ./infrastructure-templates ./infrastructure-templates
8+
COPY --chown=nonroot ./infrastructure-templates ./infrastructure-templates
99
ENV TF_PLUGIN_CACHE_DIR="/app/.terraform.d/plugin-cache"
1010
# COPY .terraform.d.zip /app/terraform.zip
1111
RUN mkdir -p $TF_PLUGIN_CACHE_DIR
@@ -20,39 +20,39 @@ RUN <<'EOF'
2020
wait
2121

2222
tdir=$(basename $(dirname $TF_PLUGIN_CACHE_DIR))
23-
# tar cf - $tdir | lz4 -v -5 > tf.lz4 && rm -rf $tdir
2423
tar cf - $tdir | zstd --compress > tf.zst && rm -rf $tdir
2524
EOF
2625

2726
# ENV DECOMPRESS_CMD="lz4 -d tf.lz4 | tar xf -"
2827
ENV DECOMPRESS_CMD="zstd --decompress tf.zst --stdout | tar xf -"
2928
ENV TEMPLATES_DIR="/app/infrastructure-templates"
30-
31-
# WORKDIR /app
32-
# ENV TF_PLUGIN_CACHE_DIR="/app/.terraform.d/plugin-cache"
33-
# RUN mkdir -p $TF_PLUGIN_CACHE_DIR
34-
# RUN cat > script.sh <<EOF
35-
# #!/usr/bin/env bash
36-
# echo "hi" >> log.file
37-
# ls -d ./infrastructure-templates/{gcp,aws}/* | tee log.file | xargs -I{} bash -c "echo name is {}; $(terraform init chdir={} -backend=false &)"
38-
# # for dir in $(ls -d ./infrastructure-templates/{gcp,aws}/*); do
39-
# # echo $dir >> log.file
40-
# # pushd $dir
41-
# # terraform init -backend=false &
42-
# # popd
43-
# # done
44-
#
45-
# wait
4629
#
47-
# tdir=$(basename $(dirname $TF_PLUGIN_CACHE_DIR))
48-
# tar cf - $tdir | lz4 -v -5 > tf.lz4 && rm -rf $tdir
49-
# EOF
50-
#
51-
# COPY --chown=nonroot ./terraform ./terraform
52-
# COPY --chown=nonroot ./infrastructure-templates ./infrastructure-templates
53-
# RUN --mount=type=cache,id=sample,target=/app/.terraform.d/plugin-cache \
54-
# chmod +x /app/script.sh && bash /app/script.sh
55-
# RUN adduser --disabled-password --home="/app" --uid 1717 nonroot
56-
# USER nonroot
30+
WORKDIR /app
31+
ENV TF_PLUGIN_CACHE_DIR="/app/.terraform.d/plugin-cache"
32+
RUN mkdir -p $TF_PLUGIN_CACHE_DIR
33+
RUN cat > script.sh <<EOF
34+
COPY --chown=nonroot ./terraform ./terraform
35+
COPY --chown=nonroot ./infrastructure-templates ./infrastructure-templates
36+
RUN --mount=type=cache,id=sample,target=$TF_PLUGIN_CACHE_DIR <<EOF
37+
#!/usr/bin/env bash
38+
echo "hi" >> log.file
39+
# ls -d ./infrastructure-templates/{gcp,aws}/* | tee log.file | xargs -I{} bash -c "echo name is {}; $(terraform init chdir={} -backend=false &)"
40+
for dir in $(ls -d ./infrastructure-templates/{gcp,aws}/*); do
41+
echo $dir >> log.file
42+
pushd $dir
43+
terraform init -backend=false &
44+
popd
45+
done
46+
47+
wait
48+
49+
tdir=$(basename $(dirname $TF_PLUGIN_CACHE_DIR))
50+
echo "hello" >> log.file
51+
# tar cf - $tdir | lz4 -v -5 > tf.lz4 && rm -rf $tdir
52+
tar cf - $tdir | zstd --compress > tf.zst && rm -rf $tdir
53+
EOF
54+
RUN adduser --disabled-password --home="/app" --uid 1717 nonroot
55+
USER nonroot
5756
# ENV DECOMPRESS_CMD="lz4 -d tf.lz4 | tar xf -"
58-
# ENV TEMPLATES_DIR="/app/infrastructure-templates"
57+
ENV DECOMPRESS_CMD="zstd --decompress tf.zst --stdout | tar xf -"
58+
ENV TEMPLATES_DIR="/app/infrastructure-templates"

DockerfileNIX

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
1+
# FROM alpine:latest AS base
2+
13
# vim: set ft=dockerfile:
24
FROM nixos/nix:latest AS builder
3-
5+
# FROM ghcr.io/nxtcoder17/nix:latest AS builder
46
WORKDIR /app
57

68
# COPY flake.nix flake.lock ./
7-
ENV NIX_STORE_DIR=/nix/store2
9+
# RUN mkdir x && \
10+
# cp -rL $(which bash) ./x/bash && \
11+
# cp -rL $(which sh) ./x/sh && \
12+
# cp -rL $(which ln) ./x/ln
13+
14+
# COPY --from=base /bin/sh /bin/sh
15+
# COPY --from=base /bin/ln /bin/ln
16+
17+
# RUN ls -al ./x && exit 1
18+
19+
# SHELL ["/app/bash", "-c"]
20+
# RUN nix-env -iv util-linux
21+
RUN cat > /etc/nix/nix.conf <<EOF
22+
build-users-group = nixbld
23+
sandbox = false
24+
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= local:2V5FDaWeruKxCtCBMS9FsCSueXMQwdz+cVLNlG3GWiY=
25+
EOF
826

927
RUN --mount=type=bind,source=flake.nix,target=flake.nix \
1028
--mount=type=bind,source=flake.lock,target=flake.lock \
11-
--mount=type=cache,target=/nix/store2 \
12-
cp -R /nix/store /nix/store2 && nix \
13-
--extra-experimental-features "nix-command flakes" \
29+
nix --extra-experimental-features "nix-command flakes" \
1430
--option filter-syscalls false \
15-
build .#container
31+
build .#container --substituters http://192.168.0.113:5000/
1632

1733
# Copy the Nix store closure into a directory. The Nix store closure is the
1834
# entire set of Nix store values that we need for our build.
@@ -45,9 +61,8 @@ ENV TF_PLUGIN_CACHE_DIR="/app/.terraform.d/plugin-cache"
4561
# COPY .terraform.d.zip /app/terraform.zip
4662
RUN mkdir -p $TF_PLUGIN_CACHE_DIR
4763
SHELL ["bash", "-c"]
48-
RUN <<'EOF'
49-
# for dir in $(ls -d ./infrastructure-templates/{gcp,aws}/*); do
50-
for dir in $(ls -d ./infrastructure-templates/${CLOUD_PROVIDER}/*); do
64+
RUN --mount=type=cache,id=sample,target=$TF_PLUGIN_CACHE_DIR <<EOF
65+
for dir in $(ls -d ./infrastructure-templates/{aws,gcp}/*); do
5166
pushd $dir
5267
terraform init -backend=false &
5368
popd
@@ -56,8 +71,7 @@ RUN <<'EOF'
5671
wait
5772

5873
tdir=$(basename $(dirname $TF_PLUGIN_CACHE_DIR))
59-
# tar cf - $tdir | lz4 -v -5 > tf.lz4 && rm -rf $tdir
60-
tar cf - $tdir | zstd -12 --compress > tf.zst && rm -rf $tdir
74+
tar cf - $tdir | zstd --compress > tf.zst && rm -rf $tdir
6175
EOF
6276
# ENV DECOMPRESS_CMD="lz4 -d tf.lz4 | tar xf -"
6377
ENV DECOMPRESS_CMD="zstd --decompress tf.zst --stdout | tar xf -"

DockerfileNIX2

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ COPY --from=nix /nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt /etc/s
99
WORKDIR /app
1010
RUN --mount=type=bind,source=context.tar,target=context.tar \
1111
tar xf context.tar && \
12-
mkdir /nix && mv nixstore /nix/store && \
12+
mkdir -p /nix && mv nixstore /nix/store && \
1313
mkdir -p /usr/local/bin && mv result/bin/* /usr/local/bin/ && rm -rf result && \
1414
mv tf.zst /app/tf.zst
1515

1616
RUN adduser --disabled-password --home="/app" --uid 1717 nonroot
17-
USER nonroot
17+
# RUN chown -R nonroot:nonroot output
18+
19+
# FROM scratch
20+
# COPY --from=base /app/output/ /
21+
# RUN mkdir -p /etc
22+
# WORKDIR /app
23+
# COPY --from=base /etc/passwd /etc/passwd
1824
COPY --chown=nonroot ./terraform ./terraform
1925
COPY --chown=nonroot ./infrastructure-templates ./infrastructure-templates
2026
# COPY --from=nixstore ./ /nix/store
@@ -26,6 +32,7 @@ COPY --chown=nonroot ./infrastructure-templates ./infrastructure-templates
2632
# COPY ./terraform ./terraform
2733
# ARG CLOUD_PROVIDER
2834
# COPY ./infrastructure-templates/${CLOUD_PROVIDER} ./infrastructure-templates/${CLOUD_PROVIDER}
35+
# USER nonroot
2936
ENV TF_PLUGIN_CACHE_DIR="/app/.terraform.d/plugin-cache"
3037
# # COPY ./tf.zst ./tf.zst
3138
ENV DECOMPRESS_CMD="zstd --decompress tf.zst --stdout | tar xf -"

Taskfile.yml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ tasks:
2424
fi
2525
2626
local:build:iac-job:
27-
preconditions:
28-
- sh: '[[ -n "{{.cloudprovider}}" ]]'
29-
msg: 'var cloudprovider must have a value'
3027
vars:
28+
# output_dir:
29+
# sh: mktemp -d --suffix '.nix'
30+
output_dir: ./result
3131
nix_store_closure: /tmp/nix-store-closure
3232
cmds:
33-
- sudo rm -rf result
34-
- nix build .#container
33+
- echo OUTPUT DIR is "{{.output_dir}}"
34+
- sudo rm -rf {{.output_dir}}
35+
- nix build .#container -o {{.output_dir}}
3536
- sudo rm -rf {{.nix_store_closure}}
3637
- mkdir {{.nix_store_closure}}
37-
- cp -R $(nix-store -qR result/) {{.nix_store_closure}}
38+
- cp -R $(nix-store -qR {{.output_dir}}/) {{.nix_store_closure}}
3839
- |+
3940
export TF_PLUGIN_CACHE_DIR="$PWD/.terraform.d/plugin-cache"
4041
# for dir in $(ls -d ./infrastructure-templates/{{.cloudprovider}}/*); do
@@ -53,25 +54,27 @@ tasks:
5354
- |+
5455
dir=$(mktemp -d)
5556
rm -rf context.tar
56-
mv tf.zst result $dir
57-
mv /tmp/nix-store-closure $dir/nixstore
57+
mv tf.zst $dir
58+
mv {{.nix_store_closure}} $dir/nixstore
59+
mv {{.output_dir}} $dir/result
5860
pushd $dir
5961
tar cf context.tar .
6062
popd
6163
mv $dir/context.tar .
62-
- nerdctl build -f DockerfileNIX2 --build-arg CLOUD_PROVIDER={{.cloudprovider}} -t {{.Image}} . --no-cache
63-
# - rm ./context.tar
64+
# - docker buildx build -f DockerfileNIX2 --build-arg CLOUD_PROVIDER={{.cloudprovider}} --output=type=image,compression=zstd,force-compression=true,compression-level=22,push=true,oci-mediatypes=true -t {{.Image}} .
65+
- nerdctl build -f DockerfileNIX2 -o type=oci,push=true,name={{.Image}},compression=zstd,compression-level=22,force-compression=true,oci-mediatype=true -t {{.Image}} .
66+
- rm ./context.tar
6467

6568
container:build-and-push:
6669
preconditions:
6770
- sh: '[[ -n "{{.Image}}" ]]'
6871
msg: 'var Image must have a value'
6972
vars:
7073
push: true
71-
dockerArgs: "{{.DockerArgs | default ''}}"
7274
cmds:
73-
- docker build -t {{.Image}} . {{.dockerArgs}}
74-
- |+
75-
if [ "{{.push}}" == "true" ]; then
76-
docker push {{.Image}}
77-
fi
75+
- docker buildx build -f DockerfileNIX --output=type=image,compression=zstd,force-compression=true,compression-level=22,push=true,oci-mediatypes=true -t {{.Image}} .
76+
# - docker build -f ./DockerfileNIX -t {{.Image}} . {{.dockerArgs}}
77+
# - |+
78+
# if [ "{{.push}}" == "true" ]; then
79+
# docker push {{.Image}}
80+
# fi

cmd/gcp-spot-node-terminator/script.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ while (true) {
9999
}
100100

101101
// checking preempted url
102-
const {stdout: preemptedStatus} = await $`curl "http://169.254.169.254/computeMetadata/v1/instance/preempted" -H "Metadata-Flavor: Google"`
102+
const {stdout: preemptedStatus} = await $`curl -s "http://169.254.169.254/computeMetadata/v1/instance/preempted" -H "Metadata-Flavor: Google"`
103103
if (preemptedStatus.trim().toLowerCase() == "true") {
104104
info("node is preempted, exiting")
105105
if (webhookURL){

flake.nix

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
allowUnfree = true;
1515
};
1616
};
17+
1718
binaries = with pkgs; [
1819
bash
1920
# coreutils
@@ -28,7 +29,7 @@
2829
];
2930

3031
container = pkgs.buildEnv {
31-
name = "container";
32+
name = "container2";
3233
paths = with pkgs; [
3334
bash
3435
envsubst
@@ -38,6 +39,16 @@
3839
terraform
3940
];
4041
};
42+
43+
gcp-spot-node-terminator = pkgs.buildEnv {
44+
name = "gcp-spot-node-terminator";
45+
paths = with pkgs; [
46+
bash
47+
curl
48+
zx
49+
kubectl
50+
];
51+
};
4152
in
4253
{
4354
devShells.default = pkgs.mkShell {
@@ -59,7 +70,7 @@
5970
# programming tools
6071

6172
# build tools
62-
podman
73+
# podman
6374
upx
6475

6576
nmap
@@ -97,7 +108,7 @@
97108
packages.container = pkgs.stdenv.mkDerivation {
98109
name = "container";
99110
src = container;
100-
installPhase = "cp -rL $src $out/";
111+
installPhase = "cp -r $src $out/";
101112
# buildInputs = binaries;
102113
# buildCommand = let
103114
# copyBinaries = builtins.concatStringsSep "\n" (builtins.map(input:

infrastructure-templates/gcp/master-nodes/main.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
resource "random_id" "name_suffix" {
2+
keepers = {
3+
# Generate a new id each time we switch to a new AMI name prefix
4+
name_prefix = var.name_prefix
5+
}
6+
7+
byte_length = 4
8+
}
9+
110
module "master-nodes-on-gcp" {
11+
# source = "../../../terraform/bundles/gcp/master-nodes"
212
source = "../../../terraform/bundles/gcp/master-nodes"
313
machine_type = var.machine_type
4-
name_prefix = var.name_prefix
14+
name_prefix = "${var.name_prefix}-${random_id.name_suffix.hex}"
515
nodes = var.nodes
616
provision_mode = var.provision_mode
717
kloudlite_params = var.kloudlite_params
818
save_ssh_key_to_path = var.save_ssh_key_to_path
919
cloudflare = var.cloudflare
1020
public_dns_host = var.public_dns_host
1121
save_kubeconfig_to_path = var.save_kubeconfig_to_path
12-
tags = var.tags
22+
labels = var.labels
1323
label_cloudprovider_region = var.gcp_region
1424
network = var.network
1525
service_account = var.service_account

infrastructure-templates/gcp/master-nodes/variables-master-nodes.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ variable "public_dns_host" {
5252

5353
variable "cloudflare" {
5454
description = "cloudflare related parameters"
55-
type = object({
55+
type = object({
5656
enabled = bool
5757
api_token = optional(string)
5858
zone_id = optional(string)
@@ -61,7 +61,7 @@ variable "cloudflare" {
6161

6262
validation {
6363
error_message = "if enabled, all mandatory Cloudflare bucket details are specified"
64-
condition = var.cloudflare == null || (var.cloudflare.enabled == true && alltrue([
64+
condition = var.cloudflare == null || (var.cloudflare.enabled == true && alltrue([
6565
var.cloudflare.api_token != "",
6666
var.cloudflare.zone_id != "",
6767
var.cloudflare.domain != "",
@@ -71,15 +71,15 @@ variable "cloudflare" {
7171

7272
variable "kloudlite_params" {
7373
description = "kloudlite related parameters"
74-
type = object({
74+
type = object({
7575
release = string
7676
install_crds = optional(bool, true)
7777
install_csi_driver = optional(bool, false)
7878
install_operators = optional(bool, false)
7979

8080
install_agent = optional(bool, false)
8181
install_autoscalers = optional(bool, true)
82-
agent_vars = optional(object({
82+
agent_vars = optional(object({
8383
account_name = string
8484
cluster_name = string
8585
cluster_token = string
@@ -111,7 +111,7 @@ variable "label_cloudprovider_region" {
111111
default = ""
112112
}
113113

114-
variable "tags" {
114+
variable "labels" {
115115
type = map(string)
116116
description = "map of Key => Value to be tagged along created resources"
117117
default = {}

infrastructure-templates/gcp/vpc/.terraform.lock.hcl

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)