/
chart_info.go
236 lines (188 loc) · 6.16 KB
/
chart_info.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package helm
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"sort"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var valuesDescriptionRegex = regexp.MustCompile("^\\s*#\\s*(.*)\\s+--\\s*(.*)$")
var commentContinuationRegex = regexp.MustCompile("^\\s*# (.*)$")
var defaultValueRegex = regexp.MustCompile("^\\s*# @default -- (.*)$")
type ChartMetaMaintainer struct {
Email string
Name string
Url string
}
type ChartMeta struct {
ApiVersion string `yaml:"apiVersion"`
AppVersion string `yaml:"appVersion"`
KubeVersion string `yaml:"kubeVersion"`
Name string
Deprecated bool
Description string
Version string
Home string
Type string
Sources []string
Engine string
Maintainers []ChartMetaMaintainer
}
type ChartRequirementsItem struct {
Name string
Version string
Repository string
}
type ChartRequirements struct {
Dependencies []ChartRequirementsItem
}
type ChartValueDescription struct {
Description string
Default string
}
type ChartDocumentationInfo struct {
ChartMeta
ChartRequirements
ChartDirectory string
ChartValues *yaml.Node
ChartValuesDescriptions map[string]ChartValueDescription
}
func getYamlFileContents(filename string) ([]byte, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, err
}
yamlFileContents, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return []byte(yamlFileContents), nil
}
func isErrorInReadingNecessaryFile(filePath string, loadError error) bool {
if loadError != nil {
if os.IsNotExist(loadError) {
log.Warnf("Required chart file %s missing. Skipping documentation for chart", filePath)
return true
} else {
log.Warnf("Error occurred in reading chart file %s. Skipping documentation for chart", filePath)
return true
}
}
return false
}
func parseChartFile(chartDirectory string) (ChartMeta, error) {
chartYamlPath := path.Join(chartDirectory, "Chart.yaml")
chartMeta := ChartMeta{}
yamlFileContents, err := getYamlFileContents(chartYamlPath)
if isErrorInReadingNecessaryFile(chartYamlPath, err) {
return chartMeta, err
}
err = yaml.Unmarshal(yamlFileContents, &chartMeta)
return chartMeta, err
}
func requirementKey(requirement ChartRequirementsItem) string {
return fmt.Sprintf("%s/%s", requirement.Repository, requirement.Name)
}
func parseChartRequirementsFile(chartDirectory string, apiVersion string) (ChartRequirements, error) {
var requirementsPath string
if apiVersion == "v1" {
requirementsPath = path.Join(chartDirectory, "requirements.yaml")
if _, err := os.Stat(requirementsPath); os.IsNotExist(err) {
return ChartRequirements{Dependencies: []ChartRequirementsItem{}}, nil
}
} else {
requirementsPath = path.Join(chartDirectory, "Chart.yaml")
}
chartRequirements := ChartRequirements{}
yamlFileContents, err := getYamlFileContents(requirementsPath)
if isErrorInReadingNecessaryFile(requirementsPath, err) {
return chartRequirements, err
}
err = yaml.Unmarshal(yamlFileContents, &chartRequirements)
if err != nil {
return chartRequirements, err
}
sort.Slice(chartRequirements.Dependencies[:], func(i, j int) bool {
return requirementKey(chartRequirements.Dependencies[i]) < requirementKey(chartRequirements.Dependencies[j])
})
return chartRequirements, nil
}
func parseChartValuesFile(chartDirectory string) (yaml.Node, error) {
valuesPath := path.Join(chartDirectory, "values.yaml")
yamlFileContents, err := getYamlFileContents(valuesPath)
var values yaml.Node
if isErrorInReadingNecessaryFile(valuesPath, err) {
return values, err
}
err = yaml.Unmarshal(yamlFileContents, &values)
return values, err
}
func parseChartValuesFileComments(chartDirectory string) (map[string]ChartValueDescription, error) {
valuesPath := path.Join(chartDirectory, "values.yaml")
valuesFile, err := os.Open(valuesPath)
if isErrorInReadingNecessaryFile(valuesPath, err) {
return map[string]ChartValueDescription{}, err
}
defer valuesFile.Close()
keyToDescriptions := make(map[string]ChartValueDescription)
scanner := bufio.NewScanner(valuesFile)
foundValuesComment := false
commentLines := make([]string, 0)
for scanner.Scan() {
currentLine := scanner.Text()
// If we've not yet found a values comment with a key name, try and find one on each line
if !foundValuesComment {
match := valuesDescriptionRegex.FindStringSubmatch(currentLine)
if len(match) < 3 {
continue
}
if match[1] == "" {
continue
}
foundValuesComment = true
commentLines = append(commentLines, currentLine)
continue
}
// If we've already found a values comment, on the next line try and parse a custom default value. If we find one
// that completes parsing for this key, add it to the list and reset to searching for a new key
defaultCommentMatch := defaultValueRegex.FindStringSubmatch(currentLine)
commentContinuationMatch := commentContinuationRegex.FindStringSubmatch(currentLine)
if len(defaultCommentMatch) > 1 || len(commentContinuationMatch) > 1 {
commentLines = append(commentLines, currentLine)
continue
}
// If we haven't continued by this point, we didn't match any of the comment formats we want, so we need to add
// the in progress value to the map, and reset to looking for a new key
key, description := ParseComment(commentLines)
keyToDescriptions[key] = description
commentLines = make([]string, 0)
foundValuesComment = false
}
return keyToDescriptions, nil
}
func ParseChartInformation(chartDirectory string) (ChartDocumentationInfo, error) {
var chartDocInfo ChartDocumentationInfo
var err error
chartDocInfo.ChartDirectory = chartDirectory
chartDocInfo.ChartMeta, err = parseChartFile(chartDirectory)
if err != nil {
return chartDocInfo, err
}
chartDocInfo.ChartRequirements, err = parseChartRequirementsFile(chartDirectory, chartDocInfo.ApiVersion)
if err != nil {
return chartDocInfo, err
}
chartValues, err := parseChartValuesFile(chartDirectory)
if err != nil {
return chartDocInfo, err
}
chartDocInfo.ChartValues = &chartValues
chartDocInfo.ChartValuesDescriptions, err = parseChartValuesFileComments(chartDirectory)
if err != nil {
return chartDocInfo, err
}
return chartDocInfo, nil
}