From bc272efaf690d27606182a270d2ef599d1b57595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Tue, 19 Jan 2021 09:56:37 +0100 Subject: [PATCH] Add shell pipe support for "play kube" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #8996 Similar to `cat pod.json | kubectl apply -f -` this commit adds support for input from shell pipe (ex `cat pod.json | podman play kube -`). To use input form pipe one may use '-' as file name, the same way as `kubectl apply -f -` Signed-off-by: Ádám Kovács --- pkg/bindings/play/play.go | 25 +++++++++++++++++++++---- pkg/domain/infra/abi/play.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/pkg/bindings/play/play.go b/pkg/bindings/play/play.go index 9ee02a093e..d43923b027 100644 --- a/pkg/bindings/play/play.go +++ b/pkg/bindings/play/play.go @@ -9,6 +9,7 @@ import ( "github.com/containers/podman/v2/pkg/auth" "github.com/containers/podman/v2/pkg/bindings" "github.com/containers/podman/v2/pkg/domain/entities" + "github.com/pkg/errors" ) func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.PlayKubeReport, error) { @@ -21,11 +22,27 @@ func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.Pla return nil, err } - f, err := os.Open(path) - if err != nil { - return nil, err + var f *os.File + + if path == "-" { + fi, err := os.Stdin.Stat() + if err != nil { + return nil, errors.Wrapf(err, "unable to read from stdin") + } + + if fi.Mode()&os.ModeNamedPipe == 0 { + return nil, errors.New("No data is supplied by a shell pipe but '-' was used as filename") + } + + f = os.Stdin + + } else { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() } - defer f.Close() params, err := options.ToParams() if err != nil { diff --git a/pkg/domain/infra/abi/play.go b/pkg/domain/infra/abi/play.go index 70c7104f1e..4d9683a0bb 100644 --- a/pkg/domain/infra/abi/play.go +++ b/pkg/domain/infra/abi/play.go @@ -1,6 +1,7 @@ package abi import ( + "bufio" "context" "fmt" "io" @@ -29,9 +30,34 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, path string, options en kubeObject v1.ObjectReference ) - content, err := ioutil.ReadFile(path) - if err != nil { - return nil, err + var content []byte + var err error + + if path == "-" { + fi, err := os.Stdin.Stat() + if err != nil { + return nil, errors.Wrapf(err, "unable to read from stdin") + } + + if fi.Mode()&os.ModeNamedPipe == 0 { + return nil, errors.New("No data is supplied by a shell pipe but '-' was used as filename") + } + + reader := bufio.NewReader(os.Stdin) + + for { + input, err := reader.ReadByte() + if err != nil && err == io.EOF { + break + } + content = append(content, input) + } + + } else { + content, err = ioutil.ReadFile(path) + if err != nil { + return nil, err + } } if err := yaml.Unmarshal(content, &kubeObject); err != nil {