Skip to content

Commit

Permalink
chore: Update globstar syntax in examples dependencies (#6614)
Browse files Browse the repository at this point in the history
* feat(watch): Update globstar syntax in examples

This change updates our examples to demonstrate use of the globstar
syntax (`**`) in the `dependencies` section of Skaffold config.

Integration tests will fail for this change until #6605 is merged and
this change is rebased on that.

**Related:** #5933, #6029, #6041, #6350, #6605
**Merge after:** #6605

* chore: Use ko v0.9.3 in custom builder example

* fix: Ensure ko executable can be found

Update the build script in the `templated-fields` example to reference
the `ko` executable by path.

Also use [`embedmd`](https://github.com/halvards/embedmd) tags in
example `README.md` files.
  • Loading branch information
halvards committed Oct 14, 2021
1 parent 262eefe commit dd15c9a
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 65 deletions.
6 changes: 4 additions & 2 deletions integration/examples/custom-buildx/skaffold.yaml
@@ -1,12 +1,14 @@
apiVersion: skaffold/v2beta24
kind: Config

build:
artifacts:
- image: skaffold-examples-buildx
custom:
buildCommand: sh buildx.sh
dependencies:
paths: ["go.mod", "**.go", "buildx.sh"]
paths:
- "**/*.go"
- buildx.sh
- go.mod
tagPolicy:
sha256: {}
1 change: 1 addition & 0 deletions integration/examples/custom/.ko.yaml
@@ -0,0 +1 @@
defaultBaseImage: gcr.io/distroless/static:nonroot
54 changes: 31 additions & 23 deletions integration/examples/custom/README.md
Expand Up @@ -20,38 +20,45 @@ This tutorial will demonstrate how Skaffold can build a simple Hello World Go ap

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

```shell
$ git clone https://github.com/GoogleContainerTools/skaffold
$ cd skaffold/examples/custom
```sh
git clone https://github.com/GoogleContainerTools/skaffold.git
```
```sh
cd skaffold/examples/custom
```

Take a look at the `build.sh` file, which uses `ko` to containerize source code:

```shell
$ cat build.sh
[embedmd]:# (build.sh bash)
```bash
#!/usr/bin/env bash
set -e
set -x

if ! [ -x "$(command -v ko)" ]; then
if ! [ -x "$(go env GOPATH)/bin/ko" ]; then
pushd $(mktemp -d)
go mod init tmp; GOFLAGS= go get github.com/google/ko/cmd/ko@v0.6.0
curl -L https://github.com/google/ko/archive/v0.9.3.tar.gz | tar --strip-components 1 -zx
go build -o $(go env GOPATH)/bin/ko .
popd
fi

output=$(ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo $output | tail -n1)
output=$($(go env GOPATH)/bin/ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo "$output" | tail -n1)

docker tag $ref $IMAGE
if $PUSH_IMAGE; then
docker push $IMAGE
docker tag "$ref" "$IMAGE"
if [[ "${PUSH_IMAGE}" == "true" ]]; then
echo "Pushing $IMAGE"
docker push "$IMAGE"
else
echo "Not pushing $IMAGE"
fi
```

and the skaffold config, which configures image `ko://github.com/GoogleContainerTools/skaffold/examples/custom` to build with `build.sh`:

[embedmd]:# (skaffold.yaml yaml)
```yaml
$ cat skaffold.yaml
apiVersion: skaffold/v2beta9
apiVersion: skaffold/v2beta24
kind: Config
build:
artifacts:
Expand All @@ -60,32 +67,33 @@ build:
buildCommand: ./build.sh
dependencies:
paths:
- "go.mod"
- "**.go"
- "**/*.go"
- go.mod
- .ko.yaml
tagPolicy:
sha256: {}
```

The `k8s/pod.yaml` manifest file uses the same image reference:

[embedmd]:# (k8s/pod.yaml yaml)
```yaml
$ cat k8s/pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: getting-started
name: getting-started-custom
spec:
containers:
- name: getting-started
- name: getting-started-custom
image: ko://github.com/GoogleContainerTools/skaffold/examples/custom
```

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

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

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

With this command, Skaffold will build the `github.com/googlecontainertools/skaffold/examples/custom` artifact with ko and deploy the application to Kubernetes.
Expand All @@ -95,6 +103,6 @@ You should be able to see *Hello, World!* printed every second in the Skaffold l

To clean up your Kubernetes cluster, run:

```shell
$ skaffold delete
```sh
skaffold delete
```
3 changes: 2 additions & 1 deletion integration/examples/custom/build.sh
@@ -1,9 +1,10 @@
#!/usr/bin/env bash
set -e
set -x

if ! [ -x "$(go env GOPATH)/bin/ko" ]; then
pushd $(mktemp -d)
curl -L https://github.com/google/ko/archive/v0.8.3.tar.gz | tar --strip-components 1 -zx
curl -L https://github.com/google/ko/archive/v0.9.3.tar.gz | tar --strip-components 1 -zx
go build -o $(go env GOPATH)/bin/ko .
popd
fi
Expand Down
5 changes: 3 additions & 2 deletions integration/examples/custom/skaffold.yaml
Expand Up @@ -7,7 +7,8 @@ build:
buildCommand: ./build.sh
dependencies:
paths:
- "go.mod"
- "**.go"
- "**/*.go"
- go.mod
- .ko.yaml
tagPolicy:
sha256: {}
38 changes: 20 additions & 18 deletions integration/examples/templated-fields/README.md
@@ -1,4 +1,4 @@
### Example: use image values in templated fields for build and deploy
### Example: use image values in templated fields for build and deploy

[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleContainerTools/skaffold&cloudshell_open_in_editor=README.md&cloudshell_workspace=examples/templated-fields)

Expand All @@ -19,31 +19,33 @@ This tutorial will demonstrate how Skaffold can inject image repo and image tag

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

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

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

```shell
#from build.sh, line 13
[embedmd]:# (build.sh bash /^img=/ /$/)
```bash
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`:

[embedmd]:# (skaffold.yaml yaml /^.*setValueTemplates:/ /imageTag: .*$/)
```yaml
// from skaffold.yaml, line 22-24
setValueTemplates:
imageRepo: "{{.IMAGE_REPO}}"
imageTag: "{{.IMAGE_TAG}}"
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.

[embedmd]:# (charts/templates/deployment.yaml yaml /^.*containers:/ $)
```yaml
// from charts/templates/deployment.yaml, line 16-24
containers:
- name: {{ .Chart.Name }}
image: {{ .Values.image }}
Expand All @@ -58,28 +60,28 @@ For more information about how this works, see the Skaffold [custom builder](htt

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

```shell
$ skaffold run --tail --default-repo <your repo>
```sh
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
```terminal
Running image skaffold-templated:a866d5efd634062ea74662b20e172cd6e2d645f9f33f929bfaf8e856ec66bd94
```

printed every second in the Skaffold logs, since the code being executed is `main.go`.


[embedmd]:# (main.go go /fmt\.Printf/ /$/)
```go
// 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
```sh
skaffold delete
```
9 changes: 5 additions & 4 deletions integration/examples/templated-fields/build.sh
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
set -e

if ! [ -x "$(command -v ko)" ]; then
if ! [ -x "$(go env GOPATH)/bin/ko" ]; then
pushd $(mktemp -d)
go mod init tmp; GOFLAGS= go get github.com/google/ko/cmd/ko@v0.4.0
curl -L https://github.com/google/ko/archive/v0.9.3.tar.gz | tar --strip-components 1 -zx
go build -o $(go env GOPATH)/bin/ko .
popd
fi

output=$(ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo $output | tail -n1)
output=$($(go env GOPATH)/bin/ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo "$output" | tail -n1)

img="${IMAGE_REPO}:${IMAGE_TAG}"
docker tag $ref $img
Expand Down
29 changes: 14 additions & 15 deletions integration/examples/templated-fields/skaffold.yaml
Expand Up @@ -2,23 +2,22 @@ apiVersion: skaffold/v2beta24
kind: Config
metadata:
name: my-app

build:
artifacts:
- image: skaffold-templated
custom:
buildCommand: "./build.sh"
dependencies:
paths:
- "go.mod"
- "**.go"
- image: skaffold-templated
custom:
buildCommand: "./build.sh"
dependencies:
paths:
- "**/*.go"
- go.mod
deploy:
helm:
releases:
- name: skaffold-templated
chartPath: charts
artifactOverrides:
image: skaffold-templated
setValueTemplates:
imageRepo: "{{.IMAGE_REPO}}"
imageTag: "{{.IMAGE_TAG}}"
- name: skaffold-templated
chartPath: charts
artifactOverrides:
image: skaffold-templated
setValueTemplates:
imageRepo: "{{.IMAGE_REPO}}"
imageTag: "{{.IMAGE_TAG}}"

0 comments on commit dd15c9a

Please sign in to comment.