forked from devfile/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerging.go
More file actions
261 lines (235 loc) · 9.41 KB
/
merging.go
File metadata and controls
261 lines (235 loc) · 9.41 KB
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
//
//
// Copyright Red Hat
//
// 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 overriding
import (
"fmt"
"reflect"
"strings"
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/yaml"
)
// MergeDevWorkspaceTemplateSpec implements the merging logic of a main devfile content with flattened, already-overridden parent devfiles or plugins.
// On a `main` `DevWorkspaceTemplateSpec` (which is the core part of a devfile, without the `apiVersion` and `metadata`),
// it allows adding all the new overridden elements provided by flattened parent and plugins
//
// Returns non-nil error if there are duplicate (== with same key) commands, components or projects between the
// main content and the parent or plugins.
//
// The result is a transformed `DevWorkspaceTemplateSpec` object, that does not contain any `plugin` component
// (since they are expected to be provided as flattened overridden devfiles in the arguments)
func MergeDevWorkspaceTemplateSpec(
mainContent *dw.DevWorkspaceTemplateSpecContent,
parentFlattenedContent *dw.DevWorkspaceTemplateSpecContent,
pluginFlattenedContents ...*dw.DevWorkspaceTemplateSpecContent) (*dw.DevWorkspaceTemplateSpecContent, error) {
allContents := []*dw.DevWorkspaceTemplateSpecContent{}
if parentFlattenedContent != nil {
allContents = append(allContents, parentFlattenedContent)
}
if len(pluginFlattenedContents) > 0 {
allContents = append(allContents, pluginFlattenedContents...)
}
allContents = append(allContents, mainContent)
// Check for conflicts
if parentFlattenedContent != nil {
if err := ensureNoConflictWithParent(mainContent, parentFlattenedContent); err != nil {
return nil, err
}
}
if len(pluginFlattenedContents) > 0 {
if err := ensureNoConflictsWithPlugins(mainContent, pluginFlattenedContents...); err != nil {
return nil, err
}
if parentFlattenedContent != nil {
// also need to ensure no conflict between parent and plugins
if err := ensureNoConflictsWithPlugins(parentFlattenedContent, pluginFlattenedContents...); err != nil {
return nil, err
}
}
}
result := dw.DevWorkspaceTemplateSpecContent{}
// Merge top-level lists (Commands, Projects, Components, etc ...)
topLevelListsNames := result.GetToplevelLists()
topLevelListsByContent := []dw.TopLevelLists{}
for _, content := range allContents {
topLevelListsByContent = append(topLevelListsByContent, content.GetToplevelLists())
}
resultValue := reflect.ValueOf(&result).Elem()
for toplevelListName := range topLevelListsNames {
listType, fieldExists := resultValue.Type().FieldByName(toplevelListName)
if !fieldExists {
return nil, fmt.Errorf("field '%v' is unknown in %v struct (this should never happen)", toplevelListName, reflect.TypeOf(resultValue).Name())
}
if listType.Type.Kind() != reflect.Slice {
return nil, fmt.Errorf("field '%v' in %v struct is not a slice (this should never happen)", toplevelListName, reflect.TypeOf(resultValue).Name())
}
listElementType := listType.Type.Elem()
resultToplevelListValue := resultValue.FieldByName(toplevelListName)
for contentIndex, content := range allContents {
toplevelLists := topLevelListsByContent[contentIndex]
keyedList := toplevelLists[toplevelListName]
for _, keyed := range keyedList {
if content == mainContent {
if component, isComponent := keyed.(dw.Component); isComponent &&
component.Plugin != nil {
continue
}
}
if resultToplevelListValue.IsNil() {
resultToplevelListValue.Set(reflect.MakeSlice(reflect.SliceOf(listElementType), 0, len(keyedList)))
}
resultToplevelListValue.Set(reflect.Append(resultToplevelListValue, reflect.ValueOf(keyed)))
}
}
}
var preStartCommands []string
var postStartCommands []string
var preStopCommands []string
var postStopCommands []string
for _, content := range allContents {
if content.Events != nil {
if result.Events == nil {
result.Events = &dw.Events{}
}
preStartCommands = UnionStrings(preStartCommands, content.Events.PreStart)
postStartCommands = UnionStrings(postStartCommands, content.Events.PostStart)
preStopCommands = UnionStrings(preStopCommands, content.Events.PreStop)
postStopCommands = UnionStrings(postStopCommands, content.Events.PostStop)
}
if len(content.Variables) > 0 {
if len(result.Variables) == 0 {
result.Variables = make(map[string]string)
}
for k, v := range content.Variables {
result.Variables[k] = v
}
}
var err error
if len(content.Attributes) > 0 {
if len(result.Attributes) == 0 {
result.Attributes = attributes.Attributes{}
}
for k, v := range content.Attributes {
result.Attributes.Put(k, v, &err)
if err != nil {
return nil, err
}
}
}
}
if result.Events != nil {
result.Events.PreStart = preStartCommands
result.Events.PostStart = postStartCommands
result.Events.PreStop = preStopCommands
result.Events.PostStop = postStopCommands
}
return &result, nil
}
// MergeDevWorkspaceTemplateSpecBytes implements the merging logic of a main devfile content with flattened, already-overridden parent devfiles or plugins.
// On an json or yaml document that contains the core content of the devfile (which is the core part of a devfile, without the `apiVersion` and `metadata`),
// it allows adding all the new overridden elements provided by flattened parent and plugins (also provided as json or yaml documents)
//
// It is not allowed for to have duplicate (== with same key) commands, components or projects between the main content and the parent or plugins.
// An error would be thrown
//
// The result is a transformed `DevfileWorkspaceTemplateSpec` object, that does not contain any `plugin` component
// (since they are expected to be provided as flattened overridden devfiles in the arguments)
func MergeDevWorkspaceTemplateSpecBytes(originalBytes []byte, flattenedParentBytes []byte, flattenPluginsBytes ...[]byte) (*dw.DevWorkspaceTemplateSpecContent, error) {
originalJson, err := yaml.ToJSON(originalBytes)
if err != nil {
return nil, err
}
original := dw.DevWorkspaceTemplateSpecContent{}
err = json.Unmarshal(originalJson, &original)
if err != nil {
return nil, err
}
flattenedParentJson, err := yaml.ToJSON(flattenedParentBytes)
if err != nil {
return nil, err
}
flattenedParent := dw.DevWorkspaceTemplateSpecContent{}
err = json.Unmarshal(flattenedParentJson, &flattenedParent)
if err != nil {
return nil, err
}
flattenedPlugins := []*dw.DevWorkspaceTemplateSpecContent{}
for _, flattenedPluginBytes := range flattenPluginsBytes {
flattenedPluginJson, err := yaml.ToJSON(flattenedPluginBytes)
if err != nil {
return nil, err
}
flattenedPlugin := dw.DevWorkspaceTemplateSpecContent{}
err = json.Unmarshal(flattenedPluginJson, &flattenedPlugin)
if err != nil {
return nil, err
}
flattenedPlugins = append(flattenedPlugins, &flattenedPlugin)
}
return MergeDevWorkspaceTemplateSpec(&original, &flattenedParent, flattenedPlugins...)
}
func ensureNoConflictWithParent(mainContent *dw.DevWorkspaceTemplateSpecContent, parentflattenedContent *dw.DevWorkspaceTemplateSpecContent) error {
return checkKeys(func(elementType string, keysSets []sets.String) []error {
mainKeys := keysSets[0]
parentOrPluginKeys := keysSets[1]
overriddenElementsInMainContent := mainKeys.Intersection(parentOrPluginKeys)
if overriddenElementsInMainContent.Len() > 0 {
return []error{fmt.Errorf("Some %s are already defined in parent: %s. "+
"If you want to override them, you should do it in the parent scope.",
elementType,
strings.Join(overriddenElementsInMainContent.List(), ", "))}
}
return []error{}
},
mainContent, parentflattenedContent)
}
func ensureNoConflictsWithPlugins(mainContent *dw.DevWorkspaceTemplateSpecContent, pluginFlattenedContents ...*dw.DevWorkspaceTemplateSpecContent) error {
getPluginKey := func(pluginIndex int) string {
index := 0
for _, comp := range mainContent.Components {
if comp.Plugin != nil {
if pluginIndex == index {
return comp.Name
}
index++
}
}
return "unknown"
}
allSpecs := []dw.TopLevelListContainer{mainContent}
for _, pluginFlattenedContent := range pluginFlattenedContents {
allSpecs = append(allSpecs, pluginFlattenedContent)
}
return checkKeys(func(elementType string, keysSets []sets.String) []error {
mainKeys := keysSets[0]
pluginKeysSets := keysSets[1:]
errs := []error{}
for pluginNumber, pluginKeys := range pluginKeysSets {
overriddenElementsInMainContent := mainKeys.Intersection(pluginKeys)
if overriddenElementsInMainContent.Len() > 0 {
errs = append(errs, fmt.Errorf("Some %s are already defined in plugin '%s': %s. "+
"If you want to override them, you should do it in the plugin scope.",
elementType,
getPluginKey(pluginNumber),
strings.Join(overriddenElementsInMainContent.List(), ", ")))
}
}
return errs
},
allSpecs...)
}