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

chore(kuma-dp) extensible DP server #1513

Merged
merged 6 commits into from
Feb 4, 2021
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
30 changes: 28 additions & 2 deletions app/kuma-cp/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/spf13/cobra"

"github.com/kumahq/kuma/pkg/dns"

api_server "github.com/kumahq/kuma/pkg/api-server"
"github.com/kumahq/kuma/pkg/clusterid"
"github.com/kumahq/kuma/pkg/config"
Expand All @@ -17,14 +15,18 @@ import (
"github.com/kumahq/kuma/pkg/core/bootstrap"
"github.com/kumahq/kuma/pkg/defaults"
"github.com/kumahq/kuma/pkg/diagnostics"
"github.com/kumahq/kuma/pkg/dns"
dp_server "github.com/kumahq/kuma/pkg/dp-server"
"github.com/kumahq/kuma/pkg/gc"
"github.com/kumahq/kuma/pkg/hds"
"github.com/kumahq/kuma/pkg/insights"
kds_global "github.com/kumahq/kuma/pkg/kds/global"
kds_remote "github.com/kumahq/kuma/pkg/kds/remote"
mads_server "github.com/kumahq/kuma/pkg/mads/server"
metrics "github.com/kumahq/kuma/pkg/metrics/components"
sds_server "github.com/kumahq/kuma/pkg/sds/server"
kuma_version "github.com/kumahq/kuma/pkg/version"
"github.com/kumahq/kuma/pkg/xds"
)

var (
Expand Down Expand Up @@ -94,6 +96,18 @@ func newRunCmdWithOpts(opts runCmdOpts) *cobra.Command {
runLog.Error(err, "unable to set up clusterID")
return err
}
if err := xds.Setup(rt); err != nil {
runLog.Error(err, "unable to set up XDS")
return err
}
if err := sds_server.Setup(rt); err != nil {
runLog.Error(err, "unable to set up SDS")
return err
}
if err := hds.Setup(rt); err != nil {
runLog.Error(err, "unable to set up HDS")
return err
}
if err := dp_server.SetupServer(rt); err != nil {
runLog.Error(err, "unable to set up DP Server")
return err
Expand Down Expand Up @@ -123,6 +137,18 @@ func newRunCmdWithOpts(opts runCmdOpts) *cobra.Command {
runLog.Error(err, "unable to set up GC")
return err
}
if err := xds.Setup(rt); err != nil {
runLog.Error(err, "unable to set up XDS")
return err
}
if err := sds_server.Setup(rt); err != nil {
runLog.Error(err, "unable to set up SDS")
return err
}
if err := hds.Setup(rt); err != nil {
runLog.Error(err, "unable to set up HDS")
return err
}
if err := dp_server.SetupServer(rt); err != nil {
runLog.Error(err, "unable to set up DP Server")
return err
Expand Down
4 changes: 4 additions & 0 deletions app/kuma-dp/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/envoy"
kumadp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
"github.com/kumahq/kuma/pkg/core/runtime/component"
leader_memory "github.com/kumahq/kuma/pkg/plugins/leader/memory"
)
Expand All @@ -14,14 +15,17 @@ import (
type RootContext struct {
ComponentManager component.Manager
BootstrapGenerator envoy.BootstrapConfigFactoryFunc
Config *kumadp.Config
}

func DefaultRootContext() *RootContext {
config := kumadp.DefaultConfig()
return &RootContext{
ComponentManager: component.NewManager(leader_memory.NewNeverLeaderElector()),
BootstrapGenerator: envoy.NewRemoteBootstrapGenerator(&http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}),
Config: &config,
}
}
42 changes: 28 additions & 14 deletions app/kuma-dp/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"path/filepath"

kumadp_config "github.com/kumahq/kuma/app/kuma-dp/pkg/config"
"github.com/kumahq/kuma/pkg/core/resources/model/rest"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/accesslogs"
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/envoy"
"github.com/kumahq/kuma/pkg/config"
kuma_dp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
config_types "github.com/kumahq/kuma/pkg/config/types"
"github.com/kumahq/kuma/pkg/core"
util_net "github.com/kumahq/kuma/pkg/util/net"
Expand All @@ -22,26 +22,36 @@ import (

var runLog = dataplaneLog.WithName("run")

// PersistentPreRunE in root command sets the logger and initial config
// PreRunE loads the Kuma DP config
// PostRunE actually runs all the components with loaded config
// To extend Kuma DP, plug your code in RunE. Use RootContext.Config and add components to RootContext.ComponentManager
func newRunCmd(rootCtx *RootContext) *cobra.Command {
cfg := kuma_dp.DefaultConfig()
cfg := rootCtx.Config
var dp *rest.Resource
var tmpDir string
cmd := &cobra.Command{
Use: "run",
Short: "Launch Dataplane (Envoy)",
Long: `Launch Dataplane (Envoy).`,
RunE: func(cmd *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
// only support configuration via environment variables and args
if err := config.Load("", &cfg); err != nil {
if err := config.Load("", cfg); err != nil {
runLog.Error(err, "unable to load configuration")
return err
}
if conf, err := config.ToJson(&cfg); err == nil {
if conf, err := config.ToJson(cfg); err == nil {
runLog.Info("effective configuration", "config", string(conf))
} else {
runLog.Error(err, "unable to format effective configuration", "config", cfg)
return err
}

dp, err := readDataplaneResource(cmd, &cfg)
var err error
dp, err = readDataplaneResource(cmd, cfg)
if err != nil {
runLog.Error(err, "unable to read provided dataplane")
return err
Expand All @@ -62,16 +72,11 @@ func newRunCmd(rootCtx *RootContext) *cobra.Command {
}

if cfg.DataplaneRuntime.ConfigDir == "" {
tmpDir, err := ioutil.TempDir("", "kuma-dp-")
tmpDir, err = ioutil.TempDir("", "kuma-dp-")
if err != nil {
runLog.Error(err, "unable to create a temporary directory to store generated Envoy config at")
return err
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
runLog.Error(err, "unable to remove a temporary directory with a generated Envoy config")
}
}()
cfg.DataplaneRuntime.ConfigDir = tmpDir
runLog.Info("generated Envoy configuration will be stored in a temporary directory", "dir", tmpDir)
}
Expand All @@ -98,11 +103,20 @@ func newRunCmd(rootCtx *RootContext) *cobra.Command {
}
cfg.ControlPlane.CaCert = string(cert)
}

return nil
},
PostRunE: func(cmd *cobra.Command, _ []string) error {
if tmpDir != "" { // clean up temp dir if it was created
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
runLog.Error(err, "unable to remove a temporary directory with a generated Envoy config")
}
}()
}
shouldQuit := setupQuitChannel()

dataplane, err := envoy.New(envoy.Opts{
Config: cfg,
Config: *cfg,
Generator: rootCtx.BootstrapGenerator,
Dataplane: dp,
Stdout: cmd.OutOrStdout(),
Expand Down
Loading