diff --git a/pkg/skaffold/deploy/deploy.go b/pkg/skaffold/deploy/deploy.go index ddb225a3429..023f3d522a9 100644 --- a/pkg/skaffold/deploy/deploy.go +++ b/pkg/skaffold/deploy/deploy.go @@ -50,7 +50,7 @@ type Deployer interface { Cleanup(context.Context, io.Writer) error } -func JoinTagsToBuildResult(builds []build.Artifact, params map[string]string) (map[string]build.Artifact, error) { +func joinTagsToBuildResult(builds []build.Artifact, params map[string]string) (map[string]build.Artifact, error) { imageToBuildResult := map[string]build.Artifact{} for _, build := range builds { imageToBuildResult[build.ImageName] = build diff --git a/pkg/skaffold/deploy/helm.go b/pkg/skaffold/deploy/helm.go index 1cf76b41863..226e6daf025 100644 --- a/pkg/skaffold/deploy/helm.go +++ b/pkg/skaffold/deploy/helm.go @@ -111,7 +111,7 @@ func (h *HelmDeployer) deployRelease(out io.Writer, r v1alpha2.HelmRelease, buil fmt.Fprintf(out, "Helm release %s not installed. Installing...\n", releaseName) isInstalled = false } - params, err := JoinTagsToBuildResult(builds, r.Values) + params, err := joinTagsToBuildResult(builds, r.Values) if err != nil { return nil, errors.Wrap(err, "matching build results to chart values") } diff --git a/pkg/skaffold/deploy/helm_test.go b/pkg/skaffold/deploy/helm_test.go index d71002163d1..b95ec68fea3 100644 --- a/pkg/skaffold/deploy/helm_test.go +++ b/pkg/skaffold/deploy/helm_test.go @@ -20,9 +20,14 @@ import ( "bytes" "context" "fmt" + "io" + "io/ioutil" + "os" "os/exec" "testing" + "github.com/sirupsen/logrus" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" @@ -36,6 +41,13 @@ var testBuilds = []build.Artifact{ }, } +var testBuildsFoo = []build.Artifact{ + { + ImageName: "foo", + Tag: "foo:3605e7bc17cf46e53f4d81c4cbc24e5b4c495184", + }, +} + var testDeployConfig = &v1alpha2.DeployConfig{ DeployType: v1alpha2.DeployType{ HelmDeploy: &v1alpha2.HelmDeploy{ @@ -74,6 +86,26 @@ var testDeployConfigParameterUnmatched = &v1alpha2.DeployConfig{ }, } +var testDeployFooWithPackaged = &v1alpha2.DeployConfig{ + DeployType: v1alpha2.DeployType{ + HelmDeploy: &v1alpha2.HelmDeploy{ + Releases: []v1alpha2.HelmRelease{ + { + Name: "foo", + ChartPath: "testdata/foo", + Values: map[string]string{ + "image.tag": "foo", + }, + Packaged: &v1alpha2.HelmPackaged{ + Version: "0.1.2", + AppVersion: "1.2.3", + }, + }, + }, + }, + }, +} + var testNamespace = "testNamespace" var validDeployYaml = ` @@ -154,6 +186,12 @@ HOOKS: MANIFEST: ` +// TestMain disables logrus output before running tests. +func TestMain(m *testing.M) { + logrus.SetOutput(ioutil.Discard) + os.Exit(m.Run()) +} + func TestHelmDeploy(t *testing.T) { var tests = []struct { description string @@ -214,6 +252,34 @@ func TestHelmDeploy(t *testing.T) { deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace), builds: testBuilds, }, + { + description: "should package chart and deploy", + cmd: &MockHelm{ + t: t, + packageOut: bytes.NewBufferString("Packaged to " + os.TempDir() + "foo-0.1.2.tgz"), + }, + shouldErr: false, + deployer: NewHelmDeployer( + testDeployFooWithPackaged, + testKubeContext, + testNamespace, + ), + builds: testBuildsFoo, + }, + { + description: "should fail to deploy when packaging fails", + cmd: &MockHelm{ + t: t, + packageResult: fmt.Errorf("packaging failed"), + }, + shouldErr: true, + deployer: NewHelmDeployer( + testDeployFooWithPackaged, + testKubeContext, + testNamespace, + ), + builds: testBuildsFoo, + }, } for _, tt := range tests { @@ -234,6 +300,9 @@ type MockHelm struct { installResult error upgradeResult error depResult error + + packageOut io.Reader + packageResult error } func (m *MockHelm) RunCmdOut(c *exec.Cmd) ([]byte, error) { @@ -259,6 +328,13 @@ func (m *MockHelm) RunCmd(c *exec.Cmd) error { return m.upgradeResult case "dep": return m.depResult + case "package": + if m.packageOut != nil { + if _, err := io.Copy(c.Stdout, m.packageOut); err != nil { + m.t.Errorf("Failed to copy stdout") + } + } + return m.packageResult default: m.t.Errorf("Unknown helm command: %+v", c) return nil diff --git a/pkg/skaffold/deploy/testdata/foo/.helmignore b/pkg/skaffold/deploy/testdata/foo/.helmignore new file mode 100644 index 00000000000..f0c13194444 --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/.helmignore @@ -0,0 +1,21 @@ +# 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 diff --git a/pkg/skaffold/deploy/testdata/foo/Chart.yaml b/pkg/skaffold/deploy/testdata/foo/Chart.yaml new file mode 100644 index 00000000000..61fd32d3e1f --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart for Kubernetes +name: foo +version: 0.1.0 diff --git a/pkg/skaffold/deploy/testdata/foo/templates/NOTES.txt b/pkg/skaffold/deploy/testdata/foo/templates/NOTES.txt new file mode 100644 index 00000000000..c338f2f9642 --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/templates/NOTES.txt @@ -0,0 +1,19 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range .Values.ingress.hosts }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ . }}{{ $.Values.ingress.path }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "foo.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 {{ template "foo.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "foo.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={{ template "foo.name" . }},release={{ .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 }} diff --git a/pkg/skaffold/deploy/testdata/foo/templates/_helpers.tpl b/pkg/skaffold/deploy/testdata/foo/templates/_helpers.tpl new file mode 100644 index 00000000000..58c26bd440e --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/templates/_helpers.tpl @@ -0,0 +1,32 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "foo.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 "foo.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 "foo.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/pkg/skaffold/deploy/testdata/foo/templates/deployment.yaml b/pkg/skaffold/deploy/testdata/foo/templates/deployment.yaml new file mode 100644 index 00000000000..0900a4c4e3d --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/templates/deployment.yaml @@ -0,0 +1,51 @@ +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: {{ template "foo.fullname" . }} + labels: + app: {{ template "foo.name" . }} + chart: {{ template "foo.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + app: {{ template "foo.name" . }} + release: {{ .Release.Name }} + template: + metadata: + labels: + app: {{ template "foo.name" . }} + release: {{ .Release.Name }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + 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 | indent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: +{{ toYaml . | indent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: +{{ toYaml . | indent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: +{{ toYaml . | indent 8 }} + {{- end }} diff --git a/pkg/skaffold/deploy/testdata/foo/templates/ingress.yaml b/pkg/skaffold/deploy/testdata/foo/templates/ingress.yaml new file mode 100644 index 00000000000..3b6d07a48e2 --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/templates/ingress.yaml @@ -0,0 +1,39 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "foo.fullname" . -}} +{{- $servicePort := .Values.service.port -}} +{{- $ingressPath := .Values.ingress.path -}} +apiVersion: extensions/v1beta1 +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + app: {{ template "foo.name" . }} + chart: {{ template "foo.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} +{{- with .Values.ingress.annotations }} + annotations: +{{ toYaml . | indent 4 }} +{{- end }} +spec: +{{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} +{{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ . }} + http: + paths: + - path: {{ $ingressPath }} + backend: + serviceName: {{ $fullName }} + servicePort: http + {{- end }} +{{- end }} diff --git a/pkg/skaffold/deploy/testdata/foo/templates/service.yaml b/pkg/skaffold/deploy/testdata/foo/templates/service.yaml new file mode 100644 index 00000000000..acec2ab44b0 --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/templates/service.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ template "foo.fullname" . }} + labels: + app: {{ template "foo.name" . }} + chart: {{ template "foo.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + app: {{ template "foo.name" . }} + release: {{ .Release.Name }} diff --git a/pkg/skaffold/deploy/testdata/foo/values.yaml b/pkg/skaffold/deploy/testdata/foo/values.yaml new file mode 100644 index 00000000000..157f7f8c31d --- /dev/null +++ b/pkg/skaffold/deploy/testdata/foo/values.yaml @@ -0,0 +1,45 @@ +# Default values for foo. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + path: / + 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: {}