/
resource_converter.go
142 lines (125 loc) · 4.05 KB
/
resource_converter.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
package v2
import (
"errors"
"k8s.io/apimachinery/pkg/api/meta"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/resource"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/pkg/util"
)
type ResourceInfoConverter interface {
ResourceInfo(obj *unstructured.Unstructured, retryOnNoMatch bool) (*resource.Info, error)
}
type ResourceToInfoConverter interface {
ResourcesToInfos(resource []shared.Resource) ([]*resource.Info, error)
UnstructuredToInfos(obj []*unstructured.Unstructured) ([]*resource.Info, error)
}
type InfoToResourceConverter interface {
InfosToResources(resourceInfo []*resource.Info) []shared.Resource
}
func NewResourceToInfoConverter(
converter ResourceInfoConverter, defaultNamespace string,
) *DefaultResourceToInfoConverter {
return &DefaultResourceToInfoConverter{converter: converter, defaultNamespace: defaultNamespace}
}
type DefaultResourceToInfoConverter struct {
converter ResourceInfoConverter
defaultNamespace string
}
func NewInfoToResourceConverter() *DefaultInfoToResourceConverter {
return &DefaultInfoToResourceConverter{}
}
type DefaultInfoToResourceConverter struct{}
func (c *DefaultInfoToResourceConverter) InfosToResources(infos []*resource.Info) []shared.Resource {
resources := make([]shared.Resource, 0, len(infos))
for _, info := range infos {
var gvk apimetav1.GroupVersionKind
if info.Mapping != nil {
gvk = apimetav1.GroupVersionKind(info.ResourceMapping().GroupVersionKind)
} else {
gvk = apimetav1.GroupVersionKind(info.Object.GetObjectKind().GroupVersionKind())
}
resources = append(
resources, shared.Resource{
Name: info.Name,
Namespace: info.Namespace,
GroupVersionKind: gvk,
},
)
}
return resources
}
func (c *DefaultResourceToInfoConverter) ResourcesToInfos(resources []shared.Resource) ([]*resource.Info, error) {
current := make([]*resource.Info, 0, len(resources))
errs := make([]error, 0, len(resources))
for _, res := range resources {
resourceInfo, err := c.converter.ResourceInfo(res.ToUnstructured(), true)
if err != nil {
errs = append(errs, err)
continue
}
current = append(current, resourceInfo)
}
if len(errs) > 0 {
return current, errors.Join(errs...)
}
return current, nil
}
func (c *DefaultResourceToInfoConverter) UnstructuredToInfos(
resources []*unstructured.Unstructured,
) ([]*resource.Info, error) {
target := make([]*resource.Info, 0, len(resources))
errs := make([]error, 0, len(resources))
for _, obj := range resources {
resourceInfo, err := c.converter.ResourceInfo(obj, true)
if util.IsConnectionRelatedError(err) {
return nil, err
}
// if there is no match we will initialize the resource anyway, just without
// the mapping. This will cause the applier and mappings to fall back to unstructured
// if this apply fails, it will continue to fail until either the mapping is resolved
// correctly or the kind is present
if meta.IsNoMatchError(err) {
target = append(
target, &resource.Info{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
Object: obj,
ResourceVersion: obj.GetResourceVersion(),
},
)
continue
}
if err != nil {
errs = append(errs, err)
continue
}
target = append(target, resourceInfo)
}
if len(errs) > 0 {
return nil, errors.Join(errs...)
}
c.normaliseNamespaces(target)
return target, nil
}
// normaliseNamespaces is only a workaround for malformed resources, e.g. by bad charts or wrong type configs.
func (c *DefaultResourceToInfoConverter) normaliseNamespaces(infos []*resource.Info) {
for _, info := range infos {
obj, ok := info.Object.(apimetav1.Object)
if !ok {
continue
}
if info.Namespaced() {
if info.Namespace == "" || obj.GetNamespace() == "" {
info.Namespace = c.defaultNamespace
obj.SetNamespace(c.defaultNamespace)
}
} else {
if info.Namespace != "" || obj.GetNamespace() != "" {
info.Namespace = ""
obj.SetNamespace("")
}
}
}
}