From 35cc554149ab8c763d32e2a7d1f079f7a49bf322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Wed, 16 Nov 2022 11:29:59 -0300 Subject: [PATCH 1/2] chore: add wrapper for telemetry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/local/local.go | 70 +++------------------------------- cmd/local/prerun.go | 57 ++++++--------------------- internal/wrappers/segmentIo.go | 45 ++++++++++++++++++++++ 3 files changed, 63 insertions(+), 109 deletions(-) create mode 100644 internal/wrappers/segmentIo.go diff --git a/cmd/local/local.go b/cmd/local/local.go index ef1fe0780..58a7a2bf1 100644 --- a/cmd/local/local.go +++ b/cmd/local/local.go @@ -3,27 +3,24 @@ package local import ( "context" "fmt" + "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/wrappers" "log" "sync" "time" "github.com/go-git/go-git/v5/plumbing" - "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/argocd" - "github.com/kubefirst/kubefirst/internal/domain" "github.com/kubefirst/kubefirst/internal/gitClient" "github.com/kubefirst/kubefirst/internal/githubWrapper" - "github.com/kubefirst/kubefirst/internal/handlers" "github.com/kubefirst/kubefirst/internal/helm" "github.com/kubefirst/kubefirst/internal/k3d" "github.com/kubefirst/kubefirst/internal/k8s" "github.com/kubefirst/kubefirst/internal/metaphor" "github.com/kubefirst/kubefirst/internal/progressPrinter" - "github.com/kubefirst/kubefirst/internal/services" "github.com/kubefirst/kubefirst/internal/terraform" "github.com/kubefirst/kubefirst/internal/vault" "github.com/kubefirst/kubefirst/pkg" - "github.com/segmentio/analytics-go" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -35,7 +32,6 @@ var ( enableConsole bool gitOpsBranch string gitOpsRepo string - awsHostedZone string metaphorBranch string adminEmail string templateTag string @@ -66,8 +62,6 @@ func NewCommand() *cobra.Command { "when running a built version, and ldflag is set for the Kubefirst version, it will use this tag value to clone the templates (gitops and metaphor's)", ) - localCmd.AddCommand(NewCommandConnect()) - // on error, doesnt show helper/usage localCmd.SilenceUsage = true @@ -87,42 +81,13 @@ func runLocal(cmd *cobra.Command, args []string) error { progressPrinter.AddTracker("step-base", "Setup base cluster", 2) progressPrinter.AddTracker("step-apps", "Install apps to cluster", 4) - if useTelemetry { - progressPrinter.AddTracker("step-telemetry", pkg.SendTelemetry, 1) - } - progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), silentMode) - // telemetry if useTelemetry { - // Instantiates a SegmentIO client to send messages to the segment API. - segmentIOClientStart := analytics.New(pkg.SegmentIOWriteKey) - - // SegmentIO library works with queue that is based on timing, we explicit close the http client connection - // to force flush in case there is still some pending message in the SegmentIO library queue. - defer func(segmentIOClient analytics.Client) { - err := segmentIOClient.Close() - if err != nil { - log.Println(err) - } - }(segmentIOClientStart) - - telemetryDomainStart, err := domain.NewTelemetry( - pkg.MetricMgmtClusterInstallStarted, - "", - configs.K1Version, - ) - if err != nil { + progressPrinter.AddTracker("step-telemetry", pkg.SendTelemetry, 2) + if err := wrappers.SendTelemetry("", pkg.MetricMgmtClusterInstallStarted); err != nil { log.Println(err) } - telemetryServiceStart := services.NewSegmentIoService(segmentIOClientStart) - telemetryHandlerStart := handlers.NewTelemetryHandler(telemetryServiceStart) - - err = telemetryHandlerStart.SendCountMetric(telemetryDomainStart) - if err != nil { - log.Println(err) - } - progressPrinter.IncrementTracker("step-telemetry", 1) } @@ -486,33 +451,10 @@ func runLocal(cmd *cobra.Command, args []string) error { log.Println("sending mgmt cluster install completed metric") if useTelemetry { - // Instantiates a SegmentIO client to send messages to the segment API. - segmentIOClientCompleted := analytics.New(pkg.SegmentIOWriteKey) - - // SegmentIO library works with queue that is based on timing, we explicit close the http client connection - // to force flush in case there is still some pending message in the SegmentIO library queue. - defer func(segmentIOClientCompleted analytics.Client) { - err := segmentIOClientCompleted.Close() - if err != nil { - log.Println(err) - } - }(segmentIOClientCompleted) - - telemetryDomainCompleted, err := domain.NewTelemetry( - pkg.MetricMgmtClusterInstallCompleted, - "", - configs.K1Version, - ) - if err != nil { - log.Println(err) - } - telemetryServiceCompleted := services.NewSegmentIoService(segmentIOClientCompleted) - telemetryHandlerCompleted := handlers.NewTelemetryHandler(telemetryServiceCompleted) - - err = telemetryHandlerCompleted.SendCountMetric(telemetryDomainCompleted) - if err != nil { + if err = wrappers.SendTelemetry("", pkg.MetricMgmtClusterInstallCompleted); err != nil { log.Println(err) } + progressPrinter.IncrementTracker("step-telemetry", 1) } log.Println("Kubefirst installation finished successfully") diff --git a/cmd/local/prerun.go b/cmd/local/prerun.go index 1f6f4c3b8..40afa1190 100644 --- a/cmd/local/prerun.go +++ b/cmd/local/prerun.go @@ -6,55 +6,32 @@ import ( "github.com/dustin/go-humanize" "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/addon" - "github.com/kubefirst/kubefirst/internal/domain" "github.com/kubefirst/kubefirst/internal/downloadManager" "github.com/kubefirst/kubefirst/internal/handlers" "github.com/kubefirst/kubefirst/internal/progressPrinter" "github.com/kubefirst/kubefirst/internal/repo" "github.com/kubefirst/kubefirst/internal/services" + "github.com/kubefirst/kubefirst/internal/wrappers" "github.com/kubefirst/kubefirst/pkg" - "github.com/segmentio/analytics-go" "github.com/spf13/cobra" "github.com/spf13/viper" "log" "net/http" "os" + "time" ) func validateLocal(cmd *cobra.Command, args []string) error { + // + installStartTime = time.Now() + config := configs.ReadConfig() log.Println("sending init started metric") - var telemetryHandler handlers.TelemetryHandler if useTelemetry { - // Instantiates a SegmentIO client to use send messages to the segment API. - segmentIOClient := analytics.New(pkg.SegmentIOWriteKey) - - // SegmentIO library works with queue that is based on timing, we explicit close the http client connection - // to force flush in case there is still some pending message in the SegmentIO library queue. - defer func(segmentIOClient analytics.Client) { - err := segmentIOClient.Close() - if err != nil { - log.Println(err) - } - }(segmentIOClient) - - // validate telemetryDomain data - telemetryDomain, err := domain.NewTelemetry( - pkg.MetricInitStarted, - awsHostedZone, - configs.K1Version, - ) - if err != nil { - log.Println(err) - } - telemetryService := services.NewSegmentIoService(segmentIOClient) - telemetryHandler = handlers.NewTelemetryHandler(telemetryService) - - err = telemetryHandler.SendCountMetric(telemetryDomain) - if err != nil { + if err := wrappers.SendTelemetry("", pkg.MetricInitStarted); err != nil { log.Println(err) } } @@ -103,16 +80,15 @@ func validateLocal(cmd *cobra.Command, args []string) error { viper.Set("cluster-name", pkg.LocalClusterName) viper.Set("adminemail", adminEmail) - // todo: set constants - viper.Set("argocd.local.service", "http://localhost:8080") - viper.Set("gitlab.local.service", "http://localhost:8888") - viper.Set("vault.local.service", "http://localhost:8200") + viper.Set("argocd.local.service", pkg.ArgoCDLocalURL) + viper.Set("vault.local.service", pkg.VaultLocalURL) + + // addons addon.AddAddon("github") addon.AddAddon("k3d") // used for letsencrypt notifications and the gitlab root account - atlantisWebhookSecret := pkg.Random(20) - viper.Set("github.atlantis.webhook.secret", atlantisWebhookSecret) + viper.Set("github.atlantis.webhook.secret", pkg.Random(20)) err = viper.WriteConfig() if err != nil { @@ -206,16 +182,7 @@ func validateLocal(cmd *cobra.Command, args []string) error { pkg.InformUser("init is done!\n", silentMode) if useTelemetry { - telemetryInitCompleted, err := domain.NewTelemetry( - pkg.MetricInitCompleted, - awsHostedZone, - configs.K1Version, - ) - if err != nil { - log.Println(err) - } - err = telemetryHandler.SendCountMetric(telemetryInitCompleted) - if err != nil { + if err = wrappers.SendTelemetry("", pkg.MetricInitCompleted); err != nil { log.Println(err) } } diff --git a/internal/wrappers/segmentIo.go b/internal/wrappers/segmentIo.go new file mode 100644 index 000000000..d6787d888 --- /dev/null +++ b/internal/wrappers/segmentIo.go @@ -0,0 +1,45 @@ +package wrappers + +import ( + "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/domain" + "github.com/kubefirst/kubefirst/internal/handlers" + "github.com/kubefirst/kubefirst/internal/services" + "github.com/kubefirst/kubefirst/pkg" + "github.com/segmentio/analytics-go" + "log" +) + +// SendTelemetry is a wrapper function that instantiate SegmentIO handler, service, and sends a track activity to +// SegmentIO. +func SendTelemetry(hostedZone string, metricName string) error { + // Instantiates a SegmentIO client to use send messages to the segment API. + segmentIOClient := analytics.New(pkg.SegmentIOWriteKey) + + // SegmentIO library works with queue that is based on timing, we explicit close the http client connection + // to force flush in case there is still some pending message in the SegmentIO library queue. + defer func(segmentIOClient analytics.Client) { + err := segmentIOClient.Close() + if err != nil { + log.Println(err) + } + }(segmentIOClient) + + // validate telemetryDomain data + telemetryDomain, err := domain.NewTelemetry( + metricName, + hostedZone, + configs.K1Version, + ) + if err != nil { + return err + } + telemetryService := services.NewSegmentIoService(segmentIOClient) + telemetryHandler := handlers.NewTelemetryHandler(telemetryService) + + err = telemetryHandler.SendCountMetric(telemetryDomain) + if err != nil { + return err + } + return nil +} From 47276bca9c82d04036f2ac077efd2bdc1c622027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Wed, 16 Nov 2022 11:32:00 -0300 Subject: [PATCH 2/2] chore: clean up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/local/prerun.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/local/prerun.go b/cmd/local/prerun.go index 40afa1190..2aa1a1503 100644 --- a/cmd/local/prerun.go +++ b/cmd/local/prerun.go @@ -18,14 +18,10 @@ import ( "log" "net/http" "os" - "time" ) func validateLocal(cmd *cobra.Command, args []string) error { - // - installStartTime = time.Now() - config := configs.ReadConfig() log.Println("sending init started metric")