Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
httpbin: convert to Helm chart
Browse files Browse the repository at this point in the history
closes: #957

Signed-off-by: knrt10 <kautilya@kinvolk.io>
  • Loading branch information
knrt10 committed Sep 15, 2020
1 parent 64aa24d commit c39bcca
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 104 deletions.
23 changes: 23 additions & 0 deletions assets/charts/components/httpbin/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
23 changes: 23 additions & 0 deletions assets/charts/components/httpbin/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions assets/charts/components/httpbin/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions assets/charts/components/httpbin/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions assets/charts/components/httpbin/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: httpbin
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: httpbin
2 changes: 2 additions & 0 deletions assets/charts/components/httpbin/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
certManagerClusterIssuer:
ingressHost:
60 changes: 60 additions & 0 deletions pkg/assets/generated_assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 18 additions & 104 deletions pkg/components/httpbin/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/components/httpbin/template.go
Original file line number Diff line number Diff line change
@@ -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 }}
`

0 comments on commit c39bcca

Please sign in to comment.