Skip to content

Commit 75ac4e0

Browse files
mayuthombreJoshArmi
authored andcommitted
Add deployment of flight controller to GCP as part of pipeline
1 parent 4402710 commit 75ac4e0

File tree

119 files changed

+1622
-74352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1622
-74352
lines changed

.github/workflows/cicd.yaml renamed to .github/workflows/aws_cicd.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ on:
66
workflow_dispatch:
77
pull_request:
88
concurrency:
9-
group: "cicd"
10-
cancel-in-progress: true
9+
group: "AWS"
10+
# cancel-in-progress: true
1111
jobs:
1212
deploy:
13-
name: CI
13+
name: AWS
1414
runs-on: ubuntu-latest
1515
permissions:
1616
id-token: write
@@ -82,4 +82,3 @@ jobs:
8282
run: make deploy
8383
env:
8484
GRAFANA_API_KEY: ${{ secrets.GRAFANA_API_KEY_PROD }}
85-

.github/workflows/gcp_cicd.yaml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: cicd
2+
on:
3+
push:
4+
branches:
5+
- main
6+
workflow_dispatch:
7+
pull_request:
8+
concurrency:
9+
group: "GCP"
10+
cancel-in-progress: true
11+
jobs:
12+
deploy:
13+
name: GCP
14+
runs-on: ubuntu-latest
15+
permissions:
16+
id-token: write
17+
contents: read
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
22+
- name: Setup Terraform
23+
uses: hashicorp/setup-terraform@v1
24+
with:
25+
terraform_wrapper: false
26+
27+
- id: 'auth'
28+
name: 'Authenticate to Google Cloud'
29+
uses: 'google-github-actions/auth@v1'
30+
with:
31+
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
32+
service_account: ${{ secrets.GCP_SERVICE_ACCOUNT }}
33+
34+
- uses: actions/setup-python@v4
35+
with:
36+
python-version: "3.10"
37+
38+
- name: Install pipenv
39+
run: pip install pipenv
40+
41+
- name: Install node
42+
uses: actions/setup-node@v3
43+
with:
44+
node-version: 18
45+
- name: Install cdktf
46+
run: npm install --global cdktf-cli@latest
47+
48+
- name: Install pip packages
49+
run: make install-dependencies
50+
51+
- name: Synthesize terraform configuration template
52+
run: make synth-gcp
53+
54+
- name: Terraform plan
55+
run: make plan-gcp
56+
57+
- name: Deploy base infrastructure
58+
run: make deploy-base-gcp
59+
60+
- name: Build & push docker image
61+
run: make build-image
62+
63+
- name: Deploy main infrastructure
64+
run: make deploy-main-gcp
65+
66+
# - name: Perform full test and check coverage
67+
# run: make test
68+
69+
# - name: Conduct e2e testing
70+
# run: make e2e

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,4 @@ all_files/
261261
api_key_rotation/
262262
!api_key_rotation/main.py
263263
imports/
264-
cdktf.out
264+
cdktf.out

12_oidc_gcp/.terraform.lock.hcl

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

12_oidc_gcp/provider.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
terraform {
2+
backend "gcs" {
3+
bucket = "flight-controller-state"
4+
prefix = "oidc/terraform.tfstate"
5+
}
6+
7+
required_providers {
8+
google = {
9+
source = "hashicorp/google"
10+
version = "4.57.0"
11+
}
12+
}
13+
}
14+
15+
# Configure project and region
16+
provider "google" {
17+
region = var.region
18+
project = var.project_id
19+
}

12_oidc_gcp/role.tf

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
data "google_project" "project" {
2+
project_id = var.project_id
3+
}
4+
5+
resource "google_iam_workload_identity_pool" "gh_pool" {
6+
project = var.project_id
7+
workload_identity_pool_id = "github-pool"
8+
}
9+
10+
resource "google_iam_workload_identity_pool_provider" "provider" {
11+
project = var.project_id
12+
workload_identity_pool_id = google_iam_workload_identity_pool.gh_pool.workload_identity_pool_id
13+
workload_identity_pool_provider_id = "github-provider"
14+
attribute_mapping = {
15+
"google.subject" = "assertion.sub"
16+
"attribute.full" = "assertion.repository"
17+
}
18+
oidc {
19+
issuer_uri = "https://token.actions.githubusercontent.com"
20+
}
21+
22+
depends_on = [
23+
google_iam_workload_identity_pool.gh_pool
24+
]
25+
}
26+
27+
resource "google_service_account" "runner_sa" {
28+
project = var.project_id
29+
account_id = "github-runner"
30+
display_name = "Service Account"
31+
32+
depends_on = [
33+
google_iam_workload_identity_pool.gh_pool
34+
]
35+
}
36+
37+
data "google_iam_policy" "wli_user_ghshr" {
38+
binding {
39+
role = "roles/iam.workloadIdentityUser"
40+
41+
members = [
42+
"principalSet://iam.googleapis.com/projects/${data.google_project.project.number}/locations/global/workloadIdentityPools/github-pool/attribute.full/${var.gh_repo}",
43+
]
44+
}
45+
}
46+
47+
resource "google_service_account_iam_policy" "admin-account-iam" {
48+
service_account_id = google_service_account.runner_sa.name
49+
policy_data = data.google_iam_policy.wli_user_ghshr.policy_data
50+
51+
depends_on = [
52+
google_service_account.runner_sa
53+
]
54+
}
55+
56+
resource "google_project_iam_member" "project" {
57+
project = var.project_id
58+
role = "roles/owner"
59+
member = "serviceAccount:${google_service_account.runner_sa.email}"
60+
}

12_oidc_gcp/variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "project_id" {
2+
type = string
3+
description = "The Google Project ID"
4+
}
5+
6+
variable "region" {
7+
type = string
8+
description = "The Google Project region"
9+
default = "australia-southeast1"
10+
}
11+
12+
variable "gh_repo" {
13+
type = string
14+
description = "The GitHub repo in the format <username/repo_name>"
15+
}

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.10-slim
2+
3+
ENV PYTHONUNBUFFERED True
4+
5+
COPY requirements.txt ./
6+
7+
RUN pip install -r requirements.txt
8+
9+
ENV APP_HOME /app
10+
WORKDIR $APP_HOME
11+
COPY ./src ./src
12+
13+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 "src.entrypoints.cloudrun:app"

Makefile

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ docs-build: docs-install
1414
docs-run: docs-build
1515
cd docs;npm run dev
1616

17+
local-grafana:
18+
docker run -d -p 3000:3000 mirror.gcr.io/grafana/grafana:latest
19+
1720
# Test Commands
1821
unittest:
1922
pipenv run pytest -m 'not integration' tests/src tests/publisher
@@ -33,38 +36,65 @@ e2e:
3336
# Infrastructure Commands
3437
build-python:
3538
pipenv requirements | tee requirements.txt
36-
rsync -avu $(shell pwd)/src $(shell pwd)/infrastructure/all_files
37-
pip install -r requirements.txt --target=$(shell pwd)/infrastructure/all_files
38-
pip install boto3 --target=$(shell pwd)/infrastructure/api_key_rotation
39+
rsync -avu $(shell pwd)/src $(shell pwd)/infrastructure/aws/all_files
40+
pip install -r requirements.txt --target=$(shell pwd)/infrastructure/aws/all_files
41+
pip install boto3 --target=$(shell pwd)/infrastructure/aws/api_key_rotation
42+
cd infrastructure/aws; cdktf provider add grafana/grafana
43+
cd infrastructure/gcp; cdktf provider add grafana/grafana
3944

4045
clean:
41-
cd infrastructure; rm -rf cdktf.out
46+
cd infrastructure/aws; rm -rf cdktf.out
47+
cd infrastructure/gcp; rm -rf cdktf.out
4248

43-
synth-core:
44-
cd infrastructure;cdktf synth infra_tf_cdk
49+
synth-aws:
50+
cd infrastructure/aws;cdktf synth aws_infra_cdktf
4551

4652
synth-grafana:
47-
cd infrastructure;cdktf synth grafana
53+
cd infrastructure/aws;cdktf synth grafana
54+
55+
synth-gcp:
56+
cd infrastructure/gcp; cdktf provider add grafana/grafana
57+
cd infrastructure/gcp; cdktf synth base_gcp_infra
58+
cd infrastructure/gcp; cdktf synth main_gcp_infra
4859

49-
synth: synth-core synth-grafana
60+
synth: synth-aws synth-grafana synth-gcp
5061

5162
build: build-python synth
5263

53-
plan-core:
54-
cd infrastructure;cdktf plan infra_tf_cdk
64+
plan-aws:
65+
cd infrastructure/aws;cdktf plan aws_infra_cdktf
5566

5667
plan-grafana:
57-
cd infrastructure;cdktf plan grafana
68+
cd infrastructure/aws;cdktf plan grafana
69+
70+
plan-gcp:
71+
cd infrastructure/gcp;cdktf plan base_gcp_infra main_gcp_infra
72+
73+
plan-gcp-grafana:
74+
cd infrastructure/gcp;cdktf plan grafana
75+
76+
plan: build-python plan-aws plan-grafana plan-gcp
5877

59-
plan: build-python plan-core plan-grafana
78+
plan-gcpstack: plan-gcp plan-grafana
6079

61-
deploy: build-python
62-
cd infrastructure;cdktf deploy infra_tf_cdk grafana --auto-approve
80+
deploy:
81+
cd infrastructure/aws;cdktf deploy aws_infra_cdktf grafana --auto-approve
6382

64-
destroy-core:
65-
cd infrastructure;cdktf destroy infra_tf_cdk
83+
destroy-aws:
84+
cd infrastructure/aws;cdktf destroy aws_infra_cdktf
6685

6786
destroy-grafana:
68-
cd infrastructure;cdktf destroy grafana
87+
cd infrastructure/aws;cdktf destroy grafana
6988

7089
destroy: destroy-core destroy-grafana
90+
91+
deploy-base-gcp:
92+
cd infrastructure/gcp; cdktf deploy base_gcp_infra --auto-approve
93+
94+
deploy-main-gcp:
95+
cd infrastructure/gcp; cdktf deploy base_gcp_infra main_gcp_infra --auto-approve
96+
97+
build-image:
98+
gcloud auth configure-docker australia-southeast1-docker.pkg.dev
99+
pipenv requirements | tee requirements.txt
100+
docker buildx build --platform=linux/amd64 --push . -t australia-southeast1-docker.pkg.dev/contino-squad0-fc/flight-contoller-event-receiver/event_receiver:latest

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ google-cloud-bigquery = "*"
99
gunicorn = "*"
1010
structlog = "*"
1111
boto3= "*"
12+
flask = "*"
1213
pydantic = "*"
1314

1415
[dev-packages]
@@ -23,6 +24,8 @@ constructs = "*"
2324
cdktf = "*"
2425
cdktf-cdktf-provider-aws = "*"
2526
cdktf-cdktf-provider-archive = "*"
27+
cdktf-cdktf-provider-google = "*"
28+
cdktf-cdktf-provider-external = "*"
2629
pytest-watch = "*"
2730
dirhash = "*"
2831

0 commit comments

Comments
 (0)