GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list_workloads_cmd.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "github.com/spf13/cobra"
  8. "github.com/fluxcd/flux/api/v6"
  9. "github.com/fluxcd/flux/policy"
  10. )
  11. type workloadListOpts struct {
  12. *rootOpts
  13. namespace string
  14. allNamespaces bool
  15. }
  16. func newWorkloadList(parent *rootOpts) *workloadListOpts {
  17. return &workloadListOpts{rootOpts: parent}
  18. }
  19. func (opts *workloadListOpts) Command() *cobra.Command {
  20. cmd := &cobra.Command{
  21. Use: "list-workloads",
  22. Aliases: []string{"list-controllers"}, // Transient backwards compatibility after replacing controller by workload
  23. Short: "List workloads currently running in the cluster.",
  24. Example: makeExample("fluxctl list-workloads"),
  25. RunE: opts.RunE,
  26. }
  27. cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", getKubeConfigContextNamespace("default"), "Confine query to namespace")
  28. cmd.Flags().BoolVarP(&opts.allNamespaces, "all-namespaces", "a", false, "Query across all namespaces")
  29. return cmd
  30. }
  31. func (opts *workloadListOpts) RunE(cmd *cobra.Command, args []string) error {
  32. if len(args) != 0 {
  33. return errorWantedNoArgs
  34. }
  35. if opts.allNamespaces {
  36. opts.namespace = ""
  37. }
  38. ctx := context.Background()
  39. workloads, err := opts.API.ListServices(ctx, opts.namespace)
  40. if err != nil {
  41. return err
  42. }
  43. sort.Sort(workloadStatusByName(workloads))
  44. w := newTabwriter()
  45. fmt.Fprintf(w, "WORKLOAD\tCONTAINER\tIMAGE\tRELEASE\tPOLICY\n")
  46. for _, workload := range workloads {
  47. if len(workload.Containers) > 0 {
  48. c := workload.Containers[0]
  49. fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", workload.ID, c.Name, c.Current.ID, workload.Status, policies(workload))
  50. for _, c := range workload.Containers[1:] {
  51. fmt.Fprintf(w, "\t%s\t%s\t\t\n", c.Name, c.Current.ID)
  52. }
  53. } else {
  54. fmt.Fprintf(w, "%s\t\t\t%s\t%s\n", workload.ID, workload.Status, policies(workload))
  55. }
  56. }
  57. w.Flush()
  58. return nil
  59. }
  60. type workloadStatusByName []v6.ControllerStatus
  61. func (s workloadStatusByName) Len() int {
  62. return len(s)
  63. }
  64. func (s workloadStatusByName) Less(a, b int) bool {
  65. return s[a].ID.String() < s[b].ID.String()
  66. }
  67. func (s workloadStatusByName) Swap(a, b int) {
  68. s[a], s[b] = s[b], s[a]
  69. }
  70. func policies(s v6.ControllerStatus) string {
  71. var ps []string
  72. if s.Automated {
  73. ps = append(ps, string(policy.Automated))
  74. }
  75. if s.Locked {
  76. ps = append(ps, string(policy.Locked))
  77. }
  78. if s.Ignore {
  79. ps = append(ps, string(policy.Ignore))
  80. }
  81. sort.Strings(ps)
  82. return strings.Join(ps, ",")
  83. }