Skip to content

Commit

Permalink
support wasm in eval and address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed Aug 17, 2022
1 parent 9fbe307 commit 67aed17
Show file tree
Hide file tree
Showing 17 changed files with 729 additions and 713 deletions.
11 changes: 3 additions & 8 deletions commands/wasmcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package commands

import (
"context"
"flag"

"github.com/GoogleContainerTools/kpt/internal/cmdwasmpull"
"github.com/GoogleContainerTools/kpt/internal/cmdwasmpush"
Expand All @@ -25,7 +24,7 @@ import (
)

func NewWasmCommand(ctx context.Context, version string) *cobra.Command {
repo := &cobra.Command{
wasmcmd := &cobra.Command{
Use: "wasm",
Short: "[Alpha] " + wasmdocs.WasmShort,
Long: "[Alpha] " + wasmdocs.WasmLong,
Expand All @@ -41,14 +40,10 @@ func NewWasmCommand(ctx context.Context, version string) *cobra.Command {
},
}

pf := repo.PersistentFlags()

pf.AddGoFlagSet(flag.CommandLine)

repo.AddCommand(
wasmcmd.AddCommand(
cmdwasmpull.NewCommand(ctx),
cmdwasmpush.NewCommand(ctx),
)

return repo
return wasmcmd
}
4 changes: 4 additions & 0 deletions internal/cmdrender/cmdrender.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func NewRunner(ctx context.Context, parent string) *Runner {
fmt.Sprintf("pull image before running the container. It must be one of %s, %s and %s.", fnruntime.AlwaysPull, fnruntime.IfNotPresentPull, fnruntime.NeverPull))
c.Flags().BoolVar(&r.allowExec, "allow-exec", false,
"allow binary executable to be run during pipeline execution.")
c.Flags().BoolVar(&r.allowWasm, "allow-alpha-wasm", false,
"allow wasm to be used during pipeline execution.")
cmdutil.FixDocs("kpt", parent, c)
r.Command = c
return r
Expand All @@ -67,6 +69,7 @@ type Runner struct {
resultsDirPath string
imagePullPolicy string
allowExec bool
allowWasm bool
dest string
Command *cobra.Command
ctx context.Context
Expand Down Expand Up @@ -121,6 +124,7 @@ func (r *Runner) runE(c *cobra.Command, _ []string) error {
Output: output,
ImagePullPolicy: cmdutil.StringToImagePullPolicy(r.imagePullPolicy),
AllowExec: r.allowExec,
AllowWasm: r.allowWasm,
FileSystem: filesys.FileSystemOrOnDisk{},
}
if err := executor.Execute(r.ctx); err != nil {
Expand Down
22 changes: 14 additions & 8 deletions internal/cmdwasmpull/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"io"
"os"
"path"
"path/filepath"

"github.com/GoogleContainerTools/kpt/internal/docs/generated/wasmdocs"
"github.com/GoogleContainerTools/kpt/internal/errors"
Expand All @@ -31,13 +31,8 @@ const (
)

func newRunner(ctx context.Context) *runner {
storeClient, err := wasm.NewClient(path.Join(os.TempDir(), "kpt"))
if err != nil {
panic(err)
}
r := &runner{
ctx: ctx,
storageClient: storeClient,
ctx: ctx,
}
c := &cobra.Command{
Use: "pull [IMAGE] [LOCAL_PATH]",
Expand Down Expand Up @@ -67,6 +62,14 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error {
return errors.E(op, "2 positional arguments (a OCI image and a local path) required")
}

var err error
if r.storageClient == nil {
r.storageClient, err = wasm.NewClient(filepath.Join(os.TempDir(), "kpt"))
if err != nil {
return err
}
}

wasmImg := args[0]
fileName := args[1]
wasmFileReadCloser, err := r.storageClient.LoadWasm(r.ctx, wasmImg)
Expand All @@ -79,8 +82,11 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error {
if err != nil {
return errors.E(op, "unable to read wasm contents")
}
err = os.WriteFile(fileName, data, 0666)
err = os.MkdirAll(filepath.Dir(fileName), 0755)
if err != nil {
return errors.E(op, "unable to create directory %q: %w", filepath.Dir(fileName), err)
}
if err = os.WriteFile(fileName, data, 0666); err != nil {
return errors.E(op, "unable to write to file", fileName)
}
return nil
Expand Down
17 changes: 10 additions & 7 deletions internal/cmdwasmpush/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ const (
)

func newRunner(ctx context.Context) *runner {
storeClient, err := wasm.NewClient(path.Join(os.TempDir(), "kpt"))
if err != nil {
panic(err)
}
r := &runner{
ctx: ctx,
storageClient: storeClient,
ctx: ctx,
}
c := &cobra.Command{
Use: "push [LOCAL_PATH] [IMAGE]",
Expand Down Expand Up @@ -67,10 +62,18 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error {
return errors.E(op, "2 positional arguments (local wasm file and OCI image) are required")
}

var err error
if r.storageClient == nil {
r.storageClient, err = wasm.NewClient(path.Join(os.TempDir(), "kpt"))
if err != nil {
return err
}
}

wasmFile := args[0]
img := args[1]

err := r.storageClient.PushWasm(r.ctx, wasmFile, img)
err = r.storageClient.PushWasm(r.ctx, wasmFile, img)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 67aed17

Please sign in to comment.