Skip to content
This repository was archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
Add node-typescript-app skaffold and helm config (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
enriched authored and fwouts committed Sep 14, 2018
1 parent 6deaa1e commit ff62b70
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 3 deletions.
12 changes: 12 additions & 0 deletions examples/node-typescript-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
This directory contains a Docker image with a Node server built from TypeScript with Bazel.

A few things worth noting:

- `tsconfig.json` must have `allowSyntheticDefaultImports` set to `false`
- We generate source maps and run `node -r source-map-support/register` so error stack traces
match the TypeScript source.
- We don't use any TypeScript aliases, because they will remain as such and confuse Node. This
could be fixed by re-introducing preprocessing which was removed in [d81c2e3](https://github.com/zenclabs/bazel-javascript/commit/d81c2e3f130fe09348952963d58fe560a416e5da).
- We use a symlinked directory to ensure `node_modules` is available to Node.
- `WORKSPACE` needs to be edited to include the remote, not local version of `bazel_javascript`.

## Deploying to Kubernetes

You can use [skaffold](https://github.com/GoogleContainerTools/skaffold) to
deploy this app to kubernetes. To run skaffold, you will need
[kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) and
[helm](https://helm.sh/) installed. For a simple local development
environment you can install docker and [enable
kubernetes](https://docs.docker.com/docker-for-mac/#kubernetes).

To get at the app you can run `kubectl port-forward service/my-service 3000:3000`
19 changes: 19 additions & 0 deletions examples/node-typescript-app/helm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# my-service Helm Chart

[Helm](https://helm.sh/) is "The package manager for kubernetes". It allows
you to define a bundle of [kubernetes
objects](https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/)
with templated variables that a downstream user can override. Helm calls the
bundles "Helm Charts" and they can be used as a dependency by other Helm Charts.

Many Helm Charts are already available, you can see some of the official ones
[here](https://github.com/helm/charts/tree/master/stable). Using a published
helm chart enables you to add something to kubernetes with minimal work.

To use helm you need to [initialize helm and install tiller
into your kubernetes cluster](https://docs.helm.sh/using_helm/#quickstart).
Helm's Tiller Server manages the Helm Charts that are deployed in your
cluster.

The chart in my-service is a default chart that was created using the helm
cli: `helm create my-service`
21 changes: 21 additions & 0 deletions examples/node-typescript-app/helm/my-service/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
5 changes: 5 additions & 0 deletions examples/node-typescript-app/helm/my-service/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: my-service
version: 0.1.0
19 changes: 19 additions & 0 deletions examples/node-typescript-app/helm/my-service/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range .Values.ingress.hosts }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ . }}{{ $.Values.ingress.path }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "my-service.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get svc -w {{ include "my-service.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "my-service.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ include "my-service.name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "my-service.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "my-service.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "my-service.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: {{ include "my-service.fullname" . }}
labels:
app: {{ include "my-service.name" . }}
chart: {{ include "my-service.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ include "my-service.name" . }}
release: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ include "my-service.name" . }}
release: {{ .Release.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.imageName }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 3000
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{{ toYaml .Values.resources | indent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "my-service.fullname" . -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
app: {{ include "my-service.name" . }}
chart: {{ include "my-service.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
{{- with .Values.ingress.annotations }}
annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . | quote }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ . | quote }}
http:
paths:
- path: {{ $ingressPath }}
backend:
serviceName: {{ $fullName }}
servicePort: 3000
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "my-service.fullname" . }}
labels:
app: {{ include "my-service.name" . }}
chart: {{ include "my-service.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app: {{ include "my-service.name" . }}
release: {{ .Release.Name }}
48 changes: 48 additions & 0 deletions examples/node-typescript-app/helm/my-service/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Default values for my-service.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
name: my-service
imageName: my.repo.com/my-service
pullPolicy: IfNotPresent

nameOverride: ""
fullnameOverride: ""

service:
type: ClusterIP
port: 3000

ingress:
enabled: false
annotations:
kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
path: /my-service
hosts:
- localhost
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local

resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi

nodeSelector: {}

tolerations: []

affinity: {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ app.use(async ctx => {
ctx.body += "\n" + renderedCss.css.toString("utf8");
// Just for fun, add a small chance of crashing. This allows us to check whether stack
// traces are readable.
if (Math.random() < 0.1) {
throw new Error("Random crash.");
}
// if (Math.random() < 0.1) {
// throw new Error("Random crash.");
// }
});

app.listen(3000, () => console.log("Server is ready."));
19 changes: 19 additions & 0 deletions examples/node-typescript-app/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: skaffold/v1alpha2
kind: Config
build:
tagPolicy:
sha256: {}
artifacts:
- imageName: my.repo.com/my-service
workspace: .
bazel:
target: //services/my-service:image.tar
local: {}
deploy:
helm:
releases:
- name: my-service
chartPath: helm/my-service
valuesFilePath: helm/my-service/values.yaml
values:
image.imageName: my.repo.com/my-service

0 comments on commit ff62b70

Please sign in to comment.