|
| 1 | +package s2i |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/docker/docker/api/types" |
| 10 | + "github.com/docker/docker/client" |
| 11 | +) |
| 12 | + |
| 13 | +// Wrapper to workaround https://github.com/containers/podman/issues/13770 |
| 14 | +type podmanDockerClient struct { |
| 15 | + client.CommonAPIClient |
| 16 | +} |
| 17 | + |
| 18 | +func (p podmanDockerClient) ContainerCommit(ctx context.Context, nameOrID string, opts types.ContainerCommitOptions) (types.IDResponse, error) { |
| 19 | + if len(opts.Config.Cmd) > 0 { |
| 20 | + bs, err := json.Marshal(opts.Config.Cmd) |
| 21 | + if err != nil { |
| 22 | + return types.IDResponse{}, err |
| 23 | + } |
| 24 | + opts.Changes = append(opts.Changes, "CMD "+string(bs)) |
| 25 | + } |
| 26 | + |
| 27 | + if len(opts.Config.Entrypoint) > 0 { |
| 28 | + bs, err := json.Marshal(opts.Config.Entrypoint) |
| 29 | + if err != nil { |
| 30 | + return types.IDResponse{}, err |
| 31 | + } |
| 32 | + opts.Changes = append(opts.Changes, "ENTRYPOINT "+string(bs)) |
| 33 | + } |
| 34 | + |
| 35 | + if opts.Config.User != "" { |
| 36 | + opts.Changes = append(opts.Changes, "USER "+opts.Config.User) |
| 37 | + } |
| 38 | + |
| 39 | + for _, e := range opts.Config.Env { |
| 40 | + parts := strings.SplitN(e, "=", 2) |
| 41 | + opts.Changes = append(opts.Changes, fmt.Sprintf("ENV %s=%q", parts[0], parts[1])) |
| 42 | + } |
| 43 | + |
| 44 | + for k, v := range opts.Config.Labels { |
| 45 | + opts.Changes = append(opts.Changes, fmt.Sprintf("LABEL %q=%q", k, v)) |
| 46 | + } |
| 47 | + |
| 48 | + return p.CommonAPIClient.ContainerCommit(ctx, nameOrID, opts) |
| 49 | +} |
| 50 | + |
| 51 | +func isPodman(ctx context.Context, cli client.CommonAPIClient) bool { |
| 52 | + v, err := cli.ServerVersion(ctx) |
| 53 | + if err != nil { |
| 54 | + return false |
| 55 | + } |
| 56 | + for _, comp := range v.Components { |
| 57 | + if comp.Name == "Podman Engine" { |
| 58 | + return true |
| 59 | + } |
| 60 | + } |
| 61 | + return false |
| 62 | +} |
0 commit comments