Skip to content

Commit

Permalink
Use OpenTofu to create GCP infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Dec 19, 2024
1 parent 8cf2608 commit 073fea8
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ trim_trailing_whitespace = true
indent_size = 4
indent_style = tab

[*.{md,yml,yaml}]
[*.{md,yml,yaml,tf}]
indent_size = 2
indent_style = space
62 changes: 31 additions & 31 deletions .github/workflows/nfr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ jobs:
id: auth
uses: google-github-actions/auth@6fc4af4b145ae7821d527454aa9bd537d1f2dc5f # v2.1.7
with:
token_format: access_token
workload_identity_provider: ${{ secrets.GCP_WORKLOAD_IDENTITY }}
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}

Expand All @@ -98,44 +97,47 @@ jobs:
project_id: ${{ secrets.GCP_PROJECT_ID }}
install_components: kubectl

- name: Setup OpenTofu
uses: opentofu/setup-opentofu@592200bd4b9bbf4772ace78f887668b1aee8f716 # v1.0.5
with:
tofu_version: 1.8.7 # renovate: datasource=github-tags depName=opentofu/opentofu
tofu_wrapper: false

- name: Initialize OpenTofu
working-directory: ./tests/tofu
run: |
tofu version
tofu init
- name: Setup tfvars file
working-directory: ./tests/tofu
run: |
cat <<EOF > terraform.tfvars
gke_project = "${{ secrets.GCP_PROJECT_ID }}"
gke_cluster_name = "nfr-tests-${{ github.run_id }}-${{ matrix.type }}"
gke_num_nodes = 12
gke_machine_type = "n2d-standard-16"
gke_nodes_service_account = "${{ secrets.GKE_NODES_SERVICE_ACCOUNT }}"
vm_service_account = "${{ secrets.GCP_SERVICE_ACCOUNT }}"
ngf_branch = "${{ github.ref_name }}"
EOF
- name: Setup dotenv file
working-directory: ./tests/scripts
run: |
echo "RESOURCE_NAME=nfr-tests-${{ github.run_id }}-${{ matrix.type }}" >> vars.env
echo "TAG=${{ needs.vars.outputs.image_tag }}" >> vars.env
echo "PREFIX=ghcr.io/nginxinc/nginx-gateway-fabric" >> vars.env
echo "NGINX_PREFIX=ghcr.io/nginxinc/nginx-gateway-fabric/nginx" >> vars.env
echo "NGINX_PLUS_PREFIX=us-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/nginx-gateway-fabric/nginx-plus" >> vars.env
echo "GKE_CLUSTER_NAME=nfr-tests-${{ github.run_id }}-${{ matrix.type }}" >> vars.env
echo "GKE_CLUSTER_ZONE=us-west1-b" >> vars.env
echo "GKE_CLUSTER_REGION=us-west1" >> vars.env
echo "GKE_PROJECT=${{ secrets.GCP_PROJECT_ID }}" >> vars.env
echo "GKE_SVC_ACCOUNT=${{ secrets.GCP_SERVICE_ACCOUNT }}" >> vars.env
echo "GKE_NODES_SERVICE_ACCOUNT=${{ secrets.GKE_NODES_SERVICE_ACCOUNT }}" >> vars.env
echo "NETWORK_TAGS=nfr-tests-${{ github.run_id }}-${{ matrix.type }}" >> vars.env
echo "NGF_BRANCH=${{ github.ref_name }}" >> vars.env
echo "SOURCE_IP_RANGE=$(curl -sS -4 icanhazip.com)/32" >> vars.env
echo "ADD_VM_IP_AUTH_NETWORKS=true" >> vars.env
echo "PLUS_ENABLED=${{ matrix.type == 'plus' }}" >> vars.env
echo "GINKGO_LABEL=" >> vars.env
echo "NGF_VERSION=${{ needs.vars.outputs.version }}" >> vars.env
echo "GKE_NUM_NODES=12" >> vars.env
echo "GKE_MACHINE_TYPE=n2d-standard-16" >> vars.env
echo "PLUS_USAGE_ENDPOINT=${{ secrets.JWT_PLUS_REPORTING_ENDPOINT }}" >> vars.env
- name: Setup license file for plus
if: matrix.type == 'plus'
env:
PLUS_LICENSE: ${{ secrets.JWT_PLUS_REPORTING }}
run: echo "${PLUS_LICENSE}" > license.jwt

- name: Create GKE cluster
working-directory: ./tests
run: make create-gke-cluster CI=true

- name: Create and setup VM
working-directory: ./tests
run: make create-and-setup-vm
- name: Create GKE cluster and VM
working-directory: ./tests/tofu
run: |
tofu apply --auto-approve
- name: Run Tests
working-directory: ./tests
Expand All @@ -153,12 +155,10 @@ jobs:
path: tests/results/**/*-${{ matrix.type }}.*

- name: Cleanup
working-directory: ./tests
working-directory: ./tests/tofu
if: always()
run: |
bash scripts/cleanup-vm.sh true
make delete-gke-cluster
rm -rf scripts/vars.env
tofu destroy --auto-approve
pr-results:
name: Open PR with results
Expand Down
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,32 @@ internal/mode/static/nginx/modules/coverage
# temporary files used for manifest generation
config/base/deploy.yaml
config/base/deploy.yaml.bak

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Ignore CLI configuration files
.terraformrc
terraform.rc
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ repos:
- --template-files=README.md.gotmpl

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

- repo: https://github.com/tofuutils/pre-commit-opentofu
rev: v2.1.0
hooks:
- id: tofu_fmt
# - id: tofu_docs

ci:
skip: [golangci-lint-full, prettier, markdownlint-cli2, yamllint]
autofix_prs: false
Expand Down
33 changes: 5 additions & 28 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,11 @@ ifeq ($(PLUS_ENABLED),true)
endif

.PHONY: setup-gcp-and-run-nfr-tests
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
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

.PHONY: create-gke-cluster
create-gke-cluster: ## Create a GKE cluster
./scripts/create-gke-cluster.sh $(CI)

.PHONY: create-and-setup-vm
create-and-setup-vm: ## Create and setup a GCP VM for tests
./scripts/create-and-setup-gcp-vm.sh

.PHONY: create-gke-router
create-gke-router: ## Create a GKE router to allow egress traffic from private nodes (allows for external image pulls)
./scripts/create-gke-router.sh
create-gcp-resources: ## Create a GKE cluster and a GCP VM for tests
tofu -chdir=tofu apply

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

.PHONY: cleanup-gcp
cleanup-gcp: cleanup-router cleanup-vm delete-gke-cluster ## Cleanup all GCP resources

.PHONY: cleanup-router
cleanup-router: ## Delete the GKE router
./scripts/cleanup-router.sh

.PHONY: cleanup-vm
cleanup-vm: ## Delete the test GCP VM and delete the firewall rule
./scripts/cleanup-vm.sh

.PHONY: delete-gke-cluster
delete-gke-cluster: ## Delete the GKE cluster
./scripts/delete-gke-cluster.sh

.PHONY: add-local-ip-to-cluster
add-local-ip-to-cluster: ## Add local IP to the GKE cluster master-authorized-networks
./scripts/add-local-ip-auth-networks.sh
cleanup-gcp: ## Cleanup all GCP resources
tofu -chdir=tofu destroy

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

Expand Down
10 changes: 0 additions & 10 deletions tests/scripts/add-local-ip-auth-networks.sh

This file was deleted.

8 changes: 0 additions & 8 deletions tests/scripts/cleanup-router.sh

This file was deleted.

17 changes: 0 additions & 17 deletions tests/scripts/cleanup-vm.sh

This file was deleted.

68 changes: 0 additions & 68 deletions tests/scripts/create-and-setup-gcp-vm.sh

This file was deleted.

40 changes: 0 additions & 40 deletions tests/scripts/create-gke-cluster.sh

This file was deleted.

15 changes: 0 additions & 15 deletions tests/scripts/create-gke-router.sh

This file was deleted.

7 changes: 0 additions & 7 deletions tests/scripts/delete-gke-cluster.sh

This file was deleted.

Loading

0 comments on commit 073fea8

Please sign in to comment.