Skip to content

Commit 3c4fdb2

Browse files
authored
Merge pull request #29 from eddycharly/master
Add support for custom default values and doc comment continuation
2 parents 0b03dcb + 30bbb44 commit 3c4fdb2

File tree

6 files changed

+486
-72
lines changed

6 files changed

+486
-72
lines changed

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ controller:
106106
# controller.publishService.enabled -- Whether to expose the ingress controller to the public world
107107
enabled: false
108108

109-
# controller.replicas -- Number of nginx-ingress pods to load balance between
109+
# controller.replicas -- Number of nginx-ingress pods to load balance between.
110+
# Do not set this below 2.
110111
replicas: 2
111112
```
112113
114+
Note that comments can continue on the next line. In that case leave out the double dash, and the lines will simply be
115+
appended with a space in-between.
116+
113117
The following rules are used to determine which values will be added to the values table in the README:
114118
115119
* By default, only _leaf nodes_, that is, fields of type `int`, `string`, `float`, `bool`, empty lists, and empty maps
@@ -169,6 +173,22 @@ controller:
169173
```
170174
This could be useful when wanting to enforce user-defined values for the chart, where there are no sensible defaults.
171175

176+
### Default values/column
177+
In cases where you do not want to include the default value from `values.yaml`, or where the real default is calculated
178+
inside the chart, you can change the contents of the column like so:
179+
180+
```yaml
181+
service:
182+
# service.annotations -- Add annotations to the service
183+
# @default -- the chart will add some internal annotations automatically
184+
annotations: []
185+
```
186+
187+
The order is important. The name must be spelled just like the column heading. The first comment must be the
188+
one specifying the key. The "@default" comment must follow.
189+
190+
See [here](./example-charts/custom-template/values.yaml) for an example.
191+
172192
### Spaces and Dots in keys
173193
If a key name contains any "." or " " characters, that section of the path must be quoted in description comments e.g.
174194

example-charts/custom-template/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ culpa qui officia deserunt mollit anim id est laborum.
2929
| controller.image.tag | string | `"18.0831"` | |
3030
| controller.ingressClass | string | `"nginx"` | Name of the ingress class to route through this controller |
3131
| controller.name | string | `"controller"` | |
32-
| controller.persistentVolumeClaims | list | `[]` | List of persistent volume claims to create |
32+
| controller.persistentVolumeClaims | list | Use this to override the default value specified here in the file | List of persistent volume claims to create. For very long comments, break them into multiple lines. |
3333
| controller.podLabels | object | `{}` | The labels to be applied to instances of the controller pod |
3434
| controller.publishService.enabled | bool | `false` | Whether to expose the ingress controller to the public world |
3535
| controller.replicas | int | `nil` | Number of nginx-ingress pods to load balance between |

example-charts/custom-template/values.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ controller:
44
repository: nginx-ingress-controller
55
tag: "18.0831"
66

7-
# controller.persistentVolumeClaims -- List of persistent volume claims to create
7+
# controller.persistentVolumeClaims -- List of persistent volume claims to create.
8+
# For very long comments, break them into multiple lines.
9+
# @default -- Use this to override the default value specified here in the file
810
persistentVolumeClaims: []
911

1012
extraVolumes:

pkg/document/values.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"regexp"
77
"sort"
88
"strings"
9+
10+
"github.com/norwoodj/helm-docs/pkg/helm"
911
)
1012

1113
const (
@@ -61,52 +63,59 @@ func getTypeName(value interface{}) string {
6163
return ""
6264
}
6365

64-
func parseNilValueType(key string, description string) valueRow {
66+
func parseNilValueType(key string, description helm.ChartValueDescription) valueRow {
6567
// Grab whatever's in between the parentheses of the description and treat it as the type
66-
t := nilValueTypeRegex.FindString(description)
68+
t := nilValueTypeRegex.FindString(description.Description)
6769

6870
if len(t) > 0 {
6971
t = t[1 : len(t)-1]
70-
description = description[len(t)+3:]
72+
description.Description = description.Description[len(t)+3:]
7173
} else {
7274
t = stringType
7375
}
7476

77+
if description.Default == "" {
78+
description.Default = "`nil`"
79+
}
80+
7581
return valueRow{
7682
Key: key,
7783
Type: t,
78-
Default: "`nil`",
79-
Description: description,
84+
Default: description.Default,
85+
Description: description.Description,
8086
}
8187
}
8288

8389
func createValueRow(
8490
key string,
8591
value interface{},
86-
description string,
92+
description helm.ChartValueDescription,
8793
) (valueRow, error) {
8894
if value == nil {
8995
return parseNilValueType(key, description), nil
9096
}
9197

92-
jsonEncodedValue, err := json.Marshal(value)
93-
if err != nil {
94-
return valueRow{}, fmt.Errorf("failed to marshal default value for %s to json: %s", key, err)
98+
if description.Default == "" {
99+
jsonEncodedValue, err := json.Marshal(value)
100+
if err != nil {
101+
return valueRow{}, fmt.Errorf("failed to marshal default value for %s to json: %s", key, err)
102+
}
103+
104+
description.Default = fmt.Sprintf("`%s`", jsonEncodedValue)
95105
}
96106

97-
defaultValue := fmt.Sprintf("`%s`", jsonEncodedValue)
98107
return valueRow{
99108
Key: key,
100109
Type: getTypeName(value),
101-
Default: defaultValue,
102-
Description: description,
110+
Default: description.Default,
111+
Description: description.Description,
103112
}, nil
104113
}
105114

106115
func createRowsFromField(
107116
nextPrefix string,
108117
value interface{},
109-
keysToDescriptions map[string]string,
118+
keysToDescriptions map[string]helm.ChartValueDescription,
110119
documentLeafNodes bool,
111120
) ([]valueRow, error) {
112121
switch value.(type) {
@@ -130,7 +139,7 @@ func createRowsFromField(
130139
func createValueRowsFromList(
131140
prefix string,
132141
values []interface{},
133-
keysToDescriptions map[string]string,
142+
keysToDescriptions map[string]helm.ChartValueDescription,
134143
documentLeafNodes bool,
135144
) ([]valueRow, error) {
136145
description, hasDescription := keysToDescriptions[prefix]
@@ -184,7 +193,7 @@ func createValueRowsFromList(
184193
func createValueRowsFromObject(
185194
prefix string,
186195
values map[interface{}]interface{},
187-
keysToDescriptions map[string]string,
196+
keysToDescriptions map[string]helm.ChartValueDescription,
188197
documentLeafNodes bool,
189198
) ([]valueRow, error) {
190199
description, hasDescription := keysToDescriptions[prefix]

0 commit comments

Comments
 (0)