forked from argoproj/argo-cd
/
lua.go
376 lines (343 loc) · 10.7 KB
/
lua.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
367
368
369
370
371
372
373
374
375
376
package lua
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/gobuffalo/packr"
lua "github.com/yuin/gopher-lua"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
luajson "layeh.com/gopher-json"
appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
)
const (
incorrectReturnType = "expect %s output from Lua script, not %s"
invalidHealthStatus = "Lua returned an invalid health status"
resourceCustomizationBuiltInPath = "../../resource_customizations"
healthScriptFile = "health.lua"
actionScriptFile = "action.lua"
actionDiscoveryScriptFile = "discovery.lua"
)
var (
box packr.Box
)
func init() {
box = packr.NewBox(resourceCustomizationBuiltInPath)
}
// VM Defines a struct that implements the luaVM
type VM struct {
ResourceOverrides map[string]appv1.ResourceOverride
// UseOpenLibs flag to enable open libraries. Libraries are always disabled while running, but enabled during testing to allow the use of print statements
UseOpenLibs bool
}
func (vm VM) runLua(obj *unstructured.Unstructured, script string) (*lua.LState, error) {
l := lua.NewState(lua.Options{
SkipOpenLibs: !vm.UseOpenLibs,
})
defer l.Close()
// Opens table library to allow access to functions to manipulate tables
for _, pair := range []struct {
n string
f lua.LGFunction
}{
{lua.LoadLibName, lua.OpenPackage},
{lua.BaseLibName, lua.OpenBase},
{lua.TabLibName, lua.OpenTable},
// load our 'safe' version of the os library
{lua.OsLibName, OpenSafeOs},
} {
if err := l.CallByParam(lua.P{
Fn: l.NewFunction(pair.f),
NRet: 0,
Protect: true,
}, lua.LString(pair.n)); err != nil {
panic(err)
}
}
// preload our 'safe' version of the os library. Allows the 'local os = require("os")' to work
l.PreloadModule(lua.OsLibName, SafeOsLoader)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
l.SetContext(ctx)
objectValue := decodeValue(l, obj.Object)
l.SetGlobal("obj", objectValue)
err := l.DoString(script)
return l, err
}
// ExecuteHealthLua runs the lua script to generate the health status of a resource
func (vm VM) ExecuteHealthLua(obj *unstructured.Unstructured, script string) (*appv1.HealthStatus, error) {
l, err := vm.runLua(obj, script)
if err != nil {
return nil, err
}
returnValue := l.Get(-1)
if returnValue.Type() == lua.LTTable {
jsonBytes, err := luajson.Encode(returnValue)
if err != nil {
return nil, err
}
healthStatus := &appv1.HealthStatus{}
err = json.Unmarshal(jsonBytes, healthStatus)
if err != nil {
return nil, err
}
if !isValidHealthStatusCode(healthStatus.Status) {
return &appv1.HealthStatus{
Status: appv1.HealthStatusUnknown,
Message: invalidHealthStatus,
}, nil
}
return healthStatus, nil
}
return nil, fmt.Errorf(incorrectReturnType, "table", returnValue.Type().String())
}
// GetHealthScript attempts to read lua script from config and then filesystem for that resource
func (vm VM) GetHealthScript(obj *unstructured.Unstructured) (string, error) {
key := getConfigMapKey(obj)
if script, ok := vm.ResourceOverrides[key]; ok && script.HealthLua != "" {
return script.HealthLua, nil
}
return vm.getPredefinedLuaScripts(key, healthScriptFile)
}
func (vm VM) ExecuteResourceAction(obj *unstructured.Unstructured, script string) (*unstructured.Unstructured, error) {
l, err := vm.runLua(obj, script)
if err != nil {
return nil, err
}
returnValue := l.Get(-1)
if returnValue.Type() == lua.LTTable {
jsonBytes, err := luajson.Encode(returnValue)
if err != nil {
return nil, err
}
newObj, err := appv1.UnmarshalToUnstructured(string(jsonBytes))
if err != nil {
return nil, err
}
cleanedNewObj := cleanReturnedObj(newObj.Object, obj.Object)
newObj.Object = cleanedNewObj
return newObj, nil
}
return nil, fmt.Errorf(incorrectReturnType, "table", returnValue.Type().String())
}
// cleanReturnedObj Lua cannot distinguish an empty table as an array or map, and the library we are using choose to
// decoded an empty table into an empty array. This function prevents the lua scripts from unintentionally changing an
// empty struct into empty arrays
func cleanReturnedObj(newObj, obj map[string]interface{}) map[string]interface{} {
mapToReturn := newObj
for key := range obj {
if newValueInterface, ok := newObj[key]; ok {
oldValueInterface, ok := obj[key]
if !ok {
continue
}
switch newValue := newValueInterface.(type) {
case map[string]interface{}:
if oldValue, ok := oldValueInterface.(map[string]interface{}); ok {
convertedMap := cleanReturnedObj(newValue, oldValue)
mapToReturn[key] = convertedMap
}
case []interface{}:
switch oldValue := oldValueInterface.(type) {
case map[string]interface{}:
if len(newValue) == 0 {
mapToReturn[key] = oldValue
}
case []interface{}:
newArray := cleanReturnedArray(newValue, oldValue)
mapToReturn[key] = newArray
}
}
}
}
return mapToReturn
}
// cleanReturnedArray allows Argo CD to recurse into nested arrays when checking for unintentional empty struct to
// empty array conversions.
func cleanReturnedArray(newObj, obj []interface{}) []interface{} {
arrayToReturn := newObj
for i := range newObj {
switch newValue := newObj[i].(type) {
case map[string]interface{}:
if oldValue, ok := obj[i].(map[string]interface{}); ok {
convertedMap := cleanReturnedObj(newValue, oldValue)
arrayToReturn[i] = convertedMap
}
case []interface{}:
if oldValue, ok := obj[i].([]interface{}); ok {
convertedMap := cleanReturnedArray(newValue, oldValue)
arrayToReturn[i] = convertedMap
}
}
}
return arrayToReturn
}
func (vm VM) ExecuteResourceActionDiscovery(obj *unstructured.Unstructured, script string) ([]appv1.ResourceAction, error) {
l, err := vm.runLua(obj, script)
if err != nil {
return nil, err
}
returnValue := l.Get(-1)
if returnValue.Type() == lua.LTTable {
jsonBytes, err := luajson.Encode(returnValue)
if err != nil {
return nil, err
}
availableActions := make([]appv1.ResourceAction, 0)
if noAvailableActions(jsonBytes) {
return availableActions, nil
}
availableActionsMap := make(map[string]interface{})
err = json.Unmarshal(jsonBytes, &availableActionsMap)
if err != nil {
return nil, err
}
for key := range availableActionsMap {
value := availableActionsMap[key]
resourceAction := appv1.ResourceAction{Name: key, Disabled: isActionDisabled(value)}
if emptyResourceActionFromLua(value) {
availableActions = append(availableActions, resourceAction)
continue
}
resourceActionBytes, err := json.Marshal(value)
if err != nil {
return nil, err
}
err = json.Unmarshal(resourceActionBytes, &resourceAction)
if err != nil {
return nil, err
}
availableActions = append(availableActions, resourceAction)
}
return availableActions, err
}
return nil, fmt.Errorf(incorrectReturnType, "table", returnValue.Type().String())
}
// Actions are enabled by default
func isActionDisabled(actionsMap interface{}) bool {
actions, ok := actionsMap.(map[string]interface{})
if !ok {
return false
}
for key, val := range actions {
switch vv := val.(type) {
case bool:
if key == "disabled" {
return vv
}
}
}
return false
}
func emptyResourceActionFromLua(i interface{}) bool {
_, ok := i.([]interface{})
return ok
}
func noAvailableActions(jsonBytes []byte) bool {
// When the Lua script returns an empty table, it is decoded as a empty array.
return string(jsonBytes) == "[]"
}
func (vm VM) GetResourceActionDiscovery(obj *unstructured.Unstructured) (string, error) {
key := getConfigMapKey(obj)
override, ok := vm.ResourceOverrides[key]
if ok && override.Actions != "" {
actions, err := override.GetActions()
if err != nil {
return "", err
}
return actions.ActionDiscoveryLua, nil
}
discoveryKey := fmt.Sprintf("%s/actions/", key)
discoveryScript, err := vm.getPredefinedLuaScripts(discoveryKey, actionDiscoveryScriptFile)
if err != nil {
return "", err
}
return discoveryScript, nil
}
// GetResourceAction attempts to read lua script from config and then filesystem for that resource
func (vm VM) GetResourceAction(obj *unstructured.Unstructured, actionName string) (appv1.ResourceActionDefinition, error) {
key := getConfigMapKey(obj)
override, ok := vm.ResourceOverrides[key]
if ok && override.Actions != "" {
actions, err := override.GetActions()
if err != nil {
return appv1.ResourceActionDefinition{}, err
}
for _, action := range actions.Definitions {
if action.Name == actionName {
return action, nil
}
}
}
actionKey := fmt.Sprintf("%s/actions/%s", key, actionName)
actionScript, err := vm.getPredefinedLuaScripts(actionKey, actionScriptFile)
if err != nil {
return appv1.ResourceActionDefinition{}, err
}
return appv1.ResourceActionDefinition{
Name: actionName,
ActionLua: actionScript,
}, nil
}
func getConfigMapKey(obj *unstructured.Unstructured) string {
gvk := obj.GroupVersionKind()
if gvk.Group == "" {
return gvk.Kind
}
return fmt.Sprintf("%s/%s", gvk.Group, gvk.Kind)
}
func (vm VM) getPredefinedLuaScripts(objKey string, scriptFile string) (string, error) {
data, err := box.MustBytes(filepath.Join(objKey, scriptFile))
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
return string(data), nil
}
func isValidHealthStatusCode(statusCode string) bool {
switch statusCode {
case appv1.HealthStatusUnknown, appv1.HealthStatusProgressing, appv1.HealthStatusSuspended, appv1.HealthStatusHealthy, appv1.HealthStatusDegraded, appv1.HealthStatusMissing:
return true
}
return false
}
// Took logic from the link below and added the int, int32, and int64 types since the value would have type int64
// while actually running in the controller and it was not reproducible through testing.
// https://github.com/layeh/gopher-json/blob/97fed8db84274c421dbfffbb28ec859901556b97/json.go#L154
func decodeValue(L *lua.LState, value interface{}) lua.LValue {
switch converted := value.(type) {
case bool:
return lua.LBool(converted)
case float64:
return lua.LNumber(converted)
case string:
return lua.LString(converted)
case json.Number:
return lua.LString(converted)
case int:
return lua.LNumber(converted)
case int32:
return lua.LNumber(converted)
case int64:
return lua.LNumber(converted)
case []interface{}:
arr := L.CreateTable(len(converted), 0)
for _, item := range converted {
arr.Append(decodeValue(L, item))
}
return arr
case map[string]interface{}:
tbl := L.CreateTable(0, len(converted))
for key, item := range converted {
tbl.RawSetH(lua.LString(key), decodeValue(L, item))
}
return tbl
case nil:
return lua.LNil
}
return lua.LNil
}