Skip to content

Commit

Permalink
play: kube: add support for in-memory kubefile
Browse files Browse the repository at this point in the history
The PlayKube and PlayKubeDown commands accept a "path" argument to a YAML file
to play. This requires the caller to write the YAML to a file path.

The file is opened & used as the body of the HTTP request, so it should be
possible to instead pass a io.Reader and use a fully in-memory request body.

This commit adds a new backwards-compatible option to PlayKubeOptions and
PlayKubeDownOptions - "Body" - which is an io.Reader for the request body. If it
is set (non-nil) it will be used instead of os.Open() for the yaml file path.

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin committed Mar 23, 2022
1 parent 9d8972e commit ba841aa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
39 changes: 25 additions & 14 deletions pkg/bindings/play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package play

import (
"context"
"io"
"net/http"
"os"
"strconv"
Expand All @@ -14,20 +15,25 @@ import (
)

func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.PlayKubeReport, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()

return KubeWithBody(ctx, f, options)
}

func KubeWithBody(ctx context.Context, body io.Reader, options *KubeOptions) (*entities.PlayKubeReport, error) {
var report entities.PlayKubeReport
if options == nil {
options = new(KubeOptions)
}
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}

f, err := os.Open(path)
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
defer f.Close()

params, err := options.ToParams()
if err != nil {
Expand All @@ -46,7 +52,7 @@ func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.Pla
return nil, err
}

response, err := conn.DoRequest(ctx, f, http.MethodPost, "/play/kube", params, header)
response, err := conn.DoRequest(ctx, body, http.MethodPost, "/play/kube", params, header)
if err != nil {
return nil, err
}
Expand All @@ -60,12 +66,6 @@ func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.Pla
}

func KubeDown(ctx context.Context, path string) (*entities.PlayKubeReport, error) {
var report entities.PlayKubeReport
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}

f, err := os.Open(path)
if err != nil {
return nil, err
Expand All @@ -75,7 +75,18 @@ func KubeDown(ctx context.Context, path string) (*entities.PlayKubeReport, error
logrus.Warn(err)
}
}()
response, err := conn.DoRequest(ctx, f, http.MethodDelete, "/play/kube", nil, nil)

return KubeDownWithBody(ctx, f)
}

func KubeDownWithBody(ctx context.Context, body io.Reader) (*entities.PlayKubeReport, error) {
var report entities.PlayKubeReport
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}

response, err := conn.DoRequest(ctx, body, http.MethodDelete, "/play/kube", nil, nil)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/domain/entities/play.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entities

import (
"io"
"net"

"github.com/containers/image/v5/types"
Expand Down Expand Up @@ -54,6 +55,9 @@ type PlayKubeOptions struct {
LogOptions []string
// Start - don't start the pod if false
Start types.OptionalBool
// Body is a reader to read the body of the request from.
// Overrides the "path" variable if set.
Body io.Reader
}

// PlayKubePod represents a single pod and associated containers created by play kube
Expand Down Expand Up @@ -87,7 +91,11 @@ type PlayKubeReport struct {
}

// PlayKubeDownOptions are options for tearing down pods
type PlayKubeDownOptions struct{}
type PlayKubeDownOptions struct {
// Body is a reader to read the body of the request from.
// Overrides the "path" variable if set.
Body io.Reader
}

// PlayKubeDownReport contains the results of tearing down play kube
type PlayKubeTeardown struct {
Expand Down
11 changes: 9 additions & 2 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, path string, options en
report := &entities.PlayKubeReport{}
validKinds := 0

// read yaml document
content, err := ioutil.ReadFile(path)
var content []byte
var err error
if body := options.Body; body != nil {
// read body io.reader
content, err = ioutil.ReadAll(body)
} else {
// read yaml document
content, err = ioutil.ReadFile(path)
}
if err != nil {
return nil, err
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/domain/infra/tunnel/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, path string, opts entit
if start := opts.Start; start != types.OptionalBoolUndefined {
options.WithStart(start == types.OptionalBoolTrue)
}

if body := opts.Body; body != nil {
return play.KubeWithBody(ctx, body, options)
}

return play.Kube(ic.ClientCtx, path, options)
}

func (ic *ContainerEngine) PlayKubeDown(ctx context.Context, path string, _ entities.PlayKubeDownOptions) (*entities.PlayKubeReport, error) {
func (ic *ContainerEngine) PlayKubeDown(ctx context.Context, path string, opts entities.PlayKubeDownOptions) (*entities.PlayKubeReport, error) {
if opts.Body != nil {
return play.KubeDownWithBody(ctx, opts.Body)
}

return play.KubeDown(ic.ClientCtx, path)
}

0 comments on commit ba841aa

Please sign in to comment.