-
Notifications
You must be signed in to change notification settings - Fork 37
/
resource_with_images.go
59 lines (45 loc) · 1.15 KB
/
resource_with_images.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
package cmd
import (
"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
const (
ImagesAnnKey = "kbld.k14s.io/images"
)
type ResourceWithImages struct {
contents map[string]interface{}
images []Image
}
func NewResourceWithImages(contents map[string]interface{}, images []Image) ResourceWithImages {
return ResourceWithImages{contents, images}
}
func (r ResourceWithImages) Bytes() ([]byte, error) {
if len(r.images) > 0 {
resUn := unstructured.Unstructured{r.contents}
imagesYAML, err := yaml.Marshal(newImageStructs(r.images))
if err != nil {
return nil, err
}
anns := resUn.GetAnnotations()
if anns == nil {
anns = map[string]string{}
}
anns[ImagesAnnKey] = string(imagesYAML)
resUn.SetAnnotations(anns)
r.contents = resUn.Object
}
return yaml.Marshal(r.contents)
}
func (r ResourceWithImages) Images() ([]Image, error) {
resUn := unstructured.Unstructured{r.contents}
anns := resUn.GetAnnotations()
if anns == nil {
return nil, nil
}
var structs []imageStruct
err := yaml.Unmarshal([]byte(anns[ImagesAnnKey]), &structs)
if err != nil {
return nil, err
}
return newImages(structs), nil
}