-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource.go
136 lines (114 loc) · 3.23 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package v3
import (
"encoding/json"
"path"
"reflect"
"strings"
corev2 "github.com/sensu/core/v2"
)
var _ corev2.Resource = &V2ResourceProxy{}
// Resource represents a Sensu resource.
type Resource interface {
// GetMetadata returns the object metadata for the resource.
GetMetadata() *corev2.ObjectMeta
GeneratedType
}
var _ Resource = new(corev2.Asset)
// GeneratedType is an interface that specifies all the methods that are
// automatically generated.
type GeneratedType interface {
// SetMetadata sets metadata on its receiver, if the receiver has metadata
// to set. If the receiver does not have metadata to set, nothing happens.
SetMetadata(*corev2.ObjectMeta)
// StoreName gives the name of the resource as it pertains to a store.
StoreName() string
// RBACName describes the name of the resource for RBAC purposes.
RBACName() string
// URIPath gives the path to the resource, e.g. /checks/checkname
URIPath() string
// Validate checks if the fields in the resource are valid.
Validate() error
}
// GlobalResource is an interface for indicating
// a resource's namespace strategy
type GlobalResource interface {
// IsGlobalResource returns true when the resource
// is not namespaced.
IsGlobalResource() bool
}
// V2ResourceProxy is a compatibility shim for converting from a v3 resource to
// a v2 resource.
//sensu:nogen
type V2ResourceProxy struct {
Resource
}
func V3ToV2Resource(resource Resource) corev2.Resource {
return &V2ResourceProxy{Resource: resource}
}
func (v *V2ResourceProxy) GetObjectMeta() corev2.ObjectMeta {
meta := v.GetMetadata()
if meta == nil {
return corev2.ObjectMeta{
Labels: make(map[string]string),
Annotations: make(map[string]string),
}
}
return *meta
}
func (v *V2ResourceProxy) SetObjectMeta(meta corev2.ObjectMeta) {
v.SetMetadata(&meta)
}
func (v *V2ResourceProxy) SetNamespace(ns string) {
// SetNamespace expected to be a no-op for
// core/v2 Resources. sensu-go and sensu-go/types
// both depend on this property.
if gr, ok := v.Resource.(GlobalResource); ok {
if gr.IsGlobalResource() {
return
}
}
if v.GetMetadata() == nil {
v.SetMetadata(&corev2.ObjectMeta{
Labels: make(map[string]string),
Annotations: make(map[string]string),
})
}
v.GetMetadata().Namespace = ns
}
func (v *V2ResourceProxy) StorePrefix() string {
return v.StoreName()
}
func (v V2ResourceProxy) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Resource)
}
func (v *V2ResourceProxy) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &v.Resource)
}
// tmGetter is useful for types that want to explicitly provide their
// TypeMeta - this is useful for lifters.
type tmGetter interface {
GetTypeMeta() corev2.TypeMeta
}
func (v V2ResourceProxy) GetTypeMeta() corev2.TypeMeta {
var tm corev2.TypeMeta
if getter, ok := v.Resource.(tmGetter); ok {
tm = getter.GetTypeMeta()
} else {
typ := reflect.Indirect(reflect.ValueOf(v.Resource)).Type()
tm = corev2.TypeMeta{
Type: typ.Name(),
APIVersion: apiVersion(typ.PkgPath()),
}
}
return tm
}
func apiVersion(version string) string {
parts := strings.Split(version, "/")
if len(parts) == 0 {
return ""
}
if len(parts) == 1 {
return parts[0]
}
return path.Join(parts[len(parts)-2], parts[len(parts)-1])
}