Select node by pod (#18)

* Select node by pod.

* Removed watch pod by regex. Irrelevant for now.

* Changed default image to develop:latest.
This commit is contained in:
nimrod-up9 2021-04-29 15:46:18 +03:00 committed by GitHub
parent 96a6ac3251
commit 38146a644d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 14 deletions

View File

@ -1,11 +1,9 @@
package cmd package cmd
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/up9inc/mizu/cli/config" "github.com/up9inc/mizu/cli/config"
"github.com/up9inc/mizu/cli/mizu" "github.com/up9inc/mizu/cli/mizu"
"regexp"
) )
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
@ -16,16 +14,11 @@ func init() {
rootCmd.Use = "cmd pod-query" rootCmd.Use = "cmd pod-query"
rootCmd.Short = "Tail HTTP traffic from multiple pods" rootCmd.Short = "Tail HTTP traffic from multiple pods"
rootCmd.RunE = func(cmd *cobra.Command, args []string) error { rootCmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) != 1 { if len(args) != 0 {
return rootCmd.Help() return rootCmd.Help()
} }
regex, err := regexp.Compile(args[0]) mizu.Run()
if err != nil {
fmt.Printf("%s is not a valid regex %s", args[0], err)
return nil
}
mizu.Run(regex)
return nil return nil
} }
@ -36,8 +29,9 @@ func init() {
rootCmd.Flags().StringVarP(&config.Configuration.Namespace, "namespace", "n", "", "Namespace selector") 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().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.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().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. // Execute adds all child commands to the root command and sets flags appropriately.

View File

@ -10,6 +10,7 @@ type Options struct {
KubeConfigPath string KubeConfigPath string
MizuImage string MizuImage string
MizuPodPort uint16 MizuPodPort uint16
TappedPodName string
} }
var Configuration = &Options{} var Configuration = &Options{}

View File

@ -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) 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 privileged := true
pod := &core.Pod{ pod := &core.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
@ -96,6 +101,7 @@ func (provider *Provider) CreatePod(ctx context.Context, podName string, podImag
}, },
}, },
TerminationGracePeriodSeconds: new(int64), 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{}) return provider.clientSet.CoreV1().Pods(provider.Namespace).Create(ctx, pod, metav1.CreateOptions{})

View File

@ -12,7 +12,7 @@ import (
"time" "time"
) )
func Run(podRegex *regexp.Regexp) { func Run() {
kubernetesProvider := kubernetes.NewProvider(config.Configuration.KubeConfigPath, config.Configuration.Namespace) kubernetesProvider := kubernetes.NewProvider(config.Configuration.KubeConfigPath, config.Configuration.Namespace)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel will be called when this function exits defer cancel() // cancel will be called when this function exits
@ -20,7 +20,6 @@ func Run(podRegex *regexp.Regexp) {
podName := "mizu-collector" 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 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 waitForFinish(ctx, cancel) //block until exit signal or error
// TODO handle incoming traffic from tapper using a channel // 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) { 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 { if err != nil {
fmt.Printf("error creating pod %s", err) fmt.Printf("error creating pod %s", err)
cancel() cancel()