From 304d9aaf17480156411f13fb746ecd83fda1ed5b Mon Sep 17 00:00:00 2001 From: nicufk Date: Fri, 24 May 2024 16:57:59 +0300 Subject: [PATCH] fix: add email to the on prem install telemetry (#5495) --- cmd/kubectl-testkube/commands/dashboard.go | 26 ++++++-------- pkg/k8sclient/k8sclient.go | 8 ++++- pkg/telemetry/license.go | 41 ++++++++++++++++++++++ pkg/telemetry/payload.go | 2 ++ pkg/telemetry/sender_sio.go | 3 ++ pkg/telemetry/telemetry.go | 1 + 6 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 pkg/telemetry/license.go diff --git a/cmd/kubectl-testkube/commands/dashboard.go b/cmd/kubectl-testkube/commands/dashboard.go index a6bd554a3f4..26194e024db 100644 --- a/cmd/kubectl-testkube/commands/dashboard.go +++ b/cmd/kubectl-testkube/commands/dashboard.go @@ -64,7 +64,6 @@ func openCloudDashboard(cfg config.Data) { } func openOnPremDashboard(cmd *cobra.Command, cfg config.Data, verbose bool, license string) { - var errs []error uiLocalPort, err := getDashboardLocalPort(config.EnterpriseApiForwardingPort) ui.PrintOnError("getting an ui forwarding available port", err) uri := fmt.Sprintf("http://localhost:%d", uiLocalPort) @@ -72,31 +71,29 @@ func openOnPremDashboard(cmd *cobra.Command, cfg config.Data, verbose bool, lice ctx, cancel := context.WithCancel(context.Background()) err = k8sclient.PortForward(ctx, cfg.Namespace, config.EnterpriseApiName, config.EnterpriseApiPort, config.EnterpriseApiForwardingPort, verbose) if err != nil { - errs = append(errs, err) + sendErrTelemetry(cmd, cfg, "port_forward", license, "port forwarding api", err) } - ui.PrintOnError("port forwarding api", err) + ui.ExitOnError("port forwarding api", err) err = k8sclient.PortForward(ctx, cfg.Namespace, config.EnterpriseUiName, config.EnterpriseUiPort, uiLocalPort, verbose) if err != nil { - errs = append(errs, err) + sendErrTelemetry(cmd, cfg, "port_forward", license, "port forwarding ui", err) } - ui.PrintOnError("port forwarding ui", err) + ui.ExitOnError("port forwarding ui", err) err = k8sclient.PortForward(ctx, cfg.Namespace, config.EnterpriseDexName, config.EnterpriseDexPort, config.EnterpriseDexForwardingPort, verbose) if err != nil { - errs = append(errs, err) + sendErrTelemetry(cmd, cfg, "port_forward", license, "port forwarding dex", err) } - ui.PrintOnError("port forwarding dex", err) + ui.ExitOnError("port forwarding dex", err) err = open.Run(uri) if err != nil { - errs = append(errs, err) - } - if len(errs) > 0 { - retErr := errors.Join(errs...) - sendErrTelemetry(cmd, cfg, "open_dashboard", license, "opening dashboard", retErr) - } else { - sendTelemetry(cmd, cfg, license, "dashbboard opened successfully") + sendErrTelemetry(cmd, cfg, "open_dashboard", license, "opening dashboard", err) } + ui.ExitOnError("opening dashboard in browser", err) + + sendTelemetry(cmd, cfg, license, "dashbboard opened successfully") + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) @@ -104,7 +101,6 @@ func openOnPremDashboard(cmd *cobra.Command, cfg config.Data, verbose bool, lice ui.Success("Port forwarding the necessary services, hit Ctrl+c (or Cmd+c) to stop") <-c cancel() - } func localPortCheck(port int) error { diff --git a/pkg/k8sclient/k8sclient.go b/pkg/k8sclient/k8sclient.go index 3b156eeed28..d40d988e89a 100644 --- a/pkg/k8sclient/k8sclient.go +++ b/pkg/k8sclient/k8sclient.go @@ -364,12 +364,18 @@ func PortForward(ctx context.Context, namespace, serviceName string, servicePort } go func() { + defer func() { + if r := recover(); r != nil { + err = errors.New("port forwarding failed:" + fmt.Sprint(r)) + readyChan <- struct{}{} + } + }() if err = forwarder.ForwardPorts(); err != nil { log.Errorf(ctx, "port forwarding failed: %v", err) } }() <-readyChan - return nil + return err } func IsPodOfServiceRunning(ctx context.Context, namespace, serviceName string) (bool, error) { diff --git a/pkg/telemetry/license.go b/pkg/telemetry/license.go new file mode 100644 index 00000000000..f5b87f982dd --- /dev/null +++ b/pkg/telemetry/license.go @@ -0,0 +1,41 @@ +package telemetry + +import ( + "bytes" + "encoding/json" + "net/http" +) + +const ( + LicenseEndpoint = "https://license.testkube.io/owner" //this is default but it can be set using ldflag -X github.com/kubeshop/testkube/pkg/telemetry.LicenseEndpoint=https://license.localhost +) + +type EmailResponse struct { + Owner struct { + Email string `json:"email"` + } `json:"owner"` +} +type EmailRequest struct { + License string `json:"license"` +} + +// GetEmail returns email +func GetEmail(license string) string { + if LicenseEndpoint != "" { + payload := EmailRequest{License: license} + jsonPayload, _ := json.Marshal(payload) + resp, err := http.Post(LicenseEndpoint, "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + return "" + } + defer resp.Body.Close() + + var emailResponse EmailResponse + err = json.NewDecoder(resp.Body).Decode(&emailResponse) + if err != nil { + return "" + } + return emailResponse.Owner.Email + } + return "" +} diff --git a/pkg/telemetry/payload.go b/pkg/telemetry/payload.go index a906a1e7d24..ece02f9a3a1 100644 --- a/pkg/telemetry/payload.go +++ b/pkg/telemetry/payload.go @@ -40,6 +40,7 @@ type Params struct { TestWorkflowKubeshopGitURI bool `json:"test_workflow_kubeshop_git_uri,omitempty"` License string `json:"license,omitempty"` Step string `json:"step,omitempty"` + Email string `json:"email,omitempty"` } type Event struct { @@ -141,6 +142,7 @@ func NewCLIWithLicensePayload(context RunContext, id, name, version, category, c ClusterType: clusterType, CliContext: GetCliRunContext(), License: license, + Email: GetEmail(license), Step: step, }, }}, diff --git a/pkg/telemetry/sender_sio.go b/pkg/telemetry/sender_sio.go index ec6fdec454d..516f6668ea5 100644 --- a/pkg/telemetry/sender_sio.go +++ b/pkg/telemetry/sender_sio.go @@ -105,6 +105,9 @@ func mapProperties(params Params) analytics.Properties { if params.Step != "" { properties = properties.Set("step", params.Step) } + if params.Email != "" { + properties = properties.Set("email", params.Email) + } if params.DataSource != "" { properties = properties.Set("dataSource", params.DataSource) diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index 8fe388da80f..1da43514e22 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -83,6 +83,7 @@ func SendCmdErrorEventWithLicense(cmd *cobra.Command, version, errType, errorSta ErrorStackTrace: errorStackTrace, License: license, Step: step, + Email: GetEmail(license), }, }}, }