|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "text/tabwriter" |
| 8 | + |
| 9 | + "github.com/fntlnz/kubectl-trace/pkg/tracejob" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/viper" |
| 12 | + "go.uber.org/zap" |
| 13 | + "k8s.io/client-go/kubernetes" |
| 14 | + "k8s.io/client-go/tools/clientcmd" |
| 15 | +) |
| 16 | + |
| 17 | +var getCmd = &cobra.Command{ |
| 18 | + Use: "get [TRACEID] [-n NAMESPACE]", |
| 19 | + Short: "Get all the running traces in a kubernetes cluster", |
| 20 | + Long: `Get all the running traces in a kubernetes cluster |
| 21 | +
|
| 22 | +Examples: |
| 23 | + # Get all traces in a namespace |
| 24 | + kubectl trace get -n mynamespace |
| 25 | +
|
| 26 | + # Get only a specific trace |
| 27 | + kubectl trace get 656ee75a-ee3c-11e8-9e7a-8c164500a77e |
| 28 | +
|
| 29 | + # Get only a specific trace in a specific namespace |
| 30 | + kubectl trace get 656ee75a-ee3c-11e8-9e7a-8c164500a77e -n mynamespace |
| 31 | +
|
| 32 | +Limitations: |
| 33 | + - Currently work only with a single namespace at time |
| 34 | + - It does not contain yet status and age for the trace |
| 35 | +`, |
| 36 | + Run: get, |
| 37 | +} |
| 38 | + |
| 39 | +func get(cmd *cobra.Command, args []string) { |
| 40 | + log, _ := zap.NewProduction() |
| 41 | + defer log.Sync() |
| 42 | + |
| 43 | + kubeconfig := viper.GetString("kubeconfig") |
| 44 | + config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + log.Fatal("cannot create kubernetes client from provider KUBECONFIG", zap.Error(err)) |
| 48 | + } |
| 49 | + |
| 50 | + var uuid *string |
| 51 | + if len(args) > 0 { |
| 52 | + uuid = &args[0] |
| 53 | + } |
| 54 | + |
| 55 | + namespace := viper.GetString("namespace") |
| 56 | + |
| 57 | + clientset, err := kubernetes.NewForConfig(config) |
| 58 | + if err != nil { |
| 59 | + log.Fatal("cannot create kubernetes config from provider KUBECONFIG", zap.Error(err)) |
| 60 | + } |
| 61 | + |
| 62 | + jobsClient := clientset.BatchV1().Jobs(namespace) |
| 63 | + |
| 64 | + tc := &tracejob.TraceJobClient{ |
| 65 | + JobClient: jobsClient, |
| 66 | + ConfigClient: clientset.CoreV1().ConfigMaps(namespace), |
| 67 | + } |
| 68 | + |
| 69 | + tf := tracejob.TraceJobFilter{ |
| 70 | + ID: uuid, |
| 71 | + } |
| 72 | + |
| 73 | + jobs, err := tc.GetJob(tf) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + log.Fatal("error getting jobs with provided filter", zap.Error(err), zap.Any("filter", tf)) |
| 77 | + } |
| 78 | + |
| 79 | + jobsTablePrint(jobs) |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +// TODO(fntlnz): This needs better printing, perhaps we could use the humanreadable table from k8s itself |
| 84 | +// to be consistent with the main project. |
| 85 | +func jobsTablePrint(jobs []tracejob.TraceJob) { |
| 86 | + format := "%s\t%s\t%s\t%s\t%s\t" |
| 87 | + if len(jobs) == 0 { |
| 88 | + fmt.Println("No resources found.") |
| 89 | + return |
| 90 | + } |
| 91 | + // initialize tabwriter |
| 92 | + w := new(tabwriter.Writer) |
| 93 | + // minwidth, tabwidth, padding, padchar, flags |
| 94 | + w.Init(os.Stdout, 8, 8, 0, '\t', 0) |
| 95 | + defer w.Flush() |
| 96 | + |
| 97 | + // TODO(fntlnz): Do the status and age fields, we don't have a way to get them now, so reporting |
| 98 | + // them as missing. |
| 99 | + fmt.Fprintf(w, format, "NAMESPACE", "NAME", "STATUS", "AGE", "HOSTNAME") |
| 100 | + for _, j := range jobs { |
| 101 | + fmt.Fprintf(w, "\n"+format, j.Namespace, j.Name, "<missing>", "<missing>", j.Hostname) |
| 102 | + } |
| 103 | +} |
0 commit comments