From 97154b81b9c38449a763a3b7bb0128a5ee985d52 Mon Sep 17 00:00:00 2001 From: Jakub Dyszkiewicz Date: Tue, 12 Nov 2019 18:02:23 +0100 Subject: [PATCH 1/2] feat(kuma-cp) serve config for gui --- app/kuma-ui/pkg/server/server.go | 27 ++++++++++++++++- app/kuma-ui/pkg/server/server_test.go | 34 ++++++++++++++++++++++ app/kuma-ui/pkg/server/types/gui_config.go | 6 ++++ pkg/config/gui-server/config.go | 11 ++++++- pkg/core/bootstrap/autoconfig.go | 9 ++++++ pkg/core/bootstrap/autoconfig_test.go | 22 ++++++++++++++ 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 app/kuma-ui/pkg/server/types/gui_config.go diff --git a/app/kuma-ui/pkg/server/server.go b/app/kuma-ui/pkg/server/server.go index fb6ae7937feb..512072f3828b 100644 --- a/app/kuma-ui/pkg/server/server.go +++ b/app/kuma-ui/pkg/server/server.go @@ -2,8 +2,10 @@ package server import ( "context" + "encoding/json" "fmt" "github.com/Kong/kuma/app/kuma-ui/pkg/resources" + "github.com/Kong/kuma/app/kuma-ui/pkg/server/types" gui_server "github.com/Kong/kuma/pkg/config/gui-server" "github.com/Kong/kuma/pkg/core" core_runtime "github.com/Kong/kuma/pkg/core/runtime" @@ -29,9 +31,13 @@ var _ core_runtime.Component = &Server{} func (g *Server) Start(stop <-chan struct{}) error { fileServer := http.FileServer(resources.GuiDir) + mux := http.NewServeMux() + mux.Handle("/", fileServer) + mux.HandleFunc("/config", g.configHandler) + guiServer := &http.Server{ Addr: fmt.Sprintf(":%d", g.Config.Port), - Handler: fileServer, + Handler: mux, } errChan := make(chan error) @@ -57,3 +63,22 @@ func (g *Server) Start(stop <-chan struct{}) error { return err } } + +func (g *Server) configHandler(writer http.ResponseWriter, request *http.Request) { + bytes, err := json.Marshal(fromServerConfig(*g.Config.GuiConfig)) + if err != nil { + log.Error(err, "could not marshall config") + writer.WriteHeader(500) + return + } + if _, err := writer.Write(bytes); err != nil { + log.Error(err, "could not write the response") + } +} + +func fromServerConfig(config gui_server.GuiConfig) types.GuiConfig { + return types.GuiConfig{ + ApiUrl: config.ApiUrl, + Environment: config.Environment, + } +} diff --git a/app/kuma-ui/pkg/server/server_test.go b/app/kuma-ui/pkg/server/server_test.go index 8e08d1387ecc..0bac50a98715 100644 --- a/app/kuma-ui/pkg/server/server_test.go +++ b/app/kuma-ui/pkg/server/server_test.go @@ -1,8 +1,10 @@ package server_test import ( + "encoding/json" "fmt" "github.com/Kong/kuma/app/kuma-ui/pkg/server" + "github.com/Kong/kuma/app/kuma-ui/pkg/server/types" gui_server "github.com/Kong/kuma/pkg/config/gui-server" "github.com/Kong/kuma/pkg/test" . "github.com/onsi/ginkgo" @@ -19,6 +21,11 @@ var _ = Describe("GUI Server", func() { var stop chan struct{} var baseUrl string + guiConfig := types.GuiConfig{ + ApiUrl: "http://kuma.internal:5681", + Environment: "kubernetes", + } + BeforeEach(func() { port, err := test.GetFreePort() Expect(err).ToNot(HaveOccurred()) @@ -27,6 +34,10 @@ var _ = Describe("GUI Server", func() { srv := server.Server{ Config: &gui_server.GuiServerConfig{ Port: uint32(port), + GuiConfig: &gui_server.GuiConfig{ + ApiUrl: "http://kuma.internal:5681", + Environment: "kubernetes", + }, }, } stop = make(chan struct{}) @@ -88,4 +99,27 @@ var _ = Describe("GUI Server", func() { expectedFile: "data.js", }), ) + + It("should serve the gui config", func() { + // when + resp, err := http.Get(fmt.Sprintf("%s/config", baseUrl)) + + // then + Expect(err).ToNot(HaveOccurred()) + + // when + received, err := ioutil.ReadAll(resp.Body) + + // then + Expect(resp.Body.Close()).To(Succeed()) + Expect(err).ToNot(HaveOccurred()) + + // when + cfg := types.GuiConfig{} + Expect(json.Unmarshal(received, &cfg)).To(Succeed()) + + // then + Expect(cfg).To(Equal(guiConfig)) + }) + }) diff --git a/app/kuma-ui/pkg/server/types/gui_config.go b/app/kuma-ui/pkg/server/types/gui_config.go new file mode 100644 index 000000000000..50a0f94f4e1c --- /dev/null +++ b/app/kuma-ui/pkg/server/types/gui_config.go @@ -0,0 +1,6 @@ +package types + +type GuiConfig struct { + ApiUrl string `json:"apiUrl"` + Environment string `json:"environment"` +} diff --git a/pkg/config/gui-server/config.go b/pkg/config/gui-server/config.go index b7fc6b13fd9e..1e0d27f970e1 100644 --- a/pkg/config/gui-server/config.go +++ b/pkg/config/gui-server/config.go @@ -9,6 +9,8 @@ import ( type GuiServerConfig struct { // Port on which the server is exposed Port uint32 `yaml:"port" envconfig:"kuma_gui_server_port"` + // Config of the GUI itself + GuiConfig *GuiConfig } func (g *GuiServerConfig) Validate() error { @@ -22,6 +24,13 @@ var _ config.Config = &GuiServerConfig{} func DefaultGuiServerConfig() *GuiServerConfig { return &GuiServerConfig{ - Port: 5683, + Port: 5683, + GuiConfig: &GuiConfig{}, } } + +// Not yet exposed via YAML and env vars on purpose. All of those are autoconfigured +type GuiConfig struct { + ApiUrl string + Environment string +} diff --git a/pkg/core/bootstrap/autoconfig.go b/pkg/core/bootstrap/autoconfig.go index d4ef2743a121..bcf4f86fd2e5 100644 --- a/pkg/core/bootstrap/autoconfig.go +++ b/pkg/core/bootstrap/autoconfig.go @@ -3,6 +3,7 @@ package bootstrap import ( "fmt" "github.com/Kong/kuma/pkg/config/api-server/catalogue" + gui_server "github.com/Kong/kuma/pkg/config/gui-server" token_server "github.com/Kong/kuma/pkg/config/token-server" "io/ioutil" "os" @@ -20,6 +21,7 @@ var autoconfigureLog = core.Log.WithName("bootstrap").WithName("auto-configure") func autoconfigure(cfg *kuma_cp.Config) error { autoconfigureDataplaneTokenServer(cfg.DataplaneTokenServer) autoconfigureCatalogue(cfg) + autoconfigureGui(cfg) return autoconfigureSds(cfg) } @@ -71,6 +73,13 @@ func autoconfigureDataplaneTokenServer(cfg *token_server.DataplaneTokenServerCon } } +func autoconfigureGui(cfg *kuma_cp.Config) { + cfg.GuiServer.GuiConfig = &gui_server.GuiConfig{ + ApiUrl: fmt.Sprintf("http://%s:%d", cfg.General.AdvertisedHostname, cfg.ApiServer.Port), + Environment: cfg.Environment, + } +} + func saveKeyPair(pair tls.KeyPair) (string, string, error) { crtFile, err := ioutil.TempFile("", "*.crt") if err != nil { diff --git a/pkg/core/bootstrap/autoconfig_test.go b/pkg/core/bootstrap/autoconfig_test.go index e7f3a5369e00..43bdff017368 100644 --- a/pkg/core/bootstrap/autoconfig_test.go +++ b/pkg/core/bootstrap/autoconfig_test.go @@ -3,6 +3,8 @@ package bootstrap import ( "github.com/Kong/kuma/pkg/config/api-server/catalogue" kuma_cp "github.com/Kong/kuma/pkg/config/app/kuma-cp" + "github.com/Kong/kuma/pkg/config/core" + gui_server "github.com/Kong/kuma/pkg/config/gui-server" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" @@ -104,4 +106,24 @@ var _ = Describe("Auto configuration", func() { }, }), ) + + It("should autoconfigure gui config", func() { + // given + cfg := kuma_cp.DefaultConfig() + cfg.Environment = core.KubernetesEnvironment + cfg.General.AdvertisedHostname = "kuma.internal" + cfg.ApiServer.Port = 1234 + + // when + err := autoconfigure(&cfg) + + // then + Expect(err).ToNot(HaveOccurred()) + + // and + Expect(*cfg.GuiServer.GuiConfig).To(Equal(gui_server.GuiConfig{ + ApiUrl: "http://kuma.internal:1234", + Environment: "kubernetes", + })) + }) }) From 48652dc18dda6a6ec87a250151d5fd9b8e0214b7 Mon Sep 17 00:00:00 2001 From: Jakub Dyszkiewicz Date: Thu, 14 Nov 2019 09:41:23 +0100 Subject: [PATCH 2/2] feat(kuma-cp) content type for gui server config --- app/kuma-ui/pkg/server/server.go | 1 + app/kuma-ui/pkg/server/server_test.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/app/kuma-ui/pkg/server/server.go b/app/kuma-ui/pkg/server/server.go index 512072f3828b..01b357d09d70 100644 --- a/app/kuma-ui/pkg/server/server.go +++ b/app/kuma-ui/pkg/server/server.go @@ -71,6 +71,7 @@ func (g *Server) configHandler(writer http.ResponseWriter, request *http.Request writer.WriteHeader(500) return } + writer.Header().Add("content-type", "application/json") if _, err := writer.Write(bytes); err != nil { log.Error(err, "could not write the response") } diff --git a/app/kuma-ui/pkg/server/server_test.go b/app/kuma-ui/pkg/server/server_test.go index 0bac50a98715..9274dba92491 100644 --- a/app/kuma-ui/pkg/server/server_test.go +++ b/app/kuma-ui/pkg/server/server_test.go @@ -114,6 +114,9 @@ var _ = Describe("GUI Server", func() { Expect(resp.Body.Close()).To(Succeed()) Expect(err).ToNot(HaveOccurred()) + // and + Expect(resp.Header.Get("content-type")).To(Equal("application/json")) + // when cfg := types.GuiConfig{} Expect(json.Unmarshal(received, &cfg)).To(Succeed())