Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update blank service with gke and cloudrun files #144

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "solutions-builder"
version = "1.17.18"
version = "1.17.20"
description = "A solution framework to generate a project with built-in structure and modules"
authors = ["Jon Chen <[email protected]>"]
license = "Apache"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# {{component_name}} Microservice

## Development

### Run locally

Create a virtualenv and install dependencies.
```
cd components/{{component_name}}
python -m virtualenv .venv
pip install -r requirements.txt
```

If this service depends on **components/common**, run the following
to install all dependencies from common.

```
pip install -r ../common/requirements.txt
```

Run `main.py`.
```
cd components/{{component_name}}/src
PYTHONPATH=../common/src python main.py
```

### Deploy to remote GKE cluster with livereload

Use Solutions Builder CLI to deploy:
```
st deploy . -m {{component_name}} --dev
```

Alternatively, deploy with Skaffold command:
```
skaffold dev -p default -m {{component_name}} --default-repo="gcr.io/{{project_id}}"
```

## Test

If this microservice uses Firestore, run Firebase emulator in a separate terminial.
```
firebase emulators:start --only firestore --project fake-project
```

Install test related dependencies
```
pip install -r requirements.txt
```

Run pytest
```
pytest
```

## Deploy

### Deploy to remote GKE cluster or Cloud Run

Use Solutions Builder CLI to deploy:
```
st deploy . -m {{component_name}}
```

Alternatively, deploy with Skaffold command:
```
skaffold run -p default -m {{component_name}} --default-repo="gcr.io/{{project_id}}"
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
backports-unittest-mock
mock
pylint
pytest
pytest-cov
pytest-custom_exit_code
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fastapi
fastapi-restful
fireo
google-cloud-bigquery
google-cloud-storage
python-multipart
uvicorn
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
apiVersion: skaffold/v4beta1
kind: Config
metadata:
name: {{component_name}}

{% if depend_on_common == true -%}
# Requires the common image for shared data models or utils.
requires:
- configs:
- common
path: ../common
{%- endif %}

build:
artifacts:
- image: {{resource_name}}
sync:
infer:
- '**/*.py'
- '**/*.json'
docker:
cacheFrom:
- {{resource_name}}
- {{resource_name}}:latest

{% if depend_on_common == true -%}
requires:
- image: common
alias: COMMON_IMAGE
{%- endif %}
googleCloudBuild: {}

# Portforwarding when running `skaffold dev` locally.
portForward:
- resourceType: service
resourceName: {{resource_name}}
port: 80
localPort: {{local_port}} # Change this when adding other microservice.

profiles:
# Profile for building images locally.
- name: local_build
build:
artifacts:
- image: {{resource_name}}
{% if depend_on_common == true -%}
requires:
- image: common
alias: COMMON_IMAGE
{%- endif %}
sync:
infer:
- '**/*.py'
- '**/*.json'
tagPolicy:
gitCommit: {}
local:
concurrency: 0

{% if deploy_gke == true -%}
# Profile for GKE deployment, building images via CloudBuild
- &gke-profile # YAML anchor used by "default" profile.
name: gke
manifests:
# Loading kustomize base file for deployment.
kustomize:
paths:
- ./kustomize/base
# Substitute system's environment vars to properties.rendered.env
hooks:
before:
- host:
dir: ./kustomize/base
command: ["sh", "-c",
"envsubst < properties.env > properties.rendered.env"]
after:
- host:
dir: ./kustomize/base
command: ["sh", "-c", "rm *.rendered.env"]
# Simple deployment using kubectl.
deploy:
kubectl: {}

# Profile for GKE Horizontal Pod Autoscaler.
# This profile only works with `gke` profile together.
# E.g. skaffold run -p gke,hpa
- name: gke-hpa
manifests:
kustomize:
paths:
- ./kustomize/hpa
# Simple deployment using kubectl.
deploy:
kubectl: {}
{%- endif %}

{% if deploy_cloudrun == true -%}
# Profile for Cloud Run deployment, building images via CloudBuild
- &cloudrun-profile # YAML anchor used by "default" profile.
name: cloudrun
manifests:
rawYaml:
- manifests/cloudrun-service.yaml
deploy:
cloudrun:
projectid: {{project_id}}
region: {{gcp_region}}
portForward:
- resourceType: service
resourceName: {{resource_name}}
port: 80
localPort: {{local_port}} # Change this when adding other microservice.
{%- endif %}

{% if default_deploy == "cloudrun" -%}
# The default-deploy profile refer to cloudrun profile above.
- <<: *cloudrun-profile
name: default-deploy
{%- endif %}

{% if default_deploy == "gke" -%}
# The default-deploy profile refer to gke profile above.
- <<: *gke-profile
name: default-deploy
{%- endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{resource_name}}
spec:
replicas: 1
selector:
matchLabels:
app: {{resource_name}}
template:
metadata:
labels:
app: {{resource_name}}
spec:
serviceAccountName: gke-sa
automountServiceAccountToken: true
containers:
- name: {{resource_name}}
image: {{resource_name}}
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: env-vars
resources:
requests:
cpu: "250m"
memory: "100Mi"
limits:
memory: "5000Mi"
cpu: "2000m"
ports:
- containerPort: 80
livenessProbe:
failureThreshold: 5
httpGet:
path: /ping
port: 80
scheme: HTTP
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 10
readinessProbe:
failureThreshold: 5
httpGet:
path: /ping
port: 80
scheme: HTTP
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
- ./service.yaml
configMapGenerator:
- name: env-vars
envs:
- properties.rendered.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PROJECT_ID=${PROJECT_ID}
DATABASE_PREFIX=${DATABASE_PREFIX}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: {{resource_name}}
labels:
app: {{resource_name}}
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: {{resource_name}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: cpu-pod-scaling-{{component_name}}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{component_name}}
minReplicas: 3
maxReplicas: 110
metrics:
- type: Resource
resource:
name: memory
targetAverageValue: 500Mi
resource:
name: cpu
targetAverageUtilization: 60
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../base
resources:
- hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
requires:
- configs:
- {{component_name}}
path: ./components/{{component_name}}
Loading