From 512384722a5b123d4193a365768faf7988d4448d Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Fri, 3 Nov 2017 19:52:20 +0800 Subject: [PATCH] Make the internal URL of UI and JobService configurable --- make/common/templates/adminserver/env | 2 ++ src/adminserver/systemcfg/systemcfg.go | 5 +++++ src/common/const.go | 2 ++ src/common/utils/test/adminserver.go | 2 ++ src/jobservice/config/config.go | 9 ++++++++- src/jobservice/config/config_test.go | 3 +++ src/ui/config/config.go | 18 ++++++++++++++++-- src/ui/config/config_test.go | 4 ++++ 8 files changed, 42 insertions(+), 3 deletions(-) diff --git a/make/common/templates/adminserver/env b/make/common/templates/adminserver/env index 7e0a9e242..a1b21104b 100644 --- a/make/common/templates/adminserver/env +++ b/make/common/templates/adminserver/env @@ -42,3 +42,5 @@ RESET=false UAA_ENDPOINT=$uaa_endpoint UAA_CLIENTID=$uaa_clientid UAA_CLIENTSECRET=$uaa_clientsecret +UI_URL=http://ui +JOBSERVICE_URL=http://jobservice diff --git a/src/adminserver/systemcfg/systemcfg.go b/src/adminserver/systemcfg/systemcfg.go index c599191ec..9cbb5d4c8 100644 --- a/src/adminserver/systemcfg/systemcfg.go +++ b/src/adminserver/systemcfg/systemcfg.go @@ -123,6 +123,11 @@ var ( parse: parseStringToBool, }, common.ClairDBPassword: "CLAIR_DB_PASSWORD", + common.UAAEndpoint: "UAA_ENDPOINT", + common.UAAClientID: "UAA_CLIENTID", + common.UAAClientSecret: "UAA_CLIENTSECRET", + common.UIURL: "UI_URL", + common.JobServiceURL: "JOBSERVICE_URL", } // configurations need read from environment variables diff --git a/src/common/const.go b/src/common/const.go index f8786c0a8..ae39ee3ea 100644 --- a/src/common/const.go +++ b/src/common/const.go @@ -38,6 +38,8 @@ const ( MySQLDatabase = "mysql_database" SQLiteFile = "sqlite_file" SelfRegistration = "self_registration" + UIURL = "ui_url" + JobServiceURL = "jobservice_url" LDAPURL = "ldap_url" LDAPSearchDN = "ldap_search_dn" LDAPSearchPwd = "ldap_search_password" diff --git a/src/common/utils/test/adminserver.go b/src/common/utils/test/adminserver.go index 5ae720fb5..a5382ce5f 100644 --- a/src/common/utils/test/adminserver.go +++ b/src/common/utils/test/adminserver.go @@ -63,6 +63,8 @@ var adminServerDefaultConfig = map[string]interface{}{ common.UAAClientID: "testid", common.UAAClientSecret: "testsecret", common.UAAEndpoint: "10.192.168.5", + common.UIURL: "http://myui:8888/", + common.JobServiceURL: "http://myjob:8888/", } // NewAdminserver returns a mock admin server diff --git a/src/jobservice/config/config.go b/src/jobservice/config/config.go index 243d212d1..1d82393a1 100644 --- a/src/jobservice/config/config.go +++ b/src/jobservice/config/config.go @@ -17,6 +17,7 @@ package config import ( "fmt" "os" + "strings" "github.com/vmware/harbor/src/adminserver/client" "github.com/vmware/harbor/src/adminserver/client/auth" @@ -107,7 +108,13 @@ func MaxJobWorkers() (int, error) { // LocalUIURL returns the local ui url, job service will use this URL to call API hosted on ui process func LocalUIURL() string { - return "http://ui" + cfg, err := mg.Get() + if err != nil { + log.Warningf("Failed to Get job service UI URL from backend, error: %v, will return default value.") + return "http://ui" + } + return strings.TrimSuffix(cfg[common.UIURL].(string), "/") + } // LocalRegURL returns the local registry url, job service will use this URL to pull image from the registry diff --git a/src/jobservice/config/config_test.go b/src/jobservice/config/config_test.go index c66a746bc..746096d0a 100644 --- a/src/jobservice/config/config_test.go +++ b/src/jobservice/config/config_test.go @@ -18,6 +18,7 @@ import ( "os" "testing" + "github.com/stretchr/testify/assert" "github.com/vmware/harbor/src/common/utils/test" ) @@ -40,6 +41,7 @@ func TestConfig(t *testing.T) { return } defer os.Remove(secretKeyPath) + assert := assert.New(t) if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil { t.Fatalf("failed to set env %s: %v", "KEY_PATH", err) @@ -76,4 +78,5 @@ func TestConfig(t *testing.T) { if _, err := ExtEndpoint(); err != nil { t.Fatalf("failed to get ext endpoint: %v", err) } + assert.Equal("http://myui:8888", LocalUIURL()) } diff --git a/src/ui/config/config.go b/src/ui/config/config.go index 5603622ef..4bb25e09f 100644 --- a/src/ui/config/config.go +++ b/src/ui/config/config.go @@ -244,12 +244,26 @@ func RegistryURL() (string, error) { // InternalJobServiceURL returns jobservice URL for internal communication between Harbor containers func InternalJobServiceURL() string { - return "http://jobservice" + cfg, err := mg.Get() + if err != nil { + log.Warningf("Failed to Get job service URL from backend, error: %v, will return default value.") + + return "http://jobservice" + } + return strings.TrimSuffix(cfg[common.JobServiceURL].(string), "/") } // InternalTokenServiceEndpoint returns token service endpoint for internal communication between Harbor containers func InternalTokenServiceEndpoint() string { - return "http://ui/service/token" + uiURL := "http://ui" + cfg, err := mg.Get() + if err != nil { + log.Warningf("Failed to Get job service UI URL from backend, error: %v, will use default value.") + + } else { + uiURL = cfg[common.UIURL].(string) + } + return strings.TrimSuffix(uiURL, "/") + "/service/token" } // InternalNotaryEndpoint returns notary server endpoint for internal communication between Harbor containers diff --git a/src/ui/config/config_test.go b/src/ui/config/config_test.go index 5d37c40ed..8788b3f84 100644 --- a/src/ui/config/config_test.go +++ b/src/ui/config/config_test.go @@ -17,6 +17,7 @@ import ( "os" "testing" + "github.com/stretchr/testify/assert" "github.com/vmware/harbor/src/common/utils/test" ) @@ -39,6 +40,7 @@ func TestConfig(t *testing.T) { return } defer os.Remove(secretKeyPath) + assert := assert.New(t) if err := os.Setenv("KEY_PATH", secretKeyPath); err != nil { t.Fatalf("failed to set env %s: %v", "KEY_PATH", err) @@ -164,5 +166,7 @@ func TestConfig(t *testing.T) { if us.ClientID != "testid" || us.ClientSecret != "testsecret" || us.Endpoint != "10.192.168.5" { t.Errorf("Unexpected UAA setting: %+v", *us) } + assert.Equal("http://myjob:8888", InternalJobServiceURL()) + assert.Equal("http://myui:8888/service/token", InternalTokenServiceEndpoint()) }