Skip to content

Commit

Permalink
Merge pull request #4313 from werf/refactor-buildah-config-method
Browse files Browse the repository at this point in the history
refactor(buildah): add Config method
  • Loading branch information
ilya-lesikov committed Mar 30, 2022
2 parents 91527db + 3129505 commit 9e242e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/buildah/common.go
Expand Up @@ -71,6 +71,18 @@ type CommitOpts struct {
Image string
}

type ConfigOpts struct {
CommonOpts
Labels []string
// TODO:
// Cmd []string
// Entrypoint []string
// Env []string
// Shell string
// User string
// WorkDir string
}

type (
FromCommandOpts CommonOpts
PushOpts CommonOpts
Expand All @@ -93,6 +105,7 @@ type Buildah interface {
Mount(ctx context.Context, container string, opts MountOpts) (string, error)
Umount(ctx context.Context, container string, opts UmountOpts) error
Commit(ctx context.Context, container string, opts CommitOpts) (string, error)
Config(ctx context.Context, container string, opts ConfigOpts) error
}

type Mode string
Expand Down
11 changes: 11 additions & 0 deletions pkg/buildah/docker_with_fuse.go
Expand Up @@ -194,6 +194,17 @@ func (b *DockerWithFuseBuildah) Commit(ctx context.Context, container string, op
return imgID, err
}

func (b *DockerWithFuseBuildah) Config(ctx context.Context, container string, opts ConfigOpts) error {
args := []string{"config"}
for _, label := range opts.Labels {
args = append(args, "--label", label)
}
args = append(args, container)

_, _, err := b.runBuildah(ctx, []string{}, args, opts.LogWriter)
return err
}

func (b *DockerWithFuseBuildah) runBuildah(ctx context.Context, dockerArgs []string, buildahArgs []string, logWriter io.Writer) (string, string, error) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
Expand Down
23 changes: 23 additions & 0 deletions pkg/buildah/native_linux.go
Expand Up @@ -323,6 +323,29 @@ func (b *NativeBuildah) Commit(ctx context.Context, container string, opts Commi
return imgID, nil
}

func (b *NativeBuildah) Config(ctx context.Context, container string, opts ConfigOpts) error {
builder, err := b.getBuilderFromContainer(ctx, container)
if err != nil {
return fmt.Errorf("error getting builder: %w", err)
}

for _, label := range opts.Labels {
labelSlice := strings.SplitN(label, "=", 2)
switch {
case len(labelSlice) > 1:
builder.SetLabel(labelSlice[0], labelSlice[1])
case labelSlice[0] == "-":
builder.ClearLabels()
case strings.HasSuffix(labelSlice[0], "-"):
builder.UnsetLabel(strings.TrimSuffix(labelSlice[0], "-"))
default:
builder.SetLabel(labelSlice[0], "")
}
}

return builder.Save()
}

func (b *NativeBuildah) getImage(ref string) (*libimage.Image, error) {
image, _, err := b.Runtime.LookupImage(ref, &libimage.LookupImageOptions{
ManifestList: true,
Expand Down

0 comments on commit 9e242e9

Please sign in to comment.