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

Make IMAGE_REPO and IMAGE_TAG templated values in custom build and helm deploy #4278

Merged
merged 9 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
79 changes: 79 additions & 0 deletions integration/examples/templated-fields/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
### Example: use image values in templated fields for build and deploy

This example shows how `IMAGE_REPO` and `IMAGE_TAG` keywords are available in templated fields for custom build and helm deploy

* **building** a single Go file app with ko
* **tagging** using the default tagPolicy (`gitCommit`)
* **deploying** two replica container pods using `helm`

#### Before you begin

For this tutorial to work, you will need to have Skaffold, Helm and a Kubernetes cluster set up.
To learn more about how to set up, see the [getting started docs](https://skaffold.dev/docs/getting-started).

#### Tutorial

This tutorial will demonstrate how Skaffold can inject image repo and image tag values into the build and deploy stanzas.

First, clone the Skaffold [repo](https://github.com/GoogleContainerTools/skaffold) and navigate to the [templated-fields example](https://github.com/GoogleContainerTools/skaffold/tree/master/examples/templated-fields) for sample code:

```shell
$ git clone https://github.com/GoogleContainerTools/skaffold
$ cd skaffold/examples/templated-fields
```

`IMAGE_REPO` and `IMAGE_TAG` are available as templated fields in `build.sh` file

```shell
#from build.sh, line 13
img="${IMAGE_REPO}:${IMAGE_TAG}"
```

and also in the `helm` deploy section of the skaffold config, which configures artifact `skaffold-templated` to build with `build.sh`:

```yaml
// from skaffold.yaml, line 22-24
setValueTemplates:
imageRepo: "{{.IMAGE_REPO}}"
imageTag: "{{.IMAGE_TAG}}"
```
These values are then being set as container environment variables `FOO_IMAGE_REPO` and `FOO_IMAGE_TAG` in the helm template `deployment.yaml` file, just as an example to show how they can be added to your helm templates.

```yaml
// from charts/templates/deployment.yaml, line 16-24
containers:
- name: {{ .Chart.Name }}
image: {{ .Values.image }}
env:
- name: FOO_IMAGE_REPO
value: {{ .Values.imageRepo }}
- name: FOO_IMAGE_TAG
value: {{ .Values.imageTag }}
```

For more information about how this works, see the Skaffold [custom builder](https://skaffold.dev/docs/how-tos/builders/#custom-build-script-run-locally) and [helm](https://skaffold.dev/docs/pipeline-stages/deployers/helm/) documentation.

Now, use Skaffold to deploy this application to your Kubernetes cluster:

```shell
$ skaffold run --tail --default-repo <your repo>
```

With this command, Skaffold will build the `skaffold-templated` artifact with ko and deploy the application to Kubernetes using helm.
You should be able to see something like:
```shell
Running image skaffold-templated:a866d5efd634062ea74662b20e172cd6e2d645f9f33f929bfaf8e856ec66bd94
```
printed every second in the Skaffold logs, since the code being executed is `main.go`.
```go
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
// from main.go, line 12
fmt.Printf("Running image %v:%v\n", os.Getenv("FOO_IMAGE_REPO"), os.Getenv("FOO_IMAGE_TAG"))
```

#### Cleanup

To clean up your Kubernetes cluster, run:

```shell
$ skaffold delete
```
17 changes: 17 additions & 0 deletions integration/examples/templated-fields/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -e

if ! [ -x "$(command -v ko)" ]; then
pushd $(mktemp -d)
go mod init tmp; GOFLAGS= go get github.com/google/ko/cmd/[email protected]
popd
fi

output=$(ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo $output | tail -n1)

img="${IMAGE_REPO}:${IMAGE_TAG}"
docker tag $ref $img
if $PUSH_IMAGE; then
docker push $img
fi
4 changes: 4 additions & 0 deletions integration/examples/templated-fields/charts/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
description: Skaffold example with Helm
name: skaffold-helm
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Chart.Name }}
labels:
app: {{ .Chart.Name }}
spec:
selector:
matchLabels:
app: {{ .Chart.Name }}
replicas: {{ .Values.replicaCount }}
template:
metadata:
labels:
app: {{ .Chart.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: {{ .Values.image }}
env:
- name: FOO_IMAGE_REPO
value: {{ .Values.imageRepo }}
- name: FOO_IMAGE_TAG
value: {{ .Values.imageTag }}
4 changes: 4 additions & 0 deletions integration/examples/templated-fields/charts/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Default values for skaffold-helm.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 2
3 changes: 3 additions & 0 deletions integration/examples/templated-fields/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/GoogleContainerTools/skaffold/examples/template-fields

go 1.13
15 changes: 15 additions & 0 deletions integration/examples/templated-fields/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"os"
"time"
)

func main() {
for {
// FOO_IMAGE_REPO and FOO_IMAGE_TAG are defined in the Helm chart using Skaffold's templated IMAGE_REPO and IMAGE_TAG
fmt.Printf("Running image %v:%v\n", os.Getenv("FOO_IMAGE_REPO"), os.Getenv("FOO_IMAGE_TAG"))
time.Sleep(time.Second * 1)
}
}
24 changes: 24 additions & 0 deletions integration/examples/templated-fields/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: skaffold/v2beta5
kind: Config
metadata:
name: my-app

build:
artifacts:
- image: skaffold-templated
custom:
buildCommand: "./build.sh"
dependencies:
paths:
- "go.mod"
- "**.go"
deploy:
helm:
releases:
- name: skaffold-templated
chartPath: charts
artifactOverrides:
image: skaffold-templated
setValueTemplates:
imageRepo: "{{.IMAGE_REPO}}"
imageTag: "{{.IMAGE_TAG}}"
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions pkg/skaffold/build/custom/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)
Expand Down Expand Up @@ -95,6 +96,16 @@ func (b *Builder) retrieveEnv(a *latest.Artifact, tag string) ([]string, error)
fmt.Sprintf("%s=%t", constants.PushImage, b.pushImages),
fmt.Sprintf("%s=%s", constants.BuildContext, buildContext),
}

ref, err := docker.ParseReference(tag)
if err != nil {
return nil, fmt.Errorf("parsing image %v: %w", tag, err)
}

// Standardize access to Image reference fields in templates
envs = append(envs, fmt.Sprintf("%s=%s", constants.ImageRef.Repo, ref.BaseName))
envs = append(envs, fmt.Sprintf("%s=%s", constants.ImageRef.Tag, ref.Tag))

envs = append(envs, b.additionalEnv...)
envs = append(envs, util.OSEnviron()...)
return envs, nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/skaffold/build/custom/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ func TestRetrieveEnv(t *testing.T) {
tag: "gcr.io/image/tag:mytag",
environ: nil,
buildContext: "/some/path",
expected: []string{"IMAGE=gcr.io/image/tag:mytag", "IMAGES=gcr.io/image/tag:mytag", "PUSH_IMAGE=false", "BUILD_CONTEXT=/some/path"},
expected: []string{"IMAGE=gcr.io/image/tag:mytag", "IMAGES=gcr.io/image/tag:mytag", "PUSH_IMAGE=false", "BUILD_CONTEXT=/some/path", "IMAGE_REPO=gcr.io/image/tag", "IMAGE_TAG=mytag"},
}, {
description: "make sure environ is correctly applied",
tag: "gcr.io/image/tag:anothertag",
environ: []string{"PATH=/path", "HOME=/root"},
buildContext: "/some/path",
expected: []string{"IMAGE=gcr.io/image/tag:anothertag", "IMAGES=gcr.io/image/tag:anothertag", "PUSH_IMAGE=false", "BUILD_CONTEXT=/some/path", "PATH=/path", "HOME=/root"},
expected: []string{"IMAGE=gcr.io/image/tag:anothertag", "IMAGES=gcr.io/image/tag:anothertag", "PUSH_IMAGE=false", "BUILD_CONTEXT=/some/path", "IMAGE_REPO=gcr.io/image/tag", "IMAGE_TAG=anothertag", "PATH=/path", "HOME=/root"},
}, {
description: "push image is true",
tag: "gcr.io/image/push:tag",
pushImages: true,
expected: []string{"IMAGE=gcr.io/image/push:tag", "IMAGES=gcr.io/image/push:tag", "PUSH_IMAGE=true", "BUILD_CONTEXT="},
expected: []string{"IMAGE=gcr.io/image/push:tag", "IMAGES=gcr.io/image/push:tag", "PUSH_IMAGE=true", "BUILD_CONTEXT=", "IMAGE_REPO=gcr.io/image/push", "IMAGE_TAG=tag"},
}, {
description: "add additional env",
tag: "gcr.io/image/push:tag",
pushImages: true,
additionalEnv: []string{"KUBECONTEXT=mycluster"},
expected: []string{"IMAGE=gcr.io/image/push:tag", "IMAGES=gcr.io/image/push:tag", "PUSH_IMAGE=true", "BUILD_CONTEXT=", "KUBECONTEXT=mycluster"},
expected: []string{"IMAGE=gcr.io/image/push:tag", "IMAGES=gcr.io/image/push:tag", "PUSH_IMAGE=true", "BUILD_CONTEXT=", "IMAGE_REPO=gcr.io/image/push", "IMAGE_TAG=tag", "KUBECONTEXT=mycluster"},
},
}
for _, test := range tests {
Expand Down Expand Up @@ -98,8 +98,8 @@ func TestRetrieveCmd(t *testing.T) {
},
},
tag: "image:tag",
expected: expectedCmd("workspace", "sh", []string{"-c", "./build.sh"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=workspace"}),
expectedOnWindows: expectedCmd("workspace", "cmd.exe", []string{"/C", "./build.sh"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=workspace"}),
expected: expectedCmd("workspace", "sh", []string{"-c", "./build.sh"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=workspace", "IMAGE_REPO=image", "IMAGE_TAG=tag"}),
expectedOnWindows: expectedCmd("workspace", "cmd.exe", []string{"/C", "./build.sh"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=workspace", "IMAGE_REPO=image", "IMAGE_TAG=tag"}),
},
{
description: "buildcommand with multiple args",
Expand All @@ -111,8 +111,8 @@ func TestRetrieveCmd(t *testing.T) {
},
},
tag: "image:tag",
expected: expectedCmd("", "sh", []string{"-c", "./build.sh --flag=$IMAGES --anotherflag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT="}),
expectedOnWindows: expectedCmd("", "cmd.exe", []string{"/C", "./build.sh --flag=$IMAGES --anotherflag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT="}),
expected: expectedCmd("", "sh", []string{"-c", "./build.sh --flag=$IMAGES --anotherflag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=", "IMAGE_REPO=image", "IMAGE_TAG=tag"}),
expectedOnWindows: expectedCmd("", "cmd.exe", []string{"/C", "./build.sh --flag=$IMAGES --anotherflag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=", "IMAGE_REPO=image", "IMAGE_TAG=tag"}),
},
{
description: "buildcommand with go template",
Expand All @@ -125,8 +125,8 @@ func TestRetrieveCmd(t *testing.T) {
},
tag: "image:tag",
env: []string{"FLAG=some-flag"},
expected: expectedCmd("", "sh", []string{"-c", "./build.sh --flag=some-flag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=", "FLAG=some-flag"}),
expectedOnWindows: expectedCmd("", "cmd.exe", []string{"/C", "./build.sh --flag=some-flag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=", "FLAG=some-flag"}),
expected: expectedCmd("", "sh", []string{"-c", "./build.sh --flag=some-flag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=", "IMAGE_REPO=image", "IMAGE_TAG=tag", "FLAG=some-flag"}),
expectedOnWindows: expectedCmd("", "cmd.exe", []string{"/C", "./build.sh --flag=some-flag"}, []string{"IMAGE=image:tag", "IMAGES=image:tag", "PUSH_IMAGE=false", "BUILD_CONTEXT=", "IMAGE_REPO=image", "IMAGE_TAG=tag", "FLAG=some-flag"}),
},
}
for _, test := range tests {
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ var (
Timeout = "TIMEOUT"
)

var ImageRef = struct {
Repo string
Tag string
Digest string
}{
Repo: "IMAGE_REPO",
Tag: "IMAGE_TAG",
Digest: "IMAGE_DIGEST",
}
var DefaultKubectlManifests = []string{"k8s/*.yaml"}

var Labels = struct {
Expand Down
59 changes: 30 additions & 29 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,9 @@ func (h *HelmDeployer) Render(ctx context.Context, out io.Writer, builds []build
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))
args, err = constructOverrideArgs(&r, builds, args, func(string) {})
if err != nil {
return err
}

if r.Namespace != "" {
Expand Down Expand Up @@ -554,18 +536,34 @@ func installArgs(r latest.HelmRelease, builds []build.Artifact, valuesSet map[st
args = append(args, "--set-string", value)
}

args, err = constructOverrideArgs(&r, builds, args, func(k string) {
Copy link
Contributor Author

@gsquared94 gsquared94 Jun 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tstromberg currently skaffold render for helm passes empty values for image.name and image.tag. Also it skips over --set-file. Shouldn't deploy and render lead to identical kubernetes yaml files? That's what I assumed and extracted the common constructOverrideArgs method. Let me know if that shouldn't be the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems logical to me that deploy and render should yield identical manifests, but there are likely corner cases to be considered. @nkubala - thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some upcoming features to render that will shift the paradigms a bit from the conventional deploy flow, but for the most part they should be going through the same code paths so this is fine.

valuesSet[k] = true
})
if err != nil {
return nil, err
}

if r.Wait {
args = append(args, "--wait")
}

return args, nil
}

// constructOverrideArgs creates the command line arguments for overrides
func constructOverrideArgs(r *latest.HelmRelease, builds []build.Artifact, args []string, record func(string)) ([]string, error) {
sortedKeys := make([]string, 0, len(r.SetValues))
for k := range r.SetValues {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
for _, k := range sortedKeys {
valuesSet[r.SetValues[k]] = true
record(r.SetValues[k])
args = append(args, "--set", fmt.Sprintf("%s=%s", k, r.SetValues[k]))
}

for k, v := range r.SetFiles {
valuesSet[v] = true
record(v)
args = append(args, "--set-file", fmt.Sprintf("%s=%s", k, v))
}

Expand Down Expand Up @@ -593,7 +591,7 @@ func installArgs(r latest.HelmRelease, builds []build.Artifact, valuesSet map[st
return nil, err
}

valuesSet[v] = true
record(v)
args = append(args, "--set", fmt.Sprintf("%s=%s", k, v))
}

Expand All @@ -610,11 +608,6 @@ func installArgs(r latest.HelmRelease, builds []build.Artifact, valuesSet map[st

args = append(args, "-f", exp)
}

if r.Wait {
args = append(args, "--wait")
}

return args, nil
}

Expand All @@ -637,6 +630,14 @@ func envVarForImage(imageName string, digest string) map[string]string {
"DIGEST": digest, // The `DIGEST` name is kept for compatibility reasons
}

// Standardize access to Image reference fields in templates
ref, err := docker.ParseReference(digest)
if err == nil {
customMap[constants.ImageRef.Repo] = ref.BaseName
customMap[constants.ImageRef.Tag] = ref.Tag
customMap[constants.ImageRef.Digest] = ref.Digest
}
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved

if digest == "" {
return customMap
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ func TestHelmRender(t *testing.T) {
shouldErr: false,
commands: testutil.
CmdRunWithOutput("helm version", version31).
AndRun("helm --kube-context kubecontext template skaffold-helm examples/test --set-string image=skaffold-helm:tag1 --set image.name=<no value> --set image.tag=<no value> --set missing.key=<no value> --set other.key=<no value> --set some.key=somevalue --kubeconfig kubeconfig"),
AndRun("helm --kube-context kubecontext template skaffold-helm examples/test --set-string image=skaffold-helm:tag1 --set image.name=skaffold-helm --set image.tag=skaffold-helm:tag1 --set missing.key=<no value> --set other.key=<no value> --set some.key=somevalue --kubeconfig kubeconfig"),
runContext: makeRunContext(testDeployConfigTemplated, false),
builds: []build.Artifact{
{
Expand Down