Skip to content

Commit

Permalink
Added source flags in sandbox (flyteorg#99)
Browse files Browse the repository at this point in the history
* added flags in sandbox

Signed-off-by: Yuvraj <code@evalsocket.dev>

* Set false debug defualt value

Signed-off-by: Yuvraj <code@evalsocket.dev>

* Remove debug flag

Signed-off-by: Yuvraj <code@evalsocket.dev>

* changes sandbox config name

Signed-off-by: Yuvraj <code@evalsocket.dev>

* Added test

Signed-off-by: Yuvraj <code@evalsocket.dev>

* Update sandbox logic to not overright user config

Signed-off-by: Yuvraj <code@evalsocket.dev>

* Added more unit test for sandbox

Signed-off-by: Yuvraj <code@evalsocket.dev>

* more unit test added

Signed-off-by: Yuvraj <code@evalsocket.dev>

* fixed unit test

Signed-off-by: Yuvraj <code@evalsocket.dev>

* more unit test

Signed-off-by: Yuvraj <code@evalsocket.dev>

* more unit test fix

Signed-off-by: Yuvraj <code@evalsocket.dev>

* more unit test fix

Signed-off-by: Yuvraj <code@evalsocket.dev>

* added more unit test

Signed-off-by: Yuvraj <code@evalsocket.dev>

* more changes

Signed-off-by: Yuvraj <code@evalsocket.dev>
  • Loading branch information
yindia committed Jun 18, 2021
1 parent 0ea6da6 commit d00a9db
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 90 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/cache@v2
with:
fetch-depth: "0"
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }}
- name: Run GoReleaser dry run
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: --snapshot --skip-publish --rm-dist
args: --snapshot --skip-publish --rm-dist
- name: Unit Tests
uses: cedrickring/golang-action@1.5.2
env:
GO111MODULE: "on"
CI_ENV: "true"
with:
args: make install && make test_unit_codecov
- name: Push CodeCov
Expand Down
55 changes: 55 additions & 0 deletions cmd/config/subcommand/sandbox/config_flags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions cmd/config/subcommand/sandbox/config_flags_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cmd/config/subcommand/sandbox/sandbox_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sandbox

//go:generate pflags Config --default-var DefaultConfig
var (
DefaultConfig = &Config{}
)

// Config
type Config struct {
SnacksRepo string `json:"flytesnacks" pflag:", Path of your flytesnacks repository"`
}
3 changes: 2 additions & 1 deletion cmd/sandbox/sandbox.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sandbox

import (
sandboxConfig "github.com/flyteorg/flytectl/cmd/config/subcommand/sandbox"
cmdcore "github.com/flyteorg/flytectl/cmd/core"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func CreateSandboxCommand() *cobra.Command {
sandboxResourcesFuncs := map[string]cmdcore.CommandEntry{
"start": {CmdFunc: startSandboxCluster, Aliases: []string{}, ProjectDomainNotRequired: true,
Short: startShort,
Long: startLong},
Long: startLong, PFlagProvider: sandboxConfig.DefaultConfig},
"teardown": {CmdFunc: teardownSandboxCluster, Aliases: []string{}, ProjectDomainNotRequired: true,
Short: teardownShort,
Long: teardownLong},
Expand Down
81 changes: 40 additions & 41 deletions cmd/sandbox/sandbox_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,32 @@ import (
"os"
"strings"

cmdUtil "github.com/flyteorg/flytectl/pkg/commandutils"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"

"github.com/enescakir/emoji"
f "github.com/flyteorg/flytectl/pkg/filesystemutils"
)

var (
Kubeconfig = f.FilePathJoin(f.UserHomeDir(), ".flyte", "k3s", "k3s.yaml")
FlytectlConfig = f.FilePathJoin(f.UserHomeDir(), ".flyte", "config.yaml")
SuccessMessage = "Flyte is ready! Flyte UI is available at http://localhost:30081/console"
ImageName = "ghcr.io/flyteorg/flyte-sandbox:dind"
SandboxClusterName = "flyte-sandbox"
Environment = []string{"SANDBOX=1", "KUBERNETES_API_PORT=30086", "FLYTE_HOST=localhost:30081", "FLYTE_AWS_ENDPOINT=http://localhost:30084"}
Kubeconfig = f.FilePathJoin(f.UserHomeDir(), ".flyte", "k3s", "k3s.yaml")
FlytectlConfig = f.FilePathJoin(f.UserHomeDir(), ".flyte", "config-sandbox.yaml")
SuccessMessage = "Flyte is ready! Flyte UI is available at http://localhost:30081/console"
ImageName = "ghcr.io/flyteorg/flyte-sandbox:dind"
flyteSandboxClusterName = "flyte-sandbox"
Environment = []string{"SANDBOX=1", "KUBERNETES_API_PORT=30086", "FLYTE_HOST=localhost:30081", "FLYTE_AWS_ENDPOINT=http://localhost:30084"}
flyteSnackDir = "/usr/src"
K3sDir = "/etc/rancher/"
)

func setupFlytectlConfig() error {

_ = os.MkdirAll(f.FilePathJoin(f.UserHomeDir(), ".flyte"), 0755)

response, err := http.Get("https://raw.githubusercontent.com/flyteorg/flytectl/master/config.yaml")
if err != nil {
return err
Expand All @@ -41,11 +47,7 @@ func setupFlytectlConfig() error {
return err
}

err = ioutil.WriteFile(FlytectlConfig, data, 0600)
if err != nil {
fmt.Printf("Please create ~/.flyte dir %v \n", emoji.ManTechnologist)
return err
}
_ = ioutil.WriteFile(FlytectlConfig, data, 0600)
return nil
}

Expand All @@ -62,21 +64,31 @@ func configCleanup() error {
}

func getSandbox(cli *client.Client) *types.Container {
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
containers, _ := cli.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
})
if err != nil {
return nil
}
for _, v := range containers {
if strings.Contains(v.Names[0], SandboxClusterName) {
if strings.Contains(v.Names[0], flyteSandboxClusterName) {
return &v
}
}
return nil
}

func startContainer(cli *client.Client) (string, error) {
func removeSandboxIfExist(cli *client.Client, reader io.Reader) error {
if c := getSandbox(cli); c != nil {
if cmdUtil.AskForConfirmation("delete existing sandbox cluster", reader) {
err := cli.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{
Force: true,
})
return err
}
os.Exit(0)
}
return nil
}

func startContainer(cli *client.Client, volumes []mount.Mount) (string, error) {
ExposedPorts, PortBindings, _ := nat.ParsePortSpecs([]string{
"127.0.0.1:30086:30086",
"127.0.0.1:30081:30081",
Expand All @@ -87,38 +99,23 @@ func startContainer(cli *client.Client) (string, error) {
if err != nil {
return "", err
}

if _, err := io.Copy(os.Stdout, r); err != nil {
return "", err
}

_, _ = io.Copy(os.Stdout, r)
resp, err := cli.ContainerCreate(context.Background(), &container.Config{
Env: Environment,
Image: ImageName,
Tty: false,
ExposedPorts: ExposedPorts,
}, &container.HostConfig{
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: f.FilePathJoin(f.UserHomeDir(), ".flyte"),
Target: "/etc/rancher/",
},
// TODO (Yuvraj) Add flytectl config in sandbox and mount with host file system
//{
// Type: mount.TypeBind,
// Source: f.FilePathJoin(f.UserHomeDir(), ".flyte", "config.yaml"),
// Target: "/.flyte/",
//},
},
Mounts: volumes,
PortBindings: PortBindings,
Privileged: true,
}, nil,
nil, SandboxClusterName)
nil, flyteSandboxClusterName)

if err != nil {
return "", err
}

go watchError(cli, resp.ID)
if err := cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
return "", err
}
Expand All @@ -127,6 +124,7 @@ func startContainer(cli *client.Client) (string, error) {

func watchError(cli *client.Client, id string) {
statusCh, errCh := cli.ContainerWait(context.Background(), id, container.WaitConditionNotRunning)

select {
case err := <-errCh:
if err != nil {
Expand All @@ -136,7 +134,7 @@ func watchError(cli *client.Client, id string) {
}
}

func readLogs(cli *client.Client, id string) error {
func readLogs(cli *client.Client, id, message string) error {
reader, err := cli.ContainerLogs(context.Background(), id, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
Expand All @@ -147,9 +145,10 @@ func readLogs(cli *client.Client, id string) error {
return err
}
scanner := bufio.NewScanner(reader)

for scanner.Scan() {
if strings.Contains(scanner.Text(), SuccessMessage) {
fmt.Printf("%v %v %v %v %v \n", emoji.ManTechnologist, SuccessMessage, emoji.Rocket, emoji.Rocket, emoji.PartyPopper)
if strings.Contains(scanner.Text(), message) {
fmt.Printf("%v %v %v %v %v \n", emoji.ManTechnologist, message, emoji.Rocket, emoji.Rocket, emoji.PartyPopper)
fmt.Printf("Please visit https://github.com/flyteorg/flytesnacks for more example %v \n", emoji.Rocket)
fmt.Printf("Register all flytesnacks example by running 'flytectl register examples -d development -p flytesnacks' \n")
break
Expand Down
Loading

0 comments on commit d00a9db

Please sign in to comment.