Skip to content

chore: updated examples/ to updated schema version #8748

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

Merged
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
2 changes: 1 addition & 1 deletion examples/bazel/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/buildpacks-java/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/buildpacks-node/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/buildpacks-python/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/buildpacks/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/cross-platform-builds/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-buildx/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
platforms: ["linux/amd64", "linux/arm64"]
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-tests/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/custom/.ko.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
defaultBaseImage: gcr.io/distroless/static:nonroot
defaultBaseImage: gcr.io/distroless/static-debian11:nonroot
19 changes: 9 additions & 10 deletions examples/custom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ Take a look at the `build.sh` file, which uses `ko` to containerize source code:
[embedmd]:# (build.sh bash)
```bash
#!/usr/bin/env bash
set -e
set -x
set -Eefuo pipefail

if ! [ -x "$(go env GOPATH)/bin/ko" ]; then
pushd $(mktemp -d)
curl -L https://github.com/google/ko/archive/v0.12.0.tar.gz | tar --strip-components 1 -zx
go build -o $(go env GOPATH)/bin/ko .
pushd "$(mktemp -d)"
curl -L https://github.com/ko-build/ko/archive/v0.13.0.tar.gz | tar --strip-components 1 -zx
go build -o "$(go env GOPATH)"/bin/ko .
popd
fi

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

docker tag "$ref" "$IMAGE"
Expand All @@ -63,17 +62,17 @@ and the skaffold config, which configures image `ko://github.com/GoogleContainer

[embedmd]:# (skaffold.yaml yaml)
```yaml
apiVersion: skaffold/v2beta24
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
- image: ko://github.com/GoogleContainerTools/skaffold/examples/custom
- image: ko://github.com/googlecontainertools/skaffold/examples/custom
custom:
buildCommand: ./build.sh
dependencies:
paths:
- "**/*.go"
- go.mod
- go.*
- .ko.yaml
tagPolicy:
sha256: {}
Expand All @@ -90,7 +89,7 @@ metadata:
spec:
containers:
- name: getting-started-custom
image: ko://github.com/GoogleContainerTools/skaffold/examples/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).
Expand Down
11 changes: 5 additions & 6 deletions examples/custom/build.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env bash
set -e
set -x
set -Eefuo pipefail

if ! [ -x "$(go env GOPATH)/bin/ko" ]; then
pushd $(mktemp -d)
curl -L https://github.com/google/ko/archive/v0.12.0.tar.gz | tar --strip-components 1 -zx
go build -o $(go env GOPATH)/bin/ko .
pushd "$(mktemp -d)"
curl -L https://github.com/ko-build/ko/archive/v0.13.0.tar.gz | tar --strip-components 1 -zx
go build -o "$(go env GOPATH)"/bin/ko .
popd
fi

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

docker tag "$ref" "$IMAGE"
Expand Down
2 changes: 1 addition & 1 deletion examples/custom/k8s/pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ metadata:
spec:
containers:
- name: getting-started-custom
image: ko://github.com/GoogleContainerTools/skaffold/examples/custom
image: ko://github.com/googlecontainertools/skaffold/examples/custom
9 changes: 5 additions & 4 deletions examples/custom/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
- image: github.com/googlecontainertools/skaffold/examples/custom
- image: ko://github.com/googlecontainertools/skaffold/examples/custom
custom:
buildCommand: ./build.sh
dependencies:
paths:
- "go.mod"
- "**.go"
- "**/*.go"
- go.*
- .ko.yaml
tagPolicy:
sha256: {}
2 changes: 1 addition & 1 deletion examples/dev-journey-buildpacks/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/docker-deploy/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
local:
Expand Down
2 changes: 1 addition & 1 deletion examples/gcb-kaniko/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
googleCloudBuild:
Expand Down
2 changes: 1 addition & 1 deletion examples/generate-pipeline/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/getting-started-kustomize/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
metadata:
name: getting-started-kustomize
Expand Down
2 changes: 1 addition & 1 deletion examples/getting-started/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v4beta4
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
Expand Down
16 changes: 16 additions & 0 deletions examples/go-integration-coverage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2023 The Skaffold Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.kpt-pipeline/
reports/
49 changes: 49 additions & 0 deletions examples/go-integration-coverage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# skaffold-go-integration-coverage

Example showing how to use Skaffold and ko to collect coverage profiles from
[integration tests](https://go.dev/testing/coverage/#glos-integration-test),
often called end-to-end tests, for Kubernetes workloads written in Go.

For a detailed explanation of how this example works, see the tutorial
[Go integration test coverage profiles](https://skaffold.dev/docs/tutorials/go-integration-coverage/)
on the Skaffold website.

## Requirements

- Go v1.20 or later
- Skaffold v2 or later

## Usage

1. If you are using a remote Kubernetes cluster, configure Skaffold to use
your image registry:

```shell
export SKAFFOLD_DEFAULT_REPO=[your image registry, e.g., gcr.io/$PROJECT_ID]
```

You can skip this step if you are using a local Kubernetes cluster such as
kind or minikube.

2. Build the container image with support for coverage profile collection,
deploy the Kubernetes resource, run the integration tests, and collect the
coverage profile data:

```shell
skaffold run --profile=coverage
```

The coverage profile data files will be in the directory `reports`.

## Cleaning up

When you are done, remove the resources from your cluster:

```shell
skaffold delete
```

## References

- [Go: Coverage profiling support for integration tests](https://go.dev/testing/coverage/)
- [`runtime/coverage` package in the Go standard library](https://pkg.go.dev/runtime/coverage)
74 changes: 74 additions & 0 deletions examples/go-integration-coverage/coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2023 The Skaffold Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"log"
"os"
"os/signal"
"runtime/coverage"
"syscall"
)

var onlyOneCoverageSignalHandler = make(chan struct{})

// SetupCoverageSignalHandler creates a channel and relays the provided signals
// to this channel. It also starts a goroutine that receives on that channel.
// When the goroutine receives a signal, it writes profile data files to the
// directory specified by the `GOCOVERDIR` environment variable. After writing
// the data, the goroutine clears the coverage counters.
//
// Clearing the counters is only possible if the binary was built with
// `-covermode=atomic`.
//
// If no signals are provided as arguments, the function defaults to relaying
// `SIGUSR1`.
//
// If the `GOCOVERDIR` environment variable is _not_ set, this function does
// nothing.
//
// References:
// - https://go.dev/testing/coverage/
// - https://pkg.go.dev/runtime/coverage
func SetupCoverageSignalHandler(signals ...os.Signal) {
close(onlyOneCoverageSignalHandler) // panics when called twice

// Default to USR1 signal if no signals provided in the function argument.
if len(signals) < 1 {
signals = []os.Signal{syscall.SIGUSR1}
}

// Set up the signal handler only if GOCOVERDIR is set.
coverDir, exists := os.LookupEnv("GOCOVERDIR")
if !exists {
return
}

log.Printf("Configuring coverage profile data signal handler, listening for %v", signals)
c := make(chan os.Signal)
signal.Notify(c, signals...)
go func() {
for {
signal := <-c
log.Printf("Got %v, writing coverage profile data files to %q", signal, coverDir)
if err := coverage.WriteCountersDir(coverDir); err != nil {
log.Printf("Could not write coverage profile data files to the directory %q: %+v", coverDir, err)
}
if err := coverage.ClearCounters(); err != nil {
log.Printf("Could not reset coverage counter variables: %+v", err)
}
}
}()
}
17 changes: 17 additions & 0 deletions examples/go-integration-coverage/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 The Skaffold Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module github.com/googlecontainertools/skaffold/examples/go-integration-coverage

go 1.20
52 changes: 52 additions & 0 deletions examples/go-integration-coverage/integration-test/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
#
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eEufo pipefail

# Find the pods created by the Skaffold run.
ns_pods=($(kubectl get pods \
--all-namespaces \
--context "$SKAFFOLD_KUBE_CONTEXT" \
--output go-template='{{range .items}}{{.metadata.namespace}}/{{.metadata.name}}{{"\n"}}{{end}}' \
--selector "skaffold.dev/run-id=$SKAFFOLD_RUN_ID"))

# Base directory for the coverage profile data files.
reportbasedir=reports/coverage

# Array to collect report directories for all pods.
reportdirs=()

# Copy coverage profile data files from pods to local host.
# Use separate local directories for each pod.
for ns_pod in "${ns_pods[@]}"; do
reportdir="$reportbasedir/$ns_pod"
mkdir -p "$reportdir"
reportdirs+=("$reportdir")
ns=$(echo "$ns_pod" | cut -d'/' -f1)
pod=$(echo "$ns_pod" | cut -d'/' -f2)
kubectl exec \
--context "$SKAFFOLD_KUBE_CONTEXT" \
--namespace "$ns" \
"$pod" \
-- \
tar -C /coverage-data -cf - . | tar -xf - -C "$reportdir"
done

# Comma-separated string of report directories for use with `go tool covdata`.
reportdirs_commasep=$(echo "${reportdirs[@]}" | tr " " ",")

# Report the percent statements covered metric per package on the terminal.
go tool covdata percent -i "$reportdirs_commasep"
Loading