From c39bccafaafffb9900ab0b46f1cf724a184bbf06 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Tue, 15 Sep 2020 11:30:01 +0530 Subject: [PATCH 1/2] httpbin: convert to Helm chart closes: #957 Signed-off-by: knrt10 --- assets/charts/components/httpbin/.helmignore | 23 ++++ assets/charts/components/httpbin/Chart.yaml | 23 ++++ .../httpbin/templates/deployment.yaml | 37 ++++++ .../components/httpbin/templates/ingress.yaml | 20 +++ .../components/httpbin/templates/service.yaml | 11 ++ assets/charts/components/httpbin/values.yaml | 2 + pkg/assets/generated_assets.go | 60 +++++++++ pkg/components/httpbin/component.go | 122 +++--------------- pkg/components/httpbin/template.go | 22 ++++ 9 files changed, 216 insertions(+), 104 deletions(-) create mode 100644 assets/charts/components/httpbin/.helmignore create mode 100644 assets/charts/components/httpbin/Chart.yaml create mode 100644 assets/charts/components/httpbin/templates/deployment.yaml create mode 100644 assets/charts/components/httpbin/templates/ingress.yaml create mode 100644 assets/charts/components/httpbin/templates/service.yaml create mode 100644 assets/charts/components/httpbin/values.yaml create mode 100644 pkg/components/httpbin/template.go diff --git a/assets/charts/components/httpbin/.helmignore b/assets/charts/components/httpbin/.helmignore new file mode 100644 index 000000000..0e8a0eb36 --- /dev/null +++ b/assets/charts/components/httpbin/.helmignore @@ -0,0 +1,23 @@ +# 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 +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/assets/charts/components/httpbin/Chart.yaml b/assets/charts/components/httpbin/Chart.yaml new file mode 100644 index 000000000..7e205c23a --- /dev/null +++ b/assets/charts/components/httpbin/Chart.yaml @@ -0,0 +1,23 @@ +apiVersion: v2 +name: httpbin +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +appVersion: v0.6.1 diff --git a/assets/charts/components/httpbin/templates/deployment.yaml b/assets/charts/components/httpbin/templates/deployment.yaml new file mode 100644 index 000000000..d21fa0062 --- /dev/null +++ b/assets/charts/components/httpbin/templates/deployment.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: httpbin + name: httpbin +spec: + replicas: 1 + selector: + matchLabels: + app: httpbin + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + template: + metadata: + labels: + app: httpbin + spec: + containers: + - image: docker.io/kennethreitz/httpbin + name: httpbin + ports: + - containerPort: 8080 + name: http + command: ["gunicorn"] + args: ["-b", "0.0.0.0:8080", "httpbin:app"] + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + runAsNonRoot: true + runAsUser: 65534 + runAsGroup: 65534 + terminationGracePeriodSeconds: 30 diff --git a/assets/charts/components/httpbin/templates/ingress.yaml b/assets/charts/components/httpbin/templates/ingress.yaml new file mode 100644 index 000000000..4481aa976 --- /dev/null +++ b/assets/charts/components/httpbin/templates/ingress.yaml @@ -0,0 +1,20 @@ +apiVersion: networking.k8s.io/v1beta1 +kind: Ingress +metadata: + name: httpbin + annotations: + kubernetes.io/tls-acme: "true" + cert-manager.io/cluster-issuer: {{ .Values.certManagerClusterIssuer }} + kubernetes.io/ingress.class: contour +spec: + tls: + - secretName: {{ .Values.ingressHost }}-tls + hosts: + - {{ .Values.ingressHost }} + rules: + - host: {{ .Values.ingressHost }} + http: + paths: + - backend: + serviceName: httpbin + servicePort: 8080 diff --git a/assets/charts/components/httpbin/templates/service.yaml b/assets/charts/components/httpbin/templates/service.yaml new file mode 100644 index 000000000..d7bd2f680 --- /dev/null +++ b/assets/charts/components/httpbin/templates/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: httpbin +spec: + ports: + - port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: httpbin diff --git a/assets/charts/components/httpbin/values.yaml b/assets/charts/components/httpbin/values.yaml new file mode 100644 index 000000000..5fe894507 --- /dev/null +++ b/assets/charts/components/httpbin/values.yaml @@ -0,0 +1,2 @@ +certManagerClusterIssuer: +ingressHost: diff --git a/pkg/assets/generated_assets.go b/pkg/assets/generated_assets.go index 033e134fa..0cf0b95f3 100644 --- a/pkg/assets/generated_assets.go +++ b/pkg/assets/generated_assets.go @@ -941,6 +941,54 @@ var vfsgenAssets = func() http.FileSystem { modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), content: []byte(""), }, + "/charts/components/httpbin": &vfsgen۰DirInfo{ + name: "httpbin", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + }, + "/charts/components/httpbin/.helmignore": &vfsgen۰CompressedFileInfo{ + name: ".helmignore", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + uncompressedSize: 349, + + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4c\x8e\x41\x6e\xe3\x30\x0c\x45\xf7\x3c\xc5\x1f\x78\x33\x63\x0c\xe4\x43\x24\xb3\x98\x55\x0b\xa4\xc8\xb6\x90\x6d\x46\x62\x22\x8b\x82\x44\x27\x6d\x17\x3d\x7b\x91\x04\x41\xbb\x79\x20\x3f\xc8\x8f\xd7\xe1\xd9\x9b\x71\xcd\x0d\xa6\x90\x90\xb5\x32\x2e\x91\x33\xc6\x55\xd2\x2c\x39\xa0\xf8\xe9\xe4\x03\x37\x47\x1d\x5e\xa2\x34\xb4\xb5\x14\xad\xd6\xd0\x22\xa7\x84\x90\x74\xc4\xe2\x6d\x8a\x92\xc3\x5f\x54\x4e\xde\xe4\xcc\x28\xde\xe2\x8f\xdc\xe7\x99\x3a\x64\x0e\xde\x44\x33\x7e\x97\xca\x07\x79\xe3\x19\x17\xb1\x88\x5f\x7f\x1c\x9e\x72\x7a\x87\xe6\xdb\xe7\x55\x09\x85\x2b\x92\x64\x76\xe4\xb6\xbb\xd7\x9d\x69\x65\xea\xb0\xd1\x65\xd1\x8c\xfd\x66\x87\x59\x6a\x23\x17\xc4\x86\x1b\xef\xfa\xe4\xc6\x8f\x3a\xdc\xf8\x08\x62\x18\xae\x78\xac\xed\x9c\x87\xef\xa2\xd1\x4f\xa7\xb5\xe0\x20\x89\x1b\xf5\xae\x5d\x0a\xf5\x6e\xf4\x27\xea\x9d\x2d\xd7\x59\xab\x04\xea\x3f\xa9\xc3\xde\x57\xd1\xb5\xe1\xff\xf6\x5f\x23\x57\xaa\x1e\x79\x32\x72\x32\xb3\x1f\xee\xe7\x55\x8f\xe4\xce\x6d\xd2\x99\x07\xfa\x0a\x00\x00\xff\xff\x16\xec\x32\x27\x5d\x01\x00\x00"), + }, + "/charts/components/httpbin/Chart.yaml": &vfsgen۰CompressedFileInfo{ + name: "Chart.yaml", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + uncompressedSize: 1098, + + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x94\x31\x6f\xdb\x30\x10\x85\x77\xfe\x8a\x03\x34\xb8\x05\x0a\x25\xe9\xd0\xc1\x5b\xb6\x02\xed\xd6\xa0\xfb\x99\x7c\xb2\xae\xa1\x48\x82\xa4\xd4\xea\xdf\x17\x27\x59\xb6\xe2\x02\x41\x47\x8b\xd4\x7b\xef\x7b\x77\x16\x27\xf9\x89\x5c\x24\x86\x23\x4d\x9f\x4d\xe0\x01\x47\xea\x6b\x4d\x27\x09\xc6\xa1\xd8\x2c\xa9\x2e\xa7\xcf\xf4\x15\x7e\x20\xdb\x73\xae\xd4\xc5\x4c\xdf\xc6\x13\x72\x40\x45\x31\xa6\xa1\xe7\xcb\x89\xe5\x40\x27\x10\xa4\xf6\xc8\xc4\x81\x0e\x9c\x92\x17\xcb\xaa\x72\xa0\x98\x89\xe9\xe0\xe5\x94\x39\xcf\x87\xf5\x9d\xd6\x34\x2a\x70\xbb\xb6\x3e\x2e\xc4\x19\xc4\x64\xa3\xf7\xb0\xcb\xf3\xd8\x51\xc5\x90\x3c\x57\x14\xaa\x3d\x5f\xed\x12\xdb\x57\x3e\xc3\x91\x84\x1a\x69\x5a\x89\xe0\x88\xb3\xed\x65\x42\x31\x0d\xd5\xa8\x17\x1d\x92\x8f\x33\xdc\xea\xf9\x7d\xcd\xb1\xf9\xa5\x1c\x27\x71\xa0\xb1\xa0\x1b\x3d\x8d\x55\xbc\x54\x41\xd1\xd4\xdd\x18\x96\x0c\x65\x61\xaf\x3d\x2e\xbc\x0e\x13\x7c\x4c\xc8\x2d\xbd\xf4\x98\x0f\x19\x24\xc1\xfa\xd1\xa9\xbb\xfa\xb2\x7a\x22\x38\x04\x3b\x2b\x00\xff\xcb\x59\x23\x49\xf8\x05\x5b\xa9\xf6\xb1\x60\x67\xcc\xc1\xed\x9c\x17\x38\xb5\xce\xaa\x97\x25\x9c\x4d\x43\x49\x12\xbc\x04\xb4\xf7\x34\x2e\x52\x88\x1a\xb0\x93\x00\xe2\x30\xef\xba\x53\x5d\x1d\x10\xba\x98\xa1\x25\xea\xcd\x37\xf5\xd4\x39\xe1\xb8\x0f\xab\x43\x7e\xe9\xa5\x90\x94\x1d\xfe\xa5\xea\x76\x3d\xba\xfc\xa2\x30\x0e\x27\x64\x2a\x7d\x1c\xbd\x53\x5d\x09\x36\x63\x40\xa8\x70\x04\xb6\x3d\x55\x19\x40\x73\x1c\x69\xe0\xd7\x45\x2b\x9c\xb7\x29\xdd\xc4\x35\xa5\x68\x3f\x5b\xee\x4f\x97\x6e\x25\x9c\x97\x6b\x9c\xd2\x35\x81\x69\xe8\xb2\xc9\xeb\xe2\xe0\x4f\x82\x55\xbf\x1a\xa9\x8b\xde\xc7\xdf\xf4\x03\x03\x87\x2a\x76\xbb\xa8\x32\x1f\x74\xd9\xcb\xf1\xe1\xa1\x60\x98\x90\xdb\x98\xcf\x0f\x1f\xcd\xb4\xfd\x27\x1e\xdb\xa7\xf6\xf1\x1e\xfd\x0e\x53\xb7\x72\x0d\x73\x1d\xec\x09\xaa\x7d\x6d\xf3\xfd\x76\x4c\xf3\x9f\xfd\x6c\xed\xec\x9c\xda\xb7\xd0\x3a\xc6\x1d\xb8\x69\xde\x41\x5f\x17\x76\x8b\x91\xd1\xf9\x75\x03\x6f\x7c\xf7\x54\x52\x68\x2c\xfa\xa6\xe1\x94\x6e\x9f\x8d\xc7\xf6\x4b\xfb\x64\xfe\x06\x00\x00\xff\xff\xd7\xe8\x88\xdb\x4a\x04\x00\x00"), + }, + "/charts/components/httpbin/templates": &vfsgen۰DirInfo{ + name: "templates", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + }, + "/charts/components/httpbin/templates/deployment.yaml": &vfsgen۰CompressedFileInfo{ + name: "deployment.yaml", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + uncompressedSize: 798, + + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x91\x41\x6f\xdb\x30\x0c\x85\xef\xfe\x15\x44\xce\x4b\x93\xa2\xeb\x50\xe8\x56\x74\x58\x2f\x43\x11\xb4\xc8\x2e\xc3\x0e\x8c\xcc\x25\x42\x24\x52\xa0\xe8\xae\xde\xaf\x2f\xd4\xc4\x8e\x8d\xd2\x17\xeb\x3d\xf2\xd3\xa3\x8d\x39\xfc\x22\x2d\x41\xd8\x01\xe6\x5c\x56\xaf\xd7\xcd\x31\x70\xeb\xe0\x3b\xe5\x28\x7d\x22\xb6\x26\x91\x61\x8b\x86\xae\x01\x88\xb8\xa3\x58\xea\x1b\xd4\x01\x07\x07\xb3\xbc\x0b\xdc\x00\x30\x26\xba\x9c\x4b\x26\x5f\xdb\x94\x72\x0c\x1e\x8b\x83\xeb\x06\xa0\x50\x24\x6f\xa2\x27\x40\x42\xf3\x87\x9f\x13\xe2\x27\x66\x31\x45\xa3\x7d\x7f\xb2\x55\x62\x0c\xbc\xdf\xe6\x16\x8d\x86\x89\x84\x6f\x2f\x9d\xee\xe9\x74\xc1\x59\xd9\x32\xbe\x62\x88\xb8\x8b\xa3\x6e\x7d\x26\x07\xcf\x53\x44\x03\x60\x94\x72\x1c\x69\xd3\x4d\x6b\xc5\x59\xb6\x4f\xe9\x00\x86\x2d\x6b\x79\x61\xc3\xc0\xa4\xe3\xc0\x12\x42\xc2\x9a\xac\x15\x7f\x24\xbd\x0a\xb2\x3a\x12\x33\xd9\x41\x29\xd8\xff\xd5\x94\x54\x6b\xfe\x05\x07\x35\x8b\xda\x24\xc3\xf2\x72\xd1\x46\xd4\x1c\xdc\xad\xef\xd6\xa3\x3b\x85\x8c\xa2\x97\x94\xb0\xfe\xd4\xdf\x8b\x7d\xc7\xc1\x8b\xf2\xe2\xcf\x65\x29\xdd\x97\x6a\x2d\x77\x8b\x2f\xb0\x58\x5f\x7d\x3c\xae\x52\xeb\xf9\x9c\xc6\x61\xce\xe3\x4c\xcb\x65\x23\x31\xf8\xde\xc1\x43\xec\x8a\x91\xfe\x08\x5a\xec\xec\x2a\x15\x43\xb5\xa1\xe3\x3e\xfe\xc3\xbe\x9c\xbd\xe2\x0f\xd4\x76\x91\xf4\xe9\x23\x65\x4b\x7f\xb1\x8b\xb6\x1c\xe5\xa1\x8d\x7c\xa7\xc1\xfa\x07\x61\xa3\x37\xbb\x2c\xaf\x1d\xdf\x97\x27\xe1\x67\x11\x73\x60\xda\xd1\xdc\xda\x16\x52\x07\xdf\x6e\x6f\x6f\xbe\xce\x8d\x47\x95\x2e\xcf\x1d\x23\x4d\x81\xd1\x82\xf0\xa3\xa2\xa7\x0d\x69\x90\xf6\x85\xbc\x70\x5b\x1c\xdc\xac\x9b\xf7\x00\x00\x00\xff\xff\x4f\x94\xf9\x92\x1e\x03\x00\x00"), + }, + "/charts/components/httpbin/templates/ingress.yaml": &vfsgen۰CompressedFileInfo{ + name: "ingress.yaml", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + uncompressedSize: 490, + + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x7c\x90\x3d\x6f\x32\x31\x0c\xc7\xf7\x7c\x0a\x8b\x3d\xf7\xc0\x86\xb2\x3e\x4b\x19\x8a\x3a\xb1\xfb\x82\x05\xd1\x05\xe7\x64\x3b\x74\x40\xf7\xdd\xab\xdc\x1d\x52\x85\xda\x66\x4b\xfc\xfb\xbf\x38\x38\xa6\x13\x89\xa6\xc2\x01\x98\xec\xb3\xc8\x90\xf8\xd2\x0d\x7b\xed\x52\xf9\x77\xdf\xf5\x64\xb8\x73\x43\xe2\x73\x80\x03\x5f\x84\x54\xdd\x8d\x0c\xcf\x68\x18\x1c\x00\xe3\x8d\x02\x5c\xcd\xc6\x3e\xb1\x03\x40\xe6\x62\x68\xa9\xb0\xb6\x31\xc0\x50\x7b\x12\x26\xa3\xd9\xd0\xb2\x7a\x8c\x4d\xb2\x31\xa9\xb4\x99\x91\x48\x62\xfe\x86\x8c\x17\x92\x06\xc5\x5c\xd5\x48\x7c\x52\xad\x24\x01\x1e\x0f\xe8\x4e\x98\x2b\x69\xd7\xd0\xf7\x85\xfc\xbf\x50\x87\x19\x82\x69\xfa\x21\x2d\x2d\x85\xbb\x98\x51\x35\x40\x2c\x6c\xa5\x8a\xd3\x91\x62\x2b\x67\x79\xee\xe8\x41\x29\x0a\xd9\x71\x5e\xe5\x5b\xd8\x2a\x7f\x2b\x6a\x30\x4d\xde\xb2\xce\x19\xd7\xa2\xb6\x2e\xe7\x7f\xc7\x1d\x80\xd4\x4c\x6b\x42\xd3\xfc\xe1\xbd\xf8\x9a\x8d\x8b\x2d\xc0\x88\x76\xd5\xe7\xc5\x43\x8f\x71\x20\x3e\x3f\x1f\xda\x51\x92\x7b\x8a\x74\x7c\xf9\xff\x97\xf1\x47\x11\x0b\xb0\xdf\xee\xb7\xee\x2b\x00\x00\xff\xff\x67\x82\x48\xa8\xea\x01\x00\x00"), + }, + "/charts/components/httpbin/templates/service.yaml": &vfsgen۰CompressedFileInfo{ + name: "service.yaml", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + uncompressedSize: 153, + + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4c\x8d\x31\xaa\xc3\x40\x0c\x05\xfb\x3d\xc5\xbb\xc0\x07\xff\xce\xa8\xcd\x05\x0c\x09\xe9\x95\xb5\x48\x44\xec\x95\xd0\x0a\x9f\x3f\x78\x9b\xa4\x1b\x86\x81\x61\xd7\xbb\x44\x57\x6b\x84\xe3\xbf\xbc\xb5\xad\x84\xab\xc4\xa1\x55\xca\x2e\xc9\x2b\x27\x53\x01\x1a\xef\x42\x78\x65\xfa\x43\x5b\xe9\x2e\xf5\xb4\x6e\x91\xfd\x84\xbf\x81\x84\x79\x9a\xa7\x02\x00\x1e\x96\x56\x6d\x23\xdc\x2e\xcb\x30\xc9\xf1\x94\x5c\x7e\xb3\x2e\x9b\xd4\xb4\xa0\x11\xb0\xfb\xf7\xf0\x09\x00\x00\xff\xff\x20\x0e\x81\x63\x99\x00\x00\x00"), + }, + "/charts/components/httpbin/values.yaml": &vfsgen۰FileInfo{ + name: "values.yaml", + modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), + content: []byte("\x63\x65\x72\x74\x4d\x61\x6e\x61\x67\x65\x72\x43\x6c\x75\x73\x74\x65\x72\x49\x73\x73\x75\x65\x72\x3a\x0a\x69\x6e\x67\x72\x65\x73\x73\x48\x6f\x73\x74\x3a\x0a"), + }, "/charts/components/istio-operator": &vfsgen۰DirInfo{ name: "istio-operator", modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), @@ -5900,6 +5948,7 @@ var vfsgenAssets = func() http.FileSystem { fs["/charts/components/contour"].(os.FileInfo), fs["/charts/components/external-dns"].(os.FileInfo), fs["/charts/components/flatcar-linux-update-operator"].(os.FileInfo), + fs["/charts/components/httpbin"].(os.FileInfo), fs["/charts/components/istio-operator"].(os.FileInfo), fs["/charts/components/linkerd2"].(os.FileInfo), fs["/charts/components/metrics-server"].(os.FileInfo), @@ -6068,6 +6117,17 @@ var vfsgenAssets = func() http.FileSystem { fs["/charts/components/flatcar-linux-update-operator/templates/update-operator-sa.yaml"].(os.FileInfo), fs["/charts/components/flatcar-linux-update-operator/templates/update-operator.yaml"].(os.FileInfo), } + fs["/charts/components/httpbin"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ + fs["/charts/components/httpbin/.helmignore"].(os.FileInfo), + fs["/charts/components/httpbin/Chart.yaml"].(os.FileInfo), + fs["/charts/components/httpbin/templates"].(os.FileInfo), + fs["/charts/components/httpbin/values.yaml"].(os.FileInfo), + } + fs["/charts/components/httpbin/templates"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ + fs["/charts/components/httpbin/templates/deployment.yaml"].(os.FileInfo), + fs["/charts/components/httpbin/templates/ingress.yaml"].(os.FileInfo), + fs["/charts/components/httpbin/templates/service.yaml"].(os.FileInfo), + } fs["/charts/components/istio-operator"].(*vfsgen۰DirInfo).entries = []os.FileInfo{ fs["/charts/components/istio-operator/Chart.yaml"].(os.FileInfo), fs["/charts/components/istio-operator/crds"].(os.FileInfo), diff --git a/pkg/components/httpbin/component.go b/pkg/components/httpbin/component.go index 890171877..2d2bd5f9d 100644 --- a/pkg/components/httpbin/component.go +++ b/pkg/components/httpbin/component.go @@ -15,109 +15,20 @@ package httpbin import ( - "bytes" "fmt" - "text/template" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" + internaltemplate "github.com/kinvolk/lokomotive/internal/template" "github.com/kinvolk/lokomotive/pkg/components" + "github.com/kinvolk/lokomotive/pkg/components/util" "github.com/kinvolk/lokomotive/pkg/k8sutil" ) const name = "httpbin" -const namespaceManifest = `apiVersion: v1 -kind: Namespace -metadata: - name: httpbin - labels: - name: httpbin -` - -const deploymentManifest = `apiVersion: apps/v1 -kind: Deployment -metadata: - labels: - app: httpbin - name: httpbin - namespace: httpbin -spec: - replicas: 1 - selector: - matchLabels: - app: httpbin - strategy: - rollingUpdate: - maxSurge: 1 - maxUnavailable: 1 - type: RollingUpdate - template: - metadata: - labels: - app: httpbin - spec: - containers: - - image: docker.io/kennethreitz/httpbin - name: httpbin - ports: - - containerPort: 8080 - name: http - command: ["gunicorn"] - args: ["-b", "0.0.0.0:8080", "httpbin:app"] - dnsPolicy: ClusterFirst - restartPolicy: Always - schedulerName: default-scheduler - securityContext: - runAsNonRoot: true - runAsUser: 65534 - runAsGroup: 65534 - terminationGracePeriodSeconds: 30 -` - -const serviceManifest = `apiVersion: v1 -kind: Service -metadata: - name: httpbin - namespace: httpbin -spec: - ports: - - port: 8080 - protocol: TCP - targetPort: 8080 - selector: - app: httpbin -` - -const ingressTmpl = `apiVersion: networking.k8s.io/v1beta1 -kind: Ingress -metadata: - name: httpbin - namespace: httpbin - labels: - app.kubernetes.io/managed-by: Helm - annotations: - kubernetes.io/tls-acme: "true" - cert-manager.io/cluster-issuer: {{ .CertManagerClusterIssuer }} - kubernetes.io/ingress.class: contour - meta.helm.sh/release-name: httpbin - meta.helm.sh/release-namespace: httpbin -spec: - tls: - - secretName: {{ .IngressHost }}-tls - hosts: - - {{ .IngressHost }} - rules: - - host: {{ .IngressHost }} - http: - paths: - - backend: - serviceName: httpbin - servicePort: 8080 -` - -func init() { +func init() { //nolint:gochecknoinits components.Register(name, newComponent()) } @@ -138,25 +49,28 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex components.HCLDiagConfigBodyNil, } } + return gohcl.DecodeBody(*configBody, evalContext, c) } -// TODO: Convert to Helm chart. func (c *component) RenderManifests() (map[string]string, error) { - tmpl, err := template.New("ingress").Parse(ingressTmpl) + helmChart, err := components.Chart(name) if err != nil { - return nil, fmt.Errorf("parsing template: %w", err) + return nil, fmt.Errorf("retrieving chart from assets: %w", err) } - var buf bytes.Buffer - if err := tmpl.Execute(&buf, c); err != nil { - return nil, fmt.Errorf("executing template: %w", err) + + values, err := internaltemplate.Render(chartValuesTmpl, c) + if err != nil { + return nil, fmt.Errorf("rendering values template failed: %w", err) } - return map[string]string{ - "namespace.yml": namespaceManifest, - "deployment.yml": deploymentManifest, - "service.yml": serviceManifest, - "ingress.yml": buf.String(), - }, nil + + // Generate YAML for the httpbin deployment. + renderedFiles, err := util.RenderChart(helmChart, name, c.Metadata().Namespace.Name, values) + if err != nil { + return nil, fmt.Errorf("rendering chart failed: %w", err) + } + + return renderedFiles, nil } func (c *component) Metadata() components.Metadata { diff --git a/pkg/components/httpbin/template.go b/pkg/components/httpbin/template.go new file mode 100644 index 000000000..0b1be93cf --- /dev/null +++ b/pkg/components/httpbin/template.go @@ -0,0 +1,22 @@ +// Copyright 2020 The Lokomotive Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package httpbin has code related to deployment of httpbin component. +package httpbin + +const chartValuesTmpl = ` +ingressHost: {{ .IngressHost }} + +certManagerClusterIssuer: {{ .CertManagerClusterIssuer }} +` From dacd9ccbd8c224628b2321badd7617fa1e86f145 Mon Sep 17 00:00:00 2001 From: knrt10 Date: Tue, 15 Sep 2020 11:33:47 +0530 Subject: [PATCH 2/2] httpbin: add test for manifest rendering Signed-off-by: knrt10 --- pkg/components/httpbin/component_test.go | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 pkg/components/httpbin/component_test.go diff --git a/pkg/components/httpbin/component_test.go b/pkg/components/httpbin/component_test.go new file mode 100644 index 000000000..fa8b69c31 --- /dev/null +++ b/pkg/components/httpbin/component_test.go @@ -0,0 +1,81 @@ +// Copyright 2020 The Lokomotive Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package httpbin_test + +import ( + "testing" + + "github.com/kinvolk/lokomotive/pkg/components" + "github.com/kinvolk/lokomotive/pkg/components/util" +) + +const name = "httpbin" + +func TestRenderManifest(t *testing.T) { + tests := []struct { + desc string + hcl string + wantErr bool + }{ + { + desc: "Valid config", + hcl: ` +component "httpbin" { + ingress_host = "foo" +} + `, + }, + { + desc: "invalid config", + hcl: ` +component "dex" { + certmanager_cluster_issuer = "letsencrypt-staging" +} + `, + wantErr: true, + }, + } + + for _, tc := range tests { + b, d := util.GetComponentBody(tc.hcl, name) + if d != nil { + t.Errorf("%s - Error getting component body: %v", tc.desc, d) + } + + c, err := components.Get(name) + if err != nil { + t.Fatalf("failed getting component: %v", err) + } + + d = c.LoadConfig(b, nil) + + if !tc.wantErr && d.HasErrors() { + t.Errorf("%s - Valid config should not return error, got: %s", tc.desc, d) + } + + if tc.wantErr && !d.HasErrors() { + t.Errorf("%s - Wrong config should have returned an error", tc.desc) + } + + m, err := c.RenderManifests() + if err != nil { + t.Errorf("%s - Rendering manifests with valid config should succeed, got: %s", tc.desc, err) + } + + if len(m) == 0 { + t.Errorf("%s - Rendered manifests shouldn't be empty", tc.desc) + } + } +}