-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
configlocations.go
366 lines (332 loc) · 12.7 KB
/
configlocations.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2021 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configlocations
import (
"context"
"path"
"reflect"
"strconv"
"strings"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
sErrors "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/errors"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
)
type YAMLInfo struct {
RNode *kyaml.RNode
SourceFile string
}
type Location struct {
SourceFile string
StartLine int
StartColumn int
EndLine int
EndColumn int
}
type YAMLInfos struct {
yamlInfos map[uintptr]map[string]YAMLInfo
FieldsOverrodeByProfile map[string]YAMLOverrideInfo // map of schema path -> profile name -- ex: /artifacts/0/image -> "overwrite-artifacte-image-profile"
}
func (m *YAMLInfos) GetYamlInfosCopy() map[uintptr]map[string]YAMLInfo {
yamlInfos := map[uintptr]map[string]YAMLInfo{}
for ptr, mp := range m.yamlInfos {
tmpmp := map[string]YAMLInfo{}
for k, v := range mp {
tmpmp[k] = YAMLInfo{
RNode: v.RNode.Copy(),
SourceFile: v.SourceFile,
}
}
yamlInfos[ptr] = tmpmp
}
return yamlInfos
}
func MissingLocation() *Location {
return &Location{
SourceFile: "",
StartLine: -1,
StartColumn: -1,
EndLine: -1,
EndColumn: -1,
}
}
func NewYAMLInfos() *YAMLInfos {
return &YAMLInfos{
yamlInfos: map[uintptr]map[string]YAMLInfo{},
}
}
type YAMLOverrideInfo struct {
ProfileName string
PatchIndex int
PatchOperation string
PatchCopyFrom string
}
// Parse parses a skaffold config entry collecting file location information for each schema config object
func Parse(sourceFile string, config *latest.SkaffoldConfig, fieldsOverrodeByProfile map[string]YAMLOverrideInfo) (*YAMLInfos, error) {
yamlInfos, err := buildMapOfSchemaObjPointerToYAMLInfos(sourceFile, config, map[uintptr]map[string]YAMLInfo{}, fieldsOverrodeByProfile)
return &YAMLInfos{
yamlInfos: yamlInfos,
FieldsOverrodeByProfile: fieldsOverrodeByProfile,
},
err
}
// Locate gets the location for a skaffold schema struct pointer
func (m *YAMLInfos) Locate(obj interface{}) *Location {
return m.locate(obj, "")
}
// Locate gets the location for a skaffold schema struct pointer
func (m *YAMLInfos) LocateElement(obj interface{}, idx int) *Location {
return m.locate(obj, strconv.Itoa(idx))
}
// Locate gets the location for a skaffold schema struct pointer
func (m *YAMLInfos) LocateField(obj interface{}, fieldName string) *Location {
return m.locate(obj, fieldName)
}
// Locate gets the location for a skaffold schema struct pointer
func (m *YAMLInfos) LocateByPointer(ptr uintptr) *Location {
if m == nil {
log.Entry(context.TODO()).Infof("YamlInfos is nil, unable to complete call to LocateByPointer for pointer: %d", ptr)
return MissingLocation()
}
if _, ok := m.yamlInfos[ptr]; !ok {
log.Entry(context.TODO()).Infof("no map entry found when attempting LocateByPointer for pointer: %d", ptr)
return MissingLocation()
}
node, ok := m.yamlInfos[ptr][""]
if !ok {
log.Entry(context.TODO()).Infof("no map entry found when attempting LocateByPointer for pointer: %d", ptr)
return MissingLocation()
}
// iterate over kyaml.RNode text to get endline and endcolumn information
nodeText, err := node.RNode.String()
if err != nil {
return MissingLocation()
}
log.Entry(context.TODO()).Infof("map entry found when executing LocateByPointer for pointer: %d", ptr)
lines, cols := getLinesAndColsOfString(nodeText)
// TODO(aaron-prindle) all line & col values seem 1 greater than expected in actual use, will need to check to see how it works with IDE
return &Location{
SourceFile: node.SourceFile,
StartLine: node.RNode.Document().Line,
StartColumn: node.RNode.Document().Column,
EndLine: node.RNode.Document().Line + lines,
EndColumn: cols,
}
}
func (m *YAMLInfos) locate(obj interface{}, key string) *Location {
if m == nil {
log.Entry(context.TODO()).Infof("YamlInfos is nil, unable to complete call to locate with params: %v of type %T", obj, obj)
return MissingLocation()
}
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Ptr {
log.Entry(context.TODO()).Infof("non pointer object passed to locate: %v of type %T", obj, obj)
return MissingLocation()
}
if _, ok := m.yamlInfos[v.Pointer()]; !ok {
log.Entry(context.TODO()).Infof("no map entry found when attempting locate for %v of type %T and pointer: %d", obj, obj, v.Pointer())
return MissingLocation()
}
node, ok := m.yamlInfos[v.Pointer()][key]
if !ok {
log.Entry(context.TODO()).Infof("no map entry found when attempting locate for %v of type %T and pointer: %d", obj, obj, v.Pointer())
return MissingLocation()
}
// iterate over kyaml.RNode text to get endline and endcolumn information
nodeText, err := node.RNode.String()
if err != nil {
return MissingLocation()
}
log.Entry(context.TODO()).Infof("map entry found when executing locate for %v of type %T and pointer: %d", obj, obj, v.Pointer())
lines, cols := getLinesAndColsOfString(nodeText)
// TODO(aaron-prindle) all line & col values seem 1 greater than expected in actual use, will need to check to see how it works with IDE
return &Location{
SourceFile: node.SourceFile,
StartLine: node.RNode.Document().Line,
StartColumn: node.RNode.Document().Column,
EndLine: node.RNode.Document().Line + lines,
EndColumn: cols,
}
}
func getLinesAndColsOfString(str string) (int, int) {
line := 0
col := 0
for i := range str {
col++
if str[i] == '\n' {
line++
col = 0
}
}
return line, col
}
func buildMapOfSchemaObjPointerToYAMLInfos(sourceFile string, config *latest.SkaffoldConfig, yamlInfos map[uintptr]map[string]YAMLInfo,
fieldsOverrodeByProfile map[string]YAMLOverrideInfo) (map[uintptr]map[string]YAMLInfo, error) {
defer func() {
if err := recover(); err != nil {
log.Entry(context.TODO()).Errorf(
"panic occurred during schema reflection for yaml line number information: %v", err)
}
}()
skaffoldConfigText, err := util.ReadConfiguration(sourceFile)
if err != nil {
return nil, sErrors.ConfigParsingError(err)
}
root, err := kyaml.Parse(string(skaffoldConfigText))
if err != nil {
return nil, err
}
return generateObjPointerToYAMLNodeMap(sourceFile, reflect.ValueOf(config), reflect.ValueOf(nil), "", "", []string{},
root, root, -1, fieldsOverrodeByProfile, yamlInfos, false)
}
// generateObjPointerToYAMLNodeMap recursively walks through a structs fields (taking into account profile and patch profile overrides)
// and collects the corresponding yaml node for each field
func generateObjPointerToYAMLNodeMap(sourceFile string, v reflect.Value, parentV reflect.Value, fieldName, yamlTag string, schemaPath []string,
rootRNode *kyaml.RNode, rNode *kyaml.RNode, containerIdx int, fieldPathsOverrodeByProfiles map[string]YAMLOverrideInfo, yamlInfos map[uintptr]map[string]YAMLInfo, isPatchProfileElemOverride bool) (map[uintptr]map[string]YAMLInfo, error) {
// TODO(aaron-prindle) need to verify if generateObjPointerToYAMLNodeMap adds entries for 'map' types, luckily the skaffold schema
// only has map[string]string and they are leaf nodes as well which this should work fine for doing the recursion for the time being
var err error
// add current obj/field to schema path if criteria met
switch {
case containerIdx >= 0:
schemaPath = append(schemaPath, strconv.Itoa(containerIdx))
case yamlTag != "":
schemaPath = append(schemaPath, yamlTag)
}
// check if current obj/field was overridden by a profile
if yamlOverrideInfo, ok := fieldPathsOverrodeByProfiles["/"+path.Join(schemaPath...)]; ok {
// reset yaml node path from root path to given profile path ("/" -> "/profile/name=profileName/etc...")
rNode, err = rootRNode.Pipe(kyaml.Lookup("profiles"), kyaml.MatchElementList([]string{"name"}, []string{yamlOverrideInfo.ProfileName}))
if err != nil {
return nil, err
}
switch {
case yamlOverrideInfo.PatchIndex < 0: // this schema obj/field has a profile override (NOT a patch profile override)
// moves parent node path from being rooted at default yaml '/' to being rooted at '/profile/name=profileName/...'
for i := 0; i < len(schemaPath)-1; i++ {
rNode, err = rNode.Pipe(kyaml.Lookup(schemaPath[i]))
if err != nil {
return nil, err
}
}
default: // this schema obj/field has a patch profile override
// NOTE: 'remove' patch operations are not included in fieldPathsOverrodeByProfiles as there
// is no work to be done on them (they were already removed from the schema)
// TODO(aaron-prindle) verify UX makes sense to use the "FROM" copy node to get yaml information from
if yamlOverrideInfo.PatchOperation == "copy" {
fromPath := strings.Split(yamlOverrideInfo.PatchCopyFrom, "/")
var kf kyaml.Filter
for i := 0; i < len(fromPath)-1; i++ {
if pathNum, err := strconv.Atoi(fromPath[i]); err == nil {
// this path element is a number
kf = kyaml.ElementIndexer{Index: pathNum}
} else {
// this path element isn't a number
kf = kyaml.Lookup(fromPath[i])
}
rNode, err = rNode.Pipe(kf)
if err != nil {
return nil, err
}
}
} else {
rNode, err = rNode.Pipe(kyaml.Lookup("patches"), kyaml.ElementIndexer{Index: yamlOverrideInfo.PatchIndex})
if err != nil {
return nil, err
}
yamlTag = "value"
}
isPatchProfileElemOverride = true
}
}
if rNode == nil {
return yamlInfos, nil
}
// drill down through pointers and interfaces to get a value we can use
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
if yamlTag != "" { // check that struct is not `yaml:",inline"`
// traverse kyaml node tree to current obj/field location
var kf kyaml.Filter
switch {
case rNode.YNode().Kind == kyaml.SequenceNode:
kf = kyaml.ElementIndexer{Index: containerIdx}
default:
kf = kyaml.Lookup(yamlTag)
}
rNode, err = rNode.Pipe(kf)
if err != nil {
return nil, err
}
if rNode == nil {
return yamlInfos, nil
}
// this case is so that the line #'s of primitive values can be "located" as they are not addressable but we can
// map the parent address and put the child field in second map
if parentV.CanAddr() {
if _, ok := yamlInfos[parentV.Addr().Pointer()]; !ok {
yamlInfos[parentV.Addr().Pointer()] = map[string]YAMLInfo{}
}
// add parent relationship entry to yaml info map
if containerIdx >= 0 {
yamlInfos[parentV.Addr().Pointer()][strconv.Itoa(containerIdx)] = YAMLInfo{
RNode: rNode,
SourceFile: sourceFile,
}
} else {
yamlInfos[parentV.Addr().Pointer()][fieldName] = YAMLInfo{
RNode: rNode,
SourceFile: sourceFile,
}
}
}
}
if v.CanAddr() {
if _, ok := yamlInfos[v.Addr().Pointer()]; !ok {
yamlInfos[v.Addr().Pointer()] = map[string]YAMLInfo{}
}
// add current node entry to yaml info map
yamlInfos[v.Addr().Pointer()][""] = YAMLInfo{
RNode: rNode,
SourceFile: sourceFile,
}
}
switch v.Kind() {
// TODO(aaron-prindle) add reflect.Map support here as well, currently no struct fields have nested struct in map field so ok for now
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
generateObjPointerToYAMLNodeMap(sourceFile, v.Index(i), v, fieldName+"["+strconv.Itoa(i)+"]", yamlTag+"["+strconv.Itoa(i)+"]", schemaPath,
rootRNode, rNode, i, fieldPathsOverrodeByProfiles, yamlInfos, isPatchProfileElemOverride)
}
case reflect.Struct:
t := v.Type() // use type to get number and names of fields
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// TODO(aaron-prindle) verify this value works for structs that are `yaml:",inline"`
newYamlTag := field.Name
if yamlTagToken := field.Tag.Get("yaml"); yamlTagToken != "" && yamlTagToken != "-" {
// check for possible comma as in "...,omitempty"
var commaIdx int
if commaIdx = strings.Index(yamlTagToken, ","); commaIdx < 0 {
commaIdx = len(yamlTagToken)
}
newYamlTag = yamlTagToken[:commaIdx]
}
generateObjPointerToYAMLNodeMap(sourceFile, v.Field(i), v, field.Name, newYamlTag, schemaPath, rootRNode, rNode, -1,
fieldPathsOverrodeByProfiles, yamlInfos, isPatchProfileElemOverride)
}
}
return yamlInfos, nil
}