Skip to content

Commit

Permalink
feat(kuma-cp) serve gui server (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdyszkiewicz authored Nov 13, 2019
1 parent c71ced8 commit 366106e
Show file tree
Hide file tree
Showing 26 changed files with 517 additions and 58 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
start/k8s start/kind start/control-plane/k8s \
deploy/example-app/k8s deploy/control-plane/k8s \
kind/load/control-plane kind/load/kuma-dp kind/load/kuma-injector \
generate protoc/pkg/config/app/kumactl/v1alpha1 protoc/pkg/test/apis/sample/v1alpha1 generate/kumactl/install/control-plane \
generate protoc/pkg/config/app/kumactl/v1alpha1 protoc/pkg/test/apis/sample/v1alpha1 generate/kumactl/install/control-plane \ generate/gui \
fmt fmt/go fmt/proto vet golangci-lint check test integration build run/k8s run/universal/memory run/universal/postgres \
images image/kuma-cp image/kuma-dp image/kumactl image/kuma-injector image/kuma-tcp-echo \
docker/build docker/build/kuma-cp docker/build/kuma-dp docker/build/kumactl docker/build/kuma-injector docker/build/kuma-tcp-echo \
Expand Down Expand Up @@ -238,6 +238,9 @@ generate/kumactl/install/control-plane:
go generate ./app/kumactl/pkg/install/k8s/control-plane/...
go generate ./app/kumactl/pkg/install/universal/control-plane/postgres/...

generate/gui: ## Generate go files with GUI static files to embed it into binary
go generate ./app/kuma-ui/pkg/resources/...

fmt: fmt/go fmt/proto ## Dev: Run various format tools

fmt/go: ## Dev: Run go fmt
Expand Down
2 changes: 2 additions & 0 deletions app/kuma-cp/cmd/cmd_k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ runtime:
admissionServer:
port: %d
certDir: %s
guiServer:
port: 0
`,
admissionServerPort,
filepath.Join("..", "..", "kuma-injector", "cmd", "testdata"))
Expand Down
2 changes: 2 additions & 0 deletions app/kuma-cp/cmd/cmd_standalone_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ sdsServer:
dataplaneTokenServer:
local:
port: 0
guiServer:
port: 0
environment: universal
store:
type: postgres
Expand Down
2 changes: 2 additions & 0 deletions app/kuma-cp/cmd/cmd_universal_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ sdsServer:
dataplaneTokenServer:
local:
port: 0
guiServer:
port: 0
environment: universal
store:
type: memory
Expand Down
5 changes: 5 additions & 0 deletions app/kuma-cp/cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
ui_server "github.com/Kong/kuma/app/kuma-ui/pkg/server"
api_server "github.com/Kong/kuma/pkg/api-server"
"github.com/Kong/kuma/pkg/config"
kuma_cp "github.com/Kong/kuma/pkg/config/app/kuma-cp"
Expand Down Expand Up @@ -62,6 +63,10 @@ func newRunCmdWithOpts(opts runCmdOpts) *cobra.Command {
runLog.Error(err, "unable to set up API server")
return err
}
if err := ui_server.SetupServer(rt); err != nil {
runLog.Error(err, "unable to set up GUI server")
return err
}

runLog.Info("starting Control Plane")
if err := rt.Start(opts.SetupSignalHandler()); err != nil {
Expand Down
1 change: 1 addition & 0 deletions app/kuma-ui/data/resources/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions app/kuma-ui/data/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
3 changes: 3 additions & 0 deletions app/kuma-ui/pkg/resources/guidir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package resources

//go:generate go run github.com/shurcooL/vfsgen/cmd/vfsgendev -source="github.com/Kong/kuma/app/kuma-ui/pkg/resources".GuiDir
19 changes: 19 additions & 0 deletions app/kuma-ui/pkg/resources/guidir_dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build dev

package resources

import (
"net/http"
"path/filepath"
"runtime"
)

var GuiDir http.FileSystem = http.Dir(guiSrcDir())

func guiSrcDir() string {
_, thisFile, _, _ := runtime.Caller(1)

thisDir := filepath.Dir(thisFile)

return filepath.Join(thisDir, "..", "..", "data", "resources")
}
36 changes: 36 additions & 0 deletions app/kuma-ui/pkg/resources/guidir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package resources_test

import (
"github.com/Kong/kuma/app/kuma-ui/pkg/resources"
"github.com/Kong/kuma/pkg/test/vfsgen"
"io/ioutil"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)

var _ = Describe("Gui Dir", func() {

guiDir := filepath.Join("..", "..", "data", "resources")
testEntries := vfsgen.GenerateEntries(guiDir)

DescribeTable("generated Go code must be in sync with the original GUI dir",
func(given vfsgen.FileTestCase) {
// given compiled file
file, err := resources.GuiDir.Open(given.Filename)
// then
Expect(err).ToNot(HaveOccurred())

// when
actualContents, err := ioutil.ReadAll(file)
// then
Expect(err).ToNot(HaveOccurred())
// and
Expect(string(actualContents)).To(Equal(string(given.ExpectedContents)), "generated Go code is no longer in sync with the original template files. To re-generate it, run `make generate/gui`")
},
testEntries...,
)

})
149 changes: 149 additions & 0 deletions app/kuma-ui/pkg/resources/guidir_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions app/kuma-ui/pkg/resources/resources_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package resources_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestGui(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GUI Resources Suite")
}
59 changes: 59 additions & 0 deletions app/kuma-ui/pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package server

import (
"context"
"fmt"
"github.com/Kong/kuma/app/kuma-ui/pkg/resources"
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"
"net/http"
)

var log = core.Log.WithName("gui-server")

func SetupServer(rt core_runtime.Runtime) error {
srv := Server{rt.Config().GuiServer}
if err := core_runtime.Add(rt, &srv); err != nil {
return err
}
return nil
}

type Server struct {
Config *gui_server.GuiServerConfig
}

var _ core_runtime.Component = &Server{}

func (g *Server) Start(stop <-chan struct{}) error {
fileServer := http.FileServer(resources.GuiDir)

guiServer := &http.Server{
Addr: fmt.Sprintf(":%d", g.Config.Port),
Handler: fileServer,
}

errChan := make(chan error)

go func() {
defer close(errChan)
if err := guiServer.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Error(err, "terminated with an error")
errChan <- err
return
}
}
log.Info("terminated normally")
}()
log.Info("starting", "port", g.Config.Port)

select {
case <-stop:
log.Info("stopping")
return guiServer.Shutdown(context.Background())
case err := <-errChan:
return err
}
}
13 changes: 13 additions & 0 deletions app/kuma-ui/pkg/server/server_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package server_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestGui(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GUI Suite")
}
Loading

0 comments on commit 366106e

Please sign in to comment.