Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/app/piped/cloudprovider/kubernetes/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewHelm(version, path string, logger *zap.Logger) *Helm {
}
}

func (c *Helm) TemplateLocalChart(ctx context.Context, appName, appDir, chartPath string, opts *config.InputHelmOptions) (string, error) {
func (c *Helm) TemplateLocalChart(ctx context.Context, appName, appDir, namespace string, chartPath string, opts *config.InputHelmOptions) (string, error) {
releaseName := appName
if opts != nil && opts.ReleaseName != "" {
releaseName = opts.ReleaseName
Expand All @@ -57,6 +57,11 @@ func (c *Helm) TemplateLocalChart(ctx context.Context, appName, appDir, chartPat
releaseName,
chartPath,
}

if namespace != "" {
args = append(args, fmt.Sprintf("--namespace=%s", namespace))
}

if opts != nil {
for _, v := range opts.ValueFiles {
args = append(args, "-f", v)
Expand Down Expand Up @@ -88,7 +93,7 @@ type helmRemoteGitChart struct {
Path string
}

func (c *Helm) TemplateRemoteGitChart(ctx context.Context, appName, appDir string, chart helmRemoteGitChart, gitClient gitClient, opts *config.InputHelmOptions) (string, error) {
func (c *Helm) TemplateRemoteGitChart(ctx context.Context, appName, appDir, namespace string, chart helmRemoteGitChart, gitClient gitClient, opts *config.InputHelmOptions) (string, error) {
// Firstly, we need to download the remote repositoy.
repoDir, err := ioutil.TempDir("", "helm-remote-chart")
if err != nil {
Expand All @@ -109,7 +114,7 @@ func (c *Helm) TemplateRemoteGitChart(ctx context.Context, appName, appDir strin
chartPath := filepath.Join(repoDir, chart.Path)

// After that handle it as a local chart.
return c.TemplateLocalChart(ctx, appName, appDir, chartPath, opts)
return c.TemplateLocalChart(ctx, appName, appDir, namespace, chartPath, opts)
}

type helmRemoteChart struct {
Expand All @@ -118,7 +123,7 @@ type helmRemoteChart struct {
Version string
}

func (c *Helm) TemplateRemoteChart(ctx context.Context, appName, appDir string, chart helmRemoteChart, opts *config.InputHelmOptions) (string, error) {
func (c *Helm) TemplateRemoteChart(ctx context.Context, appName, appDir, namespace string, chart helmRemoteChart, opts *config.InputHelmOptions) (string, error) {
releaseName := appName
if opts != nil && opts.ReleaseName != "" {
releaseName = opts.ReleaseName
Expand All @@ -131,6 +136,11 @@ func (c *Helm) TemplateRemoteChart(ctx context.Context, appName, appDir string,
fmt.Sprintf("%s/%s", chart.Repository, chart.Name),
fmt.Sprintf("--version=%s", chart.Version),
}

if namespace != "" {
args = append(args, fmt.Sprintf("--namespace=%s", namespace))
}

if opts != nil {
for _, v := range opts.ValueFiles {
args = append(args, "-f", v)
Expand Down
29 changes: 28 additions & 1 deletion pkg/app/piped/cloudprovider/kubernetes/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,37 @@ func TestTemplateLocalChart(t *testing.T) {
require.NoError(t, err)

helm := NewHelm("", helmPath, zap.NewNop())
out, err := helm.TemplateLocalChart(ctx, appName, appDir, chartPath, nil)
out, err := helm.TemplateLocalChart(ctx, appName, appDir, "", chartPath, nil)
require.NoError(t, err)

out = strings.TrimPrefix(out, "---")
manifests := strings.Split(out, "---")
assert.Equal(t, 3, len(manifests))
}

func TestTemplateLocalChart_WithNamespace(t *testing.T) {
var (
ctx = context.Background()
appName = "testapp"
appDir = "testdata"
chartPath = "testchart"
namespace = "testnamespace"
)

// TODO: Preinstall a helm version inside CI runner to avoid installing.
helmPath, _, err := toolregistry.DefaultRegistry().Helm(ctx, "")
require.NoError(t, err)

helm := NewHelm("", helmPath, zap.NewNop())
out, err := helm.TemplateLocalChart(ctx, appName, appDir, namespace, chartPath, nil)
require.NoError(t, err)

out = strings.TrimPrefix(out, "---")

manifests, _ := ParseManifests(out)
for _, manifest := range manifests {
metadata, err := manifest.GetNestedMap("metadata")
require.NoError(t, err)
require.Equal(t, namespace, metadata["namespace"])
}
}
22 changes: 19 additions & 3 deletions pkg/app/piped/cloudprovider/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,34 @@ func (p *provider) LoadManifests(ctx context.Context) (manifests []Manifest, err
Ref: p.input.HelmChart.Ref,
Path: p.input.HelmChart.Path,
}
data, err = p.helm.TemplateRemoteGitChart(ctx, p.appName, p.appDir, chart, sharedGitClient, p.input.HelmOptions)
data, err = p.helm.TemplateRemoteGitChart(ctx,
p.appName,
p.appDir,
p.input.Namespace,
chart,
sharedGitClient,
p.input.HelmOptions)

case p.input.HelmChart.Repository != "":
chart := helmRemoteChart{
Repository: p.input.HelmChart.Repository,
Name: p.input.HelmChart.Name,
Version: p.input.HelmChart.Version,
}
data, err = p.helm.TemplateRemoteChart(ctx, p.appName, p.appDir, chart, p.input.HelmOptions)
data, err = p.helm.TemplateRemoteChart(ctx,
p.appName,
p.appDir,
p.input.Namespace,
chart,
p.input.HelmOptions)

default:
data, err = p.helm.TemplateLocalChart(ctx, p.appName, p.appDir, p.input.HelmChart.Path, p.input.HelmOptions)
data, err = p.helm.TemplateLocalChart(ctx,
p.appName,
p.appDir,
p.input.Namespace,
p.input.HelmChart.Path,
p.input.HelmOptions)
}

if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ func (m Manifest) GetNestedStringMap(fields ...string) (map[string]string, error
return sm, nil
}

func (m Manifest) GetNestedMap(fields ...string) (map[string]interface{}, error) {
sm, _, err := unstructured.NestedMap(m.u.Object, fields...)
if err != nil {
return nil, err
}

return sm, nil
}

// AddStringMapValues adds or overrides the given key-values into the string map
// that can be found at the specified fields.
func (m Manifest) AddStringMapValues(values map[string]string, fields ...string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ metadata:
name: {{ include "testchart.fullname" . }}
labels:
{{- include "testchart.labels" . | nindent 4 }}
namespace: {{.Release.Namespace}}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ metadata:
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
namespace: {{.Release.Namespace}}
spec:
{{- if .Values.ingress.tls }}
tls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ metadata:
name: {{ include "testchart.fullname" . }}
labels:
{{- include "testchart.labels" . | nindent 4 }}
namespace: {{.Release.Namespace}}
spec:
type: {{ .Values.service.type }}
ports:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "testchart.serviceAccountName" . }}
namespace: {{.Release.Namespace}}
labels:
{{- include "testchart.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
Expand Down