Skip to content

Commit

Permalink
Add a healthcheck endpoint and Helm chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Siegel committed Aug 14, 2018
1 parent 3eaea5e commit 6e0fc5b
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 5 deletions.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.git/
.vscode
.pytest_cache
env/
swaggertosdk.egg_info/
tests/
.checkignore
.coverage
.gitignore
.gitattributes
.travis.yml
LICENSE
pylintrc
*.md
Dockerfile
charts/
21 changes: 21 additions & 0 deletions charts/swaggertosdk/.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 charts/swaggertosdk/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: swaggertosdk
version: 0.1.0
19 changes: 19 additions & 0 deletions charts/swaggertosdk/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 {{ template "swaggertosdk.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 {{ template "swaggertosdk.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "swaggertosdk.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={{ template "swaggertosdk.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 }}
32 changes: 32 additions & 0 deletions charts/swaggertosdk/templates/_helpers.tpl
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 "swaggertosdk.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 "swaggertosdk.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 "swaggertosdk.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
61 changes: 61 additions & 0 deletions charts/swaggertosdk/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: {{ template "swaggertosdk.fullname" . }}
labels:
app: {{ template "swaggertosdk.name" . }}
chart: {{ template "swaggertosdk.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ template "swaggertosdk.name" . }}
release: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ template "swaggertosdk.name" . }}
release: {{ .Release.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- with .Values.container.command }}
command: {{ . | toJson }}
{{- end }}
{{- with .Values.container.args }}
args: {{ . | toJson }}
{{- end }}
{{- with .Values.container.env }}
env:
{{ toYaml . | indent 12 }}
{{- end }}
ports:
- name: http
containerPort: {{ default 80 .Values.container.port }}
protocol: TCP
livenessProbe:
httpGet:
path: {{ default "/" .Values.container.probePath }}
port: http
readinessProbe:
httpGet:
path: {{ default "/" .Values.container.probePath }}
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 }}
38 changes: 38 additions & 0 deletions charts/swaggertosdk/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "swaggertosdk.fullname" . -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
app: {{ template "swaggertosdk.name" . }}
chart: {{ template "swaggertosdk.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 }}
- {{ . }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ . }}
http:
paths:
- path: {{ $ingressPath }}
backend:
serviceName: {{ $fullName }}
servicePort: http
{{- end }}
{{- end }}
19 changes: 19 additions & 0 deletions charts/swaggertosdk/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "swaggertosdk.fullname" . }}
labels:
app: {{ template "swaggertosdk.name" . }}
chart: {{ template "swaggertosdk.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: {{ template "swaggertosdk.name" . }}
release: {{ .Release.Name }}
60 changes: 60 additions & 0 deletions charts/swaggertosdk/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Default values for swaggertosdk.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
repository: bsiegel/swaggertosdk
tag: 0.1
pullPolicy: Always

container:
args: ["--rest-server", "--verbose"]
env:
- name: GH_TOKEN
valueFrom:
secretKeyRef:
name: swaggertosdk
key: gh_token
port: 5000
probePath: /healthz

service:
type: ClusterIP
port: 80

ingress:
enabled: true
annotations:
ingress.kubernetes.io/rewrite-target: /
ingress.kubernetes.io/ssl-redirect: "false"
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
nginx.org/client-max-body-size: "50m"
nginx.org/proxy-read-timeout: "900s"
path: /
hosts:
- swaggertosdk.k8s.azure-devex-tools.com
tls:
- secretName: swaggertosdk-tls
hosts:
- swaggertosdk.k8s.azure-devex-tools.com

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: {}
14 changes: 9 additions & 5 deletions swaggertosdk/SwaggerToSdkMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ def main(argv):

if "--rest-server" in argv:
from gevent.pywsgi import WSGIServer
from .restapi import app
from .restapi import app, ProxyAwareHandler, RealRequestFilter
from flask import request

old_factory = logging.getLogRecordFactory()
def request_record_factory(*args, **kwargs):
record = old_factory(*args, **kwargs)
if request:
record.requestid = request.environ.get("FLASK_REQUEST_ID")
record.requestid = request.environ.get("FLASK_REQUEST_ID") + ":"
elif record.name == "WSGIServer":
record.requestid = ""
else:
record.requestid = "(none)"
record.requestid = ":"
return record
logging.setLogRecordFactory(request_record_factory)

Expand All @@ -97,10 +99,12 @@ def request_record_factory(*args, **kwargs):
log_level = logging.DEBUG

main_logger = logging.getLogger()
logging.basicConfig(format="%(requestid)s:%(levelname)s:%(name)s:%(message)s")
logging.basicConfig(format="%(levelname)s:%(name)s:%(requestid)s%(message)s")
main_logger.setLevel(log_level)

http_server = WSGIServer(('', 5000), app, log=logging.getLogger("WSGIServer"))
http_logger = logging.getLogger("WSGIServer")
http_logger.addFilter(RealRequestFilter())
http_server = WSGIServer(('', 5000), app, log=http_logger, handler_class=ProxyAwareHandler)
main_logger.info("REST server is listening on: http://%s:%s", http_server.server_host, http_server.server_port)
http_server.serve_forever()
sys.exit(0)
Expand Down
46 changes: 46 additions & 0 deletions swaggertosdk/restapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from logging import Filter
import uuid

from flask import Flask
from flask_request_id import RequestID
from gevent.pywsgi import WSGIHandler
from jsonrpc.backend.flask import api
from werkzeug.contrib.fixers import ProxyFix

from ..SwaggerToSdkMain import generate_sdk
from ..SwaggerToSdkCore import CONFIG_FILE, DEFAULT_COMMIT_MESSAGE

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
RequestID(app)
app.add_url_rule('/', 'api', api.as_view(), methods=['POST'])

Expand All @@ -32,5 +38,45 @@ def generate_project(*args, **kwargs):
None # Destination branch
)

@app.route('/healthz', methods=['GET'])
def healthz():
return "Pong!"

class RealRequestFilter(Filter): # pylint: disable=too-few-public-methods
def filter(self, record):
return "GET /healthz" not in record.msg

class ProxyAwareHandler(WSGIHandler):
def get_environ(self):
if self.headers.get("x-request-id") is None:
self.headers["X-Request-ID"] = str(uuid.uuid4())

# Fix up client_address from headers
forward = self.headers.get("x-forwarded-for")
if forward is not None:
if "," in forward:
forward = forward.split(',')[-1].strip()
if ":" in forward:
addr, port = forward.split(':')
self.client_address = (addr, port)
else:
self.client_address = (forward, "")

env = super().get_environ()

# Fix up wsgi.url_scheme from headers
scheme = self.headers.getheader("x-forwarded-proto")
if scheme is not None:
env['wsgi.url_scheme'] = scheme

return env

def format_request(self):
msg = super().format_request()
request_id = self.headers.get("x-request-id")
if request_id is not None:
msg = request_id + ":" + msg
return msg

from .github import *
from .travis import *

0 comments on commit 6e0fc5b

Please sign in to comment.