Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of Helm Renderer #3929

Merged
Show file tree
Hide file tree
Changes from all commits
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
185 changes: 185 additions & 0 deletions integration/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,188 @@ spec:
})
}
}

func TestHelmRender(t *testing.T) {
if testing.Short() || RunOnGCP() {
t.Skip("skipping kind integration test")
}

tests := []struct {
description string
builds []build.Artifact
labels []deploy.Labeller
helmReleases []latest.HelmRelease
expectedOut string
}{
{
description: "Bare bones render",
builds: []build.Artifact{
{
ImageName: "gke-loadbalancer",
Tag: "gke-loadbalancer:test",
},
},
labels: []deploy.Labeller{},
helmReleases: []latest.HelmRelease{{
Name: "gke_loadbalancer",
ChartPath: "testdata/gke_loadbalancer/loadbalancer-helm",
ArtifactOverrides: map[string]string{
"image": "gke-loadbalancer",
},
}},
expectedOut: `---
# Source: loadbalancer-helm/templates/k8s.yaml
apiVersion: v1
kind: Service
metadata:
name: gke-loadbalancer
labels:
app: gke-loadbalancer
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
protocol: TCP
name: http
selector:
app: "gke-loadbalancer"
---
# Source: loadbalancer-helm/templates/k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: gke-loadbalancer
labels:
app: gke-loadbalancer
spec:
replicas: 1
selector:
matchLabels:
app: gke-loadbalancer
template:
metadata:
labels:
app: gke-loadbalancer
spec:
containers:
- name: gke-container
image: gke-loadbalancer:test
ports:
- containerPort: 3000

`,
},
{
description: "A more complex template",
builds: []build.Artifact{
{
ImageName: "gcr.io/k8s-skaffold/skaffold-helm",
Tag: "gcr.io/k8s-skaffold/skaffold-helm:sha256-nonsenslettersandnumbers",
},
},
labels: []deploy.Labeller{},
helmReleases: []latest.HelmRelease{{
Name: "skaffold-helm",
ChartPath: "testdata/helm/skaffold-helm",
ArtifactOverrides: map[string]string{
"image": "gcr.io/k8s-skaffold/skaffold-helm",
},
}},
expectedOut: `---
# Source: skaffold-helm/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: skaffold-helm-skaffold-helm
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Helm
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app: skaffold-helm
release: skaffold-helm
---
# Source: skaffold-helm/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: skaffold-helm
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Helm
spec:
replicas: 1
selector:
matchLabels:
app: skaffold-helm
release: skaffold-helm
template:
metadata:
labels:
app: skaffold-helm
release: skaffold-helm
spec:
containers:
- name: skaffold-helm
image: gcr.io/k8s-skaffold/skaffold-helm:sha256-nonsenslettersandnumbers
imagePullPolicy:
ports:
- containerPort: 80
resources:
{}
---
# Source: skaffold-helm/templates/ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: skaffold-helm-skaffold-helm
labels:
app: skaffold-helm
chart: skaffold-helm-0.1.0
release: skaffold-helm
heritage: Helm
annotations:
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: skaffold-helm-skaffold-helm
servicePort: 80

`,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
deployer := deploy.NewHelmDeployer(&runcontext.RunContext{
Cfg: latest.Pipeline{
Deploy: latest.DeployConfig{
DeployType: latest.DeployType{
HelmDeploy: &latest.HelmDeploy{
Releases: test.helmReleases,
},
},
},
},
})
var b bytes.Buffer
err := deployer.Render(context.Background(), &b, test.builds, test.labels, "")

t.CheckNoError(err)
t.CheckDeepEqual(test.expectedOut, b.String())
})
}
}
93 changes: 83 additions & 10 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -53,10 +52,13 @@ import (

var (
// versionRegex extracts version from "helm version --client", for instance: "2.14.0-rc.2"
versionRegex = regexp.MustCompile(`\"v(\d[\w\.\-\.]+)`)
versionRegex = regexp.MustCompile(`v(\d[\w.\-]+)`)
balopat marked this conversation as resolved.
Show resolved Hide resolved

// helm3Version represents the version cut-off for helm3 behavior
helm3Version = semver.MustParse("3.0.0-beta.0")

// error to throw when helm version can't be determined
versionErrorString = "failed to determine binary version: %w"
tete17 marked this conversation as resolved.
Show resolved Hide resolved
)

// HelmDeployer deploys workflows using the helm CLI
Expand Down Expand Up @@ -99,7 +101,7 @@ func (h *HelmDeployer) Deploy(ctx context.Context, out io.Writer, builds []build

hv, err := h.binVer(ctx)
if err != nil {
return NewDeployErrorResult(fmt.Errorf("failed to determine binary version: %w", err))
return NewDeployErrorResult(fmt.Errorf(versionErrorString, err))
}
logrus.Infof("Deploying with helm v%s ...", hv)

Expand Down Expand Up @@ -129,10 +131,10 @@ func (h *HelmDeployer) Deploy(ctx context.Context, out io.Writer, builds []build

// Let's make sure that every image tag is set with `--set`.
// Otherwise, templates have no way to use the images that were built.
for _, build := range builds {
if !valuesSet[build.Tag] {
warnings.Printf("image [%s] is not used.", build.Tag)
warnings.Printf("image [%s] is used instead.", build.ImageName)
for _, b := range builds {
if !valuesSet[b.Tag] {
warnings.Printf("image [%s] is not used.", b.Tag)
warnings.Printf("image [%s] is used instead.", b.ImageName)
warnings.Printf("See helm sample for how to replace image names with their actual tags: https://github.com/GoogleContainerTools/skaffold/blob/master/examples/helm-deployment/skaffold.yaml")
}
}
Expand Down Expand Up @@ -215,7 +217,7 @@ func (h *HelmDeployer) Dependencies() ([]string, error) {
func (h *HelmDeployer) Cleanup(ctx context.Context, out io.Writer) error {
hv, err := h.binVer(ctx)
if err != nil {
return fmt.Errorf("binary version: %w", err)
return fmt.Errorf(versionErrorString, err)
}

for _, r := range h.Releases {
Expand Down Expand Up @@ -245,8 +247,79 @@ func (h *HelmDeployer) Cleanup(ctx context.Context, out io.Writer) error {
}

// Render generates the Kubernetes manifests and writes them out
func (h *HelmDeployer) Render(context.Context, io.Writer, []build.Artifact, []Labeller, string) error {
return errors.New("not yet implemented")
func (h *HelmDeployer) Render(ctx context.Context, out io.Writer, builds []build.Artifact, labellers []Labeller, filepath string) error {
tete17 marked this conversation as resolved.
Show resolved Hide resolved
hv, err := h.binVer(ctx)
if err != nil {
return fmt.Errorf(versionErrorString, err)
}

renderedManifests := new(bytes.Buffer)

for _, r := range h.Releases {
args := []string{"template", r.ChartPath}

if hv.GTE(helm3Version) {
// Helm 3 requires the name to be before the chart path
args = append(args[:1], append([]string{r.Name}, args[1:]...)...)
} else {
args = append(args, "--name", r.Name)
}

for _, vf := range r.ValuesFiles {
args = append(args, "--values", vf)
}

params, err := pairParamsToArtifacts(builds, r.ArtifactOverrides)
if err != nil {
return fmt.Errorf("matching build results to chart values: %w", err)
}

for k, v := range params {
var value string

cfg := r.ImageStrategy.HelmImageConfig.HelmConventionConfig

value, err = imageSetFromConfig(cfg, k, v.Tag)
if err != nil {
return err
}

args = append(args, "--set-string", value)
}

sortedKeys := make([]string, 0, len(r.SetValues))
for k := range r.SetValues {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
for _, k := range sortedKeys {
args = append(args, "--set", fmt.Sprintf("%s=%s", k, r.SetValues[k]))
}

sortedKeys = make([]string, 0, len(r.SetValueTemplates))
for k := range r.SetValueTemplates {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)

for _, key := range sortedKeys {
v, err := util.ExpandEnvTemplate(r.SetValueTemplates[key], nil)
if err != nil {
return err
}
args = append(args, "--set", fmt.Sprintf("%s=%s", key, v))
}

if r.Namespace != "" {
args = append(args, "--namespace", r.Namespace)
}
tete17 marked this conversation as resolved.
Show resolved Hide resolved

if err := h.exec(ctx, renderedManifests, false, args...); err != nil {
return err
}
}

return outputRenderedManifests(renderedManifests.String(), filepath, out)
}

// exec executes the helm command, writing combined stdout/stderr to the provided writer
Expand Down
Loading