-
Notifications
You must be signed in to change notification settings - Fork 406
/
helper.go
52 lines (43 loc) · 1.03 KB
/
helper.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
package encoding
import (
"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
)
// Convert converts the from object into the to object
func Convert(from runtime.Object, to runtime.Object) error {
out, err := yaml.Marshal(from)
if err != nil {
return err
}
err = yaml.Unmarshal(out, to)
if err != nil {
return err
}
// remove api version and kind
t, err := meta.TypeAccessor(to)
if err != nil {
return err
}
t.SetAPIVersion("")
t.SetKind("")
return nil
}
// ConvertList converts the objects from the from list and puts them into the to list
func ConvertList(fromList runtime.Object, toList runtime.Object, new rest.Storage) error {
list, err := meta.ExtractList(fromList)
if err != nil {
return err
}
newItems := []runtime.Object{}
for _, item := range list {
newItem := new.New()
err = Convert(item, newItem)
if err != nil {
return err
}
newItems = append(newItems, newItem)
}
return meta.SetList(toList, newItems)
}