diff --git a/cli/cmd/root.go b/cli/cmd/root.go index cb99a9c01..3a47e5dd6 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -1,11 +1,9 @@ package cmd import ( - "fmt" "github.com/spf13/cobra" "github.com/up9inc/mizu/cli/config" "github.com/up9inc/mizu/cli/mizu" - "regexp" ) // rootCmd represents the base command when called without any subcommands @@ -16,16 +14,11 @@ func init() { rootCmd.Use = "cmd pod-query" rootCmd.Short = "Tail HTTP traffic from multiple pods" rootCmd.RunE = func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { + if len(args) != 0 { return rootCmd.Help() } - regex, err := regexp.Compile(args[0]) - if err != nil { - fmt.Printf("%s is not a valid regex %s", args[0], err) - return nil - } - mizu.Run(regex) + mizu.Run() return nil } @@ -36,8 +29,9 @@ func init() { rootCmd.Flags().StringVarP(&config.Configuration.Namespace, "namespace", "n", "", "Namespace selector") rootCmd.Flags().BoolVarP(&config.Configuration.AllNamespaces, "all-namespaces", "A", false, "Select all namespaces") rootCmd.Flags().StringVarP(&config.Configuration.KubeConfigPath, "kubeconfig", "k", "", "Path to kubeconfig file") - rootCmd.Flags().StringVarP(&config.Configuration.MizuImage, "mizu-image", "", "gcr.io/up9-docker-hub/mizu/develop/v6", "Custom image for mizu collector") + rootCmd.Flags().StringVarP(&config.Configuration.MizuImage, "mizu-image", "", "gcr.io/up9-docker-hub/mizu/develop:latest", "Custom image for mizu collector") rootCmd.Flags().Uint16VarP(&config.Configuration.MizuPodPort, "mizu-port", "", 8899, "Port which mizu cli will attempt to forward from the mizu collector pod") + rootCmd.Flags().StringVarP(&config.Configuration.TappedPodName, "pod", "", "", "View traffic of this pod") } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cli/config/config.go b/cli/config/config.go index 3be92624a..0b7d6ee76 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -10,6 +10,7 @@ type Options struct { KubeConfigPath string MizuImage string MizuPodPort uint16 + TappedPodName string } var Configuration = &Options{} diff --git a/cli/kubernetes/provider.go b/cli/kubernetes/provider.go index 15a5ed728..d49619833 100644 --- a/cli/kubernetes/provider.go +++ b/cli/kubernetes/provider.go @@ -70,7 +70,12 @@ func (provider *Provider) GetPods(ctx context.Context) { fmt.Printf("There are %d pods in Namespace %s\n", len(pods.Items), provider.Namespace) } -func (provider *Provider) CreatePod(ctx context.Context, podName string, podImage string) (*core.Pod, error) { +func (provider *Provider) CreateMizuPod(ctx context.Context, podName string, podImage string, tappedPodName string) (*core.Pod, error) { + tappedPod, err := provider.clientSet.CoreV1().Pods(provider.Namespace).Get(ctx, tappedPodName, metav1.GetOptions{}) + if err != nil { + panic(err.Error()) + } + privileged := true pod := &core.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -96,6 +101,7 @@ func (provider *Provider) CreatePod(ctx context.Context, podName string, podImag }, }, TerminationGracePeriodSeconds: new(int64), + NodeSelector: map[string]string{"kubernetes.io/hostname": tappedPod.Spec.NodeName}, }, } return provider.clientSet.CoreV1().Pods(provider.Namespace).Create(ctx, pod, metav1.CreateOptions{}) diff --git a/cli/mizu/mizuRunner.go b/cli/mizu/mizuRunner.go index f75028a8c..39cc4cb87 100644 --- a/cli/mizu/mizuRunner.go +++ b/cli/mizu/mizuRunner.go @@ -12,7 +12,7 @@ import ( "time" ) -func Run(podRegex *regexp.Regexp) { +func Run() { kubernetesProvider := kubernetes.NewProvider(config.Configuration.KubeConfigPath, config.Configuration.Namespace) ctx, cancel := context.WithCancel(context.Background()) defer cancel() // cancel will be called when this function exits @@ -20,7 +20,6 @@ func Run(podRegex *regexp.Regexp) { podName := "mizu-collector" go createPodAndPortForward(ctx, kubernetesProvider, cancel, podName) //TODO convert this to job for built in pod ttl or have the running app handle this - go watchPodsForTapping(ctx, kubernetesProvider, cancel, podRegex) waitForFinish(ctx, cancel) //block until exit signal or error // TODO handle incoming traffic from tapper using a channel @@ -54,7 +53,7 @@ func watchPodsForTapping(ctx context.Context, kubernetesProvider *kubernetes.Pro } func createPodAndPortForward(ctx context.Context, kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc, podName string) { - pod, err := kubernetesProvider.CreatePod(ctx, podName, config.Configuration.MizuImage) + pod, err := kubernetesProvider.CreateMizuPod(ctx, podName, config.Configuration.MizuImage, config.Configuration.TappedPodName) if err != nil { fmt.Printf("error creating pod %s", err) cancel()