-
Notifications
You must be signed in to change notification settings - Fork 3
/
manifest.go
46 lines (39 loc) · 1.06 KB
/
manifest.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
package objects
import (
"fmt"
"io"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
)
// Manifest represents a container for runtime objects that can be encoded into a valid Kubernetes manifest
type Manifest struct {
CoreList v1.List
}
// Append adds a runtime object to a Manifest
func (m *Manifest) Append(objects ...runtime.Object) {
for _, object := range objects {
m.CoreList.Items = append(m.CoreList.Items, runtime.RawExtension{
Object: object,
})
}
}
// Serialize encodes items in a manifest and writes them to a designated writer
func (m *Manifest) Serialize(stream io.Writer) error {
encoder := json.NewSerializerWithOptions(json.DefaultMetaFactory, nil, nil, json.SerializerOptions{
Yaml: true,
Pretty: true,
Strict: true,
})
for i, item := range m.CoreList.Items {
if err := encoder.Encode(item.Object, stream); err != nil {
return err
}
if i != len(m.CoreList.Items)-1 {
if _, err := fmt.Fprint(stream, "---\n"); err != nil {
return err
}
}
}
return nil
}