Skip to content

Commit e9aaa42

Browse files
authored
Merge pull request #194 from Haepaxlog/sections
Add Support for subsectioned Values
2 parents 3b87f0a + 75774f5 commit e9aaa42

File tree

10 files changed

+514
-10
lines changed

10 files changed

+514
-10
lines changed

example-charts/sections/Chart.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v2
2+
name: sections
3+
version: "1.0.0"
4+
type: application
5+
appVersion: "13.0.0"
6+
description: A chart for showing how to use sections
7+
home: "https://github.com/norwoodj/helm-docs/tree/master/example-charts/sections"
8+
maintainers:
9+
- email: rohdeconstantin@gmail.com
10+
name: Constantin Rohde
11+
sources: ["https://github.com/norwoodj/helm-docs/tree/master/example-charts/sections"]
12+
engine: gotpl

example-charts/sections/README.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Sections
2+
3+
This creates values, but sectioned into own section tables if a section comment is provided.
4+
5+
## Values
6+
7+
### Some Section
8+
9+
| Key | Type | Default | Description |
10+
|-----|------|---------|-------------|
11+
| controller.extraVolumes[0].configMap.name | string | `"nginx-ingress-config"` | Uses the name of the configmap created by this chart |
12+
| controller.persistentVolumeClaims | list | the chart will construct this list internally unless specified | List of persistent volume claims to create. |
13+
| controller.podLabels | object | `{}` | The labels to be applied to instances of the controller pod |
14+
15+
### Special Attention
16+
17+
| Key | Type | Default | Description |
18+
|-----|------|---------|-------------|
19+
| controller.ingressClass | string | `"nginx"` | You can also specify value comments like this |
20+
| controller.publishService | object | `{"enabled":false}` | This is a publishService |
21+
| controller.replicas | int | `nil` | Number of nginx-ingress pods to load balance between |
22+
23+
### Other Values
24+
25+
| Key | Type | Default | Description |
26+
|-----|------|---------|-------------|
27+
| controller.extraVolumes[0].name | string | `"config-volume"` | |
28+
| controller.image.repository | string | `"nginx-ingress-controller"` | |
29+
| controller.image.tag | string | `"18.0831"` | |
30+
| controller.name | string | `"controller"` | |
31+
| controller.service.annotations."external-dns.alpha.kubernetes.io/hostname" | string | `"stupidchess.jmn23.com"` | Hostname to be assigned to the ELB for the service |
32+
| controller.service.type | string | `"LoadBalancer"` | |
33+
34+
## Values
35+
36+
<h3>Some Section</h3>
37+
<table>
38+
<thead>
39+
<th>Key</th>
40+
<th>Type</th>
41+
<th>Default</th>
42+
<th>Description</th>
43+
</thead>
44+
<tbody>
45+
<tr>
46+
<td>controller.extraVolumes[0].configMap.name</td>
47+
<td>string</td>
48+
<td><pre lang="json">
49+
"nginx-ingress-config"
50+
</pre>
51+
</td>
52+
<td>Uses the name of the configmap created by this chart</td>
53+
</tr>
54+
<tr>
55+
<td>controller.persistentVolumeClaims</td>
56+
<td>list</td>
57+
<td><pre lang="">
58+
the chart will construct this list internally unless specified
59+
</pre>
60+
</td>
61+
<td>List of persistent volume claims to create.</td>
62+
</tr>
63+
<tr>
64+
<td>controller.podLabels</td>
65+
<td>object</td>
66+
<td><pre lang="json">
67+
{}
68+
</pre>
69+
</td>
70+
<td>The labels to be applied to instances of the controller pod</td>
71+
</tr>
72+
</tbody>
73+
</table>
74+
<h3>Special Attention</h3>
75+
<table>
76+
<thead>
77+
<th>Key</th>
78+
<th>Type</th>
79+
<th>Default</th>
80+
<th>Description</th>
81+
</thead>
82+
<tbody>
83+
<tr>
84+
<td>controller.ingressClass</td>
85+
<td>string</td>
86+
<td><pre lang="json">
87+
"nginx"
88+
</pre>
89+
</td>
90+
<td>You can also specify value comments like this</td>
91+
</tr>
92+
<tr>
93+
<td>controller.publishService</td>
94+
<td>object</td>
95+
<td><pre lang="json">
96+
{
97+
"enabled": false
98+
}
99+
</pre>
100+
</td>
101+
<td>This is a publishService</td>
102+
</tr>
103+
<tr>
104+
<td>controller.replicas</td>
105+
<td>int</td>
106+
<td><pre lang="json">
107+
null
108+
</pre>
109+
</td>
110+
<td>Number of nginx-ingress pods to load balance between</td>
111+
</tr>
112+
</tbody>
113+
</table>
114+
115+
<h3>Other Values</h3>
116+
<table>
117+
<thead>
118+
<th>Key</th>
119+
<th>Type</th>
120+
<th>Default</th>
121+
<th>Description</th>
122+
</thead>
123+
<tbody>
124+
<tr>
125+
<td>controller.extraVolumes[0].name</td>
126+
<td>string</td>
127+
<td><pre lang="json">
128+
"config-volume"
129+
</pre>
130+
</td>
131+
<td></td>
132+
</tr>
133+
<tr>
134+
<td>controller.image.repository</td>
135+
<td>string</td>
136+
<td><pre lang="json">
137+
"nginx-ingress-controller"
138+
</pre>
139+
</td>
140+
<td></td>
141+
</tr>
142+
<tr>
143+
<td>controller.image.tag</td>
144+
<td>string</td>
145+
<td><pre lang="json">
146+
"18.0831"
147+
</pre>
148+
</td>
149+
<td></td>
150+
</tr>
151+
<tr>
152+
<td>controller.name</td>
153+
<td>string</td>
154+
<td><pre lang="json">
155+
"controller"
156+
</pre>
157+
</td>
158+
<td></td>
159+
</tr>
160+
<tr>
161+
<td>controller.service.annotations."external-dns.alpha.kubernetes.io/hostname"</td>
162+
<td>string</td>
163+
<td><pre lang="json">
164+
"stupidchess.jmn23.com"
165+
</pre>
166+
</td>
167+
<td>Hostname to be assigned to the ELB for the service</td>
168+
</tr>
169+
<tr>
170+
<td>controller.service.type</td>
171+
<td>string</td>
172+
<td><pre lang="json">
173+
"LoadBalancer"
174+
</pre>
175+
</td>
176+
<td></td>
177+
</tr>
178+
</tbody>
179+
</table>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Sections
2+
3+
This creates values, but sectioned into own section tables if a seciton comment is provided.
4+
5+
{{ template "chart.valuesSection" . }}
6+
7+
{{ template "chart.valuesSectionHtml" . }}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
controller:
2+
name: controller
3+
image:
4+
repository: nginx-ingress-controller
5+
tag: "18.0831"
6+
7+
# controller.persistentVolumeClaims -- List of persistent volume claims to create.
8+
# @default -- the chart will construct this list internally unless specified
9+
# @section -- Some Section
10+
persistentVolumeClaims: []
11+
12+
extraVolumes:
13+
- name: config-volume
14+
configMap:
15+
# controller.extraVolumes[0].configMap.name -- Uses the name of the configmap created by this chart
16+
# @section -- Some Section
17+
name: nginx-ingress-config
18+
19+
# -- You can also specify value comments like this
20+
# @section -- Special Attention
21+
ingressClass: nginx
22+
23+
24+
# controller.podLabels -- The labels to be applied to instances of the controller pod
25+
# @section -- Some Section
26+
podLabels: {}
27+
28+
# controller.publishService -- This is a publishService
29+
# @section -- Special Attention
30+
publishService:
31+
enabled: false
32+
33+
# -- (int) Number of nginx-ingress pods to load balance between
34+
# @raw
35+
# @section -- Special Attention
36+
replicas:
37+
38+
service:
39+
annotations:
40+
# controller.service.annotations."external-dns.alpha.kubernetes.io/hostname" -- Hostname to be assigned to the ELB for the service
41+
external-dns.alpha.kubernetes.io/hostname: stupidchess.jmn23.com
42+
43+
type: LoadBalancer

pkg/document/model.go

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type valueRow struct {
2020
Default string
2121
AutoDescription string
2222
Description string
23+
Section string
2324
Column int
2425
LineNumber int
2526
Dependency string
@@ -30,17 +31,21 @@ type chartTemplateData struct {
3031
helm.ChartDocumentationInfo
3132
HelmDocsVersion string
3233
Values []valueRow
34+
Sections sections
3335
Files files
3436
}
3537

36-
func sortValueRows(valueRows []valueRow) {
37-
sortOrder := viper.GetString("sort-values-order")
38+
type sections struct {
39+
DefaultSection section
40+
Sections []section
41+
}
3842

39-
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
40-
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
41-
sortOrder = AlphaNumSortOrder
42-
}
43+
type section struct {
44+
SectionName string
45+
SectionItems []valueRow
46+
}
4347

48+
func sortValueRowsByOrder(valueRows []valueRow, sortOrder string) {
4449
sort.Slice(valueRows, func(i, j int) bool {
4550
// Globals sort above non-globals.
4651
if valueRows[i].IsGlobal != valueRows[j].IsGlobal {
@@ -75,6 +80,32 @@ func sortValueRows(valueRows []valueRow) {
7580
})
7681
}
7782

83+
func sortValueRows(valueRows []valueRow) {
84+
sortOrder := viper.GetString("sort-values-order")
85+
86+
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
87+
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
88+
sortOrder = AlphaNumSortOrder
89+
}
90+
91+
sortValueRowsByOrder(valueRows, sortOrder)
92+
}
93+
94+
func sortSectionedValueRows(sectionedValueRows sections) {
95+
sortOrder := viper.GetString("sort-values-order")
96+
97+
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
98+
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
99+
sortOrder = AlphaNumSortOrder
100+
}
101+
102+
sortValueRowsByOrder(sectionedValueRows.DefaultSection.SectionItems, sortOrder)
103+
104+
for _, section := range sectionedValueRows.Sections {
105+
sortValueRowsByOrder(section.SectionItems, sortOrder)
106+
}
107+
}
108+
78109
func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.ChartValueDescription) ([]valueRow, error) {
79110
// Handle empty values file case.
80111
if document.Kind == 0 {
@@ -92,6 +123,39 @@ func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.Char
92123
return createValueRowsFromField("", nil, document.Content[0], descriptions, true)
93124
}
94125

126+
func getSectionedValueRows(valueRows []valueRow) sections {
127+
var valueRowsSectionSorted sections
128+
valueRowsSectionSorted.DefaultSection = section{
129+
SectionName: "Other Values",
130+
SectionItems: []valueRow{},
131+
}
132+
133+
for _, row := range valueRows {
134+
if row.Section == "" {
135+
valueRowsSectionSorted.DefaultSection.SectionItems = append(valueRowsSectionSorted.DefaultSection.SectionItems, row)
136+
continue
137+
}
138+
139+
containsSection := false
140+
for i, section := range valueRowsSectionSorted.Sections {
141+
if section.SectionName == row.Section {
142+
containsSection = true
143+
valueRowsSectionSorted.Sections[i].SectionItems = append(valueRowsSectionSorted.Sections[i].SectionItems, row)
144+
break
145+
}
146+
}
147+
148+
if !containsSection {
149+
valueRowsSectionSorted.Sections = append(valueRowsSectionSorted.Sections, section{
150+
SectionName: row.Section,
151+
SectionItems: []valueRow{row},
152+
})
153+
}
154+
}
155+
156+
return valueRowsSectionSorted
157+
}
158+
95159
func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion string, dependencyValues []DependencyValues) (chartTemplateData, error) {
96160
valuesTableRows, err := getUnsortedValueRows(info.ChartValues, info.ChartValuesDescriptions)
97161
if err != nil {
@@ -135,6 +199,8 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
135199
}
136200

137201
sortValueRows(valuesTableRows)
202+
valueRowsSectionSorted := getSectionedValueRows(valuesTableRows)
203+
sortSectionedValueRows(valueRowsSectionSorted)
138204

139205
files, err := getFiles(info.ChartDirectory)
140206
if err != nil {
@@ -145,6 +211,7 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
145211
ChartDocumentationInfo: info,
146212
HelmDocsVersion: helmDocsVersion,
147213
Values: valuesTableRows,
214+
Sections: valueRowsSectionSorted,
148215
Files: files,
149216
}, nil
150217
}

0 commit comments

Comments
 (0)