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 1 commit
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.19"
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,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}}
48 changes: 42 additions & 6 deletions solutions_builder/modules/blank_service/copier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ _metadata:
destination_path: .

# questions
module_version:
type: str
help: Module version (Default to 1.0)?
default: "1.0"

component_name:
type: str
help: What is the name of this component (snake_case)?
default: blank_service
default: my_service
validator: "{% if not component_name %}Required{% endif %}"

resource_name:
Expand All @@ -26,22 +31,54 @@ gcp_region:
help: Which Google Cloud region?
default: us-central1

deploy_cloudrun:
type: bool
help: Add Cloud Run to deployment methods (using Skaffold)?
default: yes

cloudrun_neg:
type: bool
help: Create network endpoint group (NEG) for serverless ingress?
default: yes
when: "{{deploy_cloudrun}}"

deploy_gke:
type: bool
help: Add GKE to deployment methods (using Skaffold)?
default: yes

default_deploy:
type: str
help: Default deploy method? (cloudrun or gke)
choices:
Cloud Run: cloudrun
GKE: gke
default: cloudrun

depend_on_common:
type: bool
help: Does this component require the Common image?
default: no
default: yes

module_version:
local_port:
type: str
help: Module version?
default: "1.0"
help: What's the port for local port forwarding?
default: 9001

use_github_action:
type: bool
help: Use Github Action as the default CI/CD?
default: yes

_subdirectory: "{{module_version}}"

_answers_file: ".st/module_answers/{{component_name}}.yaml"

_templates_suffix: ""

_patch:
- "skaffold.yaml"

_exclude:
- "README.md"
- "copier.yaml"
Expand All @@ -59,4 +96,3 @@ _jinja_extensions:
- jinja2_strcase.StrcaseExtension
- copier_templates_extensions.TemplateExtensionLoader
- ../../copier_extensions/sb_helpers.py:SolutionsTemplateHelpersExtension
# - ../../copier_extensions/context.py:ContextUpdater
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: {{resource_name}}
spec:
template:
spec:
containers:
- image: {{resource_name}}
ports:
- containerPort: 80