Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Add ENV VAR FLTYE_ADMIN_ENDPOINT (#4948) #466

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package config

import (
"context"
"net/url"
"os"
"testing"

"github.com/flyteorg/flyte/flyteidl/clients/go/admin"
"github.com/flyteorg/flytectl/pkg/printer"
"github.com/stretchr/testify/assert"
)
Expand All @@ -28,5 +32,21 @@ func TestInvalidOutputFormat(t *testing.T) {
}
}()
result = c.MustOutputFormat()
}

func TestUpdateConfigWithEnvVar(t *testing.T) {
originalValue := os.Getenv("FLYTE_ADMIN_ENDPOINT")
defer os.Setenv("FLYTE_ADMIN_ENDPOINT", originalValue)

dummyURL := "dns://dummyHost"
os.Setenv("FLYTE_ADMIN_ENDPOINT", dummyURL)

parsedDummyURL, _ := url.Parse(dummyURL)

adminCfg := admin.GetConfig(context.Background())

assert.NotEqual(t, adminCfg.Endpoint.URL, *parsedDummyURL)
err := UpdateConfigWithEnvVar()
assert.Nil(t, err)
assert.Equal(t, adminCfg.Endpoint.URL, *parsedDummyURL)
}
46 changes: 46 additions & 0 deletions cmd/config/env_var_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"context"
"fmt"
"net/url"
"os"

"github.com/flyteorg/flyte/flyteidl/clients/go/admin"
"github.com/flyteorg/flyte/flytestdlib/config"
)

const flyteAdminEndpoint = "FLYTE_ADMIN_ENDPOINT"
zychen5186 marked this conversation as resolved.
Show resolved Hide resolved

type UpdateFunc func(context.Context) error

zychen5186 marked this conversation as resolved.
Show resolved Hide resolved
var envToUpdateFunc = map[string]UpdateFunc{flyteAdminEndpoint: updateAdminEndpoint}

func UpdateConfigWithEnvVar() error {
ctx := context.Background()

for envVar, updateFunc := range envToUpdateFunc {
if os.Getenv(envVar) != "" {
if err := updateFunc(ctx); err != nil {
return fmt.Errorf("error update config with env var: %v", err)
}

Check warning on line 26 in cmd/config/env_var_reader.go

View check run for this annotation

Codecov / codecov/patch

cmd/config/env_var_reader.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}
}
return nil
}

func updateAdminEndpoint(ctx context.Context) error {
cfg := admin.GetConfig(ctx)

if len(os.Getenv(flyteAdminEndpoint)) > 0 {
envEndpoint, err := url.Parse(os.Getenv(flyteAdminEndpoint))
if err != nil {
return fmt.Errorf("error parsing env var %v: %v", flyteAdminEndpoint, err)
}

Check warning on line 39 in cmd/config/env_var_reader.go

View check run for this annotation

Codecov / codecov/patch

cmd/config/env_var_reader.go#L38-L39

Added lines #L38 - L39 were not covered by tests
cfg.Endpoint = config.URL{URL: *envEndpoint}
if err := admin.SetConfig(cfg); err != nil {
return err
}

Check warning on line 43 in cmd/config/env_var_reader.go

View check run for this annotation

Codecov / codecov/patch

cmd/config/env_var_reader.go#L42-L43

Added lines #L42 - L43 were not covered by tests
}
return nil
}
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
return err
}

if err := config.UpdateConfigWithEnvVar(); err != nil {
return err
}

Check warning on line 139 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L137-L139

Added lines #L137 - L139 were not covered by tests

return nil
}

Expand Down
Loading