Skip to content

Commit 073fea8

Browse files
committed
Use OpenTofu to create GCP infrastructure
1 parent 8cf2608 commit 073fea8

20 files changed

+457
-239
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ trim_trailing_whitespace = true
88
indent_size = 4
99
indent_style = tab
1010

11-
[*.{md,yml,yaml}]
11+
[*.{md,yml,yaml,tf}]
1212
indent_size = 2
1313
indent_style = space

.github/workflows/nfr.yml

+31-31
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ jobs:
8888
id: auth
8989
uses: google-github-actions/auth@6fc4af4b145ae7821d527454aa9bd537d1f2dc5f # v2.1.7
9090
with:
91-
token_format: access_token
9291
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY }}
9392
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
9493

@@ -98,44 +97,47 @@ jobs:
9897
project_id: ${{ secrets.GCP_PROJECT_ID }}
9998
install_components: kubectl
10099

100+
- name: Setup OpenTofu
101+
uses: opentofu/setup-opentofu@592200bd4b9bbf4772ace78f887668b1aee8f716 # v1.0.5
102+
with:
103+
tofu_version: 1.8.7 # renovate: datasource=github-tags depName=opentofu/opentofu
104+
tofu_wrapper: false
105+
106+
- name: Initialize OpenTofu
107+
working-directory: ./tests/tofu
108+
run: |
109+
tofu version
110+
tofu init
111+
112+
- name: Setup tfvars file
113+
working-directory: ./tests/tofu
114+
run: |
115+
cat <<EOF > terraform.tfvars
116+
gke_project = "${{ secrets.GCP_PROJECT_ID }}"
117+
gke_cluster_name = "nfr-tests-${{ github.run_id }}-${{ matrix.type }}"
118+
gke_num_nodes = 12
119+
gke_machine_type = "n2d-standard-16"
120+
gke_nodes_service_account = "${{ secrets.GKE_NODES_SERVICE_ACCOUNT }}"
121+
vm_service_account = "${{ secrets.GCP_SERVICE_ACCOUNT }}"
122+
ngf_branch = "${{ github.ref_name }}"
123+
EOF
124+
101125
- name: Setup dotenv file
102126
working-directory: ./tests/scripts
103127
run: |
104-
echo "RESOURCE_NAME=nfr-tests-${{ github.run_id }}-${{ matrix.type }}" >> vars.env
105128
echo "TAG=${{ needs.vars.outputs.image_tag }}" >> vars.env
106129
echo "PREFIX=ghcr.io/nginxinc/nginx-gateway-fabric" >> vars.env
107130
echo "NGINX_PREFIX=ghcr.io/nginxinc/nginx-gateway-fabric/nginx" >> vars.env
108131
echo "NGINX_PLUS_PREFIX=us-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/nginx-gateway-fabric/nginx-plus" >> vars.env
109-
echo "GKE_CLUSTER_NAME=nfr-tests-${{ github.run_id }}-${{ matrix.type }}" >> vars.env
110-
echo "GKE_CLUSTER_ZONE=us-west1-b" >> vars.env
111-
echo "GKE_CLUSTER_REGION=us-west1" >> vars.env
112-
echo "GKE_PROJECT=${{ secrets.GCP_PROJECT_ID }}" >> vars.env
113-
echo "GKE_SVC_ACCOUNT=${{ secrets.GCP_SERVICE_ACCOUNT }}" >> vars.env
114-
echo "GKE_NODES_SERVICE_ACCOUNT=${{ secrets.GKE_NODES_SERVICE_ACCOUNT }}" >> vars.env
115-
echo "NETWORK_TAGS=nfr-tests-${{ github.run_id }}-${{ matrix.type }}" >> vars.env
116-
echo "NGF_BRANCH=${{ github.ref_name }}" >> vars.env
117-
echo "SOURCE_IP_RANGE=$(curl -sS -4 icanhazip.com)/32" >> vars.env
118-
echo "ADD_VM_IP_AUTH_NETWORKS=true" >> vars.env
119132
echo "PLUS_ENABLED=${{ matrix.type == 'plus' }}" >> vars.env
120133
echo "GINKGO_LABEL=" >> vars.env
121134
echo "NGF_VERSION=${{ needs.vars.outputs.version }}" >> vars.env
122-
echo "GKE_NUM_NODES=12" >> vars.env
123-
echo "GKE_MACHINE_TYPE=n2d-standard-16" >> vars.env
124135
echo "PLUS_USAGE_ENDPOINT=${{ secrets.JWT_PLUS_REPORTING_ENDPOINT }}" >> vars.env
125136
126-
- name: Setup license file for plus
127-
if: matrix.type == 'plus'
128-
env:
129-
PLUS_LICENSE: ${{ secrets.JWT_PLUS_REPORTING }}
130-
run: echo "${PLUS_LICENSE}" > license.jwt
131-
132-
- name: Create GKE cluster
133-
working-directory: ./tests
134-
run: make create-gke-cluster CI=true
135-
136-
- name: Create and setup VM
137-
working-directory: ./tests
138-
run: make create-and-setup-vm
137+
- name: Create GKE cluster and VM
138+
working-directory: ./tests/tofu
139+
run: |
140+
tofu apply --auto-approve
139141
140142
- name: Run Tests
141143
working-directory: ./tests
@@ -153,12 +155,10 @@ jobs:
153155
path: tests/results/**/*-${{ matrix.type }}.*
154156

155157
- name: Cleanup
156-
working-directory: ./tests
158+
working-directory: ./tests/tofu
157159
if: always()
158160
run: |
159-
bash scripts/cleanup-vm.sh true
160-
make delete-gke-cluster
161-
rm -rf scripts/vars.env
161+
tofu destroy --auto-approve
162162
163163
pr-results:
164164
name: Open PR with results

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,32 @@ internal/mode/static/nginx/modules/coverage
6161
# temporary files used for manifest generation
6262
config/base/deploy.yaml
6363
config/base/deploy.yaml.bak
64+
65+
# Local .terraform directories
66+
**/.terraform/*
67+
68+
# .tfstate files
69+
*.tfstate
70+
*.tfstate.*
71+
72+
# Crash log files
73+
crash.log
74+
crash.*.log
75+
76+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
77+
# password, private keys, and other secrets. These should not be part of version
78+
# control as they are data points which are potentially sensitive and subject
79+
# to change depending on the environment.
80+
*.tfvars
81+
*.tfvars.json
82+
83+
# Ignore override files as they are usually used to override resources locally and so
84+
# are not checked in
85+
override.tf
86+
override.tf.json
87+
*_override.tf
88+
*_override.tf.json
89+
90+
# Ignore CLI configuration files
91+
.terraformrc
92+
terraform.rc

.pre-commit-config.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ repos:
8080
- --template-files=README.md.gotmpl
8181

8282
- repo: https://github.com/scop/pre-commit-shfmt
83-
rev: v3.8.0-1
83+
rev: v3.10.0-2
8484
hooks:
8585
- id: shfmt-src
8686
args: [-w, -s, -i, "4"]
@@ -95,6 +95,12 @@ repos:
9595
- "--skip-auto-generation=required,additionalProperties"
9696
- --append-newline
9797

98+
- repo: https://github.com/tofuutils/pre-commit-opentofu
99+
rev: v2.1.0
100+
hooks:
101+
- id: tofu_fmt
102+
# - id: tofu_docs
103+
98104
ci:
99105
skip: [golangci-lint-full, prettier, markdownlint-cli2, yamllint]
100106
autofix_prs: false

tests/Makefile

+5-28
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,11 @@ ifeq ($(PLUS_ENABLED),true)
9090
endif
9191

9292
.PHONY: setup-gcp-and-run-nfr-tests
93-
setup-gcp-and-run-nfr-tests: create-gke-router create-and-setup-vm nfr-test ## Create and setup a GKE router and GCP VM for tests and run the NFR tests
93+
setup-gcp-and-run-nfr-tests: create-gcp-resources nfr-test ## Create and setup a GKE router and GCP VM for tests and run the NFR tests
9494

9595
.PHONY: create-gke-cluster
96-
create-gke-cluster: ## Create a GKE cluster
97-
./scripts/create-gke-cluster.sh $(CI)
98-
99-
.PHONY: create-and-setup-vm
100-
create-and-setup-vm: ## Create and setup a GCP VM for tests
101-
./scripts/create-and-setup-gcp-vm.sh
102-
103-
.PHONY: create-gke-router
104-
create-gke-router: ## Create a GKE router to allow egress traffic from private nodes (allows for external image pulls)
105-
./scripts/create-gke-router.sh
96+
create-gcp-resources: ## Create a GKE cluster and a GCP VM for tests
97+
tofu -chdir=tofu apply
10698

10799
.PHONY: sync-files-to-vm
108100
sync-files-to-vm: ## Syncs your local NGF files with the NGF repo on the VM
@@ -150,23 +142,8 @@ test-with-plus: PLUS_ENABLED=true
150142
test-with-plus: check-for-plus-usage-endpoint test ## Runs the functional tests for NGF with NGINX Plus on your default k8s cluster
151143

152144
.PHONY: cleanup-gcp
153-
cleanup-gcp: cleanup-router cleanup-vm delete-gke-cluster ## Cleanup all GCP resources
154-
155-
.PHONY: cleanup-router
156-
cleanup-router: ## Delete the GKE router
157-
./scripts/cleanup-router.sh
158-
159-
.PHONY: cleanup-vm
160-
cleanup-vm: ## Delete the test GCP VM and delete the firewall rule
161-
./scripts/cleanup-vm.sh
162-
163-
.PHONY: delete-gke-cluster
164-
delete-gke-cluster: ## Delete the GKE cluster
165-
./scripts/delete-gke-cluster.sh
166-
167-
.PHONY: add-local-ip-to-cluster
168-
add-local-ip-to-cluster: ## Add local IP to the GKE cluster master-authorized-networks
169-
./scripts/add-local-ip-auth-networks.sh
145+
cleanup-gcp: ## Cleanup all GCP resources
146+
tofu -chdir=tofu destroy
170147

171148
HELM_PARAMETERS += --set nameOverride=nginx-gateway --set nginxGateway.kind=skip --set service.create=false --skip-schema-validation
172149

tests/scripts/add-local-ip-auth-networks.sh

-10
This file was deleted.

tests/scripts/cleanup-router.sh

-8
This file was deleted.

tests/scripts/cleanup-vm.sh

-17
This file was deleted.

tests/scripts/create-and-setup-gcp-vm.sh

-68
This file was deleted.

tests/scripts/create-gke-cluster.sh

-40
This file was deleted.

tests/scripts/create-gke-router.sh

-15
This file was deleted.

tests/scripts/delete-gke-cluster.sh

-7
This file was deleted.

0 commit comments

Comments
 (0)