Skip to content

Commit

Permalink
feat(kuma-cp) serve config for gui
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdyszkiewicz committed Nov 13, 2019
1 parent 366106e commit 97154b8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
27 changes: 26 additions & 1 deletion app/kuma-ui/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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,
}
}
34 changes: 34 additions & 0 deletions app/kuma-ui/pkg/server/server_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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())
Expand All @@ -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{})
Expand Down Expand Up @@ -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))
})

})
6 changes: 6 additions & 0 deletions app/kuma-ui/pkg/server/types/gui_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

type GuiConfig struct {
ApiUrl string `json:"apiUrl"`
Environment string `json:"environment"`
}
11 changes: 10 additions & 1 deletion pkg/config/gui-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions pkg/core/bootstrap/autoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/core/bootstrap/autoconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
}))
})
})

0 comments on commit 97154b8

Please sign in to comment.