Skip to content

Commit dc74260

Browse files
committed
Use OpenTofu to create GCP infrastructure
1 parent 7d4e20c commit dc74260

21 files changed

+451
-251
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
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

Lines changed: 31 additions & 30 deletions
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,48 @@ 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
112132
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
119133
echo "PLUS_ENABLED=${{ matrix.type == 'plus' }}" >> vars.env
120134
echo "GINKGO_LABEL=" >> vars.env
121135
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
124136
echo "PLUS_USAGE_ENDPOINT=${{ secrets.JWT_PLUS_REPORTING_ENDPOINT }}" >> vars.env
125137
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
138+
- name: Create GKE cluster and VM
139+
working-directory: ./tests/tofu
140+
run: |
141+
tofu apply --auto-approve
139142
140143
- name: Run Tests
141144
working-directory: ./tests
@@ -153,12 +156,10 @@ jobs:
153156
path: tests/results/**/*-${{ matrix.type }}.*
154157

155158
- name: Cleanup
156-
working-directory: ./tests
159+
working-directory: ./tests/tofu
157160
if: always()
158161
run: |
159-
bash scripts/cleanup-vm.sh true
160-
make delete-gke-cluster
161-
rm -rf scripts/vars.env
162+
tofu destroy --auto-approve
162163
163164
pr-results:
164165
name: Open PR with results

.gitignore

Lines changed: 29 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 6 additions & 29 deletions
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
@@ -124,7 +116,7 @@ stop-longevity-test: nfr-test ## Stop the longevity test and collects results
124116
.PHONY: .vm-nfr-test
125117
.vm-nfr-test: ## Runs the NFR tests on the GCP VM (called by `nfr-test`)
126118
go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --keep-going --fail-on-pending \
127-
--trace -r -v --buildvcs --force-newlines $(GITHUB_OUTPUT) \
119+
--trace -r -vv --buildvcs --force-newlines $(GITHUB_OUTPUT) \
128120
--label-filter "nfr" $(GINKGO_FLAGS) --timeout 5h ./suite -- --gateway-api-version=$(GW_API_VERSION) \
129121
--gateway-api-prev-version=$(GW_API_PREV_VERSION) --image-tag=$(TAG) --version-under-test=$(NGF_VERSION) \
130122
--ngf-image-repo=$(PREFIX) --nginx-image-repo=$(NGINX_PREFIX) --nginx-plus-image-repo=$(NGINX_PLUS_PREFIX) \
@@ -151,23 +143,8 @@ test-with-plus: PLUS_ENABLED=true
151143
test-with-plus: check-for-plus-usage-endpoint test ## Runs the functional tests for NGF with NGINX Plus on your default k8s cluster
152144

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

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

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/scripts/cleanup-router.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/scripts/cleanup-vm.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 0 additions & 68 deletions
This file was deleted.

tests/scripts/create-gke-cluster.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/scripts/create-gke-router.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/scripts/delete-gke-cluster.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)