Skip to content

Commit

Permalink
Add shell pipe support for "play kube"
Browse files Browse the repository at this point in the history
Closes containers#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 <[email protected]>
  • Loading branch information
ADIX7 committed Jan 23, 2021
1 parent 6cef7c7 commit bc272ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
25 changes: 21 additions & 4 deletions pkg/bindings/play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
32 changes: 29 additions & 3 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package abi

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bc272ef

Please sign in to comment.