Skip to content

Commit 4bef652

Browse files
Merge pull request #21364 from giuseppe/reduce-bindings-deps
reduce binary size for programs using the bindings
2 parents 56d3d59 + 93510a2 commit 4bef652

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1131
-869
lines changed

pkg/bindings/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,6 @@ $
239239
240240
You can also verify that the information being passed back and forth is correct by putting
241241
with a tool like `socat`, which can dump what the socket is seeing.
242+
243+
## Reducing Binary Size with "remote" Build Tag
244+
When building a program that uses the Podman Go bindings, you can reduce the binary size by passing the "remote" build tag to the go build command. This tag excludes code related to local Podman operations, which is not needed for applications that only interact with Podman over a network.

pkg/bindings/containers/archive.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
"github.com/containers/podman/v4/pkg/bindings"
1111
"github.com/containers/podman/v4/pkg/copy"
12-
"github.com/containers/podman/v4/pkg/domain/entities"
12+
"github.com/containers/podman/v4/pkg/domain/entities/types"
1313
)
1414

1515
// Stat checks if the specified path is on the container. Note that the stat
1616
// report may be set even in case of an error. This happens when the path
1717
// resolves to symlink pointing to a non-existent path.
18-
func Stat(ctx context.Context, nameOrID string, path string) (*entities.ContainerStatReport, error) {
18+
func Stat(ctx context.Context, nameOrID string, path string) (*types.ContainerStatReport, error) {
1919
conn, err := bindings.GetClient(ctx)
2020
if err != nil {
2121
return nil, err
@@ -36,26 +36,26 @@ func Stat(ctx context.Context, nameOrID string, path string) (*entities.Containe
3636
finalErr = errors.New(response.Status)
3737
}
3838

39-
var statReport *entities.ContainerStatReport
39+
var statReport *types.ContainerStatReport
4040

4141
fileInfo, err := copy.ExtractFileInfoFromHeader(&response.Header)
4242
if err != nil && finalErr == nil {
4343
return nil, err
4444
}
4545

4646
if fileInfo != nil {
47-
statReport = &entities.ContainerStatReport{FileInfo: *fileInfo}
47+
statReport = &types.ContainerStatReport{FileInfo: *fileInfo}
4848
}
4949

5050
return statReport, finalErr
5151
}
5252

53-
func CopyFromArchive(ctx context.Context, nameOrID string, path string, reader io.Reader) (entities.ContainerCopyFunc, error) {
53+
func CopyFromArchive(ctx context.Context, nameOrID string, path string, reader io.Reader) (types.ContainerCopyFunc, error) {
5454
return CopyFromArchiveWithOptions(ctx, nameOrID, path, reader, nil)
5555
}
5656

5757
// CopyFromArchiveWithOptions copy files into container
58-
func CopyFromArchiveWithOptions(ctx context.Context, nameOrID string, path string, reader io.Reader, options *CopyOptions) (entities.ContainerCopyFunc, error) {
58+
func CopyFromArchiveWithOptions(ctx context.Context, nameOrID string, path string, reader io.Reader, options *CopyOptions) (types.ContainerCopyFunc, error) {
5959
conn, err := bindings.GetClient(ctx)
6060
if err != nil {
6161
return nil, err
@@ -82,7 +82,7 @@ func CopyFromArchiveWithOptions(ctx context.Context, nameOrID string, path strin
8282
}
8383

8484
// CopyToArchive copy files from container
85-
func CopyToArchive(ctx context.Context, nameOrID string, path string, writer io.Writer) (entities.ContainerCopyFunc, error) {
85+
func CopyToArchive(ctx context.Context, nameOrID string, path string, writer io.Writer) (types.ContainerCopyFunc, error) {
8686
conn, err := bindings.GetClient(ctx)
8787
if err != nil {
8888
return nil, err

pkg/bindings/containers/checkpoint.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"os"
88

99
"github.com/containers/podman/v4/pkg/bindings"
10-
"github.com/containers/podman/v4/pkg/domain/entities"
10+
"github.com/containers/podman/v4/pkg/domain/entities/types"
1111
)
1212

1313
// Checkpoint checkpoints the given container (identified by nameOrID). All additional
1414
// options are options and allow for more fine grained control of the checkpoint process.
15-
func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions) (*entities.CheckpointReport, error) {
16-
var report entities.CheckpointReport
15+
func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions) (*types.CheckpointReport, error) {
16+
var report types.CheckpointReport
1717
if options == nil {
1818
options = new(CheckpointOptions)
1919
}
@@ -52,13 +52,13 @@ func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions
5252
return nil, err
5353
}
5454

55-
return &entities.CheckpointReport{}, nil
55+
return &types.CheckpointReport{}, nil
5656
}
5757

5858
// Restore restores a checkpointed container to running. The container is identified by the nameOrID option. All
5959
// additional options are optional and allow finer control of the restore process.
60-
func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*entities.RestoreReport, error) {
61-
var report entities.RestoreReport
60+
func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*types.RestoreReport, error) {
61+
var report types.RestoreReport
6262
if options == nil {
6363
options = new(RestoreOptions)
6464
}

pkg/bindings/containers/commit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ import (
1111

1212
"github.com/containers/podman/v4/pkg/bindings"
1313
"github.com/containers/podman/v4/pkg/bindings/images"
14-
"github.com/containers/podman/v4/pkg/domain/entities"
1514
"github.com/containers/storage/pkg/regexp"
15+
dockerAPI "github.com/docker/docker/api/types"
1616
)
1717

1818
var iidRegex = regexp.Delayed(`^[0-9a-f]{12}`)
1919

2020
// Commit creates a container image from a container. The container is defined by nameOrID. Use
2121
// the CommitOptions for finer grain control on characteristics of the resulting image.
22-
func Commit(ctx context.Context, nameOrID string, options *CommitOptions) (entities.IDResponse, error) {
22+
func Commit(ctx context.Context, nameOrID string, options *CommitOptions) (dockerAPI.IDResponse, error) {
2323
if options == nil {
2424
options = new(CommitOptions)
2525
}
26-
id := entities.IDResponse{}
26+
id := dockerAPI.IDResponse{}
2727
conn, err := bindings.GetClient(ctx)
2828
if err != nil {
2929
return id, err
3030
}
3131
params, err := options.ToParams()
3232
if err != nil {
33-
return entities.IDResponse{}, err
33+
return dockerAPI.IDResponse{}, err
3434
}
3535
params.Set("container", nameOrID)
3636
var requestBody io.Reader

pkg/bindings/containers/containers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/containers/podman/v4/libpod/define"
1313
"github.com/containers/podman/v4/pkg/api/handlers"
1414
"github.com/containers/podman/v4/pkg/bindings"
15-
"github.com/containers/podman/v4/pkg/domain/entities"
1615
"github.com/containers/podman/v4/pkg/domain/entities/reports"
16+
"github.com/containers/podman/v4/pkg/domain/entities/types"
1717
)
1818

1919
var (
@@ -25,15 +25,15 @@ var (
2525
// the most recent number of containers. The pod and size booleans indicate that pod information and rootfs
2626
// size information should also be included. Finally, the sync bool synchronizes the OCI runtime and
2727
// container state.
28-
func List(ctx context.Context, options *ListOptions) ([]entities.ListContainer, error) {
28+
func List(ctx context.Context, options *ListOptions) ([]types.ListContainer, error) {
2929
if options == nil {
3030
options = new(ListOptions)
3131
}
3232
conn, err := bindings.GetClient(ctx)
3333
if err != nil {
3434
return nil, err
3535
}
36-
var containers []entities.ListContainer
36+
var containers []types.ListContainer
3737
params, err := options.ToParams()
3838
if err != nil {
3939
return nil, err
@@ -218,7 +218,7 @@ func Start(ctx context.Context, nameOrID string, options *StartOptions) error {
218218
return response.Process(nil)
219219
}
220220

221-
func Stats(ctx context.Context, containers []string, options *StatsOptions) (chan entities.ContainerStatsReport, error) {
221+
func Stats(ctx context.Context, containers []string, options *StatsOptions) (chan types.ContainerStatsReport, error) {
222222
if options == nil {
223223
options = new(StatsOptions)
224224
}
@@ -243,7 +243,7 @@ func Stats(ctx context.Context, containers []string, options *StatsOptions) (cha
243243
return nil, response.Process(nil)
244244
}
245245

246-
statsChan := make(chan entities.ContainerStatsReport)
246+
statsChan := make(chan types.ContainerStatsReport)
247247

248248
go func() {
249249
defer close(statsChan)
@@ -263,9 +263,9 @@ func Stats(ctx context.Context, containers []string, options *StatsOptions) (cha
263263
// fall through and do some work
264264
}
265265

266-
var report entities.ContainerStatsReport
266+
var report types.ContainerStatsReport
267267
if err := dec.Decode(&report); err != nil {
268-
report = entities.ContainerStatsReport{Error: err}
268+
report = types.ContainerStatsReport{Error: err}
269269
}
270270
statsChan <- report
271271

pkg/bindings/containers/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"strings"
77

88
"github.com/containers/podman/v4/pkg/bindings"
9-
"github.com/containers/podman/v4/pkg/domain/entities"
9+
"github.com/containers/podman/v4/pkg/domain/entities/types"
1010
"github.com/containers/podman/v4/pkg/specgen"
1111
jsoniter "github.com/json-iterator/go"
1212
)
1313

14-
func CreateWithSpec(ctx context.Context, s *specgen.SpecGenerator, options *CreateOptions) (entities.ContainerCreateResponse, error) {
15-
var ccr entities.ContainerCreateResponse
14+
func CreateWithSpec(ctx context.Context, s *specgen.SpecGenerator, options *CreateOptions) (types.ContainerCreateResponse, error) {
15+
var ccr types.ContainerCreateResponse
1616
if options == nil {
1717
options = new(CreateOptions)
1818
}

pkg/bindings/containers/exec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/containers/podman/v4/libpod/define"
1212
"github.com/containers/podman/v4/pkg/api/handlers"
1313
"github.com/containers/podman/v4/pkg/bindings"
14-
"github.com/containers/podman/v4/pkg/domain/entities"
14+
dockerAPI "github.com/docker/docker/api/types"
1515
jsoniter "github.com/json-iterator/go"
1616
"github.com/sirupsen/logrus"
1717
)
@@ -43,7 +43,7 @@ func ExecCreate(ctx context.Context, nameOrID string, config *handlers.ExecCreat
4343
}
4444
defer resp.Body.Close()
4545

46-
respStruct := new(entities.IDResponse)
46+
respStruct := new(dockerAPI.IDResponse)
4747
if err := resp.Process(respStruct); err != nil {
4848
return "", err
4949
}

pkg/bindings/containers/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"strings"
77

88
"github.com/containers/podman/v4/pkg/bindings"
9-
"github.com/containers/podman/v4/pkg/domain/entities"
9+
"github.com/containers/podman/v4/pkg/domain/entities/types"
1010
jsoniter "github.com/json-iterator/go"
1111
)
1212

13-
func Update(ctx context.Context, options *entities.ContainerUpdateOptions) (string, error) {
13+
func Update(ctx context.Context, options *types.ContainerUpdateOptions) (string, error) {
1414
conn, err := bindings.GetClient(ctx)
1515
if err != nil {
1616
return "", err

pkg/bindings/generate/generate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"strconv"
88

99
"github.com/containers/podman/v4/pkg/bindings"
10-
"github.com/containers/podman/v4/pkg/domain/entities"
10+
"github.com/containers/podman/v4/pkg/domain/entities/types"
1111
)
1212

13-
func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*entities.GenerateSystemdReport, error) {
13+
func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*types.GenerateSystemdReport, error) {
1414
if options == nil {
1515
options = new(SystemdOptions)
1616
}
@@ -29,14 +29,14 @@ func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*en
2929
}
3030
defer response.Body.Close()
3131

32-
report := &entities.GenerateSystemdReport{}
32+
report := &types.GenerateSystemdReport{}
3333
return report, response.Process(&report.Units)
3434
}
3535

3636
// Kube generate Kubernetes YAML (v1 specification)
3737
//
3838
// Note: Caller is responsible for closing returned reader
39-
func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*entities.GenerateKubeReport, error) {
39+
func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*types.GenerateKubeReport, error) {
4040
if options == nil {
4141
options = new(KubeOptions)
4242
}
@@ -64,7 +64,7 @@ func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*entit
6464
}
6565

6666
if response.StatusCode == http.StatusOK {
67-
return &entities.GenerateKubeReport{Reader: response.Body}, nil
67+
return &types.GenerateKubeReport{Reader: response.Body}, nil
6868
}
6969

7070
// Unpack the error.

pkg/bindings/images/build.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
"strings"
1919

2020
"github.com/containers/buildah/define"
21-
"github.com/containers/image/v5/types"
21+
imageTypes "github.com/containers/image/v5/types"
2222
ldefine "github.com/containers/podman/v4/libpod/define"
2323
"github.com/containers/podman/v4/pkg/auth"
2424
"github.com/containers/podman/v4/pkg/bindings"
25-
"github.com/containers/podman/v4/pkg/domain/entities"
25+
"github.com/containers/podman/v4/pkg/domain/entities/types"
2626
"github.com/containers/podman/v4/pkg/util"
2727
"github.com/containers/storage/pkg/fileutils"
2828
"github.com/containers/storage/pkg/ioutils"
@@ -50,7 +50,7 @@ type BuildResponse struct {
5050
}
5151

5252
// Build creates an image using a containerfile reference
53-
func Build(ctx context.Context, containerFiles []string, options entities.BuildOptions) (*entities.BuildReport, error) {
53+
func Build(ctx context.Context, containerFiles []string, options types.BuildOptions) (*types.BuildReport, error) {
5454
if options.CommonBuildOpts == nil {
5555
options.CommonBuildOpts = new(define.CommonBuildOptions)
5656
}
@@ -255,9 +255,9 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
255255
}
256256

257257
switch options.SkipUnusedStages {
258-
case types.OptionalBoolTrue:
258+
case imageTypes.OptionalBoolTrue:
259259
params.Set("skipunusedstages", "1")
260-
case types.OptionalBoolFalse:
260+
case imageTypes.OptionalBoolFalse:
261261
params.Set("skipunusedstages", "0")
262262
}
263263

@@ -342,9 +342,9 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
342342
params.Set("pullpolicy", options.PullPolicy.String())
343343

344344
switch options.CommonBuildOpts.IdentityLabel {
345-
case types.OptionalBoolTrue:
345+
case imageTypes.OptionalBoolTrue:
346346
params.Set("identitylabel", "1")
347-
case types.OptionalBoolFalse:
347+
case imageTypes.OptionalBoolFalse:
348348
params.Set("identitylabel", "0")
349349
}
350350
if options.Quiet {
@@ -416,7 +416,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
416416
} else {
417417
headers, err = auth.MakeXRegistryConfigHeader(options.SystemContext, "", "")
418418
}
419-
if options.SystemContext.DockerInsecureSkipTLSVerify == types.OptionalBoolTrue {
419+
if options.SystemContext.DockerInsecureSkipTLSVerify == imageTypes.OptionalBoolTrue {
420420
params.Set("tlsVerify", "false")
421421
}
422422
}
@@ -618,7 +618,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
618618
// even when the server quit but it seems desirable to
619619
// distinguish a proper build from a transient EOF.
620620
case <-response.Request.Context().Done():
621-
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, nil
621+
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, nil
622622
default:
623623
// non-blocking select
624624
}
@@ -632,7 +632,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
632632
if errors.Is(err, io.EOF) && id != "" {
633633
break
634634
}
635-
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, fmt.Errorf("decoding stream: %w", err)
635+
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, fmt.Errorf("decoding stream: %w", err)
636636
}
637637

638638
switch {
@@ -645,12 +645,12 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
645645
case s.Error != nil:
646646
// If there's an error, return directly. The stream
647647
// will be closed on return.
648-
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, errors.New(s.Error.Message)
648+
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, errors.New(s.Error.Message)
649649
default:
650-
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, errors.New("failed to parse build results stream, unexpected input")
650+
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, errors.New("failed to parse build results stream, unexpected input")
651651
}
652652
}
653-
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, nil
653+
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, nil
654654
}
655655

656656
func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {

0 commit comments

Comments
 (0)