Skip to content

Commit

Permalink
Add conmon-rs streaming server support
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Mar 21, 2024
1 parent 069787e commit 0e4642a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type RuntimeImpl interface {
ReopenContainerLog(context.Context, *Container) error
CheckpointContainer(context.Context, *Container, *rspec.Spec, bool) error
RestoreContainer(context.Context, *Container, string, string) error
ServeExecContainer(context.Context, *Container, []string, bool, bool, bool, bool) (string, error)
ServeAttachContainer(context.Context, *Container, bool, bool, bool) (string, error)
}

// New creates a new Runtime with options provided
Expand Down Expand Up @@ -466,3 +468,21 @@ func (r *Runtime) RestoreContainer(ctx context.Context, c *Container, cgroupPare

return impl.RestoreContainer(ctx, c, cgroupParent, mountLabel)
}

func (r *Runtime) ServeExecContainer(ctx context.Context, c *Container, cmd []string, tty, stdin, stdout, stderr bool) (string, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return "", err
}

return impl.ServeExecContainer(ctx, c, cmd, tty, stdin, stdout, stderr)
}

func (r *Runtime) ServeAttachContainer(ctx context.Context, c *Container, stdin, stdout, stderr bool) (string, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return "", err
}

return impl.ServeAttachContainer(ctx, c, stdin, stdout, stderr)
}
28 changes: 28 additions & 0 deletions internal/oci/runtime_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ func (r *runtimePod) RestoreContainer(
return r.oci.RestoreContainer(ctx, c, cgroupParent, mountLabel)
}

func (r *runtimePod) ServeExecContainer(ctx context.Context, c *Container, cmd []string, tty, stdin, stdout, stderr bool) (string, error) {
res, err := r.client.ServeExecContainer(ctx, &conmonClient.ServeExecContainerConfig{

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / binaries / amd64

r.client.ServeExecContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeExecContainer)

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / binaries / amd64

undefined: conmonClient.ServeExecContainerConfig

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build-freebsd

r.client.ServeExecContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeExecContainer)

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build-freebsd

undefined: conmonClient.ServeExecContainerConfig

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / security-checks

r.client.ServeExecContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeExecContainer)

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / security-checks

undefined: conmonClient.ServeExecContainerConfig

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

r.client.ServeExecContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeExecContainer)

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

undefined: conmonClient.ServeExecContainerConfig

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

r.client.ServeExecContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeExecContainer)

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

undefined: conmonClient.ServeExecContainerConfig

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build

r.client.ServeExecContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeExecContainer)

Check failure on line 175 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build

undefined: conmonClient.ServeExecContainerConfig
ID: c.ID(),
Cmd: cmd,
Tty: tty,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
if err != nil {
return "", fmt.Errorf("call ServeExecContainer RPC: %w", err)
}
return res.URL, nil
}

func (r *runtimePod) ServeAttachContainer(ctx context.Context, c *Container, stdin, stdout, stderr bool) (string, error) {
res, err := r.client.ServeAttachContainer(ctx, &conmonClient.ServeAttachContainerConfig{

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / binaries / amd64

r.client.ServeAttachContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeAttachContainer)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / binaries / amd64

undefined: conmonClient.ServeAttachContainerConfig

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build-freebsd

r.client.ServeAttachContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeAttachContainer)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build-freebsd

undefined: conmonClient.ServeAttachContainerConfig

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / security-checks

r.client.ServeAttachContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeAttachContainer)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / security-checks

undefined: conmonClient.ServeAttachContainerConfig

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

r.client.ServeAttachContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeAttachContainer)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

undefined: conmonClient.ServeAttachContainerConfig (typecheck)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

r.client.ServeAttachContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeAttachContainer)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / lint

undefined: conmonClient.ServeAttachContainerConfig) (typecheck)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build

r.client.ServeAttachContainer undefined (type *"github.com/containers/conmon-rs/pkg/client".ConmonClient has no field or method ServeAttachContainer)

Check failure on line 190 in internal/oci/runtime_pod.go

View workflow job for this annotation

GitHub Actions / build

undefined: conmonClient.ServeAttachContainerConfig
ID: c.ID(),
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
if err != nil {
return "", fmt.Errorf("call ServeAttachContainer RPC: %w", err)
}
return res.URL, nil
}

func (r *runtimePod) ExecContainer(ctx context.Context, c *Container, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resizeChan <-chan remotecommand.TerminalSize) error {
return r.oci.ExecContainer(ctx, c, cmd, stdin, stdout, stderr, tty, resizeChan)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/oci/runtime_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,3 +1248,11 @@ func EncodeKataVirtualVolumeToBase64(ctx context.Context, volume *katavolume.Kat
option := base64.StdEncoding.EncodeToString(validKataVirtualVolumeJSON)
return option, nil
}

func (r *runtimeVM) ServeExecContainer(context.Context, *Container, []string, bool, bool, bool, bool) (string, error) {
return "", nil
}

func (r *runtimeVM) ServeAttachContainer(context.Context, *Container, bool, bool, bool) (string, error) {
return "", nil
}
15 changes: 15 additions & 0 deletions server/container_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"errors"
"fmt"
"io"

"github.com/cri-o/cri-o/internal/log"
Expand All @@ -15,6 +16,20 @@ import (

// Attach prepares a streaming endpoint to attach to a running container.
func (s *Server) Attach(ctx context.Context, req *types.AttachRequest) (*types.AttachResponse, error) {
c, err := s.GetContainerFromShortID(ctx, req.ContainerId)
if err != nil {
return nil, fmt.Errorf("could not find container %q: %w", req.ContainerId, err)
}

url, err := s.Runtime().ServeAttachContainer(ctx, c, req.Stdin, req.Stdout, req.Stderr)
if err != nil {
return nil, fmt.Errorf("could not serve attach for container %q: %w", req.ContainerId, err)
}
if url != "" {
log.Infof(ctx, "Using attach URL from runtime: %v", url)
return &types.AttachResponse{Url: url}, nil
}

resp, err := s.getAttach(req)
if err != nil {
return nil, errors.New("unable to prepare attach endpoint")
Expand Down
14 changes: 14 additions & 0 deletions server/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ import (

// Exec prepares a streaming endpoint to execute a command in the container.
func (s *Server) Exec(ctx context.Context, req *types.ExecRequest) (*types.ExecResponse, error) {
c, err := s.GetContainerFromShortID(ctx, req.ContainerId)
if err != nil {
return nil, fmt.Errorf("could not find container %q: %w", req.ContainerId, err)
}

url, err := s.Runtime().ServeExecContainer(ctx, c, req.Cmd, req.Tty, req.Stdin, req.Stdout, req.Stderr)
if err != nil {
return nil, fmt.Errorf("could not serve exec for container %q: %w", req.ContainerId, err)
}
if url != "" {
log.Infof(ctx, "Using exec URL from runtime: %v", url)
return &types.ExecResponse{Url: url}, nil
}

resp, err := s.getExec(req)
if err != nil {
return nil, fmt.Errorf("unable to prepare exec endpoint: %w", err)
Expand Down

0 comments on commit 0e4642a

Please sign in to comment.