Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI server config #428

Merged
merged 2 commits into from
Nov 14, 2019
Merged
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
28 changes: 27 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,23 @@ 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
}
writer.Header().Add("content-type", "application/json")
if _, err := writer.Write(bytes); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set Content-Type: application/json

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,
}
}
37 changes: 37 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,30 @@ 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())

// and
Expect(resp.Header.Get("content-type")).To(Equal("application/json"))

// 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
yskopets marked this conversation as resolved.
Show resolved Hide resolved
}
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",
}))
})
})