-
Notifications
You must be signed in to change notification settings - Fork 36
/
operator_list_operands.go
125 lines (110 loc) · 3.43 KB
/
operator_list_operands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/duration"
"sigs.k8s.io/yaml"
"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"
"github.com/operator-framework/kubectl-operator/pkg/action"
)
func newOperatorListOperandsCmd(cfg *action.Configuration) *cobra.Command {
l := action.NewOperatorListOperands(cfg)
output := ""
validOutputs := []string{"json", "yaml"}
cmd := &cobra.Command{
Use: "list-operands <operator>",
Short: "List operands of an installed operator",
Long: `List operands of an installed operator.
This command lists all operands found throughout the cluster for the operator
specified on the command line. Since the scope of an operator is restricted by
its operator group, the output will include namespace-scoped operands from the
operator group's target namespaces and all cluster-scoped operands.
To search for operands for an operator in a different namespace, use the
--namespace flag. By default, the namespace from the current context is used.
Operand kinds are determined from the owned CustomResourceDefinitions listed in
the operator's ClusterServiceVersion.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
writeOutput := func(io.Writer, *unstructured.UnstructuredList) error { panic("writeOutput was not set") }
switch output {
case "json":
writeOutput = writeJSON
case "yaml":
writeOutput = writeYAML
case "":
writeOutput = writeTable
default:
log.Fatalf("invalid value for flag output %q, expected one of %s", output, strings.Join(validOutputs, "|"))
}
operands, err := l.Run(cmd.Context(), args[0])
if err != nil {
log.Fatalf("list operands: %v", err)
}
if len(operands.Items) == 0 {
log.Print("No resources found")
return
}
if err := writeOutput(os.Stdout, operands); err != nil {
log.Fatal(err)
}
},
}
cmd.Flags().StringVarP(&output, "output", "o", output, fmt.Sprintf("Output format. One of: %s", strings.Join(validOutputs, "|")))
return cmd
}
func writeTable(w io.Writer, operands *unstructured.UnstructuredList) error {
var buf bytes.Buffer
tw := tabwriter.NewWriter(&buf, 3, 4, 2, ' ', 0)
if _, err := fmt.Fprintf(tw, "APIVERSION\tKIND\tNAMESPACE\tNAME\tAGE\n"); err != nil {
return err
}
for _, o := range operands.Items {
age := time.Since(o.GetCreationTimestamp().Time)
if _, err := fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", o.GetAPIVersion(), o.GetKind(), o.GetNamespace(), o.GetName(), duration.HumanDuration(age)); err != nil {
return err
}
}
if err := tw.Flush(); err != nil {
return err
}
if _, err := w.Write(buf.Bytes()); err != nil {
return err
}
return nil
}
func writeJSON(w io.Writer, operands *unstructured.UnstructuredList) error {
out, err := json.Marshal(operands)
if err != nil {
return err
}
var buf bytes.Buffer
if err := json.Indent(&buf, out, "", " "); err != nil {
return err
}
if _, err := w.Write(buf.Bytes()); err != nil {
return err
}
return nil
}
func writeYAML(w io.Writer, operands *unstructured.UnstructuredList) error {
var jsonWriter bytes.Buffer
if err := writeJSON(&jsonWriter, operands); err != nil {
return err
}
out, err := yaml.JSONToYAML(jsonWriter.Bytes())
if err != nil {
return err
}
if _, err := w.Write(out); err != nil {
return err
}
return nil
}