Skip to content

Commit b12af86

Browse files
committed
e2e: build and install trust plugin
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent eb71e0a commit b12af86

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ RUN --mount=type=bind,target=.,ro \
7474
TARGET=/out ./scripts/build/binary && \
7575
xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker
7676

77+
FROM build-${BASE_VARIANT} AS build-trust
78+
ARG GO_LINKMODE=static
79+
ARG GO_BUILDTAGS
80+
ARG GO_STRIP
81+
ARG CGO_ENABLED
82+
ARG VERSION
83+
RUN --mount=ro --mount=type=cache,target=/root/.cache \
84+
xx-go --wrap && \
85+
TARGET=/out ./scripts/build/trust-plugin
86+
87+
FROM scratch AS trust
88+
COPY --link --from=build-trust /out/docker-trust /
89+
7790
FROM build-${BASE_VARIANT} AS test
7891
COPY --link --from=gotestsum /out/gotestsum /usr/bin/gotestsum
7992
ENV GO111MODULE=auto
@@ -114,6 +127,7 @@ COPY --link --from=build /out ./build/
114127
COPY --link --from=build-plugins /out ./build/
115128
COPY --link --from=buildx /buildx /usr/libexec/docker/cli-plugins/docker-buildx
116129
COPY --link --from=compose /docker-compose /usr/libexec/docker/cli-plugins/docker-compose
130+
COPY --link --from=trust /docker-trust /usr/libexec/docker/cli-plugins/docker-trust
117131
COPY --link . .
118132
ENV DOCKER_BUILDKIT=1
119133
ENV PATH=/go/src/github.com/docker/cli/build:$PATH

cmd/docker-trust/main.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78
"path/filepath"
9+
"syscall"
810

11+
cerrdefs "github.com/containerd/errdefs"
912
"github.com/docker/cli/cli"
1013
"github.com/docker/cli/cli-plugins/metadata"
1114
"github.com/docker/cli/cli-plugins/plugin"
@@ -53,6 +56,14 @@ func run(cmd *command.DockerCli) error {
5356
return runPlugin(cmd)
5457
}
5558

59+
type errCtxSignalTerminated struct {
60+
signal os.Signal
61+
}
62+
63+
func (errCtxSignalTerminated) Error() string {
64+
return ""
65+
}
66+
5667
func main() {
5768
cmd, err := command.NewDockerCli()
5869
if err != nil {
@@ -64,18 +75,42 @@ func main() {
6475
return
6576
}
6677

67-
// Check the error from the run function above.
68-
if sterr, ok := err.(cli.StatusError); ok {
69-
if sterr.Status != "" {
70-
_, _ = fmt.Fprintln(cmd.Err(), sterr.Status)
78+
if errors.As(err, &errCtxSignalTerminated{}) {
79+
os.Exit(getExitCode(err))
80+
}
81+
82+
if !cerrdefs.IsCanceled(err) {
83+
if err.Error() != "" {
84+
_, _ = fmt.Fprintln(cmd.Err(), err)
7185
}
72-
// StatusError should only be used for errors, and all errors should
73-
// have a non-zero exit status, so never exit with 0
74-
if sterr.StatusCode == 0 {
75-
os.Exit(1)
86+
os.Exit(getExitCode(err))
87+
}
88+
// Keep CI happy, which expects a newline to be printed when terminated.
89+
_, _ = fmt.Fprintln(cmd.Out())
90+
}
91+
92+
// getExitCode returns the exit-code to use for the given error.
93+
// If err is a [cli.StatusError] and has a StatusCode set, it uses the
94+
// status-code from it, otherwise it returns "1" for any error.
95+
func getExitCode(err error) int {
96+
if err == nil {
97+
return 0
98+
}
99+
100+
var userTerminatedErr errCtxSignalTerminated
101+
if errors.As(err, &userTerminatedErr) {
102+
s, ok := userTerminatedErr.signal.(syscall.Signal)
103+
if !ok {
104+
return 1
76105
}
77-
os.Exit(sterr.StatusCode)
106+
return 128 + int(s)
107+
}
108+
109+
var stErr cli.StatusError
110+
if errors.As(err, &stErr) && stErr.StatusCode != 0 { // FIXME(thaJeztah): StatusCode should never be used with a zero status-code. Check if we do this anywhere.
111+
return stErr.StatusCode
78112
}
79113

80-
os.Exit(1)
114+
// No status-code provided; all errors should have a non-zero exit code.
115+
return 1
81116
}

scripts/build/trust-plugin

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Build plugins examples for the host OS/ARCH
4+
#
5+
6+
set -eu -o pipefail
7+
8+
# Disable CGO - we don't need it for these plugins.
9+
#
10+
# Important: this must be done before sourcing "./scripts/build/.variables",
11+
# because some other variables are conditionally set whether CGO is enabled.
12+
export CGO_ENABLED=0
13+
14+
source ./scripts/build/.variables
15+
16+
TARGET_PLUGIN="$(dirname "${TARGET}")/plugins-${GOOS}-${GOARCH}/docker-trust"
17+
mkdir -p "$(dirname "${TARGET_PLUGIN}")"
18+
19+
echo "Building $GO_LINKMODE $(basename "${TARGET_PLUGIN}")"
20+
(set -x ; GO111MODULE=auto go build -o "${TARGET_PLUGIN}" -tags "${GO_BUILDTAGS}" -ldflags "${GO_LDFLAGS}" ${GO_BUILDMODE} "github.com/docker/cli/cmd/docker-trust")
21+
22+
ln -sf "${TARGET_PLUGIN}" "$(dirname "${TARGET}")/docker-trust"

0 commit comments

Comments
 (0)