Skip to content

Instantly share code, notes, and snippets.

View jwillker's full-sized avatar
:octocat:
...

Jhonn W. Frazão jwillker

:octocat:
...
View GitHub Profile
View kind.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
View asserts.go
func assertName(t assert.TestingT, name string, deployment appsv1.Deployment) bool {
// Only assert if theres a want value to assert
// if not return true to don't fail the test
if name == "" {
return true
}
return assert.Equal(
t,
name,
View deploymentV3_test.go
//more code
...
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var deployment appsv1.Deployment
// Render the deployment object
err := Render(t, "../", "deployment.yaml", tt.args.Release, tt.args.Values, &deployment)
if err != nil {
fmt.Println(err)
}
View deploymentV2_test.go
func TestDeployTemplateRenders(t *testing.T) {
t.Parallel()
type args struct {
Release string
Values map[string]string
}
type want struct {
DeployName string
View render.go
// Render receive helm parameters and render a k8s object
func Render(t *testing.T, helmChartPath, templateFile, release string, values map[string]string, k8sObject interface{}) error {
// Passing values to helm chart.
options := &helm.Options{
SetValues: values,
}
templateFile = fmt.Sprintf("templates/%s", templateFile)
output, err := helm.RenderTemplateE(t, options, helmChartPath, release, []string{templateFile})
View deployment_test.go
func TestDeployTemplateRendersMetadata(t *testing.T) {
t.Parallel()
var (
// Default Helm values
// Work as values.yaml for tests purposes
deployDefaultValues = map[string]string{
"app.version": "0.1.0",
"app.replicas": "2",
"app.component": "api",
"app.env": "dev",
View _helpers.tpl
{{- define "generic.fullname" -}}
{{- .Release.Name | trunc 6 -}}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "generic.selectorLabels" -}}
app.kubernetes.io/name: {{ include "generic.fullname" .}}
app.kubernetes.io/instance: '{{ include "generic.fullname" . }}-{{ .Values.app.version }}'
View deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "generic.fullname" .}}
labels: &labels
{{- include "generic.commonLabels" . | nindent 4 }}
{{- with .Values.app.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
annotations: &annotations
View helm_basic_example_template_test.go
// An example of how to verify the rendered template object of a Helm Chart given various inputs.
func TestHelmBasicExampleTemplateRenderedDeployment(t *testing.T) {
t.Parallel()
// Path to the helm chart we will test
helmChartPath, err := filepath.Abs("../examples/helm-basic-example")
releaseName := "helm-basic"
require.NoError(t, err)
// Since we aren't deploying any resources, there is no need to setup kubectl authentication or helm home.
View deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "helm-basic-example.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
# These labels are required by helm. You can read more about required labels in the chart best pracices guide:
# https://docs.helm.sh/chart_best_practices/#standard-labels
helm.sh/chart: {{ include "helm-basic-example.chart" . }}
app.kubernetes.io/name: {{ include "helm-basic-example.name" . }}