Skip to content

Commit

Permalink
Added sandbox command (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
yindia committed Jun 14, 2021
1 parent 517beb9 commit c5ab11a
Show file tree
Hide file tree
Showing 18 changed files with 988 additions and 33 deletions.
12 changes: 5 additions & 7 deletions flytectl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@ Generating docs locally can be accomplished by running make gendocs from within
## Installation

```bash
$ brew tap flyteorg/homebrew-tap
$ brew install flytectl
$ brew tap flyteorg/homebrew-tap/flytectl
```
## Get Started

### Create a sandbox cluster
```bash
$ docker run --rm --privileged -p 30081:30081 -p 30082:30082 -p 30084:30084 ghcr.io/flyteorg/flyte-sandbox
$ flytectl sandbox start
```

### Register your first workflow

### Register flytesnacks example
```bash
# Run Core workflows
$ flytectl register files https://github.com/flyteorg/flytesnacks/releases/download/v0.2.89/flytesnacks-core.tgz -d development -p flytesnacks --archive
# You can find all example at flytesnacks release page https://github.com/flyteorg/flytesnacks/releases/tag/v0.2.89
$ flytectl register examples -d development -p flytesnacks
```

## Contributing

[Contribution guidelines for this project](docs/CONTRIBUTING.md)
44 changes: 44 additions & 0 deletions flytectl/cmd/register/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package register

import (
"context"
"fmt"

rconfig "github.com/flyteorg/flytectl/cmd/config/subcommand/register"
cmdCore "github.com/flyteorg/flytectl/cmd/core"
)

const (
registerExampleShort = "Registers flytesnack example"
registerExampleLong = `
Registers all latest flytesnacks example
::
bin/flytectl register examples -d development -p flytesnacks
Usage
`
githubOrg = "flyteorg"
githubRepository = "flytesnacks"
archive = true
snackReleaseURL = "https://github.com/flyteorg/flytesnacks/releases/download/%s/flytesnacks-%s.tgz"
flyteManifest = "https://github.com/flyteorg/flytesnacks/releases/download/%s/flyte_tests_manifest.json"
)

func registerExamplesFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
flytesnacks, tag, err := getFlyteTestManifest()
if err != nil {
return err
}
rconfig.DefaultFilesConfig.Archive = archive
for _, v := range flytesnacks {
args := []string{
fmt.Sprintf(snackReleaseURL, tag, v.Name),
}
if err := Register(ctx, args, cmdCtx); err != nil {
return fmt.Errorf("Example %v failed to register %v", v.Name, err)
}
}
return nil
}
4 changes: 4 additions & 0 deletions flytectl/cmd/register/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Usage
)

func registerFromFilesFunc(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
return Register(ctx, args, cmdCtx)
}

func Register(ctx context.Context, args []string, cmdCtx cmdCore.CommandContext) error {
dataRefs, tmpDir, _err := getSortedFileList(ctx, args)
if _err != nil {
logger.Errorf(ctx, "error while un-archiving files in tmp dir due to %v", _err)
Expand Down
2 changes: 2 additions & 0 deletions flytectl/cmd/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func RemoteRegisterCommand() *cobra.Command {
registerResourcesFuncs := map[string]cmdcore.CommandEntry{
"files": {CmdFunc: registerFromFilesFunc, Aliases: []string{"file"}, PFlagProvider: rconfig.DefaultFilesConfig,
Short: registerFilesShort, Long: registerFilesLong},
"examples": {CmdFunc: registerExamplesFunc, Aliases: []string{"example", "flytesnack", "flytesnacks"}, PFlagProvider: rconfig.DefaultFilesConfig,
Short: registerExampleShort, Long: registerExampleLong},
}
cmdcore.AddCommands(registerCmd, registerResourcesFuncs)
return registerCmd
Expand Down
13 changes: 9 additions & 4 deletions flytectl/cmd/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ func TestRegisterCommand(t *testing.T) {
assert.Equal(t, registerCommand.Use, "register")
assert.Equal(t, registerCommand.Short, "Registers tasks/workflows/launchplans from list of generated serialized files.")
fmt.Println(registerCommand.Commands())
assert.Equal(t, len(registerCommand.Commands()), 1)
assert.Equal(t, len(registerCommand.Commands()), 2)
cmdNouns := registerCommand.Commands()
// Sort by Use value.
sort.Slice(cmdNouns, func(i, j int) bool {
return cmdNouns[i].Use < cmdNouns[j].Use
})
assert.Equal(t, cmdNouns[0].Use, "files")
assert.Equal(t, cmdNouns[0].Aliases, []string{"file"})
assert.Equal(t, cmdNouns[0].Short, "Registers file resources")

assert.Equal(t, cmdNouns[0].Use, "examples")
assert.Equal(t, cmdNouns[0].Aliases, []string{"example", "flytesnack", "flytesnacks"})
assert.Equal(t, cmdNouns[0].Short, "Registers flytesnack example")

assert.Equal(t, cmdNouns[1].Use, "files")
assert.Equal(t, cmdNouns[1].Aliases, []string{"file"})
assert.Equal(t, cmdNouns[1].Short, "Registers file resources")
}
43 changes: 43 additions & 0 deletions flytectl/cmd/register/register_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -13,6 +14,8 @@ import (
"sort"
"strings"

"github.com/google/go-github/github"

"github.com/flyteorg/flytectl/cmd/config"
rconfig "github.com/flyteorg/flytectl/cmd/config/subcommand/register"
cmdCore "github.com/flyteorg/flytectl/cmd/core"
Expand Down Expand Up @@ -43,6 +46,21 @@ type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

var FlyteSnacksRelease []FlyteSnack

// FlyteSnack Defines flyte test manifest structure
type FlyteSnack struct {
Name string `json:"name"`
Priority string `json:"priority"`
Path string `json:"path"`
ExitCondition Condition `json:"exitCondition"`
}

type Condition struct {
ExitSuccess bool `json:"exit_success"`
ExitMessage string `json:"exit_message"`
}

var httpClient HTTPClient

func init() {
Expand Down Expand Up @@ -395,3 +413,28 @@ func getJSONSpec(message proto.Message) string {
jsonSpec, _ := marshaller.MarshalToString(message)
return jsonSpec
}

func getFlyteTestManifest() ([]FlyteSnack, string, error) {
c := github.NewClient(nil)
opt := &github.ListOptions{Page: 1, PerPage: 1}
releases, _, err := c.Repositories.ListReleases(context.Background(), githubOrg, githubRepository, opt)
if err != nil {
return nil, "", err
}
response, err := http.Get(fmt.Sprintf(flyteManifest, *releases[0].TagName))
if err != nil {
return nil, "", err
}
defer response.Body.Close()

data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, "", err
}

err = json.Unmarshal(data, &FlyteSnacksRelease)
if err != nil {
return nil, "", err
}
return FlyteSnacksRelease, *releases[0].TagName, nil
}
8 changes: 8 additions & 0 deletions flytectl/cmd/register/register_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,11 @@ func TestHydrateLaunchPlanSpec(t *testing.T) {
assert.Equal(t, &admin.RawOutputDataConfig{OutputLocationPrefix: "prefix"}, lpSpec.RawOutputDataConfig)
})
}

func TestFlyteManifest(t *testing.T) {
flytesnacks, tag, err := getFlyteTestManifest()
assert.Nil(t, err)
assert.Contains(t, tag, "v")
assert.NotEmpty(t, tag)
assert.Greater(t, len(flytesnacks), 1)
}
3 changes: 3 additions & 0 deletions flytectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

"github.com/flyteorg/flytectl/cmd/sandbox"

f "github.com/flyteorg/flytectl/pkg/filesystemutils"

"github.com/flyteorg/flytectl/cmd/config"
Expand Down Expand Up @@ -58,6 +60,7 @@ func newRootCmd() *cobra.Command {
rootCmd.AddCommand(update.CreateUpdateCommand())
rootCmd.AddCommand(register.RemoteRegisterCommand())
rootCmd.AddCommand(delete.RemoteDeleteCommand())
rootCmd.AddCommand(sandbox.CreateSandboxCommand())
// Added version command
versioncmd := version.GetVersionCommand(rootCmd)
cmdCore.AddCommands(rootCmd, versioncmd)
Expand Down
45 changes: 45 additions & 0 deletions flytectl/cmd/sandbox/sandbox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package sandbox

import (
cmdcore "github.com/flyteorg/flytectl/cmd/core"
"github.com/spf13/cobra"
)

// Long descriptions are whitespace sensitive when generating docs using sphinx.
const (
sandboxShort = `Used for testing flyte sandbox.`
sandboxLong = `
Example Create sandbox cluster.
::
bin/flytectl sandbox start
Example Remove sandbox cluster.
::
bin/flytectl sandbox teardown
`
)

// CreateSandboxCommand will return sandbox command
func CreateSandboxCommand() *cobra.Command {
sandbox := &cobra.Command{
Use: "sandbox",
Short: sandboxShort,
Long: sandboxLong,
}

sandboxResourcesFuncs := map[string]cmdcore.CommandEntry{
"start": {CmdFunc: startSandboxCluster, Aliases: []string{}, ProjectDomainNotRequired: true,
Short: startShort,
Long: startLong},
"teardown": {CmdFunc: teardownSandboxCluster, Aliases: []string{}, ProjectDomainNotRequired: true,
Short: teardownShort,
Long: teardownLong},
}

cmdcore.AddCommands(sandbox, sandboxResourcesFuncs)

return sandbox
}
31 changes: 31 additions & 0 deletions flytectl/cmd/sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sandbox

import (
"fmt"
"sort"
"testing"

"gotest.tools/assert"
)

func TestCreateSandboxCommand(t *testing.T) {
sandboxCommand := CreateSandboxCommand()
assert.Equal(t, sandboxCommand.Use, "sandbox")
assert.Equal(t, sandboxCommand.Short, "Used for testing flyte sandbox.")
fmt.Println(sandboxCommand.Commands())
assert.Equal(t, len(sandboxCommand.Commands()), 2)
cmdNouns := sandboxCommand.Commands()
// Sort by Use value.
sort.Slice(cmdNouns, func(i, j int) bool {
return cmdNouns[i].Use < cmdNouns[j].Use
})

assert.Equal(t, cmdNouns[0].Use, "start")
assert.Equal(t, cmdNouns[0].Short, startShort)
assert.Equal(t, cmdNouns[0].Long, startLong)

assert.Equal(t, cmdNouns[1].Use, "teardown")
assert.Equal(t, cmdNouns[1].Short, teardownShort)
assert.Equal(t, cmdNouns[1].Long, teardownLong)

}
Loading

0 comments on commit c5ab11a

Please sign in to comment.