Skip to content

Commit

Permalink
feat(kuma-dp) use dp server port instead of catalog
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dyszkiewicz <[email protected]>
  • Loading branch information
jakubdyszkiewicz committed Oct 19, 2020
1 parent 8c7103e commit f151cc9
Show file tree
Hide file tree
Showing 36 changed files with 135 additions and 256 deletions.
42 changes: 1 addition & 41 deletions app/kuma-dp/cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"context"
"crypto/tls"
"io/ioutil"
"net/http"
Expand All @@ -10,13 +9,11 @@ import (
"time"

"github.com/pkg/errors"
"github.com/sethvargo/go-retry"
"github.com/spf13/cobra"

kumadp_config "github.com/kumahq/kuma/app/kuma-dp/pkg/config"
"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/catalog"
"github.com/kumahq/kuma/pkg/catalog/client"
"github.com/kumahq/kuma/pkg/config"
kuma_dp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
Expand Down Expand Up @@ -60,11 +57,6 @@ func newRunCmd() *cobra.Command {
return err
}

catalog, err := fetchCatalog(cfg)
if err != nil {
return err
}

dp, err := readDataplaneResource(cmd, &cfg)
if err != nil {
runLog.Error(err, "unable to read provided dataplane")
Expand Down Expand Up @@ -123,7 +115,6 @@ func newRunCmd() *cobra.Command {
}

dataplane, err := envoy.New(envoy.Opts{
Catalog: *catalog,
Config: cfg,
Generator: bootstrapGenerator,
Dataplane: dp,
Expand Down Expand Up @@ -153,7 +144,7 @@ func newRunCmd() *cobra.Command {
cmd.PersistentFlags().StringVar(&cfg.Dataplane.Name, "name", cfg.Dataplane.Name, "Name of the Dataplane")
cmd.PersistentFlags().Var(&cfg.Dataplane.AdminPort, "admin-port", `Port (or range of ports to choose from) for Envoy Admin API to listen on. Empty value indicates that Envoy Admin API should not be exposed over TCP. Format: "9901 | 9901-9999 | 9901- | -9901"`)
cmd.PersistentFlags().StringVar(&cfg.Dataplane.Mesh, "mesh", cfg.Dataplane.Mesh, "Mesh that Dataplane belongs to")
cmd.PersistentFlags().StringVar(&cfg.ControlPlane.ApiServer.URL, "cp-address", cfg.ControlPlane.ApiServer.URL, "URL of the Control Plane API Server")
cmd.PersistentFlags().StringVar(&cfg.ControlPlane.URL, "cp-address", cfg.ControlPlane.URL, "URL of the Control Plane API Server")
cmd.PersistentFlags().StringVar(&cfg.ControlPlane.CaCertFile, "ca-cert-file", cfg.ControlPlane.CaCert, "Path to CA cert by which connection to the Control Plane will be verified if HTTPS is used")
cmd.PersistentFlags().StringVar(&cfg.DataplaneRuntime.BinaryPath, "binary-path", cfg.DataplaneRuntime.BinaryPath, "Binary path of Envoy executable")
cmd.PersistentFlags().StringVar(&cfg.DataplaneRuntime.ConfigDir, "config-dir", cfg.DataplaneRuntime.ConfigDir, "Directory in which Envoy config will be generated")
Expand All @@ -171,34 +162,3 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
}
return ioutil.WriteFile(filename, data, perm)
}

// fetchCatalog tries to fetch Kuma CP catalog several times
// The main reason for introducing retries here is situation when DP is deployed in the same time as CP (ex. Ingress for Remote CP)
func fetchCatalog(cfg kuma_dp.Config) (*catalog.Catalog, error) {
runLog.Info("connecting to the Control Plane API for Bootstrap API location")
catalogClient, err := catalogClientFactory(cfg.ControlPlane.ApiServer.URL)
if err != nil {
return nil, errors.Wrap(err, "could not create catalog client")
}

backoff, err := retry.NewConstant(cfg.ControlPlane.ApiServer.Retry.Backoff)
if err != nil {
return nil, errors.Wrap(err, "could not create retry backoff")
}
backoff = retry.WithMaxDuration(cfg.ControlPlane.ApiServer.Retry.MaxDuration, backoff)
var c catalog.Catalog
err = retry.Do(context.Background(), backoff, func(ctx context.Context) error {
c, err = catalogClient.Catalog()
if err != nil {
runLog.Info("could not connect to the Control Plane API. Retrying.", "backoff", cfg.ControlPlane.ApiServer.Retry.Backoff, "err", err.Error())
return retry.RetryableError(err)
}
return nil
})

if err != nil {
return nil, errors.Wrap(err, "could not retrieve catalog")
}
runLog.Info("connection successful", "catalog", c)
return &c, nil
}
4 changes: 1 addition & 3 deletions app/kuma-dp/pkg/dataplane/envoy/envoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"

"github.com/kumahq/kuma/pkg/catalog"
kuma_dp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
"github.com/kumahq/kuma/pkg/core"
"github.com/kumahq/kuma/pkg/core/runtime/component"
Expand All @@ -27,7 +26,6 @@ var (
type BootstrapConfigFactoryFunc func(url string, cfg kuma_dp.Config, dp *rest.Resource) (proto.Message, error)

type Opts struct {
Catalog catalog.Catalog
Config kuma_dp.Config
Generator BootstrapConfigFactoryFunc
Dataplane *rest.Resource
Expand Down Expand Up @@ -98,7 +96,7 @@ func lookupEnvoyPath(configuredPath string) (string, error) {

func (e *Envoy) Start(stop <-chan struct{}) error {
runLog.Info("generating bootstrap configuration")
bootstrapConfig, err := e.opts.Generator(e.opts.Catalog.Apis.Bootstrap.Url, e.opts.Config, e.opts.Dataplane)
bootstrapConfig, err := e.opts.Generator(e.opts.Config.ControlPlane.URL, e.opts.Config, e.opts.Dataplane)
if err != nil {
return errors.Errorf("Failed to generate Envoy bootstrap config. %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions app/kuma-dp/pkg/dataplane/envoy/remote_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func (b *remoteBootstrap) Generate(url string, cfg kuma_dp.Config, dp *rest_type
}
}

backoff, err := retry.NewConstant(cfg.ControlPlane.BootstrapServer.Retry.Backoff)
backoff, err := retry.NewConstant(cfg.ControlPlane.Retry.Backoff)
if err != nil {
return nil, errors.Wrap(err, "could not create retry backoff")
}
backoff = retry.WithMaxDuration(cfg.ControlPlane.BootstrapServer.Retry.MaxDuration, backoff)
backoff = retry.WithMaxDuration(cfg.ControlPlane.Retry.MaxDuration, backoff)
var respBytes []byte
err = retry.Do(context.Background(), backoff, func(ctx context.Context) error {
log.Info("trying to fetch bootstrap configuration from the Control Plane")
Expand All @@ -87,9 +87,9 @@ func (b *remoteBootstrap) Generate(url string, cfg kuma_dp.Config, dp *rest_type
}
switch err {
case DpNotFoundErr:
log.Info("Dataplane entity is not yet found in the Control Plane. If you are running on Kubernetes, CP is most likely still in the process of converting Pod to Dataplane. Retrying.", "backoff", cfg.ControlPlane.ApiServer.Retry.Backoff)
log.Info("Dataplane entity is not yet found in the Control Plane. If you are running on Kubernetes, CP is most likely still in the process of converting Pod to Dataplane. Retrying.", "backoff", cfg.ControlPlane.Retry.Backoff)
default:
log.Info("could not fetch bootstrap configuration. Retrying.", "backoff", cfg.ControlPlane.BootstrapServer.Retry.Backoff, "err", err.Error())
log.Info("could not fetch bootstrap configuration. Retrying.", "backoff", cfg.ControlPlane.Retry.Backoff, "err", err.Error())
}
return retry.RetryableError(err)
})
Expand Down
6 changes: 3 additions & 3 deletions app/kuma-dp/pkg/dataplane/envoy/remote_bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var _ = Describe("Remote Bootstrap", func() {

// when
cfg := kuma_dp.DefaultConfig()
cfg.ControlPlane.BootstrapServer.Retry.Backoff = 10 * time.Millisecond
cfg.ControlPlane.Retry.Backoff = 10 * time.Millisecond
_, err = generator(fmt.Sprintf("http://localhost:%d", port), cfg, &rest.Resource{
Meta: rest.ResourceMeta{
Type: "Dataplane",
Expand Down Expand Up @@ -200,8 +200,8 @@ var _ = Describe("Remote Bootstrap", func() {

// when
config := kuma_dp.DefaultConfig()
config.ControlPlane.BootstrapServer.Retry.Backoff = 10 * time.Millisecond
config.ControlPlane.BootstrapServer.Retry.MaxDuration = 100 * time.Millisecond
config.ControlPlane.Retry.Backoff = 10 * time.Millisecond
config.ControlPlane.Retry.MaxDuration = 100 * time.Millisecond
_, err = generator(fmt.Sprintf("http://localhost:%d", port), config, &rest.Resource{
Meta: rest.ResourceMeta{Mesh: "default", Name: "dp-1"},
})
Expand Down
24 changes: 0 additions & 24 deletions app/kumactl/cmd/generate/generate_dataplane_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,4 @@ var _ = Describe("kumactl generate dataplane-token", func() {
Expect(buf.String()).To(Equal("Error: failed to generate a dataplane token: could not connect to API\n"))
})

It("should throw an error when dataplane token server is disabled", func() {
// setup
ctx.Runtime.NewCatalogClient = func(s string) (catalog_client.CatalogClient, error) {
return &test_catalog.StaticCatalogClient{
Resp: catalog.Catalog{
Apis: catalog.Apis{
DataplaneToken: catalog.DataplaneTokenApi{
LocalUrl: "", // disabled dataplane token server
},
},
},
}, nil
}

// when
rootCmd.SetArgs([]string{"generate", "dataplane-token", "--dataplane=example"})
err := rootCmd.Execute()

// then
Expect(err).To(HaveOccurred())

// and
Expect(buf.String()).To(Equal("Error: failed to create dataplane token client: Enable the server to be able to generate tokens.\n"))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5941,8 +5941,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: KUMA_CONTROL_PLANE_API_SERVER_URL
value: "http://kuma-control-plane.kuma-system:5681"
- name: KUMA_CONTROL_PLANE_URL
value: "https://kuma-control-plane.kuma-system:5678"
- name: KUMA_CONTROL_PLANE_CA_CERT_FILE
value: /var/run/secrets/kuma.io/tls-cert/ca.crt
- name: KUMA_DATAPLANE_MESH
value: default
- name: KUMA_DATAPLANE_NAME
Expand Down Expand Up @@ -5987,6 +5989,14 @@ spec:
limits:
cpu: 1000m
memory: 512Mi
volumeMounts:
- mountPath: /var/run/secrets/kuma.io/tls-cert
name: kuma-tls-cert
readOnly: true
volumes:
- name: kuma-tls-cert
secret:
secretName: kuma-tls-cert
---
apiVersion: admissionregistration.k8s.io/v1beta1
kind: MutatingWebhookConfiguration
Expand Down
11 changes: 1 addition & 10 deletions app/kumactl/pkg/cmd/root_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ func (rc *RootContext) catalog() (catalog.Catalog, error) {
}

func (rc *RootContext) CurrentDataplaneTokenClient() (tokens.DataplaneTokenClient, error) {
// todo(jakubdyszkiewicz) check enable/disable by checking cp config
components, err := rc.catalog()
if err != nil {
return nil, err
}
if !components.Apis.DataplaneToken.Enabled() {
return nil, errors.New("Enable the server to be able to generate tokens.")
}

ctx, err := rc.CurrentContext()
if err != nil {
return nil, err
Expand Down Expand Up @@ -203,7 +194,7 @@ func (rc *RootContext) adminServerUrl() (string, error) {
if err := validateRemoteAdminServerSettings(ctx, components); err != nil {
return "", err
}
return components.Apis.DataplaneToken.PublicUrl, nil
return components.Apis.Admin.PublicUrl, nil
}
}

Expand Down
Loading

0 comments on commit f151cc9

Please sign in to comment.