Skip to content

Commit

Permalink
fix: add email to the on prem install telemetry (#5495)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicufk committed May 24, 2024
1 parent 8f28ac0 commit 304d9aa
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
26 changes: 11 additions & 15 deletions cmd/kubectl-testkube/commands/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,43 @@ 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)

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)

ui.Success("The dashboard is accessible here:", uri)
ui.Success("Port forwarding the necessary services, hit Ctrl+c (or Cmd+c) to stop")
<-c
cancel()

}

func localPortCheck(port int) error {
Expand Down
8 changes: 7 additions & 1 deletion pkg/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions pkg/telemetry/license.go
Original file line number Diff line number Diff line change
@@ -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 ""
}
2 changes: 2 additions & 0 deletions pkg/telemetry/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -141,6 +142,7 @@ func NewCLIWithLicensePayload(context RunContext, id, name, version, category, c
ClusterType: clusterType,
CliContext: GetCliRunContext(),
License: license,
Email: GetEmail(license),
Step: step,
},
}},
Expand Down
3 changes: 3 additions & 0 deletions pkg/telemetry/sender_sio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func SendCmdErrorEventWithLicense(cmd *cobra.Command, version, errType, errorSta
ErrorStackTrace: errorStackTrace,
License: license,
Step: step,
Email: GetEmail(license),
},
}},
}
Expand Down

0 comments on commit 304d9aa

Please sign in to comment.