Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell pipe support for "play kube" #9018

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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