-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper_convert.go
More file actions
74 lines (65 loc) · 1.86 KB
/
helper_convert.go
File metadata and controls
74 lines (65 loc) · 1.86 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
// SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0
package testing
import (
fnapi "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
func convertResultsToMap(r []*fnapi.Result) []map[string]interface{} {
if r == nil {
return nil
}
res := make([]map[string]interface{}, len(r))
for i, rr := range r {
if rr == nil {
continue
}
res[i] = map[string]interface{}{
"Message": rr.GetMessage(),
"Severity": rr.GetSeverity(),
}
}
return res
}
func convertResourcesMapToUnstructured(r map[string]*fnapi.Resource) map[string]*unstructured.Unstructured {
if r == nil {
return nil
}
res := map[string]*unstructured.Unstructured{}
for k, v := range r {
u := convertResourceToUnstructured(v)
// If the name annotation was the only annotation in the resource,
// delete the entire field to avoid creating unnecessary diffs.
if len(u.GetAnnotations()) == 0 {
u.SetAnnotations(nil)
}
res[k] = u
}
return res
}
func convertResourceToUnstructured(r *fnapi.Resource) *unstructured.Unstructured {
if r == nil {
return nil
}
u := &unstructured.Unstructured{}
if err := resource.AsObject(r.GetResource(), u); err != nil {
panic(err)
}
return u
}
func ConvertDesiredCompositeToObject(r *fnapi.RunFunctionResponse, o runtime.Object) {
if err := resource.AsObject(r.GetDesired().GetComposite().GetResource(), o); err != nil {
panic(err)
}
}
func ConvertDesiredResourceToObject(r *fnapi.RunFunctionResponse, name string, o runtime.Object) {
if obj, exists := r.GetDesired().GetResources()[name]; !exists {
panic("could not get resource from response: " + name)
} else {
if err := resource.AsObject(obj.GetResource(), o); err != nil {
panic(err)
}
}
}