Skip to content

Commit

Permalink
Merge pull request #2881 from weaveworks/2771-set-analytics
Browse files Browse the repository at this point in the history
Add analytics to helm chart on dashboard installation
  • Loading branch information
opudrovs authored Nov 24, 2022
2 parents 7a3ad44 + 26d3417 commit c005e1a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 32 deletions.
7 changes: 4 additions & 3 deletions cmd/gitops/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ func RootCmd() *cobra.Command {

gitopsConfig, err = config.GetConfig(false)
if err != nil {
fmt.Println("To improve our product, we would like to collect analytics data. You can read more about what data we collect here: https://docs.gitops.weave.works/docs/feedback-and-telemetry/")
fmt.Fprintln(os.Stderr, "To improve our product, we would like to collect analytics data. You can read more about what data we collect here: https://docs.gitops.weave.works/docs/feedback-and-telemetry/")

enableAnalytics := false

prompt := promptui.Prompt{
Label: "Would you like to turn on analytics to help us improve our product",
IsConfirm: true,
Default: "Y",
Stdout: os.Stderr,
}

enableAnalytics := false

// Answering "n" causes err to not be nil. Hitting enter without typing
// does not return the default.
_, err := prompt.Run()
Expand Down
18 changes: 17 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const (
Please set you configuration with: gitops set config`
)

var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/")
var (
sessionConfig *GitopsCLIConfig
letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/")
)

type GitopsCLIConfig struct {
Analytics bool `json:"analytics"`
Expand All @@ -37,8 +40,17 @@ func (config *GitopsCLIConfig) String() (string, error) {
return string(data), nil
}

// SetConfig sets global config to the provided value
func SetConfig(config *GitopsCLIConfig) {
sessionConfig = config
}

// GetConfig reads the CLI configuration for Weave GitOps from the config file
func GetConfig(shouldCreate bool) (*GitopsCLIConfig, error) {
if sessionConfig != nil {
return sessionConfig, nil
}

configPath, err := getConfigPath(ConfigFileName)
if err != nil {
return nil, err
Expand Down Expand Up @@ -70,6 +82,8 @@ func GetConfig(shouldCreate bool) (*GitopsCLIConfig, error) {
}
}

sessionConfig = config

return config, nil
}

Expand Down Expand Up @@ -107,6 +121,8 @@ func SaveConfig(config *GitopsCLIConfig) error {
return fmt.Errorf("error writing to config file: %w", err)
}

sessionConfig = config

return nil
}

Expand Down
44 changes: 28 additions & 16 deletions pkg/run/install/install_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
loglevels "github.com/weaveworks/weave-gitops/core/logger"
"github.com/weaveworks/weave-gitops/pkg/config"
"github.com/weaveworks/weave-gitops/pkg/logger"
"github.com/weaveworks/weave-gitops/pkg/run"
"github.com/weaveworks/weave-gitops/pkg/utils"
Expand Down Expand Up @@ -271,13 +272,13 @@ func makeHelmRelease(log logger.Logger, name string, namespace string, username
helmRelease.Spec.Chart.Spec.Version = chartVersion
}

if username != "" && passwordHash != "" {
values, err := makeValues(username, passwordHash)
if err != nil {
log.Failuref("Error generating values from passwordHash")
return nil, err
}
values, err := makeValues(username, passwordHash)
if err != nil {
log.Failuref("Error generating chart values")
return nil, err
}

if values != nil {
helmRelease.Spec.Values = &apiextensionsv1.JSON{Raw: values}
}

Expand All @@ -286,20 +287,31 @@ func makeHelmRelease(log logger.Logger, name string, namespace string, username

// makeValues creates a values object for installing the GitOps Dashboard.
func makeValues(username string, passwordHash string) ([]byte, error) {
valuesMap := map[string]interface{}{
"adminUser": map[string]interface{}{
"create": true,
"username": username,
"passwordHash": passwordHash,
},
valuesMap := make(map[string]interface{})
if username != "" && passwordHash != "" {
valuesMap["adminUser"] =
map[string]interface{}{
"create": true,
"username": username,
"passwordHash": passwordHash,
}
}

jsonRaw, err := json.Marshal(valuesMap)
if err != nil {
return nil, fmt.Errorf("encoding values failed: %w", err)
gitopsConfig, err := config.GetConfig(false)
if err == nil && gitopsConfig.Analytics {
valuesMap["WEAVE_GITOPS_FEATURE_TELEMETRY"] = "true"
}

if len(valuesMap) > 0 {
jsonRaw, err := json.Marshal(valuesMap)
if err != nil {
return nil, fmt.Errorf("encoding values failed: %w", err)
}

return jsonRaw, nil
}

return jsonRaw, nil
return nil, nil
}

func SanitizeResourceData(log logger.Logger, resourceData []byte) ([]byte, error) {
Expand Down
85 changes: 73 additions & 12 deletions pkg/run/install/install_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/weaveworks/weave-gitops/pkg/config"
"github.com/weaveworks/weave-gitops/pkg/logger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -31,6 +32,8 @@ const (
testNamespace = "test-namespace"
testAdminUser = "testUser"
testSecret = "test-secret"
testUserID = "abcdefgh90"
helmChartVersion = "3.0.0"

stateGetDashboardHelmChartGetReturnErr stateGetDashboardHelmChart = "get-return-err"

Expand All @@ -49,7 +52,7 @@ var _ = Describe("InstallDashboard", func() {
It("should install dashboard successfully", func() {
man := &mockResourceManagerForApply{}

manifests, err := CreateDashboardObjects(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, "3.0.0")
manifests, err := CreateDashboardObjects(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, helmChartVersion)
Expect(err).NotTo(HaveOccurred())

err = InstallDashboard(fakeLogger, fakeContext, man, manifests)
Expand All @@ -59,7 +62,7 @@ var _ = Describe("InstallDashboard", func() {
It("should return an apply all error if the resource manager returns an apply all error", func() {
man := &mockResourceManagerForApply{state: stateApplyAllReturnErr}

manifests, err := CreateDashboardObjects(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, "3.0.0")
manifests, err := CreateDashboardObjects(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, helmChartVersion)
Expect(err).NotTo(HaveOccurred())

err = InstallDashboard(fakeLogger, fakeContext, man, manifests)
Expand Down Expand Up @@ -118,10 +121,9 @@ var _ = Describe("generateManifestsForDashboard", func() {
fakeLogger = logger.From(logr.Discard())
})

It("generates manifests successfully", func() {
It("generates manifests", func() {
helmRepository := makeHelmRepository(testDashboardName, testNamespace)

helmChartVersion := "3.0.0"
helmRelease, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, helmChartVersion)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -160,8 +162,12 @@ var _ = Describe("makeHelmRelease", func() {
fakeLogger = logger.From(logr.Discard())
})

It("creates helmrelease with chart version and values successfully", func() {
helmChartVersion := "3.0.0"
It("creates helmrelease with chart version and values", func() {
config.SetConfig(&config.GitopsCLIConfig{
UserID: testUserID,
Analytics: true,
})

actual, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, helmChartVersion)
Expect(err).NotTo(HaveOccurred())
Expect(actual.Kind).To(Equal(helmv2.HelmReleaseKind))
Expand All @@ -185,25 +191,74 @@ var _ = Describe("makeHelmRelease", func() {
Expect(adminUser["create"]).To(BeTrue())
Expect(adminUser["username"]).To(Equal(testAdminUser))
Expect(adminUser["passwordHash"]).To(Equal(testSecret))

Expect(values["WEAVE_GITOPS_FEATURE_TELEMETRY"]).To(Equal("true"))
})

It("creates helmrelease without chart version successfully", func() {
It("creates helmrelease without chart version", func() {
actual, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, "")
Expect(err).NotTo(HaveOccurred())

chart := actual.Spec.Chart
Expect(chart.Spec.Version).To(BeEmpty())
})

It("does not add values to helmrelease if both username and secret are empty successfully", func() {
actual, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, "", "", "3.0.0")
It("does not add values to helmrelease if username and secret are empty and analytics is off", func() {
config.SetConfig(&config.GitopsCLIConfig{
UserID: testUserID,
Analytics: false,
})

actual, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, "", "", helmChartVersion)
Expect(err).NotTo(HaveOccurred())
Expect(actual.Spec.Values).To(BeNil())
})

It("adds only telemetry value to helmrelease if username and secret are empty but analytics is on", func() {
config.SetConfig(&config.GitopsCLIConfig{
UserID: testUserID,
Analytics: true,
})

actual, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, "", "", helmChartVersion)
Expect(err).NotTo(HaveOccurred())

values := map[string]interface{}{}
err = json.Unmarshal(actual.Spec.Values.Raw, &values)
Expect(err).NotTo(HaveOccurred())

Expect(values).NotTo(BeNil())
Expect(values["adminUser"]).To(BeNil())

Expect(values["WEAVE_GITOPS_FEATURE_TELEMETRY"]).To(Equal("true"))
})

It("does not add telemetry value to helmrelease if analytics is off", func() {
config.SetConfig(&config.GitopsCLIConfig{
UserID: testUserID,
Analytics: false,
})

actual, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, helmChartVersion)
Expect(err).NotTo(HaveOccurred())

values := map[string]interface{}{}
err = json.Unmarshal(actual.Spec.Values.Raw, &values)
Expect(err).NotTo(HaveOccurred())

Expect(values).NotTo(BeNil())

adminUser := values["adminUser"].(map[string]interface{})
Expect(adminUser["create"]).To(BeTrue())
Expect(adminUser["username"]).To(Equal(testAdminUser))
Expect(adminUser["passwordHash"]).To(Equal(testSecret))

Expect(values["WEAVE_GITOPS_FEATURE_TELEMETRY"]).To(BeNil())
})
})

var _ = Describe("makeHelmRepository", func() {
It("creates helmrepository successfully", func() {
It("creates helmrepository", func() {
actual := makeHelmRepository(testDashboardName, testNamespace)
Expect(actual.Kind).To(Equal(sourcev1.HelmRepositoryKind))
Expect(actual.APIVersion).To(Equal(sourcev1.GroupVersion.Identifier()))
Expand All @@ -225,7 +280,12 @@ var _ = Describe("makeHelmRepository", func() {
})

var _ = Describe("makeValues", func() {
It("creates values successfully", func() {
It("creates all values", func() {
config.SetConfig(&config.GitopsCLIConfig{
UserID: testUserID,
Analytics: true,
})

values, err := makeValues(testAdminUser, testSecret)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -237,6 +297,8 @@ var _ = Describe("makeValues", func() {
Expect(adminUser["create"]).To(BeTrue())
Expect(adminUser["username"]).To(Equal(testAdminUser))
Expect(adminUser["passwordHash"]).To(Equal(testSecret))

Expect(actual["WEAVE_GITOPS_FEATURE_TELEMETRY"]).To(Equal("true"))
})
})

Expand Down Expand Up @@ -266,7 +328,6 @@ var _ = Describe("SanitizeResourceData", func() {
})

It("sanitizes helmrelease data", func() {
helmChartVersion := "3.0.0"
helmRelease, err := makeHelmRelease(fakeLogger, testDashboardName, testNamespace, testAdminUser, testSecret, helmChartVersion)
Expect(err).NotTo(HaveOccurred())

Expand Down

0 comments on commit c005e1a

Please sign in to comment.