github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/get/get_devpod.go (about) 1 package get 2 3 import ( 4 "time" 5 6 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 7 "github.com/pkg/errors" 8 9 "github.com/jenkins-x/jx-logging/pkg/log" 10 11 "github.com/spf13/cobra" 12 13 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 14 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 15 "github.com/jenkins-x/jx/v2/pkg/kube" 16 ) 17 18 // GetDevPodOptions the command line options 19 type GetDevPodOptions struct { 20 Options 21 opts.CommonDevPodOptions 22 23 AllUsernames bool 24 } 25 26 var ( 27 getDevPodLong = templates.LongDesc(` 28 Display the available DevPods 29 30 For more documentation see: [https://jenkins-x.io/developing/devpods/](https://jenkins-x.io/developing/devpods/) 31 32 `) 33 34 getDevPodExample = templates.Examples(` 35 # List all the possible DevPods 36 jx get devPod 37 `) 38 ) 39 40 // NewCmdGetDevPod creates the command 41 func NewCmdGetDevPod(commonOpts *opts.CommonOptions) *cobra.Command { 42 options := &GetDevPodOptions{ 43 Options: Options{ 44 CommonOptions: commonOpts, 45 }, 46 } 47 48 cmd := &cobra.Command{ 49 Use: "devpod [flags]", 50 Short: "Lists the DevPods", 51 Long: getDevPodLong, 52 Example: getDevPodExample, 53 Aliases: []string{"buildpod", "buildpods", "devpods"}, 54 Run: func(cmd *cobra.Command, args []string) { 55 options.Cmd = cmd 56 options.Args = args 57 err := options.Run() 58 helper.CheckErr(err) 59 }, 60 } 61 62 cmd.Flags().BoolVarP(&options.AllUsernames, "all-usernames", "", false, "Gets devpods for all usernames") 63 64 options.AddCommonDevPodFlags(cmd) 65 66 return cmd 67 } 68 69 // Run implements this command 70 func (o *GetDevPodOptions) Run() error { 71 client, curNs, err := o.KubeClientAndNamespace() 72 if err != nil { 73 return errors.Wrap(err, "get kubernetes client") 74 } 75 ns, _, err := kube.GetDevNamespace(client, curNs) 76 if err != nil { 77 return errors.Wrap(err, "get dev namespace") 78 } 79 80 var userName string 81 if o.AllUsernames { 82 if o.Username != "" { 83 log.Logger().Warn("getting devpods for all usernames. Explicit username will be ignored") 84 } 85 } else { 86 userName, err = o.GetUsername(o.Username) 87 if err != nil { 88 return errors.Wrap(err, "getting the current user") 89 } 90 } 91 names, m, err := kube.GetDevPodNames(client, ns, userName) 92 if err != nil { 93 return errors.Wrap(err, "getting the DevPod names") 94 } 95 96 table := o.CreateTable() 97 table.AddRow("NAME", "POD TEMPLATE", "AGE", "STATUS") 98 99 for _, k := range names { 100 pod := m[k] 101 if pod != nil { 102 podTemplate := "" 103 status := kube.PodStatus(pod) 104 labels := pod.Labels 105 d := time.Now().Sub(pod.CreationTimestamp.Time).Round(time.Second) 106 age := d.String() 107 if labels != nil { 108 podTemplate = labels[kube.LabelPodTemplate] 109 } 110 table.AddRow(k, podTemplate, age, status) 111 } 112 } 113 114 table.Render() 115 return nil 116 }