Last active
February 4, 2025 10:47
-
-
Save paparaka/076b8c5e4b0904d4852f85a55a4da45b to your computer and use it in GitHub Desktop.
K8.Jinja.Templating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
# A sample Terraform file that creates the cloud build trigger. | |
############################################################################################ | |
resource "google_cloudbuild_trigger" "develop_trigger" { | |
description = "[DEVELOP] Build develop branch" | |
filename = "cloudbuild.yaml" | |
trigger_template { | |
branch_name = "^develop$" | |
repo_name = "houston" | |
} | |
substitutions = { | |
_ENVR = "dev" | |
_PROVISION_WEBUI = "true" | |
_CLOUDSDK_COMPUTE_REGION = var.region | |
_CLOUDSDK_COMPUTE_ZONE = var.zone | |
_CLOUDSDK_CONTAINER_CLUSTER = var.gke_name | |
} | |
} | |
variable "region" { | |
type = "string" | |
default = "xxxxx" | |
} | |
variable "zone" { | |
type = "string" | |
default = "xxxxxx" | |
} | |
variable "gke_name" { | |
default = "houston-launchpad" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
# A sample Cloud Build YAML file thay shows | |
############################################################################################ | |
steps: | |
# Build Web App container | |
- name: 'gcr.io/cloud-builders/docker' | |
args: ['build', '-f', 'Dockerfile', | |
'--build-arg', '_ENVR=$_ENVR', | |
'-t', 'eu.gcr.io/$PROJECT_ID/houston-web-app:$SHORT_SHA', | |
'.'] | |
dir: 'web-app' | |
# Push container | |
- name: 'gcr.io/cloud-builders/docker' | |
args: ['push', 'eu.gcr.io/$PROJECT_ID/houston-web-app:$SHORT_SHA'] | |
# Run the Jinja Template on all YAML file inside the 'deployment/kubernetes' directory | |
# Using a public container from docker hub by user: pinterb | |
- name: 'pinterb/jinja2' | |
entrypoint: 'python' | |
args: ['../scripts/k8_template_eval.py'] | |
dir: 'deployment/kubernetes' | |
env: | |
- '_ENVR=$_ENVR' | |
- '_APP_VERSION=$SHORT_SHA' | |
- '_PROVISION_WEBUI=$_PROVISION_WEBUI' | |
# Apply K8 Deployments | |
- name: 'gcr.io/cloud-builders/kubectl' | |
args: ['apply', '-f', 'deployment/kubernetes/deployments'] | |
env: | |
- 'CLOUDSDK_COMPUTE_ZONE=$_CLOUDSDK_COMPUTE_ZONE' | |
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CLOUDSDK_CONTAINER_CLUSTER' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
# Template Parser for YAML files | |
# | |
# Reads all of the YAML file in the directory it was run in and parses them with Jinja2 | |
# All variables are taken from the environment | |
import os | |
from os.path import join | |
from jinja2 import Environment, FileSystemLoader | |
def get_yaml_files(dirName='.'): | |
file_list = list() | |
for (dirpath, dirnames, filenames) in os.walk(dirName): | |
file_list += [join(dirpath, file) for file in filenames if (file.endswith('.yaml') or file.endswith('.yml'))] | |
return file_list | |
def parse_file(file): | |
template = jinja_env.get_template(file) | |
template.stream(os.environ).dump(file) | |
def parse_dir(dirName='.'): | |
for f in get_yaml_files(dirName=dirName): | |
print('Parsing file: {f}'.format(f=f)) | |
parse_file(f) | |
if __name__ == '__main__': | |
config_path = '.' | |
jinja_env = Environment(loader=FileSystemLoader(config_path)) | |
parse_dir(dirName=config_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################ | |
# A sample Kubernetes deployment that shows how the Jinja template is used | |
############################################################################################ | |
{% if _PROVISION_WEBUI == "true" %} | |
kind: Deployment | |
apiVersion: apps/v1 | |
metadata: | |
name: web-app-deployment | |
namespace: {{ _ENVR }} | |
labels: | |
app: web-app | |
appVersion: '{{ _APP_VERSION }}' | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: web-app | |
template: | |
metadata: | |
labels: | |
app: web-app | |
spec: | |
containers: | |
- name: web-app | |
image: 'eu.gcr.io/{{ PROJECT_ID }}/houston-web-app:{{ _APP_VERSION }}' | |
imagePullPolicy: Always | |
ports: | |
- containerPort: 80 | |
env: | |
- name: NODE_ENV | |
value: {{ 'production' if _ENVR == 'prod' else 'development' }} | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment