diff --git a/cli/cmd/tap.go b/cli/cmd/tap.go index 7985a10c1..a8b67d603 100644 --- a/cli/cmd/tap.go +++ b/cli/cmd/tap.go @@ -19,10 +19,8 @@ type MizuTapOptions struct { AnalyzeDestination string KubeConfigPath string MizuImage string - MizuPodPort uint16 PlainTextFilterRegexes []string TapOutgoing bool - ServiceType string } var mizuTapOptions = &MizuTapOptions{} @@ -69,8 +67,6 @@ func init() { 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") - tapCmd.Flags().Uint16VarP(&mizuTapOptions.MizuPodPort, "mizu-port", "", 8899, "Port which mizu cli will attempt to forward from the mizu collector pod") tapCmd.Flags().StringArrayVarP(&mizuTapOptions.PlainTextFilterRegexes, "regex-masking", "r", nil, "List of regex expressions that are used to filter matching values from text/plain http bodies") tapCmd.Flags().StringVarP(&direction, "direction", "", "in", "Record traffic that goes in this direction (relative to the tapped pod): in/any") - tapCmd.Flags().StringVarP(&mizuTapOptions.ServiceType, "service-type", "", "ClusterIP", "Set a service type for mizu collector's kubernetes service") } diff --git a/cli/cmd/tapRunner.go b/cli/cmd/tapRunner.go index 089a34c97..b1b0c2326 100644 --- a/cli/cmd/tapRunner.go +++ b/cli/cmd/tapRunner.go @@ -103,7 +103,7 @@ func createMizuAggregator(ctx context.Context, kubernetesProvider *kubernetes.Pr return err } - aggregatorService, err = kubernetesProvider.CreateService(ctx, mizu.ResourcesNamespace, mizu.AggregatorPodName, mizu.AggregatorPodName, tappingOptions.ServiceType) + aggregatorService, err = kubernetesProvider.CreateService(ctx, mizu.ResourcesNamespace, mizu.AggregatorPodName, mizu.AggregatorPodName) if err != nil { fmt.Printf("Error creating mizu collector service: %v\n", err) return err @@ -244,12 +244,11 @@ func portForwardApiPod(ctx context.Context, kubernetesProvider *kubernetes.Provi go func() { err := kubernetes.StartProxy(kubernetesProvider, tappingOptions.GuiPort, mizu.ResourcesNamespace, mizu.AggregatorPodName) if err != nil { - fmt.Printf("Error starting k8s proxy %v\n", err) + fmt.Printf("Error occured while running k8s proxy %v\n", err) cancel() - } else { - fmt.Printf("Mizu is available at http://%s\n", kubernetes.GetMizuCollectorProxiesHostAndPath(tappingOptions.GuiPort, mizu.ResourcesNamespace, mizu.AggregatorPodName)) } }() + fmt.Printf("Mizu is available at http://%s\n", kubernetes.GetMizuCollectorProxiesHostAndPath(tappingOptions.GuiPort, mizu.ResourcesNamespace, mizu.AggregatorPodName)) isPodReady = true } diff --git a/cli/cmd/view.go b/cli/cmd/view.go index 529c756b1..acb2091c9 100644 --- a/cli/cmd/view.go +++ b/cli/cmd/view.go @@ -4,15 +4,24 @@ import ( "github.com/spf13/cobra" ) +type MizuViewOptions struct { + GuiPort uint16 +} + +var mizuViewOptions = &MizuViewOptions{} + var viewCmd = &cobra.Command{ Use: "view", Short: "Open GUI in browser", RunE: func(cmd *cobra.Command, args []string) error { - runMizuView() + runMizuView(mizuViewOptions) return nil }, } func init() { rootCmd.AddCommand(viewCmd) + + viewCmd.Flags().Uint16VarP(&mizuViewOptions.GuiPort, "gui-port", "p", 8899, "Provide a custom port for the web interface webserver") + } diff --git a/cli/cmd/viewRunner.go b/cli/cmd/viewRunner.go index 059a9b2e2..9a35e43a2 100644 --- a/cli/cmd/viewRunner.go +++ b/cli/cmd/viewRunner.go @@ -8,7 +8,7 @@ import ( "net/http" ) -func runMizuView() { +func runMizuView(mizuViewOptions *MizuViewOptions) { kubernetesProvider := kubernetes.NewProvider("") ctx, cancel := context.WithCancel(context.Background()) @@ -23,11 +23,17 @@ func runMizuView() { return } - _, err = http.Get("http://localhost:8899/") + mizuProxiedUrl := kubernetes.GetMizuCollectorProxiesHostAndPath(mizuViewOptions.GuiPort, mizu.ResourcesNamespace, mizu.AggregatorPodName) + _, err = http.Get(fmt.Sprintf("http://%s/", mizuProxiedUrl)) if err == nil { - fmt.Printf("Found a running service %s and open port 8899\n", mizu.AggregatorPodName) + fmt.Printf("Found a running service %s and open port %d\n", mizu.AggregatorPodName, mizuViewOptions.GuiPort) return } - fmt.Printf("Found service %s, creating port forwarding to 8899\n", mizu.AggregatorPodName) - portForwardApiPod(ctx, kubernetesProvider, cancel, &MizuTapOptions{GuiPort: 8899, MizuPodPort: 8899}) + fmt.Printf("Found service %s, creating k8s proxy\n", mizu.AggregatorPodName) + + fmt.Printf("Mizu is available at http://%s\n", kubernetes.GetMizuCollectorProxiesHostAndPath(mizuViewOptions.GuiPort, mizu.ResourcesNamespace, mizu.AggregatorPodName)) + err = kubernetes.StartProxy(kubernetesProvider, mizuViewOptions.GuiPort, mizu.ResourcesNamespace, mizu.AggregatorPodName) + if err != nil { + fmt.Printf("Error occured while running k8s proxy %v\n", err) + } } diff --git a/cli/kubernetes/provider.go b/cli/kubernetes/provider.go index 39a5bf848..0bf40c1b8 100644 --- a/cli/kubernetes/provider.go +++ b/cli/kubernetes/provider.go @@ -111,7 +111,7 @@ func (provider *Provider) CreateMizuAggregatorPod(ctx context.Context, namespace return provider.clientSet.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{}) } -func (provider *Provider) CreateService(ctx context.Context, namespace string, serviceName string, appLabelValue string, serviceType string) (*core.Service, error) { +func (provider *Provider) CreateService(ctx context.Context, namespace string, serviceName string, appLabelValue string) (*core.Service, error) { service := core.Service{ ObjectMeta: metav1.ObjectMeta{ Name: serviceName, @@ -119,7 +119,7 @@ func (provider *Provider) CreateService(ctx context.Context, namespace string, s }, Spec: core.ServiceSpec{ Ports: []core.ServicePort{{TargetPort: intstr.FromInt(8899), Port: 80}}, - Type: core.ServiceType(serviceType), + Type: core.ServiceTypeClusterIP, Selector: map[string]string{"app": appLabelValue}, }, }