From 0918505d7b4723ca614eead150596e5f35db064d Mon Sep 17 00:00:00 2001 From: Charlie Stanley Date: Tue, 12 Jan 2021 16:05:15 -0800 Subject: [PATCH] Add config file with --slim (#556) --- pkg/standalone/standalone.go | 41 +++++++++++++++-------- pkg/standalone/standalone_test.go | 54 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 pkg/standalone/standalone_test.go diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 52c71a8c3..5a94ed594 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -61,11 +61,11 @@ type configuration struct { } `yaml:"metadata"` Spec struct { Tracing struct { - SamplingRate string `yaml:"samplingRate"` + SamplingRate string `yaml:"samplingRate,omitempty"` Zipkin struct { - EndpointAddress string `yaml:"endpointAddress"` - } `yaml:"zipkin"` - } `yaml:"tracing"` + EndpointAddress string `yaml:"endpointAddress,omitempty"` + } `yaml:"zipkin,omitempty"` + } `yaml:"tracing,omitempty"` } `yaml:"spec"` } @@ -124,14 +124,16 @@ func Init(runtimeVersion string, dockerNetwork string, slimMode bool) error { if slimMode { // Install 3 binaries in slim mode: daprd, dashboard, placement wg.Add(3) + initSteps = append(initSteps, createSlimConfiguration) } else { // Install 2 binaries: daprd, dashboard wg.Add(2) initSteps = append(initSteps, createComponentsAndConfiguration, runPlacementService, runRedis, runZipkin) - // Init other configurations, containers - wg.Add(len(initSteps)) } + // Init other configurations, containers + wg.Add(len(initSteps)) + msg := "Downloading binaries and setting up components..." var s *spinner.Spinner if runtime.GOOS == daprWindowsOS { @@ -159,11 +161,11 @@ func Init(runtimeVersion string, dockerNetwork string, slimMode bool) error { if slimMode { // Initialize placement binary only on slim install go installBinary(&wg, errorChan, daprBinDir, runtimeVersion, placementServiceFilePrefix, dockerNetwork, cli_ver.DaprGitHubRepo) - } else { - for _, step := range initSteps { - // Run init on the configurations and containers - go step(&wg, errorChan, daprBinDir, runtimeVersion, dockerNetwork) - } + } + + for _, step := range initSteps { + // Run init on the configurations and containers + go step(&wg, errorChan, daprBinDir, runtimeVersion, dockerNetwork) } go func() { @@ -569,6 +571,17 @@ func createComponentsAndConfiguration(wg *sync.WaitGroup, errorChan chan<- error } } +func createSlimConfiguration(wg *sync.WaitGroup, errorChan chan<- error, _, _ string, _ string) { + defer wg.Done() + + // For --slim we pass empty string so that we do not configure zipkin. + err := createDefaultConfiguration("", DefaultConfigFilePath()) + if err != nil { + errorChan <- fmt.Errorf("error creating default configuration file: %s", err) + return + } +} + func makeDefaultComponentsDir() error { // Make default components directory componentsDir := DefaultComponentsDirPath() @@ -870,8 +883,10 @@ func createDefaultConfiguration(zipkinHost, filePath string) error { Kind: "Configuration", } defaultConfig.Metadata.Name = "daprConfig" - defaultConfig.Spec.Tracing.SamplingRate = "1" - defaultConfig.Spec.Tracing.Zipkin.EndpointAddress = fmt.Sprintf("http://%s:9411/api/v2/spans", zipkinHost) + if zipkinHost != "" { + defaultConfig.Spec.Tracing.SamplingRate = "1" + defaultConfig.Spec.Tracing.Zipkin.EndpointAddress = fmt.Sprintf("http://%s:9411/api/v2/spans", zipkinHost) + } b, err := yaml.Marshal(&defaultConfig) if err != nil { return err diff --git a/pkg/standalone/standalone_test.go b/pkg/standalone/standalone_test.go new file mode 100644 index 000000000..65429dfd9 --- /dev/null +++ b/pkg/standalone/standalone_test.go @@ -0,0 +1,54 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. +// ------------------------------------------------------------ + +package standalone + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStandaloneConfig(t *testing.T) { + testFile := "./test.yaml" + + t.Run("Standalone config", func(t *testing.T) { + expectConfigZipkin := `apiVersion: dapr.io/v1alpha1 +kind: Configuration +metadata: + name: daprConfig +spec: + tracing: + samplingRate: "1" + zipkin: + endpointAddress: http://test_zipkin_host:9411/api/v2/spans +` + os.Remove(testFile) + createDefaultConfiguration("test_zipkin_host", testFile) + assert.FileExists(t, testFile) + content, err := ioutil.ReadFile(testFile) + assert.NoError(t, err) + assert.Equal(t, expectConfigZipkin, string(content)) + }) + + t.Run("Standalone config slim", func(t *testing.T) { + expectConfigSlim := `apiVersion: dapr.io/v1alpha1 +kind: Configuration +metadata: + name: daprConfig +spec: {} +` + os.Remove(testFile) + createDefaultConfiguration("", testFile) + assert.FileExists(t, testFile) + content, err := ioutil.ReadFile(testFile) + assert.NoError(t, err) + assert.Equal(t, expectConfigSlim, string(content)) + }) + + os.Remove(testFile) +}