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

feat: logic to only emit the tags related with verify on skaffold verify #8851

Merged
merged 2 commits into from
Jun 6, 2023
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
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func getBuildArtifactsAndSetTagsForAction(configs []util.VersionedConfig, defaul
return nil, nil
}

fromBuildArtifactsFile := getArtifactsFromBuildArtifactsFile(imgs)
fromBuildArtifactsFile := joinWithArtifactsFromBuildArtifactsFile(imgs)

// We only use the images from previous builds, read from the --build-artifacts flag.
// `exec` itself does not perform a build, so we don't care about the configuration in the build stanza.
Expand All @@ -78,11 +78,11 @@ func getBuildArtifactsAndSetTagsForAction(configs []util.VersionedConfig, defaul
return applyDefaultRepoToArtifacts(buildArtifacts, defaulterFn)
}

func getArtifactsFromBuildArtifactsFile(actionImgs map[string]bool) (artifacts []graph.Artifact) {
func joinWithArtifactsFromBuildArtifactsFile(imgs map[string]bool) (artifacts []graph.Artifact) {
allArtifacts := fromBuildOutputFile.BuildArtifacts()

for _, a := range allArtifacts {
if actionImgs[a.ImageName] {
if imgs[a.ImageName] {
artifacts = append(artifacts, a)
}
}
Expand Down
31 changes: 26 additions & 5 deletions cmd/skaffold/app/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"

"github.com/GoogleContainerTools/skaffold/v2/cmd/skaffold/app/tips"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/manifest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/runner"
Expand All @@ -42,11 +43,7 @@ func NewCmdVerify() *cobra.Command {

func doVerify(ctx context.Context, out io.Writer) error {
return withRunner(ctx, out, func(r runner.Runner, configs []util.VersionedConfig) error {
var artifacts []*latest.Artifact
for _, cfg := range configs {
artifacts = append(artifacts, cfg.(*latest.SkaffoldConfig).Build.Artifacts...)
}
buildArtifacts, err := getBuildArtifactsAndSetTags(artifacts, r.ApplyDefaultRepo)
buildArtifacts, err := getBuildArtifactsAndSetTagsForVerify(configs, r.ApplyDefaultRepo)
if err != nil {
tips.PrintUseRunVsDeploy(out)
return err
Expand All @@ -60,3 +57,27 @@ func doVerify(ctx context.Context, out io.Writer) error {
return r.VerifyAndLog(ctx, out, buildArtifacts)
})
}

func getBuildArtifactsAndSetTagsForVerify(configs []util.VersionedConfig, defaulterFn DefaultRepoFn) ([]graph.Artifact, error) {
verifyImgs := getVerifyImgs(configs)

allImgs := joinWithArtifactsFromBuildArtifactsFile(verifyImgs)

buildArtifacts, err := mergeBuildArtifacts(allImgs, preBuiltImages.Artifacts(), []*latest.Artifact{})
if err != nil {
return nil, err
}

return applyDefaultRepoToArtifacts(buildArtifacts, defaulterFn)
}

func getVerifyImgs(configs []util.VersionedConfig) map[string]bool {
imgs := make(map[string]bool)
for _, cfg := range configs {
for _, vtc := range cfg.(*latest.SkaffoldConfig).Verify {
imgs[vtc.Container.Image] = true
}
}

return imgs
}
13 changes: 13 additions & 0 deletions integration/testdata/verify-succeed/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.15 as builder
COPY main.go .

# `skaffold debug` sets SKAFFOLD_GO_GCFLAGS to disable compiler optimizations
ARG SKAFFOLD_GO_GCFLAGS
RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o /app main.go

FROM alpine:3
# Define GOTRACEBACK to mark this container as using the Go language runtime
# for `skaffold debug` (https://skaffold.dev/docs/workflows/debug/).
ENV GOTRACEBACK=single
CMD ["./app"]
COPY --from=builder /app .
17 changes: 17 additions & 0 deletions integration/testdata/verify-succeed/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

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

func main() {
seconds := 0
env := os.Getenv("FOO")
for seconds < 5 {
fmt.Printf("Hello world %v! %v\n", env, seconds)
seconds++
time.Sleep(time.Second * 1)
}
}
25 changes: 24 additions & 1 deletion integration/testdata/verify-succeed/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,27 @@ profiles:
name: alpine-1
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo alpine-1; sleep 2; echo bye alpine-1"]
args: ["-c", "echo alpine-1; sleep 2; echo bye alpine-1"]

- name: local-built-artifact

build:
artifacts:
- image: localtask
context: ./app
- image: img-not-used-in-verify
context: ./app


verify:
- name: alpine-1
container:
name: alpine-1
image: alpine:3.15.4
command: ["/bin/sh"]
args: ["-c", "echo alpine-1; sleep 2; echo bye alpine-1"]

- name: localtask
container:
name: localtask
image: localtask
49 changes: 49 additions & 0 deletions integration/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,52 @@ func checkUniqueLogs(t *testutil.T, logs string, expectedUniqueLogs []string) {
}
}
}

func TestVerify_WithLocalArtifact(t *testing.T) {
tests := []struct {
description string
dir string
profile string
expectedMsgs []string
notExpectedMsgs []string
}{
{
description: "build and verify",
dir: "testdata/verify-succeed",
profile: "local-built-artifact",
expectedMsgs: []string{
"Tags used in verification:",
"- localtask ->",
"[localtask] Hello world ! 0",
"[alpine-1] alpine-1",
},
notExpectedMsgs: []string{
"- img-not-used-in-verify ->",
},
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
MarkIntegrationTest(t.T, CanRunWithoutGcp)

args := []string{"-p", test.profile}

tmpfile := testutil.TempFile(t.T, "", []byte{})
skaffold.Build(append(args, "--file-output", tmpfile)...).InDir(test.dir).RunOrFail(t.T)

out, err := skaffold.Verify(append(args, "--build-artifacts", tmpfile)...).InDir(test.dir).RunWithCombinedOutput(t.T)
logs := string(out)

t.CheckNoError(err)

for _, expectedMsg := range test.expectedMsgs {
t.CheckContains(expectedMsg, logs)
}

for _, notExpectedMsg := range test.notExpectedMsgs {
testutil.CheckNotContains(t.T, notExpectedMsg, logs)
}
})
}
}
10 changes: 6 additions & 4 deletions pkg/skaffold/runner/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ func (r *SkaffoldRunner) Verify(ctx context.Context, out io.Writer, artifacts []

out, ctx = output.WithEventContext(ctx, out, constants.Verify, constants.SubtaskIDNone)

output.Default.Fprintln(out, "Tags used in verification:")
if len(artifacts) > 0 {
output.Default.Fprintln(out, "Tags used in verification:")

for _, artifact := range artifacts {
output.Default.Fprintf(out, " - %s -> ", artifact.ImageName)
fmt.Fprintln(out, artifact.Tag)
for _, artifact := range artifacts {
output.Default.Fprintf(out, " - %s -> ", artifact.ImageName)
fmt.Fprintln(out, artifact.Tag)
}
}

var localImages []graph.Artifact
Expand Down