Skip to content

Commit

Permalink
Merge pull request #21364 from giuseppe/reduce-bindings-deps
Browse files Browse the repository at this point in the history
reduce binary size for programs using the bindings
  • Loading branch information
openshift-merge-bot[bot] authored Jan 26, 2024
2 parents 56d3d59 + 93510a2 commit 4bef652
Show file tree
Hide file tree
Showing 52 changed files with 1,131 additions and 869 deletions.
3 changes: 3 additions & 0 deletions pkg/bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,6 @@ $
You can also verify that the information being passed back and forth is correct by putting
with a tool like `socat`, which can dump what the socket is seeing.
## Reducing Binary Size with "remote" Build Tag
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.
14 changes: 7 additions & 7 deletions pkg/bindings/containers/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/copy"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/types"
)

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

var statReport *entities.ContainerStatReport
var statReport *types.ContainerStatReport

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

if fileInfo != nil {
statReport = &entities.ContainerStatReport{FileInfo: *fileInfo}
statReport = &types.ContainerStatReport{FileInfo: *fileInfo}
}

return statReport, finalErr
}

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

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

// CopyToArchive copy files from container
func CopyToArchive(ctx context.Context, nameOrID string, path string, writer io.Writer) (entities.ContainerCopyFunc, error) {
func CopyToArchive(ctx context.Context, nameOrID string, path string, writer io.Writer) (types.ContainerCopyFunc, error) {
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/bindings/containers/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"os"

"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/types"
)

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

return &entities.CheckpointReport{}, nil
return &types.CheckpointReport{}, nil
}

// Restore restores a checkpointed container to running. The container is identified by the nameOrID option. All
// additional options are optional and allow finer control of the restore process.
func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*entities.RestoreReport, error) {
var report entities.RestoreReport
func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*types.RestoreReport, error) {
var report types.RestoreReport
if options == nil {
options = new(RestoreOptions)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/bindings/containers/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import (

"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/bindings/images"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/storage/pkg/regexp"
dockerAPI "github.com/docker/docker/api/types"
)

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

// Commit creates a container image from a container. The container is defined by nameOrID. Use
// the CommitOptions for finer grain control on characteristics of the resulting image.
func Commit(ctx context.Context, nameOrID string, options *CommitOptions) (entities.IDResponse, error) {
func Commit(ctx context.Context, nameOrID string, options *CommitOptions) (dockerAPI.IDResponse, error) {
if options == nil {
options = new(CommitOptions)
}
id := entities.IDResponse{}
id := dockerAPI.IDResponse{}
conn, err := bindings.GetClient(ctx)
if err != nil {
return id, err
}
params, err := options.ToParams()
if err != nil {
return entities.IDResponse{}, err
return dockerAPI.IDResponse{}, err
}
params.Set("container", nameOrID)
var requestBody io.Reader
Expand Down
14 changes: 7 additions & 7 deletions pkg/bindings/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/api/handlers"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/reports"
"github.com/containers/podman/v4/pkg/domain/entities/types"
)

var (
Expand All @@ -25,15 +25,15 @@ var (
// the most recent number of containers. The pod and size booleans indicate that pod information and rootfs
// size information should also be included. Finally, the sync bool synchronizes the OCI runtime and
// container state.
func List(ctx context.Context, options *ListOptions) ([]entities.ListContainer, error) {
func List(ctx context.Context, options *ListOptions) ([]types.ListContainer, error) {
if options == nil {
options = new(ListOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
var containers []entities.ListContainer
var containers []types.ListContainer
params, err := options.ToParams()
if err != nil {
return nil, err
Expand Down Expand Up @@ -218,7 +218,7 @@ func Start(ctx context.Context, nameOrID string, options *StartOptions) error {
return response.Process(nil)
}

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

statsChan := make(chan entities.ContainerStatsReport)
statsChan := make(chan types.ContainerStatsReport)

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

var report entities.ContainerStatsReport
var report types.ContainerStatsReport
if err := dec.Decode(&report); err != nil {
report = entities.ContainerStatsReport{Error: err}
report = types.ContainerStatsReport{Error: err}
}
statsChan <- report

Expand Down
6 changes: 3 additions & 3 deletions pkg/bindings/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"strings"

"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/types"
"github.com/containers/podman/v4/pkg/specgen"
jsoniter "github.com/json-iterator/go"
)

func CreateWithSpec(ctx context.Context, s *specgen.SpecGenerator, options *CreateOptions) (entities.ContainerCreateResponse, error) {
var ccr entities.ContainerCreateResponse
func CreateWithSpec(ctx context.Context, s *specgen.SpecGenerator, options *CreateOptions) (types.ContainerCreateResponse, error) {
var ccr types.ContainerCreateResponse
if options == nil {
options = new(CreateOptions)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/bindings/containers/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/api/handlers"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
dockerAPI "github.com/docker/docker/api/types"
jsoniter "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -43,7 +43,7 @@ func ExecCreate(ctx context.Context, nameOrID string, config *handlers.ExecCreat
}
defer resp.Body.Close()

respStruct := new(entities.IDResponse)
respStruct := new(dockerAPI.IDResponse)
if err := resp.Process(respStruct); err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/bindings/containers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strings"

"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/types"
jsoniter "github.com/json-iterator/go"
)

func Update(ctx context.Context, options *entities.ContainerUpdateOptions) (string, error) {
func Update(ctx context.Context, options *types.ContainerUpdateOptions) (string, error) {
conn, err := bindings.GetClient(ctx)
if err != nil {
return "", err
Expand Down
10 changes: 5 additions & 5 deletions pkg/bindings/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"strconv"

"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/types"
)

func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*entities.GenerateSystemdReport, error) {
func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*types.GenerateSystemdReport, error) {
if options == nil {
options = new(SystemdOptions)
}
Expand All @@ -29,14 +29,14 @@ func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*en
}
defer response.Body.Close()

report := &entities.GenerateSystemdReport{}
report := &types.GenerateSystemdReport{}
return report, response.Process(&report.Units)
}

// Kube generate Kubernetes YAML (v1 specification)
//
// Note: Caller is responsible for closing returned reader
func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*entities.GenerateKubeReport, error) {
func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*types.GenerateKubeReport, error) {
if options == nil {
options = new(KubeOptions)
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*entit
}

if response.StatusCode == http.StatusOK {
return &entities.GenerateKubeReport{Reader: response.Body}, nil
return &types.GenerateKubeReport{Reader: response.Body}, nil
}

// Unpack the error.
Expand Down
26 changes: 13 additions & 13 deletions pkg/bindings/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"strings"

"github.com/containers/buildah/define"
"github.com/containers/image/v5/types"
imageTypes "github.com/containers/image/v5/types"
ldefine "github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/auth"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/entities/types"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/ioutils"
Expand Down Expand Up @@ -50,7 +50,7 @@ type BuildResponse struct {
}

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

switch options.SkipUnusedStages {
case types.OptionalBoolTrue:
case imageTypes.OptionalBoolTrue:
params.Set("skipunusedstages", "1")
case types.OptionalBoolFalse:
case imageTypes.OptionalBoolFalse:
params.Set("skipunusedstages", "0")
}

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

switch options.CommonBuildOpts.IdentityLabel {
case types.OptionalBoolTrue:
case imageTypes.OptionalBoolTrue:
params.Set("identitylabel", "1")
case types.OptionalBoolFalse:
case imageTypes.OptionalBoolFalse:
params.Set("identitylabel", "0")
}
if options.Quiet {
Expand Down Expand Up @@ -416,7 +416,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
} else {
headers, err = auth.MakeXRegistryConfigHeader(options.SystemContext, "", "")
}
if options.SystemContext.DockerInsecureSkipTLSVerify == types.OptionalBoolTrue {
if options.SystemContext.DockerInsecureSkipTLSVerify == imageTypes.OptionalBoolTrue {
params.Set("tlsVerify", "false")
}
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
// even when the server quit but it seems desirable to
// distinguish a proper build from a transient EOF.
case <-response.Request.Context().Done():
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, nil
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, nil
default:
// non-blocking select
}
Expand All @@ -632,7 +632,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
if errors.Is(err, io.EOF) && id != "" {
break
}
return &entities.BuildReport{ID: id, SaveFormat: saveFormat}, fmt.Errorf("decoding stream: %w", err)
return &types.BuildReport{ID: id, SaveFormat: saveFormat}, fmt.Errorf("decoding stream: %w", err)
}

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

func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {
Expand Down
Loading

0 comments on commit 4bef652

Please sign in to comment.