Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubdyszkiewicz committed Oct 21, 2019
1 parent 3fdd282 commit b639d8e
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 62 deletions.
7 changes: 6 additions & 1 deletion app/kuma-dp/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

kumadp_config "github.com/Kong/kuma/app/kuma-dp/pkg/config"
"github.com/Kong/kuma/app/kuma-dp/pkg/dataplane/accesslogs"
"github.com/Kong/kuma/app/kuma-dp/pkg/dataplane/envoy"
"github.com/Kong/kuma/pkg/config"
Expand Down Expand Up @@ -41,6 +42,10 @@ func newRunCmd() *cobra.Command {
return err
}

if err := kumadp_config.ValidateTokenPath(cfg.DataplaneRuntime.TokenPath); err != nil {
return err
}

if cfg.DataplaneRuntime.ConfigDir == "" {
tmpDir, err := ioutil.TempDir("", "kuma-dp-")
if err != nil {
Expand Down Expand Up @@ -106,6 +111,6 @@ func newRunCmd() *cobra.Command {
cmd.PersistentFlags().StringVar(&cfg.ControlPlane.BootstrapServer.URL, "cp-address", cfg.ControlPlane.BootstrapServer.URL, "Mesh that Dataplane belongs to")
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")
cmd.PersistentFlags().StringVar(&cfg.DataplaneRuntime.TokenPath, "dataplane-token", cfg.DataplaneRuntime.TokenPath, "Path to file in which generated token is stored")
cmd.PersistentFlags().StringVar(&cfg.DataplaneRuntime.TokenPath, "dataplane-token", cfg.DataplaneRuntime.TokenPath, "Path to a file with dataplane token (use 'kumactl generate dataplane-token' to get one)")
return cmd
}
13 changes: 13 additions & 0 deletions app/kuma-dp/pkg/config/config_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config_test

import (
"testing"

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

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Kuma DP Config Suite")
}
20 changes: 20 additions & 0 deletions app/kuma-dp/pkg/config/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config

import (
util_files "github.com/Kong/kuma/pkg/util/files"
"github.com/pkg/errors"
)

func ValidateTokenPath(path string) error {
if path == "" {
return nil
}
empty, err := util_files.FileEmpty(path)
if err != nil {
return errors.Wrap(err, "could not read file")
}
if empty {
return errors.Errorf("token under file %s is empty", path)
}
return nil
}
57 changes: 57 additions & 0 deletions app/kuma-dp/pkg/config/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package config_test

import (
"fmt"
"github.com/Kong/kuma/app/kuma-dp/pkg/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"os"
)

var _ = Describe("ValidateTokenPath", func() {

var tokenFile *os.File

BeforeEach(func() {
tf, err := ioutil.TempFile("", "")
Expect(err).ToNot(HaveOccurred())
tokenFile = tf
})

It("should pass validation for empty path", func() {
// when
err := config.ValidateTokenPath("")

// then
Expect(err).ToNot(HaveOccurred())
})

It("should pass validation for empty path", func() {
// given
_, err := tokenFile.WriteString("sampletoken")
Expect(err).ToNot(HaveOccurred())

// when
err = config.ValidateTokenPath("")

// then
Expect(err).ToNot(HaveOccurred())
})

It("should fail for non existing file", func() {
// when
err := config.ValidateTokenPath("nonexistingfile")

// then
Expect(err).To(MatchError("could not read file: stat nonexistingfile: no such file or directory"))
})

It("should fail for empty file", func() {
// when
err := config.ValidateTokenPath(tokenFile.Name())

// then
Expect(err).To(MatchError(fmt.Sprintf("token under file %s is empty", tokenFile.Name())))
})
})
3 changes: 2 additions & 1 deletion app/kumactl/pkg/cmd/root_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
config_proto "github.com/Kong/kuma/pkg/config/app/kumactl/v1alpha1"
core_model "github.com/Kong/kuma/pkg/core/resources/model"
core_store "github.com/Kong/kuma/pkg/core/resources/store"
util_files "github.com/Kong/kuma/pkg/util/files"
"github.com/pkg/errors"

kumactl_resources "github.com/Kong/kuma/app/kumactl/pkg/resources"
Expand Down Expand Up @@ -127,5 +128,5 @@ func (rc *RootContext) CurrentDataplaneTokenClient() (tokens.DataplaneTokenClien
}

func (rc *RootContext) IsFirstTimeUsage() bool {
return rc.Args.ConfigFile == "" && !config.FileExists(config.DefaultConfigFile)
return rc.Args.ConfigFile == "" && !util_files.FileExists(config.DefaultConfigFile)
}
10 changes: 3 additions & 7 deletions app/kumactl/pkg/config/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

config_proto "github.com/Kong/kuma/pkg/config/app/kumactl/v1alpha1"
util_files "github.com/Kong/kuma/pkg/util/files"
util_proto "github.com/Kong/kuma/pkg/util/proto"
"github.com/pkg/errors"
)
Expand All @@ -15,13 +16,13 @@ var DefaultConfigFile = filepath.Join(os.Getenv("HOME"), ".kumactl", "config")
func Load(file string, cfg *config_proto.Configuration) error {
configFile := DefaultConfigFile
if file != "" {
if FileExists(file) {
if util_files.FileExists(file) {
configFile = file
} else {
return errors.Errorf("Failed to access configuration file %q", file)
}
}
if FileExists(configFile) {
if util_files.FileExists(configFile) {
if contents, err := ioutil.ReadFile(configFile); err != nil {
return errors.Wrapf(err, "Failed to read configuration from file %q", configFile)
} else if err := util_proto.FromYAML(contents, cfg); err != nil {
Expand Down Expand Up @@ -57,8 +58,3 @@ func Save(file string, cfg *config_proto.Configuration) error {
}
return nil
}

func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
3 changes: 1 addition & 2 deletions pkg/config/app/kuma-dp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/Kong/kuma/pkg/config"

"github.com/pkg/errors"
"go.uber.org/multierr"
)
Expand Down Expand Up @@ -69,7 +68,7 @@ type DataplaneRuntime struct {
BinaryPath string `yaml:"binaryPath,omitempty" envconfig:"kuma_dataplane_runtime_binary_path"`
// Dir to store auto-generated Envoy bootstrap config in.
ConfigDir string `yaml:"configDir,omitempty" envconfig:"kuma_dataplane_runtime_config_dir"`
// TokenPath defines the path for the generated dataplane token
// Path to a file with dataplane token (use 'kumactl generate dataplane-token' to get one)
TokenPath string `yaml:"dataplaneTokenPath,omitempty" envconfig:"kuma_dataplane_runtime_token_path"`
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/util/files/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package files

import "os"

func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

func FileEmpty(path string) (bool, error) {
file, err := os.Stat(path)
if err != nil {
return true, err
}
return file.Size() == 0, nil
}
44 changes: 38 additions & 6 deletions pkg/xds/bootstrap/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var _ = Describe("bootstrapGenerator", func() {

type testCase struct {
config *bootstrap_config.BootstrapParamsConfig
request types.BootstrapRequest
expectedConfigFile string
}
DescribeTable("should generate bootstrap configuration",
Expand All @@ -68,11 +69,7 @@ var _ = Describe("bootstrapGenerator", func() {
generator := NewDefaultBootstrapGenerator(resManager, given.config)

// when
bootstrapConfig, err := generator.Generate(context.Background(), types.BootstrapRequest{
Mesh: "mesh",
Name: "name.namespace",
DataplaneTokenPath: "/tmp/token",
})
bootstrapConfig, err := generator.Generate(context.Background(), given.request)
// then
Expect(err).ToNot(HaveOccurred())

Expand All @@ -89,10 +86,39 @@ var _ = Describe("bootstrapGenerator", func() {
// expect
Expect(actual).To(MatchYAML(expected))
},
Entry("default config with minimal request", testCase{
config: bootstrap_config.DefaultBootstrapParamsConfig(),
request: types.BootstrapRequest{
Mesh: "mesh",
Name: "name.namespace",
},
expectedConfigFile: "generator.default-config-minimal-request.golden.yaml",
}),
Entry("default config", testCase{
config: bootstrap_config.DefaultBootstrapParamsConfig(),
config: bootstrap_config.DefaultBootstrapParamsConfig(),
request: types.BootstrapRequest{
Mesh: "mesh",
Name: "name.namespace",
AdminPort: 1234,
DataplaneTokenPath: "/tmp/token",
},
expectedConfigFile: "generator.default-config.golden.yaml",
}),
Entry("custom config with minimal request", testCase{
config: &bootstrap_config.BootstrapParamsConfig{
AdminAddress: "192.168.0.1", // by default, Envoy Admin interface should listen on loopback address
AdminPort: 9902, // by default, turn off Admin interface of Envoy
AdminAccessLogPath: "/var/log",
XdsHost: "kuma-control-plane.internal",
XdsPort: 15678,
XdsConnectTimeout: 2 * time.Second,
},
request: types.BootstrapRequest{
Mesh: "mesh",
Name: "name.namespace",
},
expectedConfigFile: "generator.custom-config-minimal-request.golden.yaml",
}),
Entry("custom config", testCase{
config: &bootstrap_config.BootstrapParamsConfig{
AdminAddress: "192.168.0.1", // by default, Envoy Admin interface should listen on loopback address
Expand All @@ -102,6 +128,12 @@ var _ = Describe("bootstrapGenerator", func() {
XdsPort: 15678,
XdsConnectTimeout: 2 * time.Second,
},
request: types.BootstrapRequest{
Mesh: "mesh",
Name: "name.namespace",
AdminPort: 1234,
DataplaneTokenPath: "/tmp/token",
},
expectedConfigFile: "generator.custom-config.golden.yaml",
}),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
admin:
accessLogPath: /var/log
address:
socketAddress:
address: 192.168.0.1
portValue: 9902
dynamicResources:
adsConfig:
apiType: GRPC
grpcServices:
- envoyGrpc:
clusterName: ads_cluster
cdsConfig:
ads: {}
ldsConfig:
ads: {}
node:
cluster: backend
id: mesh.name.namespace
staticResources:
clusters:
- connectTimeout: 2s
http2ProtocolOptions: {}
loadAssignment:
clusterName: ads_cluster
endpoints:
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: kuma-control-plane.internal
portValue: 15678
name: ads_cluster
type: STRICT_DNS
upstreamConnectionOptions:
tcpKeepalive: {}
- connectTimeout: 2s
http2ProtocolOptions: {}
loadAssignment:
clusterName: access_log_sink
endpoints:
- lbEndpoints:
- endpoint:
address:
pipe:
path: /tmp/kuma-access-logs-name.namespace-mesh.sock
name: access_log_sink
type: STATIC
upstreamConnectionOptions:
tcpKeepalive: {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ admin:
address:
socketAddress:
address: 192.168.0.1
portValue: 9902
portValue: 1234
dynamicResources:
adsConfig:
apiType: GRPC
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
dynamicResources:
adsConfig:
apiType: GRPC
grpcServices:
- envoyGrpc:
clusterName: ads_cluster
cdsConfig:
ads: {}
ldsConfig:
ads: {}
node:
cluster: backend
id: mesh.name.namespace
staticResources:
clusters:
- connectTimeout: 1s
http2ProtocolOptions: {}
loadAssignment:
clusterName: ads_cluster
endpoints:
- lbEndpoints:
- endpoint:
address:
socketAddress:
address: 127.0.0.1
portValue: 5678
name: ads_cluster
type: STRICT_DNS
upstreamConnectionOptions:
tcpKeepalive: {}
- connectTimeout: 1s
http2ProtocolOptions: {}
loadAssignment:
clusterName: access_log_sink
endpoints:
- lbEndpoints:
- endpoint:
address:
pipe:
path: /tmp/kuma-access-logs-name.namespace-mesh.sock
name: access_log_sink
type: STATIC
upstreamConnectionOptions:
tcpKeepalive: {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
admin:
accessLogPath: /dev/null
address:
socketAddress:
address: 127.0.0.1
portValue: 1234
dynamicResources:
adsConfig:
apiType: GRPC
Expand Down
Loading

0 comments on commit b639d8e

Please sign in to comment.