|
3 | 3 | A NPM package containing utility functions for parsing output from the Kubernetes `kubectl` command
|
4 | 4 | line tool.
|
5 | 5 |
|
| 6 | +# Reference |
| 7 | + |
| 8 | +## Table format functions |
| 9 | + |
| 10 | +These functions are for use with `kubectl` commands that return data in tabular format - |
| 11 | +that is, of the form: |
| 12 | + |
| 13 | +``` |
| 14 | +TITLE1 TITLE2 TITLE3 |
| 15 | +value11 value12 value13 |
| 16 | +value21 value22 value23 |
| 17 | +``` |
| 18 | + |
| 19 | +where no column header or value contains a space, and columns are separated by one or more spaces. |
| 20 | +Examples include the default behaviour of `kubectl get`, for example `kubectl get pods` or |
| 21 | +`kubectl get pods -o wide` to get a list of pods. |
| 22 | + |
| 23 | +In all cases: |
| 24 | + |
| 25 | +* The function takes a `KubectlOutput`: that is, either a `ShellResult` (an object with numeric `code`, |
| 26 | +`stdout` string and `stderr` string properties), or `undefined`. `code` and `stderr` are used |
| 27 | +for error handling; on the happy path, the function will operate on the `stdout` string. |
| 28 | +* The function returns an `Errorable`: that is, an object with a boolean `succeeded` property. If |
| 29 | +`succeeded` is true, the object also has a `result` property containing the result of the function; |
| 30 | +if `succeeded` is false, the object has `reason` and `error` properties describing the failure. |
| 31 | +* If the input is `undefined`, or has a non-zero `code`, the function returns a _failed_ Errorable. |
| 32 | +* The function does not check the format of the provided `stdout`. You should use it **only** on the output |
| 33 | +of `kubectl` commands that return tabular data without spaces or missing values. |
| 34 | + |
| 35 | +### parseTabular |
| 36 | + |
| 37 | +**JavaScript:** `parseTabular(output)` |
| 38 | + |
| 39 | +**TypeScript:** `parseTabular(output: KubectlOutput): Errorable<Dictionary<string>[]>` |
| 40 | + |
| 41 | +Parses tabular `kubectl` output into an array of key-value objects, one per line. |
| 42 | + |
| 43 | +The result is an array of the form: |
| 44 | + |
| 45 | +```javascript |
| 46 | +[ |
| 47 | + { |
| 48 | + title1: "value11", |
| 49 | + title2: "value12", |
| 50 | + title3: "value13" |
| 51 | + }, |
| 52 | + { |
| 53 | + title1: "value21", |
| 54 | + title2: "value22", |
| 55 | + title3: "value23" |
| 56 | + } |
| 57 | +] |
| 58 | +``` |
| 59 | + |
| 60 | +Each non-header row is parsed as an object within the array. Each object's keys are the lowercased |
| 61 | +column headers, and the value of each key is the string under that header in the object's row. |
| 62 | + |
| 63 | +If `output.stdout` is empty then the function returns success with an empty array. |
| 64 | + |
| 65 | +**TypeScript:** `Dictionary<T>` is an alias for `{ [key: string]: T }`. |
| 66 | + |
| 67 | +### asTableLines |
| 68 | + |
| 69 | +**JavaScript:** `asTableLines(output)` |
| 70 | + |
| 71 | +**TypeScript:** `asTableLines(output: KubectlOutput): Errorable<TableLines>` |
| 72 | + |
| 73 | +Splits tabular `kubectl` output into a header line and an array of body lines. The result is |
| 74 | +an object of the form: |
| 75 | + |
| 76 | +```javascript |
| 77 | +{ |
| 78 | + header: "TITLE1 TITLE2 TITLE3", |
| 79 | + body: [ |
| 80 | + "value11 value12 value13". |
| 81 | + "value21 value22 value23" |
| 82 | + ] |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +If `output.stdout` is empty then the function returns success with an empty string `header` and |
| 87 | +an empty `body` array. |
| 88 | + |
| 89 | +## JSON format functions |
| 90 | + |
| 91 | +These functions are for use with `kubectl` commands that return data in JSON format. |
| 92 | +This is typically triggered by the `-o json` option and may be used for lists or for single |
| 93 | +resources, e.g. `kubectl get pods -o json` or `kubectl get deploy/nginx -o json`. |
| 94 | + |
| 95 | +In all cases: |
| 96 | + |
| 97 | +* The function takes a `KubectlOutput`: that is, either a `ShellResult` (an object with numeric `code`, |
| 98 | +`stdout` string and `stderr` string properties), or `undefined`. `code` and `stderr` are used |
| 99 | +for error handling; on the happy path, the function will operate on the `stdout` string. |
| 100 | +* The function returns an `Errorable`: that is, an object with a boolean `succeeded` property. If |
| 101 | +`succeeded` is true, the object also has a `result` property containing the result of the function; |
| 102 | +if `succeeded` is false, the object has `reason` and `error` properties describing the failure. |
| 103 | +* If the input is `undefined`, or has a non-zero `code`, or if `stdout` is empty, the function |
| 104 | +returns a _failed_ Errorable. |
| 105 | +* The function does not check the format of the provided `stdout`. You should use it **only** on the output |
| 106 | +of `kubectl` commands that return JSON data. |
| 107 | + |
| 108 | +### parseJSON |
| 109 | + |
| 110 | +**JavaScript:** `parseJSON(output)` |
| 111 | + |
| 112 | +**TypeScript:** `parseJSON<T>(output: KubectlOutput): Errorable<T>` |
| 113 | + |
| 114 | +Checks for `kubectl` failure and then returns the deserialised object corresponding to the |
| 115 | +`stdout` JSON. |
| 116 | + |
| 117 | +### parseJSONCollection |
| 118 | + |
| 119 | +**TypeScript:** `parseJSONCollection<T>(output: KubectlOutput): Errorable<KubernetesList<T>>` |
| 120 | + |
| 121 | +Checks for `kubectl` failure and then returns the deserialised object corresponding to the |
| 122 | +`stdout` JSON, where this is a Kubernetes item list object. |
| 123 | + |
| 124 | +This is equivalent to writing `parseJSON<KubernetesList<T>>`; it is provided for TypeScript |
| 125 | +users to reduce generics clutter in their code. (In untyped JavaScript, there's no difference |
| 126 | +between this and the `parseJSON` function.) |
| 127 | + |
6 | 128 | # Contributing
|
7 | 129 |
|
8 | 130 | This project welcomes contributions and suggestions. Most contributions require you to agree to a
|
|
0 commit comments