This repository was archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathresource.go
90 lines (76 loc) · 2.08 KB
/
resource.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
package resource
import (
"fmt"
yaml "gopkg.in/yaml.v2"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/resource"
)
// -- unmarshaling code for specific object and field types
// struct to embed in objects, to provide default implementation
type baseObject struct {
source string
bytes []byte
Kind string `yaml:"kind"`
Meta struct {
Namespace string `yaml:"namespace"`
Name string `yaml:"name"`
Annotations map[string]string `yaml:"annotations,omitempty"`
} `yaml:"metadata"`
}
func (o baseObject) ResourceID() string {
ns := o.Meta.Namespace
if ns == "" {
ns = "default"
}
return fmt.Sprintf("%s %s/%s", o.Kind, ns, o.Meta.Name)
}
// It's useful for comparisons in tests to be able to remove the
// record of bytes
func (o *baseObject) debyte() {
o.bytes = nil
}
// ServiceIDs reports the services that depend on this resource.
func (o baseObject) ServiceIDs(all map[string]resource.Resource) []flux.ServiceID {
return nil
}
func (o baseObject) Annotations() map[string]string {
return o.Meta.Annotations
}
func (o baseObject) Source() string {
return o.source
}
func (o baseObject) Bytes() []byte {
return o.bytes
}
func unmarshalObject(source string, bytes []byte) (resource.Resource, error) {
var base = baseObject{source: source, bytes: bytes}
if err := yaml.Unmarshal(bytes, &base); err != nil {
return nil, err
}
switch base.Kind {
case "Deployment":
var dep = Deployment{baseObject: base}
if err := yaml.Unmarshal(bytes, &dep); err != nil {
return nil, err
}
return &dep, nil
case "Service":
var svc = Service{baseObject: base}
if err := yaml.Unmarshal(bytes, &svc); err != nil {
return nil, err
}
return &svc, nil
case "Namespace":
var ns = Namespace{baseObject: base}
if err := yaml.Unmarshal(bytes, &ns); err != nil {
return nil, err
}
return &ns, nil
// The remainder are things we have to care about, but not
// treat specially
default:
return &base, nil
}
}
// For reference, the Kubernetes v1 types are in:
// https://github.com/kubernetes/client-go/blob/master/pkg/api/v1/types.go