Skip to content

Commit a3f1773

Browse files
committed
Move Helm dependency manager to helm package
Signed-off-by: Hidde Beydals <[email protected]>
1 parent 1297f8b commit a3f1773

24 files changed

+679
-30
lines changed

controllers/helmchart_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
454454
// Load from lockfile if exists
455455
reqs = lock.Dependencies
456456
}
457-
var dwr []*DependencyWithRepository
457+
var dwr []*helm.DependencyWithRepository
458458
for _, dep := range reqs {
459459
// Exclude existing dependencies
460460
for _, existing := range deps {
@@ -465,7 +465,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
465465

466466
// Continue loop if file scheme detected
467467
if strings.HasPrefix(dep.Repository, "file://") {
468-
dwr = append(dwr, &DependencyWithRepository{
468+
dwr = append(dwr, &helm.DependencyWithRepository{
469469
Dependency: dep,
470470
Repo: nil,
471471
})
@@ -527,15 +527,15 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
527527
}
528528
}
529529

530-
dwr = append(dwr, &DependencyWithRepository{
530+
dwr = append(dwr, &helm.DependencyWithRepository{
531531
Dependency: dep,
532532
Repo: chartRepo,
533533
})
534534
}
535535

536536
// Construct dependencies for chart if any
537537
if len(dwr) > 0 {
538-
dm := &DependencyManager{
538+
dm := &helm.DependencyManager{
539539
Chart: helmChart,
540540
ChartPath: chartPath,
541541
Dependencies: dwr,
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controllers
17+
package helm
1818

1919
import (
2020
"context"
@@ -25,7 +25,6 @@ import (
2525
"strings"
2626

2727
"github.com/Masterminds/semver/v3"
28-
"github.com/fluxcd/source-controller/internal/helm"
2928
"golang.org/x/sync/errgroup"
3029
helmchart "helm.sh/helm/v3/pkg/chart"
3130
"helm.sh/helm/v3/pkg/chart/loader"
@@ -35,7 +34,7 @@ import (
3534
// repository
3635
type DependencyWithRepository struct {
3736
Dependency *helmchart.Dependency
38-
Repo *helm.ChartRepository
37+
Repo *ChartRepository
3938
}
4039

4140
// DependencyManager manages dependencies for helm charts
@@ -115,7 +114,7 @@ func chartForLocalDependency(dep *helmchart.Dependency, cp string) (*helmchart.C
115114
return ch, nil
116115
}
117116

118-
func chartForRemoteDependency(dep *helmchart.Dependency, chartrepo *helm.ChartRepository) (*helmchart.Chart, error) {
117+
func chartForRemoteDependency(dep *helmchart.Dependency, chartrepo *ChartRepository) (*helmchart.Chart, error) {
119118
if chartrepo == nil {
120119
err := fmt.Errorf("chartrepo should not be nil")
121120
return nil, err
Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,29 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controllers
17+
package helm
1818

1919
import (
20-
"bytes"
2120
"fmt"
2221
"io/ioutil"
2322
"strings"
2423
"testing"
2524

26-
"github.com/fluxcd/source-controller/internal/helm"
2725
helmchart "helm.sh/helm/v3/pkg/chart"
28-
"helm.sh/helm/v3/pkg/getter"
2926
"helm.sh/helm/v3/pkg/repo"
3027
)
3128

3229
var (
3330
helmPackageFile = "testdata/charts/helmchart-0.1.0.tgz"
3431

35-
chartName = "helmchart"
36-
chartVersion = "0.1.0"
37-
chartLocalRepository = "file://../helmchart"
38-
remoteDepFixture helmchart.Dependency = helmchart.Dependency{
32+
chartName = "helmchart"
33+
chartVersion = "0.1.0"
34+
chartLocalRepository = "file://../helmchart"
35+
remoteDepFixture = helmchart.Dependency{
3936
Name: chartName,
4037
Version: chartVersion,
4138
Repository: "https://example.com/charts",
4239
}
43-
chartFixture helmchart.Chart = helmchart.Chart{
44-
Metadata: &helmchart.Metadata{
45-
Name: "test",
46-
},
47-
}
4840
)
4941

5042
func TestBuild_WithEmptyDependencies(t *testing.T) {
@@ -164,7 +156,7 @@ func TestBuild_WithRemoteChart(t *testing.T) {
164156
i := repo.NewIndexFile()
165157
i.Add(&helmchart.Metadata{Name: chartName, Version: chartVersion}, fmt.Sprintf("%s-%s.tgz", chartName, chartVersion), "http://example.com/charts", "sha256:1234567890")
166158
mg := mockGetter{response: b}
167-
cr := &helm.ChartRepository{
159+
cr := &ChartRepository{
168160
URL: remoteDepFixture.Repository,
169161
Index: i,
170162
Client: &mg,
@@ -202,11 +194,3 @@ func TestBuild_WithRemoteChart(t *testing.T) {
202194
t.Errorf("Build() expected to return different error, got: %s", err)
203195
}
204196
}
205-
206-
type mockGetter struct {
207-
response []byte
208-
}
209-
210-
func (g *mockGetter) Get(url string, options ...getter.Option) (*bytes.Buffer, error) {
211-
return bytes.NewBuffer(g.response), nil
212-
}
3.2 KB
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj
22+
.vscode/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v2
2+
name: helmchart
3+
description: A Helm chart for Kubernetes
4+
5+
# A chart can be either an 'application' or a 'library' chart.
6+
#
7+
# Application charts are a collection of templates that can be packaged into versioned archives
8+
# to be deployed.
9+
#
10+
# Library charts provide useful utilities or functions for the chart developer. They're included as
11+
# a dependency of application charts to inject those utilities and functions into the rendering
12+
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
13+
type: application
14+
15+
# This is the chart version. This version number should be incremented each time you make changes
16+
# to the chart and its templates, including the app version.
17+
version: 0.1.0
18+
19+
# This is the version number of the application being deployed. This version number should be
20+
# incremented each time you make changes to the application.
21+
appVersion: 1.16.0
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
1. Get the application URL by running these commands:
2+
{{- if .Values.ingress.enabled }}
3+
{{- range $host := .Values.ingress.hosts }}
4+
{{- range .paths }}
5+
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }}
6+
{{- end }}
7+
{{- end }}
8+
{{- else if contains "NodePort" .Values.service.type }}
9+
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helmchart.fullname" . }})
10+
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
11+
echo http://$NODE_IP:$NODE_PORT
12+
{{- else if contains "LoadBalancer" .Values.service.type }}
13+
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
14+
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "helmchart.fullname" . }}'
15+
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helmchart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
16+
echo http://$SERVICE_IP:{{ .Values.service.port }}
17+
{{- else if contains "ClusterIP" .Values.service.type }}
18+
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}")
19+
echo "Visit http://127.0.0.1:8080 to use your application"
20+
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80
21+
{{- end }}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "helmchart.name" -}}
6+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7+
{{- end -}}
8+
9+
{{/*
10+
Create a default fully qualified app name.
11+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12+
If release name contains chart name it will be used as a full name.
13+
*/}}
14+
{{- define "helmchart.fullname" -}}
15+
{{- if .Values.fullnameOverride -}}
16+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17+
{{- else -}}
18+
{{- $name := default .Chart.Name .Values.nameOverride -}}
19+
{{- if contains $name .Release.Name -}}
20+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21+
{{- else -}}
22+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23+
{{- end -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{/*
28+
Create chart name and version as used by the chart label.
29+
*/}}
30+
{{- define "helmchart.chart" -}}
31+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32+
{{- end -}}
33+
34+
{{/*
35+
Common labels
36+
*/}}
37+
{{- define "helmchart.labels" -}}
38+
helm.sh/chart: {{ include "helmchart.chart" . }}
39+
{{ include "helmchart.selectorLabels" . }}
40+
{{- if .Chart.AppVersion }}
41+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
42+
{{- end }}
43+
app.kubernetes.io/managed-by: {{ .Release.Service }}
44+
{{- end -}}
45+
46+
{{/*
47+
Selector labels
48+
*/}}
49+
{{- define "helmchart.selectorLabels" -}}
50+
app.kubernetes.io/name: {{ include "helmchart.name" . }}
51+
app.kubernetes.io/instance: {{ .Release.Name }}
52+
{{- end -}}
53+
54+
{{/*
55+
Create the name of the service account to use
56+
*/}}
57+
{{- define "helmchart.serviceAccountName" -}}
58+
{{- if .Values.serviceAccount.create -}}
59+
{{ default (include "helmchart.fullname" .) .Values.serviceAccount.name }}
60+
{{- else -}}
61+
{{ default "default" .Values.serviceAccount.name }}
62+
{{- end -}}
63+
{{- end -}}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ include "helmchart.fullname" . }}
5+
labels:
6+
{{- include "helmchart.labels" . | nindent 4 }}
7+
spec:
8+
replicas: {{ .Values.replicaCount }}
9+
selector:
10+
matchLabels:
11+
{{- include "helmchart.selectorLabels" . | nindent 6 }}
12+
template:
13+
metadata:
14+
labels:
15+
{{- include "helmchart.selectorLabels" . | nindent 8 }}
16+
spec:
17+
{{- with .Values.imagePullSecrets }}
18+
imagePullSecrets:
19+
{{- toYaml . | nindent 8 }}
20+
{{- end }}
21+
serviceAccountName: {{ include "helmchart.serviceAccountName" . }}
22+
securityContext:
23+
{{- toYaml .Values.podSecurityContext | nindent 8 }}
24+
containers:
25+
- name: {{ .Chart.Name }}
26+
securityContext:
27+
{{- toYaml .Values.securityContext | nindent 12 }}
28+
image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
29+
imagePullPolicy: {{ .Values.image.pullPolicy }}
30+
ports:
31+
- name: http
32+
containerPort: 80
33+
protocol: TCP
34+
livenessProbe:
35+
httpGet:
36+
path: /
37+
port: http
38+
readinessProbe:
39+
httpGet:
40+
path: /
41+
port: http
42+
resources:
43+
{{- toYaml .Values.resources | nindent 12 }}
44+
{{- with .Values.nodeSelector }}
45+
nodeSelector:
46+
{{- toYaml . | nindent 8 }}
47+
{{- end }}
48+
{{- with .Values.affinity }}
49+
affinity:
50+
{{- toYaml . | nindent 8 }}
51+
{{- end }}
52+
{{- with .Values.tolerations }}
53+
tolerations:
54+
{{- toYaml . | nindent 8 }}
55+
{{- end }}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{- if .Values.ingress.enabled -}}
2+
{{- $fullName := include "helmchart.fullname" . -}}
3+
{{- $svcPort := .Values.service.port -}}
4+
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
5+
apiVersion: networking.k8s.io/v1beta1
6+
{{- else -}}
7+
apiVersion: extensions/v1beta1
8+
{{- end }}
9+
kind: Ingress
10+
metadata:
11+
name: {{ $fullName }}
12+
labels:
13+
{{- include "helmchart.labels" . | nindent 4 }}
14+
{{- with .Values.ingress.annotations }}
15+
annotations:
16+
{{- toYaml . | nindent 4 }}
17+
{{- end }}
18+
spec:
19+
{{- if .Values.ingress.tls }}
20+
tls:
21+
{{- range .Values.ingress.tls }}
22+
- hosts:
23+
{{- range .hosts }}
24+
- {{ . | quote }}
25+
{{- end }}
26+
secretName: {{ .secretName }}
27+
{{- end }}
28+
{{- end }}
29+
rules:
30+
{{- range .Values.ingress.hosts }}
31+
- host: {{ .host | quote }}
32+
http:
33+
paths:
34+
{{- range .paths }}
35+
- path: {{ . }}
36+
backend:
37+
serviceName: {{ $fullName }}
38+
servicePort: {{ $svcPort }}
39+
{{- end }}
40+
{{- end }}
41+
{{- end }}

0 commit comments

Comments
 (0)