From 5f603e3291a7257a4fe21d81157ac426dc032374 Mon Sep 17 00:00:00 2001 From: Roee Gadot Date: Tue, 29 Jun 2021 19:16:46 +0300 Subject: [PATCH] improvements and fixes --- api/pkg/controllers/entries_controller.go | 21 ++++++++++++--------- api/pkg/models/models.go | 1 + cli/cmd/tap.go | 4 +++- cli/cmd/tapRunner.go | 9 ++++----- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/api/pkg/controllers/entries_controller.go b/api/pkg/controllers/entries_controller.go index 86359f55d..507831b37 100644 --- a/api/pkg/controllers/entries_controller.go +++ b/api/pkg/controllers/entries_controller.go @@ -144,9 +144,8 @@ func GetHARs(c *fiber.Ctx) error { return c.Status(fiber.StatusOK).SendStream(buffer) } -func uploadEntriesImpl(token string, model string) { - baseUrl := "igorgov-dev.dev.testr.io" - sleepTime := int64(time.Second * 10) +func uploadEntriesImpl(token string, model string, envPrefix string) { + sleepTime := time.Second * 10 var timestampFrom int64 = 0 @@ -165,11 +164,12 @@ func uploadEntriesImpl(token string, model string) { var in bytes.Buffer w := zlib.NewWriter(&in) - w.Write(body) - w.Close() + _, _ = w.Write(body) + _ = w.Close() reqBody := ioutil.NopCloser(bytes.NewReader(in.Bytes())) - postUrl, _ := url.Parse("https://traffic." + baseUrl + "/dumpTrafficBulk/" + model) + postUrl, _ := url.Parse("https://traffic." + envPrefix + "/dumpTrafficBulk/" + model) + fmt.Println(postUrl) req := &http.Request{ Method: "POST", URL: postUrl, @@ -184,12 +184,15 @@ func uploadEntriesImpl(token string, model string) { if postErr != nil { log.Fatal(postErr) } + + fmt.Printf("Finish uploading %v entries to %s\n", len(entriesArray), postUrl) + } else { fmt.Println("Nothing to upload") } - fmt.Println("Sleeping for " + string(sleepTime) + "seconds...") - time.Sleep(time.Duration(sleepTime)) + fmt.Printf("Sleeping for %v...\n", sleepTime) + time.Sleep(sleepTime) timestampFrom = timestampTo } } @@ -202,7 +205,7 @@ func UploadEntries(c *fiber.Ctx) error { if err := validation.Validate(entriesFilter); err != nil { return c.Status(fiber.StatusBadRequest).JSON(err) } - go uploadEntriesImpl(entriesFilter.Token, entriesFilter.Model) + go uploadEntriesImpl(entriesFilter.Token, entriesFilter.Model, entriesFilter.Dest) return c.Status(fiber.StatusOK).SendString("OK") } diff --git a/api/pkg/models/models.go b/api/pkg/models/models.go index cf588fcec..b36ea7528 100644 --- a/api/pkg/models/models.go +++ b/api/pkg/models/models.go @@ -52,6 +52,7 @@ type EntriesFilter struct { type UploadEntriesRequestBody struct { Token string `query:"token"` Model string `query:"model"` + Dest string `query:"dest"` } type HarFetchRequestBody struct { diff --git a/cli/cmd/tap.go b/cli/cmd/tap.go index 05f03072e..bd649baa9 100644 --- a/cli/cmd/tap.go +++ b/cli/cmd/tap.go @@ -16,6 +16,7 @@ type MizuTapOptions struct { Namespace string AllNamespaces bool Analyze bool + AnalyzeDestination string KubeConfigPath string MizuImage string MizuPodPort uint16 @@ -62,7 +63,8 @@ func init() { tapCmd.Flags().Uint16VarP(&mizuTapOptions.GuiPort, "gui-port", "p", 8899, "Provide a custom port for the web interface webserver") tapCmd.Flags().StringVarP(&mizuTapOptions.Namespace, "namespace", "n", "", "Namespace selector") - tapCmd.Flags().BoolVarP(&mizuTapOptions.Analyze, "analyze", "", false, "Analyze traffic") + tapCmd.Flags().BoolVar(&mizuTapOptions.Analyze, "analyze", false, "Analyze traffic") + tapCmd.Flags().StringVar(&mizuTapOptions.AnalyzeDestination, "dest", "up9.app", "Destination environment") tapCmd.Flags().BoolVarP(&mizuTapOptions.AllNamespaces, "all-namespaces", "A", false, "Tap all namespaces") tapCmd.Flags().StringVarP(&mizuTapOptions.KubeConfigPath, "kube-config", "k", "", "Path to kube-config file") tapCmd.Flags().StringVarP(&mizuTapOptions.MizuImage, "mizu-image", "", fmt.Sprintf("gcr.io/up9-docker-hub/mizu/%s:latest", mizu.Branch), "Custom image for mizu collector") diff --git a/cli/cmd/tapRunner.go b/cli/cmd/tapRunner.go index cf9101f69..c57b29a01 100644 --- a/cli/cmd/tapRunner.go +++ b/cli/cmd/tapRunner.go @@ -74,7 +74,7 @@ func RunMizuTap(podRegexQuery *regexp.Regexp, tappingOptions *MizuTapOptions) { return } - go portForwardApiPod(ctx, kubernetesProvider, cancel, tappingOptions) // TODO convert this to job for built in pod ttl or have the running app handle this + go portForwardApiPod(ctx, kubernetesProvider, cancel, tappingOptions, ) // TODO convert this to job for built in pod ttl or have the running app handle this go watchPodsForTapping(ctx, kubernetesProvider, cancel, podRegexQuery, tappingOptions) go syncApiStatus(ctx, cancel, tappingOptions) @@ -275,13 +275,12 @@ func portForwardApiPod(ctx context.Context, kubernetesProvider *kubernetes.Provi fmt.Printf("Web interface is now available at http://localhost:%d\n", tappingOptions.GuiPort) if tappingOptions.Analyze { - baseUrl := "igorgov-dev.dev.testr.io" - token := CreateAnonymousToken(baseUrl) - _, err := http.Get("http://localhost:8899/api/uploadEntries") + token := CreateAnonymousToken(tappingOptions.AnalyzeDestination) + _, err := http.Get(fmt.Sprintf("http://localhost:8899/api/uploadEntries?token=%s&model=%s&dest=%s", token.Token, token.Model, tappingOptions.AnalyzeDestination)) if err != nil { fmt.Println(err) } - fmt.Println("https://" + baseUrl + "/share/" + token.Token) + fmt.Println("https://" + tappingOptions.AnalyzeDestination + "/share/" + token.Token) } if err != nil {