Skip to content

Commit

Permalink
Fix S2I build with podman.
Browse files Browse the repository at this point in the history
Workaround a bug in podman:
containers/podman#13770

Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed May 11, 2022
1 parent 1fd60ee commit 2ede6ee
Show file tree
Hide file tree
Showing 5 changed files with 552 additions and 0 deletions.
1 change: 1 addition & 0 deletions .codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ ignore:
- vendor
- testdata
- docker/docker_client_generated.go
- s2i/zz_docker_client_wrapper_generated.go
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ testdata/repository-a.git/objects/*/* ignore-lint=true
testdata/repository.git/objects/*/* ignore-lint=true
version.txt linguist-generated=true
zz_filesystem_generated.go linguist-generated=true
s2i/zz_docker_client_wrapper_generated.go linguist-generated=true
4 changes: 4 additions & 0 deletions s2i/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions s2i/docker_client_wrapper.go
Original file line number Diff line number Diff line change
@@ -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 {
pimpl 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.pimpl.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
}
Loading

0 comments on commit 2ede6ee

Please sign in to comment.