-
Notifications
You must be signed in to change notification settings - Fork 5
/
auto_detection.go
262 lines (234 loc) · 6.76 KB
/
auto_detection.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package ci
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/fairwindsops/insights-plugins/plugins/ci/pkg/models"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/ghodss/yaml" // supports both yaml and json
)
var supportedKubeExtensions = []string{"yaml", "yml", "json"}
var supportedExtensions = []string{"yaml", "yml", "json", "tf"}
type KubernetesManifest struct {
ApiVersion *string `json:"apiVersion"` // Affects YAML field names too.
Kind *string `json:"kind"`
}
// ConfigFileAutoDetection reads recursively a path looking for kubernetes manifests and helm charts, returns a fairwinds-insights configuration struct or error
func ConfigFileAutoDetection(basePath string) (*models.Configuration, error) {
k8sManifests := []string{}
helmFolders := []string{}
terraformPaths := []string{}
err := filepath.Walk(basePath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("Could not walk into dir: %v", err)
}
if info.IsDir() {
if strings.HasPrefix(info.Name(), ".") { // hidden
logrus.Infof("this is a hidden directory: %s, skipping...", info.Name())
return filepath.SkipDir
}
helmFolder, err := isHelmBaseFolder(path)
if err != nil {
return err
}
if helmFolder {
relPath, err := filepath.Rel(basePath, path)
if err != nil {
return err
}
helmFolders = append(helmFolders, relPath)
return filepath.SkipDir
}
terraformFolder, err := isTerraformFolder(path)
if err != nil {
return err
}
if terraformFolder {
terraformPaths = append(terraformPaths, path)
return filepath.SkipDir
}
logrus.Debugf("this is a directory: %s", info.Name())
return nil
}
fileExtension := filepath.Ext(path)
if fileExtension == "" {
return nil
}
if !lo.Contains(supportedExtensions, fileExtension[1:]) {
logrus.Debugf("file extension '%s' not supported for file %v", fileExtension, path)
return nil
}
if isFluxManifest(path) {
logrus.Debugf("file '%s' is a flux manifest, skipping...", path)
return nil
}
if !isKubernetesManifest(path) {
logrus.Debugf("file %s is NOT a k8s manifest, skipping...", path)
return nil
}
relPath, err := filepath.Rel(basePath, path)
if err != nil {
return err
}
logrus.Debugf("it is a k8s manifest: %s", path)
k8sManifests = append(k8sManifests, relPath)
return nil
})
if err != nil {
return nil, err
}
config := models.Configuration{
Manifests: models.ManifestConfig{
YamlPaths: k8sManifests,
Helm: toHelmConfigs(basePath, helmFolders),
},
Terraform: models.TerraformConfig{
Paths: terraformPaths,
},
}
return &config, nil
}
func isFluxManifest(path string) bool {
k8sManifest := getPossibleKubernetesManifest(path)
if k8sManifest == nil {
return false
}
return strings.Contains(*k8sManifest.ApiVersion, "toolkit.fluxcd.io")
}
func isKubernetesManifest(path string) bool {
return getPossibleKubernetesManifest(path) != nil
}
// getPossibleKubernetesManifest returns a kubernetesManifest from given path, nil if could not be open or parsed
func getPossibleKubernetesManifest(path string) *KubernetesManifest {
file, err := os.Open(path)
if err != nil {
logrus.Debugf("Could not open file %s", path)
return nil
}
content, err := io.ReadAll(file)
if err != nil {
logrus.Debugf("Could not read contents from file %s", file.Name())
return nil
}
var k8sManifest KubernetesManifest
err = yaml.Unmarshal(content, &k8sManifest)
if err != nil {
// not being to unmarshal means it is not a k8s file
return nil
}
if k8sManifest.ApiVersion == nil || k8sManifest.Kind == nil {
// not having either apiVersion nor kind means it is not a k8s file
return nil
}
return &k8sManifest
}
func isHelmBaseFolder(path string) (bool, error) {
files, err := os.ReadDir(path)
if err != nil {
return false, fmt.Errorf("Could not read dir %s: %v", path, err)
}
for _, file := range files {
for _, ext := range supportedKubeExtensions {
if file.Name() == "Chart."+ext {
return true, nil
}
}
}
return false, nil
}
// isTerraformFOlder returns true if the specified directory contains a file
// with a .tf extension.
func isTerraformFolder(path string) (bool, error) {
files, err := os.ReadDir(path)
if err != nil {
return false, fmt.Errorf("Could not read dir %s: %v", path, err)
}
for _, file := range files {
if file.IsDir() {
continue
}
fileExtension := filepath.Ext(file.Name())
if strings.EqualFold(fileExtension, ".tf") {
logrus.Debugf("Directory %q contains Terraform because a .tf file was found", path)
return true, nil
}
}
return false, nil
}
func toHelmConfigs(baseFolder string, helmPaths []string) []models.HelmConfig {
result := []models.HelmConfig{}
for _, path := range helmPaths {
name := tryFetchNameFromChartFile(baseFolder, path)
hc := models.HelmConfig{
Name: name,
Path: path,
}
valuesFilePath := tryDiscoverValuesFile(baseFolder, path)
if valuesFilePath != "" {
hc.ValuesFile = valuesFilePath
} else {
// if default values file does not exists, use a empty map as values
hc.Values = map[string]interface{}{}
}
result = append(result, hc)
}
logDuplicatedHelmConfigNames(result)
return result
}
func logDuplicatedHelmConfigNames(arr []models.HelmConfig) {
visited := map[string]bool{}
for _, hc := range arr {
if _, ok := visited[hc.Name]; ok {
logrus.Warnf("helm config name '%s' is duplicated", hc.Name)
} else {
visited[hc.Name] = true
}
}
}
// tries to extract name from Chart.yaml file, return chart (dir name) as fallback
func tryFetchNameFromChartFile(baseFolder, chart string) string {
for _, ext := range supportedKubeExtensions {
f := "Chart." + ext
file, err := os.Open(filepath.Join(baseFolder, chart, f))
if err != nil {
logrus.Debugf("Could not open file %s: %v", f, err)
continue
}
content, err := io.ReadAll(file)
if err != nil {
logrus.Warnf("Could not read %s: %v", f, err)
continue
}
var s map[string]interface{}
err = yaml.Unmarshal(content, &s)
if err != nil {
logrus.Warnf("Could not unmarshal %s: %v", string(content), err)
continue
}
name, ok := s["name"].(string)
if !ok {
logrus.Warnf("Could not get name from file %s: %v", string(content), err)
continue
}
return name
}
return chart
}
// tries to discover the default values file, returns empty str if not found
func tryDiscoverValuesFile(baseFolder, path string) string {
for _, ext := range supportedKubeExtensions {
f := "values." + ext
possibleValuesFile := filepath.Join(baseFolder, path, f)
if _, err := os.Stat(possibleValuesFile); errors.Is(err, os.ErrNotExist) {
continue
}
relPath, _ := filepath.Rel(baseFolder, possibleValuesFile)
return relPath
}
return ""
}