Skip to content

Commit

Permalink
fix(helm): use build path for charts for helm modules converted to ac… (
Browse files Browse the repository at this point in the history
#5190)

* fix(helm): use build path for charts for helm modules converted to actions

* chore: prettier

* chore: fix test for includes in helm modules

* chore: review feedback
  • Loading branch information
twelvemo authored Oct 5, 2023
1 parent a4bb0e7 commit eb5e859
Show file tree
Hide file tree
Showing 29 changed files with 577 additions and 20 deletions.
26 changes: 13 additions & 13 deletions core/src/plugins/kubernetes/helm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ export const helmModuleHandlers: Partial<ModuleActionHandlers<HelmModule>> = {
baseFields,
tasks,
tests,
dummyBuild,
dummyBuild: d,
convertBuildDependency,
prepareRuntimeDependencies,
} = params
let dummyBuild = d
const actions: (ExecBuildConfig | HelmActionConfig)[] = []

if (dummyBuild) {
actions.push(dummyBuild)
} else {
// We make a dummy build without a `copyFrom` or any build dependencies. This is to ensure there's a build action
// for this module if it's used as a base by another Helm module.
actions.push(makeDummyBuild({ module, copyFrom: undefined, dependencies: undefined }))
if (!dummyBuild) {
// We create a dummy build without a `copyFrom` or any build dependencies, to ensure there's a build action
// for this module. This is needed for compatibility reasions e.g. if there was a `base` field on the module
// or if a helm chart references dependent local charts relative to the modules build directory.
// We set the deploy actions `build` param to the dummy build to use the `buildPath` for all helm operations.
dummyBuild = makeDummyBuild({
module,
copyFrom: undefined,
dependencies: undefined,
})
}
actions.push(dummyBuild)

// There's one service on helm modules expect when skipDeploy = true
const service: (typeof services)[0] | undefined = services[0]

// The helm Deploy type does not support the `base` field. We handle the field here during conversion,
// for compatibility.
// Note: A dummyBuild will be set if `base` is set on the Module, because the module configure handler then
// sets a `build.dependencies[].copy` directive.

let deployAction: HelmDeployConfig | null = null

// If this Helm module has `skipDeploy = true`, there won't be a service config for us to convert here.
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/helm/module-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export async function configureHelmModule({
]
}

const yamlPath = join(module.path, chartPath, helmChartYamlFilename)
const yamlPath = join(moduleConfig.path, chartPath, helmChartYamlFilename)
const containsSources = await pathExists(yamlPath)

if (base) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ dependencies:
- name: postgresql
version: "11.6.12"
repository: https://charts.bitnami.com/bitnami
- name: frontend
version: 0.1.x
repository: file://../frontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
kind: Deploy
description: The API backend for the voting UI
type: helm
name: chart-with-dependency-action
spec:
values:
image:
tag: ${modules.api-image.version}
ingress:
enabled: false

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

Original file line number Diff line number Diff line change
@@ -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/
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: chart-with-dependency
version: 0.1.0
image:
repository: busybox
tag: latest
pullPolicy: IfNotPresent
dependencies:
- name: postgresql
version: "11.6.12"
repository: https://charts.bitnami.com/bitnami
- name: frontend
version: 0.1.x
repository: file://../../../frontend
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
kind: Module
description: The API backend for the voting UI
type: helm
name: chart-with-dependency
name: chart-with-dependency-module
serviceResource:
kind: Deployment
containerModule: api-image
Expand All @@ -10,4 +10,3 @@ values:
tag: ${modules.api-image.version}
ingress:
enabled: false

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range $.Values.ingress.paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $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 "chart-with-namespace.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 {{ include "chart-with-namespace.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "chart-with-namespace.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.kubernetes.io/name={{ include "chart-with-namespace.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 port-forward $POD_NAME 8080:80
{{- end }}
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 "chart-with-namespace.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 "chart-with-namespace.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 "chart-with-namespace.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "chart-with-namespace.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "chart-with-namespace.name" . }}
helm.sh/chart: {{ include "chart-with-namespace.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "chart-with-namespace.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app.kubernetes.io/name: {{ include "chart-with-namespace.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
args: [python, app.py]
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
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 }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "chart-with-namespace.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "chart-with-namespace.name" . }}
helm.sh/chart: {{ include "chart-with-namespace.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: {{ include "chart-with-namespace.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Default values for chart-with-namespace.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
repository: api-image
tag: stable
pullPolicy: IfNotPresent

nameOverride: ""
fullnameOverride: ""

service:
type: ClusterIP
port: 80

ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
paths: []
hosts:
- chart-example.local
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: {}
22 changes: 22 additions & 0 deletions core/test/data/test-projects/helm/frontend/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
9 changes: 9 additions & 0 deletions core/test/data/test-projects/helm/frontend/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: frontend
version: 0.1.0
image:
repository: busybox
tag: latest
pullPolicy: IfNotPresent
21 changes: 21 additions & 0 deletions core/test/data/test-projects/helm/frontend/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range $.Values.ingress.paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $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 "frontend.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 {{ include "frontend.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "frontend.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.kubernetes.io/name={{ include "frontend.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 port-forward $POD_NAME 8080:80
{{- end }}
32 changes: 32 additions & 0 deletions core/test/data/test-projects/helm/frontend/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 "frontend.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 "frontend.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 "frontend.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Loading

0 comments on commit eb5e859

Please sign in to comment.