Skip to content

Commit

Permalink
podman v3 container bindings
Browse files Browse the repository at this point in the history
convert the golang container bindings to all use options so that changes
in the future are more managable.

Signed-off-by: baude <[email protected]>
  • Loading branch information
baude committed Dec 21, 2020
1 parent d692518 commit 401dcff
Show file tree
Hide file tree
Showing 107 changed files with 4,474 additions and 539 deletions.
67 changes: 38 additions & 29 deletions pkg/bindings/containers/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
)

// Attach attaches to a running container
func Attach(ctx context.Context, nameOrID string, detachKeys *string, logs, stream *bool, stdin io.Reader, stdout io.Writer, stderr io.Writer, attachReady chan bool) error {
func Attach(ctx context.Context, nameOrID string, stdin io.Reader, stdout io.Writer, stderr io.Writer, attachReady chan bool, options *AttachOptions) error {
if options == nil {
options = new(AttachOptions)
}
isSet := struct {
stdin bool
stdout bool
Expand Down Expand Up @@ -55,27 +58,24 @@ func Attach(ctx context.Context, nameOrID string, detachKeys *string, logs, stre
}

// Do we need to wire in stdin?
ctnr, err := Inspect(ctx, nameOrID, bindings.PFalse)
ctnr, err := Inspect(ctx, nameOrID, new(InspectOptions).WithSize(false))
if err != nil {
return err
}

params := url.Values{}
params, err := options.ToParams()
if err != nil {
return err
}
detachKeysInBytes := []byte{}
if detachKeys != nil {
params.Add("detachKeys", *detachKeys)
if options.Changed("DetachKeys") {
params.Add("detachKeys", options.GetDetachKeys())

detachKeysInBytes, err = term.ToBytes(*detachKeys)
detachKeysInBytes, err = term.ToBytes(options.GetDetachKeys())
if err != nil {
return errors.Wrapf(err, "invalid detach keys")
}
}
if logs != nil {
params.Add("logs", fmt.Sprintf("%t", *logs))
}
if stream != nil {
params.Add("stream", fmt.Sprintf("%t", *stream))
}
if isSet.stdin {
params.Add("stdin", "true")
}
Expand Down Expand Up @@ -278,13 +278,19 @@ func DemuxFrame(r io.Reader, buffer []byte, length int) (frame []byte, err error
}

// ResizeContainerTTY sets container's TTY height and width in characters
func ResizeContainerTTY(ctx context.Context, nameOrID string, height *int, width *int) error {
return resizeTTY(ctx, bindings.JoinURL("containers", nameOrID, "resize"), height, width)
func ResizeContainerTTY(ctx context.Context, nameOrID string, options *ResizeTTYOptions) error {
if options == nil {
options = new(ResizeTTYOptions)
}
return resizeTTY(ctx, bindings.JoinURL("containers", nameOrID, "resize"), options.Height, options.Width)
}

// ResizeExecTTY sets session's TTY height and width in characters
func ResizeExecTTY(ctx context.Context, nameOrID string, height *int, width *int) error {
return resizeTTY(ctx, bindings.JoinURL("exec", nameOrID, "resize"), height, width)
func ResizeExecTTY(ctx context.Context, nameOrID string, options *ResizeExecTTYOptions) error {
if options == nil {
options = new(ResizeExecTTYOptions)
}
return resizeTTY(ctx, bindings.JoinURL("exec", nameOrID, "resize"), options.Height, options.Width)
}

// resizeTTY set size of TTY of container
Expand Down Expand Up @@ -337,9 +343,9 @@ func attachHandleResize(ctx, winCtx context.Context, winChange chan os.Signal, i

var resizeErr error
if isExec {
resizeErr = ResizeExecTTY(ctx, id, &h, &w)
resizeErr = ResizeExecTTY(ctx, id, new(ResizeExecTTYOptions).WithHeight(h).WithWidth(w))
} else {
resizeErr = ResizeContainerTTY(ctx, id, &h, &w)
resizeErr = ResizeContainerTTY(ctx, id, new(ResizeTTYOptions).WithHeight(h).WithWidth(w))
}
if resizeErr != nil {
logrus.Warnf("failed to resize TTY: %v", err)
Expand All @@ -361,7 +367,10 @@ func setRawTerminal(file *os.File) (*terminal.State, error) {
}

// ExecStartAndAttach starts and attaches to a given exec session.
func ExecStartAndAttach(ctx context.Context, sessionID string, streams *define.AttachStreams) error {
func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStartAndAttachOptions) error {
if options == nil {
options = new(ExecStartAndAttachOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -450,10 +459,10 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, streams *define.A
go attachHandleResize(ctx, winCtx, winChange, true, sessionID, terminalFile)
}

if streams.AttachInput {
if options.GetAttachInput() {
go func() {
logrus.Debugf("Copying STDIN to socket")
_, err := utils.CopyDetachable(socket, streams.InputStream, []byte{})
_, err := utils.CopyDetachable(socket, options.InputStream, []byte{})
if err != nil {
logrus.Error("failed to write input to service: " + err.Error())
}
Expand All @@ -463,11 +472,11 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, streams *define.A
buffer := make([]byte, 1024)
if isTerm {
logrus.Debugf("Handling terminal attach to exec")
if !streams.AttachOutput {
if !options.GetAttachOutput() {
return fmt.Errorf("exec session %s has a terminal and must have STDOUT enabled", sessionID)
}
// If not multiplex'ed, read from server and write to stdout
_, err := utils.CopyDetachable(streams.OutputStream, socket, []byte{})
_, err := utils.CopyDetachable(options.GetOutputStream(), socket, []byte{})
if err != nil {
return err
}
Expand All @@ -489,22 +498,22 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, streams *define.A

switch {
case fd == 0:
if streams.AttachInput {
if options.GetAttachInput() {
// Write STDIN to STDOUT (echoing characters
// typed by another attach session)
if _, err := streams.OutputStream.Write(frame[0:l]); err != nil {
if _, err := options.GetOutputStream().Write(frame[0:l]); err != nil {
return err
}
}
case fd == 1:
if streams.AttachOutput {
if _, err := streams.OutputStream.Write(frame[0:l]); err != nil {
if options.GetAttachOutput() {
if _, err := options.GetOutputStream().Write(frame[0:l]); err != nil {
return err
}
}
case fd == 2:
if streams.AttachError {
if _, err := streams.ErrorStream.Write(frame[0:l]); err != nil {
if options.GetAttachError() {
if _, err := options.GetErrorStream().Write(frame[0:l]); err != nil {
return err
}
}
Expand Down
57 changes: 18 additions & 39 deletions pkg/bindings/containers/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@ package containers
import (
"context"
"net/http"
"net/url"
"strconv"

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

// 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, keep, leaveRunning, tcpEstablished, ignoreRootFS *bool, export *string) (*entities.CheckpointReport, error) {
func Checkpoint(ctx context.Context, nameOrID string, options *CheckpointOptions) (*entities.CheckpointReport, error) {
var report entities.CheckpointReport
if options == nil {
options = new(CheckpointOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if keep != nil {
params.Set("keep", strconv.FormatBool(*keep))
}
if leaveRunning != nil {
params.Set("leaveRunning", strconv.FormatBool(*leaveRunning))
}
if tcpEstablished != nil {
params.Set("TCPestablished", strconv.FormatBool(*tcpEstablished))
}
if ignoreRootFS != nil {
params.Set("ignoreRootFS", strconv.FormatBool(*ignoreRootFS))
}
if export != nil {
params.Set("export", *export)
params, err := options.ToParams()
if err != nil {
return nil, err
}
response, err := conn.DoRequest(nil, http.MethodPost, "/containers/%s/checkpoint", params, nil, nameOrID)
if err != nil {
Expand All @@ -43,33 +32,23 @@ func Checkpoint(ctx context.Context, nameOrID string, keep, leaveRunning, tcpEst

// 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, keep, tcpEstablished, ignoreRootFS, ignoreStaticIP, ignoreStaticMAC *bool, name, importArchive *string) (*entities.RestoreReport, error) {
func Restore(ctx context.Context, nameOrID string, options *RestoreOptions) (*entities.RestoreReport, error) {
var report entities.RestoreReport
if options == nil {
options = new(RestoreOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if keep != nil {
params.Set("keep", strconv.FormatBool(*keep))
}
if tcpEstablished != nil {
params.Set("TCPestablished", strconv.FormatBool(*tcpEstablished))
}
if ignoreRootFS != nil {
params.Set("ignoreRootFS", strconv.FormatBool(*ignoreRootFS))
}
if ignoreStaticIP != nil {
params.Set("ignoreStaticIP", strconv.FormatBool(*ignoreStaticIP))
}
if ignoreStaticMAC != nil {
params.Set("ignoreStaticMAC", strconv.FormatBool(*ignoreStaticMAC))
}
if name != nil {
params.Set("name", *name)
params, err := options.ToParams()
if err != nil {
return nil, err
}
if importArchive != nil {
params.Set("import", *importArchive)
// The import key is a reserved golang term
params.Del("ImportArchive")
if i := options.GetImportAchive(); options.Changed("ImportArchive") {
params.Set("import", i)
}
response, err := conn.DoRequest(nil, http.MethodPost, "/containers/%s/restore", params, nil, nameOrID)
if err != nil {
Expand Down
33 changes: 8 additions & 25 deletions pkg/bindings/containers/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,27 @@ package containers
import (
"context"
"net/http"
"net/url"
"strconv"

"github.com/containers/podman/v2/pkg/api/handlers"
"github.com/containers/podman/v2/pkg/bindings"
)

// 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) (handlers.IDResponse, error) {
func Commit(ctx context.Context, nameOrID string, options *CommitOptions) (handlers.IDResponse, error) {
if options == nil {
options = new(CommitOptions)
}
id := handlers.IDResponse{}
conn, err := bindings.GetClient(ctx)
if err != nil {
return id, err
}
params := url.Values{}
params.Set("container", nameOrID)
if options.Author != nil {
params.Set("author", *options.Author)
}
for _, change := range options.Changes {
params.Set("changes", change)
}
if options.Comment != nil {
params.Set("comment", *options.Comment)
}
if options.Format != nil {
params.Set("format", *options.Format)
}
if options.Pause != nil {
params.Set("pause", strconv.FormatBool(*options.Pause))
}
if options.Repo != nil {
params.Set("repo", *options.Repo)
}
if options.Tag != nil {
params.Set("tag", *options.Tag)
params, err := options.ToParams()
if err != nil {
return handlers.IDResponse{}, err
}
params.Set("container", nameOrID)
response, err := conn.DoRequest(nil, http.MethodPost, "/commit", params, nil)
if err != nil {
return id, err
Expand Down
Loading

0 comments on commit 401dcff

Please sign in to comment.