From a3f177328a9e09d087c1159b58b9ea8850b41c1d Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Wed, 18 Nov 2020 23:16:13 +0100 Subject: [PATCH] Move Helm dependency manager to `helm` package Signed-off-by: Hidde Beydals --- controllers/helmchart_controller.go | 8 +-- .../helm}/dependency_manager.go | 7 +- .../helm}/dependency_manager_test.go | 28 ++------ .../helm/testdata/charts/helmchart-0.1.0.tgz | Bin 0 -> 3277 bytes .../testdata/charts/helmchart/.helmignore | 22 ++++++ .../helm/testdata/charts/helmchart/Chart.yaml | 21 ++++++ .../charts/helmchart/templates/NOTES.txt | 21 ++++++ .../charts/helmchart/templates/_helpers.tpl | 63 +++++++++++++++++ .../helmchart/templates/deployment.yaml | 55 +++++++++++++++ .../charts/helmchart/templates/ingress.yaml | 41 +++++++++++ .../charts/helmchart/templates/service.yaml | 15 ++++ .../helmchart/templates/serviceaccount.yaml | 8 +++ .../templates/tests/test-connection.yaml | 15 ++++ .../testdata/charts/helmchart/values.yaml | 66 ++++++++++++++++++ .../charts/helmchartwithdeps/.helmignore | 22 ++++++ .../charts/helmchartwithdeps/Chart.yaml | 33 +++++++++ .../helmchartwithdeps/templates/NOTES.txt | 21 ++++++ .../helmchartwithdeps/templates/_helpers.tpl | 63 +++++++++++++++++ .../templates/deployment.yaml | 55 +++++++++++++++ .../helmchartwithdeps/templates/ingress.yaml | 41 +++++++++++ .../helmchartwithdeps/templates/service.yaml | 15 ++++ .../templates/serviceaccount.yaml | 8 +++ .../templates/tests/test-connection.yaml | 15 ++++ .../charts/helmchartwithdeps/values.yaml | 66 ++++++++++++++++++ 24 files changed, 679 insertions(+), 30 deletions(-) rename {controllers => internal/helm}/dependency_manager.go (95%) rename {controllers => internal/helm}/dependency_manager_test.go (87%) create mode 100644 internal/helm/testdata/charts/helmchart-0.1.0.tgz create mode 100644 internal/helm/testdata/charts/helmchart/.helmignore create mode 100644 internal/helm/testdata/charts/helmchart/Chart.yaml create mode 100644 internal/helm/testdata/charts/helmchart/templates/NOTES.txt create mode 100644 internal/helm/testdata/charts/helmchart/templates/_helpers.tpl create mode 100644 internal/helm/testdata/charts/helmchart/templates/deployment.yaml create mode 100644 internal/helm/testdata/charts/helmchart/templates/ingress.yaml create mode 100644 internal/helm/testdata/charts/helmchart/templates/service.yaml create mode 100644 internal/helm/testdata/charts/helmchart/templates/serviceaccount.yaml create mode 100644 internal/helm/testdata/charts/helmchart/templates/tests/test-connection.yaml create mode 100644 internal/helm/testdata/charts/helmchart/values.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/.helmignore create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/Chart.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/NOTES.txt create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/_helpers.tpl create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/deployment.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/ingress.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/service.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/serviceaccount.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/templates/tests/test-connection.yaml create mode 100644 internal/helm/testdata/charts/helmchartwithdeps/values.yaml diff --git a/controllers/helmchart_controller.go b/controllers/helmchart_controller.go index 97b4af80..d0682d1c 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -454,7 +454,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context, // Load from lockfile if exists reqs = lock.Dependencies } - var dwr []*DependencyWithRepository + var dwr []*helm.DependencyWithRepository for _, dep := range reqs { // Exclude existing dependencies for _, existing := range deps { @@ -465,7 +465,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context, // Continue loop if file scheme detected if strings.HasPrefix(dep.Repository, "file://") { - dwr = append(dwr, &DependencyWithRepository{ + dwr = append(dwr, &helm.DependencyWithRepository{ Dependency: dep, Repo: nil, }) @@ -527,7 +527,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context, } } - dwr = append(dwr, &DependencyWithRepository{ + dwr = append(dwr, &helm.DependencyWithRepository{ Dependency: dep, Repo: chartRepo, }) @@ -535,7 +535,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context, // Construct dependencies for chart if any if len(dwr) > 0 { - dm := &DependencyManager{ + dm := &helm.DependencyManager{ Chart: helmChart, ChartPath: chartPath, Dependencies: dwr, diff --git a/controllers/dependency_manager.go b/internal/helm/dependency_manager.go similarity index 95% rename from controllers/dependency_manager.go rename to internal/helm/dependency_manager.go index 90d55da0..c4427891 100644 --- a/controllers/dependency_manager.go +++ b/internal/helm/dependency_manager.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package helm import ( "context" @@ -25,7 +25,6 @@ import ( "strings" "github.com/Masterminds/semver/v3" - "github.com/fluxcd/source-controller/internal/helm" "golang.org/x/sync/errgroup" helmchart "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" @@ -35,7 +34,7 @@ import ( // repository type DependencyWithRepository struct { Dependency *helmchart.Dependency - Repo *helm.ChartRepository + Repo *ChartRepository } // DependencyManager manages dependencies for helm charts @@ -115,7 +114,7 @@ func chartForLocalDependency(dep *helmchart.Dependency, cp string) (*helmchart.C return ch, nil } -func chartForRemoteDependency(dep *helmchart.Dependency, chartrepo *helm.ChartRepository) (*helmchart.Chart, error) { +func chartForRemoteDependency(dep *helmchart.Dependency, chartrepo *ChartRepository) (*helmchart.Chart, error) { if chartrepo == nil { err := fmt.Errorf("chartrepo should not be nil") return nil, err diff --git a/controllers/dependency_manager_test.go b/internal/helm/dependency_manager_test.go similarity index 87% rename from controllers/dependency_manager_test.go rename to internal/helm/dependency_manager_test.go index 0e0c3c68..cfadaf21 100644 --- a/controllers/dependency_manager_test.go +++ b/internal/helm/dependency_manager_test.go @@ -14,37 +14,29 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package helm import ( - "bytes" "fmt" "io/ioutil" "strings" "testing" - "github.com/fluxcd/source-controller/internal/helm" helmchart "helm.sh/helm/v3/pkg/chart" - "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/repo" ) var ( helmPackageFile = "testdata/charts/helmchart-0.1.0.tgz" - chartName = "helmchart" - chartVersion = "0.1.0" - chartLocalRepository = "file://../helmchart" - remoteDepFixture helmchart.Dependency = helmchart.Dependency{ + chartName = "helmchart" + chartVersion = "0.1.0" + chartLocalRepository = "file://../helmchart" + remoteDepFixture = helmchart.Dependency{ Name: chartName, Version: chartVersion, Repository: "https://example.com/charts", } - chartFixture helmchart.Chart = helmchart.Chart{ - Metadata: &helmchart.Metadata{ - Name: "test", - }, - } ) func TestBuild_WithEmptyDependencies(t *testing.T) { @@ -164,7 +156,7 @@ func TestBuild_WithRemoteChart(t *testing.T) { i := repo.NewIndexFile() i.Add(&helmchart.Metadata{Name: chartName, Version: chartVersion}, fmt.Sprintf("%s-%s.tgz", chartName, chartVersion), "http://example.com/charts", "sha256:1234567890") mg := mockGetter{response: b} - cr := &helm.ChartRepository{ + cr := &ChartRepository{ URL: remoteDepFixture.Repository, Index: i, Client: &mg, @@ -202,11 +194,3 @@ func TestBuild_WithRemoteChart(t *testing.T) { t.Errorf("Build() expected to return different error, got: %s", err) } } - -type mockGetter struct { - response []byte -} - -func (g *mockGetter) Get(url string, options ...getter.Option) (*bytes.Buffer, error) { - return bytes.NewBuffer(g.response), nil -} diff --git a/internal/helm/testdata/charts/helmchart-0.1.0.tgz b/internal/helm/testdata/charts/helmchart-0.1.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..f64a32eeeb54fc24a44390478a3adf5bcb5cf754 GIT binary patch literal 3277 zcmV;;3^MZ{iwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PH(^Z`-(&{ac@6PU#P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a/internal/helm/testdata/charts/helmchart/.helmignore b/internal/helm/testdata/charts/helmchart/.helmignore new file mode 100644 index 00000000..50af0317 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/.helmignore @@ -0,0 +1,22 @@ +# 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 +.vscode/ diff --git a/internal/helm/testdata/charts/helmchart/Chart.yaml b/internal/helm/testdata/charts/helmchart/Chart.yaml new file mode 100644 index 00000000..46eaf150 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/Chart.yaml @@ -0,0 +1,21 @@ +apiVersion: v2 +name: helmchart +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. +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. +appVersion: 1.16.0 diff --git a/internal/helm/testdata/charts/helmchart/templates/NOTES.txt b/internal/helm/testdata/charts/helmchart/templates/NOTES.txt new file mode 100644 index 00000000..741a77d8 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/NOTES.txt @@ -0,0 +1,21 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helmchart.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 --namespace {{ .Release.Namespace }} svc -w {{ include "helmchart.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helmchart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + 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.kubernetes.io/name={{ include "helmchart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80 +{{- end }} diff --git a/internal/helm/testdata/charts/helmchart/templates/_helpers.tpl b/internal/helm/testdata/charts/helmchart/templates/_helpers.tpl new file mode 100644 index 00000000..f6431fcb --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/_helpers.tpl @@ -0,0 +1,63 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "helmchart.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 "helmchart.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 "helmchart.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Common labels +*/}} +{{- define "helmchart.labels" -}} +helm.sh/chart: {{ include "helmchart.chart" . }} +{{ include "helmchart.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end -}} + +{{/* +Selector labels +*/}} +{{- define "helmchart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "helmchart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end -}} + +{{/* +Create the name of the service account to use +*/}} +{{- define "helmchart.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} + {{ default (include "helmchart.fullname" .) .Values.serviceAccount.name }} +{{- else -}} + {{ default "default" .Values.serviceAccount.name }} +{{- end -}} +{{- end -}} diff --git a/internal/helm/testdata/charts/helmchart/templates/deployment.yaml b/internal/helm/testdata/charts/helmchart/templates/deployment.yaml new file mode 100644 index 00000000..daa9f8e5 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/deployment.yaml @@ -0,0 +1,55 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "helmchart.fullname" . }} + labels: + {{- include "helmchart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "helmchart.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "helmchart.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "helmchart.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/internal/helm/testdata/charts/helmchart/templates/ingress.yaml b/internal/helm/testdata/charts/helmchart/templates/ingress.yaml new file mode 100644 index 00000000..c2069e9c --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/ingress.yaml @@ -0,0 +1,41 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "helmchart.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "helmchart.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 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: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ . }} + backend: + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} +{{- end }} diff --git a/internal/helm/testdata/charts/helmchart/templates/service.yaml b/internal/helm/testdata/charts/helmchart/templates/service.yaml new file mode 100644 index 00000000..12e16ef7 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "helmchart.fullname" . }} + labels: + {{- include "helmchart.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "helmchart.selectorLabels" . | nindent 4 }} diff --git a/internal/helm/testdata/charts/helmchart/templates/serviceaccount.yaml b/internal/helm/testdata/charts/helmchart/templates/serviceaccount.yaml new file mode 100644 index 00000000..da351264 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/serviceaccount.yaml @@ -0,0 +1,8 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "helmchart.serviceAccountName" . }} + labels: +{{ include "helmchart.labels" . | nindent 4 }} +{{- end -}} diff --git a/internal/helm/testdata/charts/helmchart/templates/tests/test-connection.yaml b/internal/helm/testdata/charts/helmchart/templates/tests/test-connection.yaml new file mode 100644 index 00000000..11b0b1a9 --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "helmchart.fullname" . }}-test-connection" + labels: +{{ include "helmchart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test-success +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "helmchart.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/internal/helm/testdata/charts/helmchart/values.yaml b/internal/helm/testdata/charts/helmchart/values.yaml new file mode 100644 index 00000000..40e7aa0b --- /dev/null +++ b/internal/helm/testdata/charts/helmchart/values.yaml @@ -0,0 +1,66 @@ +# Default values for helmchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + 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: {} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/.helmignore b/internal/helm/testdata/charts/helmchartwithdeps/.helmignore new file mode 100644 index 00000000..50af0317 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/.helmignore @@ -0,0 +1,22 @@ +# 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 +.vscode/ diff --git a/internal/helm/testdata/charts/helmchartwithdeps/Chart.yaml b/internal/helm/testdata/charts/helmchartwithdeps/Chart.yaml new file mode 100644 index 00000000..99dac50b --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/Chart.yaml @@ -0,0 +1,33 @@ +apiVersion: v2 +name: helmchartwithdeps +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. +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. +appVersion: 1.16.0 + +dependencies: + - name: helmchart + version: "0.1.0" + repository: "file://../helmchart" + - name: helmchart + alias: aliased + version: "0.1.0" + repository: "file://../helmchart" + - name: grafana + version: ">=5.7.0" + repository: "https://grafana.github.io/helm-charts" diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/NOTES.txt b/internal/helm/testdata/charts/helmchartwithdeps/templates/NOTES.txt new file mode 100644 index 00000000..105423d2 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/NOTES.txt @@ -0,0 +1,21 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helmchartwithdeps.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 --namespace {{ .Release.Namespace }} svc -w {{ include "helmchartwithdeps.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helmchartwithdeps.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + 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.kubernetes.io/name={{ include "helmchartwithdeps.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80 +{{- end }} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/_helpers.tpl b/internal/helm/testdata/charts/helmchartwithdeps/templates/_helpers.tpl new file mode 100644 index 00000000..a718f8b3 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/_helpers.tpl @@ -0,0 +1,63 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "helmchartwithdeps.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 "helmchartwithdeps.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 "helmchartwithdeps.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Common labels +*/}} +{{- define "helmchartwithdeps.labels" -}} +helm.sh/chart: {{ include "helmchartwithdeps.chart" . }} +{{ include "helmchartwithdeps.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end -}} + +{{/* +Selector labels +*/}} +{{- define "helmchartwithdeps.selectorLabels" -}} +app.kubernetes.io/name: {{ include "helmchartwithdeps.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end -}} + +{{/* +Create the name of the service account to use +*/}} +{{- define "helmchartwithdeps.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} + {{ default (include "helmchartwithdeps.fullname" .) .Values.serviceAccount.name }} +{{- else -}} + {{ default "default" .Values.serviceAccount.name }} +{{- end -}} +{{- end -}} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/deployment.yaml b/internal/helm/testdata/charts/helmchartwithdeps/templates/deployment.yaml new file mode 100644 index 00000000..08f62c74 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/deployment.yaml @@ -0,0 +1,55 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "helmchartwithdeps.fullname" . }} + labels: + {{- include "helmchartwithdeps.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "helmchartwithdeps.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "helmchartwithdeps.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "helmchartwithdeps.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/ingress.yaml b/internal/helm/testdata/charts/helmchartwithdeps/templates/ingress.yaml new file mode 100644 index 00000000..6c1b0314 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/ingress.yaml @@ -0,0 +1,41 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "helmchartwithdeps.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "helmchartwithdeps.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 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: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ . }} + backend: + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} +{{- end }} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/service.yaml b/internal/helm/testdata/charts/helmchartwithdeps/templates/service.yaml new file mode 100644 index 00000000..2c270c67 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "helmchartwithdeps.fullname" . }} + labels: + {{- include "helmchartwithdeps.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "helmchartwithdeps.selectorLabels" . | nindent 4 }} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/serviceaccount.yaml b/internal/helm/testdata/charts/helmchartwithdeps/templates/serviceaccount.yaml new file mode 100644 index 00000000..2eec29c5 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/serviceaccount.yaml @@ -0,0 +1,8 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "helmchartwithdeps.serviceAccountName" . }} + labels: +{{ include "helmchartwithdeps.labels" . | nindent 4 }} +{{- end -}} diff --git a/internal/helm/testdata/charts/helmchartwithdeps/templates/tests/test-connection.yaml b/internal/helm/testdata/charts/helmchartwithdeps/templates/tests/test-connection.yaml new file mode 100644 index 00000000..bbcd0920 --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "helmchartwithdeps.fullname" . }}-test-connection" + labels: +{{ include "helmchartwithdeps.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test-success +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "helmchartwithdeps.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/internal/helm/testdata/charts/helmchartwithdeps/values.yaml b/internal/helm/testdata/charts/helmchartwithdeps/values.yaml new file mode 100644 index 00000000..8213f28c --- /dev/null +++ b/internal/helm/testdata/charts/helmchartwithdeps/values.yaml @@ -0,0 +1,66 @@ +# Default values for helmchartwithdeps. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + 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: {}