Skip to content

Commit

Permalink
Improve flytectl config init UX (flyteorg#241)
Browse files Browse the repository at this point in the history
* Fix flytectl ux issue

Signed-off-by: Yuvraj <[email protected]>
  • Loading branch information
yindia authored Dec 27, 2021
1 parent 50a91fb commit 7625fc6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/config/subcommand/config/init_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
//go:generate pflags Config --default-var DefaultConfig --bind-default-var
var (
DefaultConfig = &Config{
Insecure: true,
Insecure: false,
Storage: false,
}
)
Expand Down
51 changes: 44 additions & 7 deletions cmd/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package configuration

import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"regexp"
"strings"

"github.com/flyteorg/flytestdlib/logger"
Expand All @@ -31,18 +34,24 @@ Generates sandbox config. Flyte Sandbox is a fully standalone minimal environmen
::
flytectl configuration config
flytectl config init
Generates remote cluster config. Read more about the remote deployment https://docs.flyte.org/en/latest/deployment/index.html
Generates remote cluster config, By default connection is secure. Read more about the remote deployment https://docs.flyte.org/en/latest/deployment/index.html
::
flytectl configuration config --host=flyte.myexample.com
flytectl config init --host=flyte.myexample.com
Generates remote cluster config with insecure connection
::
flytectl config init --host=flyte.myexample.com --insecure
Generates FlyteCTL config with a storage provider
::
flytectl configuration config --host=flyte.myexample.com --storage
flytectl config init --host=flyte.myexample.com --storage
`
)

Expand All @@ -51,6 +60,8 @@ var prompt = promptui.Select{
Items: []string{"S3", "GCS"},
}

var endpointPrefix = [3]string{"dns://", "http://", "https://"}

// CreateConfigCommand will return configuration command
func CreateConfigCommand() *cobra.Command {
configCmd := viper.GetConfigCommand()
Expand All @@ -77,12 +88,18 @@ func initFlytectlConfig(ctx context.Context, reader io.Reader) error {

templateValues := configutil.ConfigTemplateSpec{
Host: "dns:///localhost:30081",
Insecure: initConfig.DefaultConfig.Insecure,
Insecure: true,
}
templateStr := configutil.GetSandboxTemplate()

if len(initConfig.DefaultConfig.Host) > 0 {
templateValues.Host = fmt.Sprintf("dns:///%v", initConfig.DefaultConfig.Host)
trimHost := trim(initConfig.DefaultConfig.Host)
host := strings.Split(trimHost, ":")
if !validateEndpointName(host[0]) {
return errors.New("Please use a valid endpoint")
}
templateValues.Host = fmt.Sprintf("dns://%s", trimHost)
templateValues.Insecure = initConfig.DefaultConfig.Insecure
templateStr = configutil.AdminConfigTemplate
if initConfig.DefaultConfig.Storage {
templateStr = configutil.GetAWSCloudTemplate()
Expand Down Expand Up @@ -114,3 +131,23 @@ func initFlytectlConfig(ctx context.Context, reader io.Reader) error {
fmt.Printf("Init flytectl config file at [%s]", configutil.ConfigFile)
return nil
}

func trim(hostname string) string {
for _, prefix := range endpointPrefix {
hostname = strings.TrimPrefix(hostname, prefix)
}
return hostname

}

func validateEndpointName(domain string) bool {
RegExp := regexp.MustCompile(`^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z
]{2,3})$`)
if RegExp.MatchString(domain) || domain == "localhost" {
return true
}
if net.ParseIP(domain) == nil {
return false
}
return true
}
18 changes: 17 additions & 1 deletion cmd/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,24 @@ func TestSetupConfigFunc(t *testing.T) {
assert.Nil(t, initFlytectlConfig(ctx, yes))
assert.Nil(t, initFlytectlConfig(ctx, yes))
assert.Nil(t, initFlytectlConfig(ctx, no))
initConfig.DefaultConfig.Host = "test"
initConfig.DefaultConfig.Host = "flyte.org"
assert.Nil(t, initFlytectlConfig(ctx, no))
initConfig.DefaultConfig.Host = "localhost:30081"
assert.Nil(t, initFlytectlConfig(ctx, no))
initConfig.DefaultConfig.Storage = true
assert.NotNil(t, initFlytectlConfig(ctx, yes))
}

func TestTrimFunc(t *testing.T) {
assert.Equal(t, trim("dns://localhost"), "localhost")
assert.Equal(t, trim("http://localhost"), "localhost")
assert.Equal(t, trim("https://localhost"), "localhost")
}

func TestValidateEndpointName(t *testing.T) {
assert.Equal(t, validateEndpointName("127.0.0.1"), true)
assert.Equal(t, validateEndpointName("127.0.0.1.0"), false)
assert.Equal(t, validateEndpointName("localhost"), true)
assert.Equal(t, validateEndpointName("flyte.org"), true)
assert.Equal(t, validateEndpointName("flyte"), false)
}

0 comments on commit 7625fc6

Please sign in to comment.