|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | + |
| 13 | + "github.com/grafana/tanka/pkg/kubernetes/manifest" |
| 14 | +) |
| 15 | + |
| 16 | +// Resources the Kubernetes API knows |
| 17 | +type Resources []Resource |
| 18 | + |
| 19 | +// Namespaced returns whether a resource is namespace-specific or cluster-wide |
| 20 | +func (r Resources) Namespaced(m manifest.Manifest) bool { |
| 21 | + for _, res := range r { |
| 22 | + if m.Kind() == res.Kind { |
| 23 | + return res.Namespaced |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return false |
| 28 | +} |
| 29 | + |
| 30 | +// Resource is a Kubernetes API Resource |
| 31 | +type Resource struct { |
| 32 | + APIGroup string `json:"APIGROUP"` |
| 33 | + Kind string `json:"KIND"` |
| 34 | + Name string `json:"NAME"` |
| 35 | + Namespaced bool `json:"NAMESPACED,string"` |
| 36 | + Shortnames string `json:"SHORTNAMES"` |
| 37 | +} |
| 38 | + |
| 39 | +// Resources returns all API resources known to the server |
| 40 | +func (k Kubectl) Resources() (Resources, error) { |
| 41 | + cmd := k.ctl("api-resources", "--cached") |
| 42 | + var out bytes.Buffer |
| 43 | + cmd.Stdout = &out |
| 44 | + cmd.Stderr = os.Stderr |
| 45 | + |
| 46 | + if err := cmd.Run(); err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + var res Resources |
| 51 | + if err := UnmarshalTable(out.String(), &res); err != nil { |
| 52 | + return nil, errors.Wrap(err, "parsing table") |
| 53 | + } |
| 54 | + |
| 55 | + return res, nil |
| 56 | +} |
| 57 | + |
| 58 | +// UnmarshalTable unmarshals a raw CLI table into ptr. `json` package is used |
| 59 | +// for getting the dat into the ptr, `json:` struct tags can be used. |
| 60 | +func UnmarshalTable(raw string, ptr interface{}) error { |
| 61 | + raw = strings.TrimSpace(raw) |
| 62 | + |
| 63 | + lines := strings.Split(raw, "\n") |
| 64 | + if len(lines) == 0 { |
| 65 | + return ErrorNoHeader |
| 66 | + } |
| 67 | + |
| 68 | + headerStr := lines[0] |
| 69 | + // headers are ALL-CAPS |
| 70 | + if !regexp.MustCompile(`^[A-Z\s]+$`).MatchString(headerStr) { |
| 71 | + return ErrorNoHeader |
| 72 | + } |
| 73 | + |
| 74 | + lines = lines[1:] |
| 75 | + |
| 76 | + spc := regexp.MustCompile(`[A-Z]+\s*`) |
| 77 | + header := spc.FindAllString(headerStr, -1) |
| 78 | + |
| 79 | + tbl := make([]map[string]string, 0, len(lines)) |
| 80 | + for _, l := range lines { |
| 81 | + elems := splitRow(l, header) |
| 82 | + if len(elems) != len(header) { |
| 83 | + return ErrorElementsMismatch{Header: len(header), Row: len(elems)} |
| 84 | + } |
| 85 | + |
| 86 | + row := make(map[string]string) |
| 87 | + for i, e := range elems { |
| 88 | + key := strings.TrimSpace(header[i]) |
| 89 | + row[key] = strings.TrimSpace(e) |
| 90 | + } |
| 91 | + tbl = append(tbl, row) |
| 92 | + } |
| 93 | + |
| 94 | + j, err := json.Marshal(tbl) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + return json.Unmarshal(j, ptr) |
| 100 | +} |
| 101 | + |
| 102 | +// ErrorNoHeader occurs when the table lacks an ALL-CAPS header |
| 103 | +var ErrorNoHeader = fmt.Errorf("table has no header") |
| 104 | + |
| 105 | +// ErrorElementsMismatch occurs when a row has a different count of elements |
| 106 | +// than it's header |
| 107 | +type ErrorElementsMismatch struct { |
| 108 | + Header, Row int |
| 109 | +} |
| 110 | + |
| 111 | +func (e ErrorElementsMismatch) Error() string { |
| 112 | + return fmt.Sprintf("header and row have different element count: %v != %v", e.Header, e.Row) |
| 113 | +} |
| 114 | + |
| 115 | +func splitRow(s string, header []string) (elems []string) { |
| 116 | + pos := 0 |
| 117 | + for i, h := range header { |
| 118 | + if i == len(header)-1 { |
| 119 | + elems = append(elems, s[pos:]) |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + lim := len(h) |
| 124 | + elems = append(elems, s[pos:pos+lim]) |
| 125 | + pos += lim |
| 126 | + } |
| 127 | + return elems |
| 128 | +} |
0 commit comments