From 1783153d9bd4d8ae8e78116c37276295c5d7624c Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Wed, 11 May 2022 19:23:54 +0200 Subject: [PATCH] Fix S2I build with podman. Workaround a bug in podman: https://github.com/containers/podman/issues/13770 Signed-off-by: Matej Vasek --- s2i/builder.go | 4 +++ s2i/docker_client_wrapper.go | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 s2i/docker_client_wrapper.go diff --git a/s2i/builder.go b/s2i/builder.go index b5f9fc673b..0b2c533390 100644 --- a/s2i/builder.go +++ b/s2i/builder.go @@ -61,6 +61,10 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) { } defer client.Close() + if isPodman(ctx, client) { + client = podmanDockerClient{client} + } + // Build Config cfg := &api.Config{} cfg.Quiet = !b.verbose diff --git a/s2i/docker_client_wrapper.go b/s2i/docker_client_wrapper.go new file mode 100644 index 0000000000..e67f37256e --- /dev/null +++ b/s2i/docker_client_wrapper.go @@ -0,0 +1,62 @@ +package s2i + +import ( + "context" + "encoding/json" + "fmt" + "strings" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" +) + +// Wrapper to workaround https://github.com/containers/podman/issues/13770 +type podmanDockerClient struct { + client.CommonAPIClient +} + +func (p podmanDockerClient) ContainerCommit(ctx context.Context, nameOrID string, opts types.ContainerCommitOptions) (types.IDResponse, error) { + if len(opts.Config.Cmd) > 0 { + bs, err := json.Marshal(opts.Config.Cmd) + if err != nil { + return types.IDResponse{}, err + } + opts.Changes = append(opts.Changes, "CMD "+string(bs)) + } + + if len(opts.Config.Entrypoint) > 0 { + bs, err := json.Marshal(opts.Config.Entrypoint) + if err != nil { + return types.IDResponse{}, err + } + opts.Changes = append(opts.Changes, "ENTRYPOINT "+string(bs)) + } + + if opts.Config.User != "" { + opts.Changes = append(opts.Changes, "USER "+opts.Config.User) + } + + for _, e := range opts.Config.Env { + parts := strings.SplitN(e, "=", 2) + opts.Changes = append(opts.Changes, fmt.Sprintf("ENV %s=%q", parts[0], parts[1])) + } + + for k, v := range opts.Config.Labels { + opts.Changes = append(opts.Changes, fmt.Sprintf("LABEL %q=%q", k, v)) + } + + return p.CommonAPIClient.ContainerCommit(ctx, nameOrID, opts) +} + +func isPodman(ctx context.Context, cli client.CommonAPIClient) bool { + v, err := cli.ServerVersion(ctx) + if err != nil { + return false + } + for _, comp := range v.Components { + if comp.Name == "Podman Engine" { + return true + } + } + return false +}