|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/gianarb/kube-profefe/pkg/kubeutil" |
| 10 | + "github.com/gianarb/kube-profefe/pkg/pprofutil" |
| 11 | + "github.com/gianarb/kube-profefe/pkg/profefe" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/pflag" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 17 | + "k8s.io/client-go/kubernetes" |
| 18 | + "k8s.io/client-go/rest" |
| 19 | +) |
| 20 | + |
| 21 | +func NewKProfefeCmd(streams genericclioptions.IOStreams) *cobra.Command { |
| 22 | + flags := pflag.NewFlagSet("kubectl-profefe", pflag.ExitOnError) |
| 23 | + pflag.CommandLine = flags |
| 24 | + |
| 25 | + kubeConfigFlags := genericclioptions.NewConfigFlags(false) |
| 26 | + kubeResouceBuilderFlags := genericclioptions.NewResourceBuilderFlags() |
| 27 | + |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: "kprofefe", |
| 30 | + Short: "kprofefe collects profiles from inside a kubernetes cluster", |
| 31 | + PersistentPreRun: func(c *cobra.Command, args []string) { |
| 32 | + c.SetOutput(streams.ErrOut) |
| 33 | + }, |
| 34 | + Run: func(cmd *cobra.Command, args []string) { |
| 35 | + var config *rest.Config |
| 36 | + var err error |
| 37 | + |
| 38 | + config, err = rest.InClusterConfig() |
| 39 | + if err != nil { |
| 40 | + config, err = kubeConfigFlags.ToRESTConfig() |
| 41 | + } |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + if config == nil { |
| 46 | + panic("woww") |
| 47 | + } |
| 48 | + // creates the clientset |
| 49 | + clientset, err := kubernetes.NewForConfig(config) |
| 50 | + if err != nil { |
| 51 | + panic(err.Error()) |
| 52 | + } |
| 53 | + |
| 54 | + // Contains the pool of pods that we need to gather profiles from |
| 55 | + selectedPods := []corev1.Pod{} |
| 56 | + |
| 57 | + namespace := kubeutil.GetNamespaceFromKubernetesFlags(kubeConfigFlags, kubeResouceBuilderFlags) |
| 58 | + |
| 59 | + // If the arguments are more than zero we should check by pod name |
| 60 | + // (args == resourceName) |
| 61 | + if len(args) > 0 { |
| 62 | + for _, podName := range args { |
| 63 | + pod, err := kubeutil.GetPodByName(clientset, namespace, podName, metav1.GetOptions{}) |
| 64 | + if err != nil { |
| 65 | + println(err.Error()) |
| 66 | + continue |
| 67 | + } |
| 68 | + selectedPods = append(selectedPods, *pod) |
| 69 | + } |
| 70 | + } else { |
| 71 | + selectedPods, err = kubeutil.GetSelectedPods(clientset, namespace, metav1.ListOptions{ |
| 72 | + LabelSelector: *kubeResouceBuilderFlags.LabelSelector, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + println(err.Error()) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // If the selectedPods are zero there is nothing to do. |
| 81 | + if len(selectedPods) == 0 { |
| 82 | + println("there are not pod that matches your research") |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + |
| 86 | + println(fmt.Sprintf("selected %d pod/s", len(selectedPods))) |
| 87 | + |
| 88 | + pClient := profefe.NewClient(profefe.Config{ |
| 89 | + HostPort: ProfefeHostPort, |
| 90 | + }, http.Client{}) |
| 91 | + |
| 92 | + for _, target := range selectedPods { |
| 93 | + targetPort := pprofutil.GetProfefePortByPod(target) |
| 94 | + profiles, err := pprofutil.GatherAllByPod(context.Background(), fmt.Sprintf("http://%s", target.Status.PodIP), target, targetPort) |
| 95 | + if err != nil { |
| 96 | + panic(err) |
| 97 | + } |
| 98 | + for profileType, profile := range profiles { |
| 99 | + profefeType := profefe.NewProfileTypeFromString(profileType.String()) |
| 100 | + if profefeType == profefe.UnknownProfile { |
| 101 | + println("unknown profile type: :" + profileType.String()) |
| 102 | + continue |
| 103 | + } |
| 104 | + saved, err := pClient.SavePprof(context.Background(), profefe.SavePprofRequest{ |
| 105 | + Profile: profile, |
| 106 | + Service: target.Name, |
| 107 | + InstanceID: target.Status.HostIP, |
| 108 | + Type: profefeType, |
| 109 | + Labels: map[string]string{ |
| 110 | + "namespace": target.Namespace, |
| 111 | + "from": "kube-profefe", |
| 112 | + }, |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + println(fmt.Sprintf("%s type=%s profile_type=%s", err.Error(), profefeType, profile.PeriodType.Type)) |
| 116 | + } else { |
| 117 | + println(fmt.Sprintf("%s/api/0/profiles/%s type=%s", ProfefeHostPort, saved.Body.ID, profefeType)) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + flags.AddFlagSet(cmd.PersistentFlags()) |
| 125 | + flags.StringVar(&ProfefeHostPort, "profefe-hostport", "http://localhost:10100", `where profefe is located`) |
| 126 | + kubeConfigFlags.AddFlags(flags) |
| 127 | + kubeResouceBuilderFlags.WithLabelSelector("") |
| 128 | + kubeResouceBuilderFlags.AddFlags(flags) |
| 129 | + |
| 130 | + return cmd |
| 131 | +} |
0 commit comments