-
Notifications
You must be signed in to change notification settings - Fork 858
/
handle.go
328 lines (279 loc) · 9.57 KB
/
handle.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
/*
Copyright 2021 The KubeVela 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 process
import (
"context"
"encoding/json"
"fmt"
"strings"
"unicode"
"github.com/pkg/errors"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/pkg/cue/model"
"github.com/oam-dev/kubevela/pkg/oam/util"
)
// Context defines Rendering Context Interface
type Context interface {
SetBase(base model.Instance) error
AppendAuxiliaries(auxiliaries ...Auxiliary) error
Output() (model.Instance, []Auxiliary)
BaseContextFile() string
ExtendedContextFile() string
BaseContextLabels() map[string]string
SetParameters(params map[string]interface{})
PushData(key string, data interface{})
GetCtx() context.Context
SetCtx(context.Context)
}
// Auxiliary are objects rendered by definition template.
// the format for auxiliary resource is always: `outputs.<resourceName>`, it can be auxiliary workload or trait
type Auxiliary struct {
Ins model.Instance
// Type will be used to mark definition label for OAM runtime to get the CRD
// It's now required for trait and main workload object. Extra workload CR object will not have the type.
Type string
// Workload or trait with multiple `outputs` will have a name, if name is empty, than it's the main of this type.
Name string
}
type templateContext struct {
// name is the component name of Application
name string
// appName is the name of Application
appName string
// appRevision is the revision name of Application
appRevision string
workflowName string
publishVersion string
configs []map[string]string
base model.Instance
auxiliaries []Auxiliary
// namespace is the namespace of Application which is used to set the namespace for Crossplane connection secret,
// ComponentDefinition/TratiDefinition OpenAPI v3 schema
namespace string
// parameters is used to store the properties passed into the current component
parameters map[string]interface{}
// outputSecretName is used to store all secret names which are generated by cloud resource components
outputSecretName string
// requiredSecrets is used to store all secret names which are generated by cloud resource components and required by current component
requiredSecrets []RequiredSecrets
baseHooks []BaseHook
auxiliaryHooks []AuxiliaryHook
components []common.ApplicationComponent
data map[string]interface{}
// appLabels is the labels of Application
appLabels map[string]string
// appAnnotations is the annotations of Application
appAnnotations map[string]string
ctx context.Context
}
// RequiredSecrets is used to store all secret names which are generated by cloud resource components and required by current component
type RequiredSecrets struct {
Namespace string
Name string
ContextName string
Data map[string]interface{}
}
// ContextData is the core data of process context
type ContextData struct {
Namespace string
AppName string
CompName string
AppRevisionName string
WorkflowName string
PublishVersion string
Ctx context.Context
BaseHooks []BaseHook
AuxiliaryHooks []AuxiliaryHook
Components []common.ApplicationComponent
AppLabels map[string]string
AppAnnotations map[string]string
}
// NewContext create render templateContext
func NewContext(data ContextData) Context {
ctx := &templateContext{
namespace: data.Namespace,
name: data.CompName,
appName: data.AppName,
appRevision: data.AppRevisionName,
workflowName: data.WorkflowName,
publishVersion: data.PublishVersion,
configs: []map[string]string{},
auxiliaries: []Auxiliary{},
parameters: map[string]interface{}{},
ctx: data.Ctx,
baseHooks: data.BaseHooks,
auxiliaryHooks: data.AuxiliaryHooks,
components: data.Components,
appLabels: data.AppLabels,
appAnnotations: data.AppAnnotations,
}
return ctx
}
// SetParameters sets templateContext parameters
func (ctx *templateContext) SetParameters(params map[string]interface{}) {
ctx.parameters = params
}
// SetBase set templateContext base model
func (ctx *templateContext) SetBase(base model.Instance) error {
for _, hook := range ctx.baseHooks {
if err := hook.Exec(ctx, base); err != nil {
return errors.Wrap(err, "cannot set base into context")
}
}
ctx.base = base
return nil
}
// AppendAuxiliaries add Assist model to templateContext
func (ctx *templateContext) AppendAuxiliaries(auxiliaries ...Auxiliary) error {
for _, hook := range ctx.auxiliaryHooks {
if err := hook.Exec(ctx, auxiliaries); err != nil {
return errors.Wrap(err, "cannot append auxiliaries into context")
}
}
ctx.auxiliaries = append(ctx.auxiliaries, auxiliaries...)
return nil
}
// BaseContextFile return cue format string of templateContext
func (ctx *templateContext) BaseContextFile() string {
var buff string
buff += fmt.Sprintf(model.ContextName+": \"%s\"\n", ctx.name)
buff += fmt.Sprintf(model.ContextAppName+": \"%s\"\n", ctx.appName)
buff += fmt.Sprintf(model.ContextAppRevision+": \"%s\"\n", ctx.appRevision)
// the appRevision is generated by vela, the error always is nil, so ignore it
revNum, _ := util.ExtractRevisionNum(ctx.appRevision, "-")
buff += fmt.Sprintf(model.ContextAppRevisionNum+": %d\n", revNum)
buff += fmt.Sprintf(model.ContextNamespace+": \"%s\"\n", ctx.namespace)
buff += fmt.Sprintf(model.ContextCompRevisionName+": \"%s\"\n", model.ComponentRevisionPlaceHolder)
buff += fmt.Sprintf(model.ContextWorkflowName+": \"%s\"\n", ctx.workflowName)
buff += fmt.Sprintf(model.ContextPublishVersion+": \"%s\"\n", ctx.publishVersion)
if ctx.appLabels != nil {
bt, _ := json.Marshal(ctx.appLabels)
buff += model.ContextAppLabels + ": " + string(bt) + "\n"
}
if ctx.appAnnotations != nil {
bt, _ := json.Marshal(ctx.appAnnotations)
buff += model.ContextAppAnnotations + ": " + string(bt) + "\n"
}
if ctx.base != nil {
buff += fmt.Sprintf(model.OutputFieldName+": %s\n", structMarshal(ctx.base.String()))
}
if ctx.components != nil {
bt, _ := json.Marshal(ctx.components)
buff += fmt.Sprintf(model.ContextComponents+":%s\n", string(bt))
}
if len(ctx.auxiliaries) > 0 {
var auxLines []string
for _, auxiliary := range ctx.auxiliaries {
auxLines = append(auxLines, fmt.Sprintf("\"%s\": %s", auxiliary.Name, structMarshal(auxiliary.Ins.String())))
}
if len(auxLines) > 0 {
buff += fmt.Sprintf(model.OutputsFieldName+": {%s}\n", strings.Join(auxLines, "\n"))
}
}
if len(ctx.configs) > 0 {
bt, _ := json.Marshal(ctx.configs)
buff += model.ConfigFieldName + ": " + string(bt) + "\n"
}
if len(ctx.requiredSecrets) > 0 {
for _, s := range ctx.requiredSecrets {
data, _ := json.Marshal(s.Data)
buff += s.ContextName + ":" + string(data) + "\n"
}
}
if ctx.parameters != nil {
bt, _ := json.Marshal(ctx.parameters)
buff += model.ParameterFieldName + ": " + string(bt) + "\n"
}
if ctx.outputSecretName != "" {
buff += fmt.Sprintf("%s:\"%s\"", model.OutputSecretName, ctx.outputSecretName)
}
if ctx.data != nil {
d, _ := json.Marshal(ctx.data)
buff += fmt.Sprintf("\n %s", structMarshal(string(d)))
}
return fmt.Sprintf("context: %s", structMarshal(buff))
}
// ExtendedContextFile return cue format string of templateContext and extended secret context
func (ctx *templateContext) ExtendedContextFile() string {
context := ctx.BaseContextFile()
var bareSecret string
if len(ctx.requiredSecrets) > 0 {
for _, s := range ctx.requiredSecrets {
data, _ := json.Marshal(s.Data)
bareSecret += s.ContextName + ":" + string(data) + "\n"
}
}
if bareSecret != "" {
return context + "\n" + bareSecret
}
return context
}
func (ctx *templateContext) BaseContextLabels() map[string]string {
return map[string]string{
// appName is oam.LabelAppName
model.ContextAppName: ctx.appName,
// name is oam.LabelAppComponent
model.ContextName: ctx.name,
// appRevision is oam.LabelAppRevision
model.ContextAppRevision: ctx.appRevision,
}
}
// Output return model and auxiliaries of templateContext
func (ctx *templateContext) Output() (model.Instance, []Auxiliary) {
return ctx.base, ctx.auxiliaries
}
// InsertSecrets will add cloud resource secret stuff to context
func (ctx *templateContext) InsertSecrets(outputSecretName string, requiredSecrets []RequiredSecrets) {
if outputSecretName != "" {
ctx.outputSecretName = outputSecretName
}
if requiredSecrets != nil {
ctx.requiredSecrets = requiredSecrets
}
}
// PushData appends arbitrary extension data to context
func (ctx *templateContext) PushData(key string, data interface{}) {
if ctx.data == nil {
ctx.data = map[string]interface{}{key: data}
return
}
ctx.data[key] = data
}
func (ctx *templateContext) GetCtx() context.Context {
if ctx.ctx != nil {
return ctx.ctx
}
return context.TODO()
}
func (ctx *templateContext) SetCtx(newContext context.Context) {
if ctx.ctx != nil {
return
}
ctx.ctx = newContext
}
func structMarshal(v string) string {
skip := false
v = strings.TrimFunc(v, func(r rune) bool {
if !skip {
if unicode.IsSpace(r) {
return true
}
skip = true
}
return false
})
if strings.HasPrefix(v, "{") {
return v
}
return fmt.Sprintf("{%s}", v)
}