/
noop_converter.go
40 lines (34 loc) · 1.34 KB
/
noop_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
package manifestmerge
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// noopConverter is a converter that only sets the apiVersion fields, but does not real conversion.
type noopConverter struct {
}
func (c noopConverter) Convert(in, out, context interface{}) error {
realOut, err := c.convert(in.(runtime.Object), context.(schema.GroupVersion))
if err != nil {
return err
}
out = realOut
return nil
}
func (c noopConverter) ConvertToVersion(in runtime.Object, gv runtime.GroupVersioner) (out runtime.Object, err error) {
return in, nil
}
func (c noopConverter) ConvertFieldLabel(gvk schema.GroupVersionKind, label, value string) (string, string, error) {
return label, value, nil
}
// ConvertToVersion converts in object to the given gv in place and returns the same `in` object.
func (c noopConverter) convert(in runtime.Object, targetGV schema.GroupVersion) (runtime.Object, error) {
// Run the converter on the list items instead of list itself
if list, ok := in.(*unstructured.UnstructuredList); ok {
for i := range list.Items {
list.Items[i].SetGroupVersionKind(targetGV.WithKind(list.Items[i].GroupVersionKind().Kind))
}
}
in.GetObjectKind().SetGroupVersionKind(targetGV.WithKind(in.GetObjectKind().GroupVersionKind().Kind))
return in, nil
}