-
Notifications
You must be signed in to change notification settings - Fork 18
/
types.go
276 lines (232 loc) · 7.47 KB
/
types.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package v1
import (
"reflect"
"time"
"github.com/mandelsoft/goutils/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/equivalent"
)
// These constants describe identity attributes predefined by the
// model used to identify elements (resources, sources and references)
// in a component version.
const (
// SystemIdentityName is the name attribute of an element in
// a component version. It is always present.
SystemIdentityName = "name"
// SystemIdentityVersion is the version attribute optionally
// added to the identity of an element in a component version.
// It is required, if the name and the other explicitly defined
// extra identity attributes are not unique for a dedicated
// kind of element in the context of a component version.
SystemIdentityVersion = "version"
)
// Metadata defines the metadata of the component descriptor.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Metadata struct {
// Version is the schema version of the component descriptor.
Version string `json:"schemaVersion"`
}
// ProviderName describes the provider type of component in the origin's context.
// Defines whether the component is created by a third party or internally.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ProviderName string
// ResourceRelation describes the type of a resource.
// Defines whether the component is created by a third party or internally.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ResourceRelation string
const (
// LocalRelation defines a internal relation
// which describes a internally maintained resource in the origin's context.
LocalRelation ResourceRelation = "local"
// ExternalRelation defines a external relation
// which describes a resource maintained by a third party vendor in the origin's context.
ExternalRelation ResourceRelation = "external"
)
func ValidateRelation(fldPath *field.Path, relation ResourceRelation) *field.Error {
if len(relation) == 0 {
return field.Required(fldPath, "relation must be set")
}
if relation != LocalRelation && relation != ExternalRelation {
return field.NotSupported(fldPath, relation, []string{string(LocalRelation), string(ExternalRelation)})
}
return nil
}
const (
GROUP = "ocm.software"
KIND = "ComponentVersion"
)
// TypeMeta describes the schema of a descriptor.
type TypeMeta struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
}
// ObjectMeta defines the metadata of the component descriptor.
type ObjectMeta struct {
// Name is the name of the component.
Name string `json:"name"`
// Version is the version of the component.
Version string `json:"version"`
// Labels describe additional properties of the component version
Labels Labels `json:"labels,omitempty"`
// Provider described the component provider
Provider Provider `json:"provider"`
// CreationTime is the creation time of component version
// +optional
CreationTime *Timestamp `json:"creationTime,omitempty"`
}
func (o *ObjectMeta) Equal(obj interface{}) bool {
if e, ok := obj.(*ObjectMeta); ok {
if o.Name == e.Name &&
o.Version == e.Version &&
reflect.DeepEqual(o.Provider, e.Provider) &&
reflect.DeepEqual(o.Labels, e.Labels) {
return true
}
// check Creation time ?
}
return false
}
func (o ObjectMeta) Equivalent(a ObjectMeta) equivalent.EqualState {
state := equivalent.StateLocalHashEqual(o.Name == a.Name && o.Version == a.Version)
return state.Apply(
o.Provider.Equivalent(a.Provider),
o.Labels.Equivalent(a.Labels),
)
}
// GetName returns the name of the object.
func (o *ObjectMeta) GetName() string {
return o.Name
}
// SetName sets the name of the object.
func (o *ObjectMeta) SetName(name string) {
o.Name = name
}
// GetVersion returns the version of the object.
func (o ObjectMeta) GetVersion() string {
return o.Version
}
// SetVersion sets the version of the object.
func (o *ObjectMeta) SetVersion(version string) {
o.Version = version
}
// GetLabels returns the label of the object.
func (o ObjectMeta) GetLabels() Labels {
return o.Labels
}
// SetLabels sets the labels of the object.
func (o *ObjectMeta) SetLabels(labels []Label) {
o.Labels = labels
}
// GetName returns the name of the object.
func (o *ObjectMeta) Copy() *ObjectMeta {
return &ObjectMeta{
Name: o.Name,
Version: o.Version,
Labels: o.Labels.Copy(),
Provider: *o.Provider.Copy(),
CreationTime: o.CreationTime.DeepCopy(),
}
}
////////////////////////////////////////////////////////////////////////////////
// Provider describes the provider information of a component version.
type Provider struct {
Name ProviderName `json:"name"`
// Labels describe additional properties of provider
Labels Labels `json:"labels,omitempty"`
}
// GetName returns the name of the provider.
func (o Provider) GetName() ProviderName {
return o.Name
}
// SetName sets the name of the provider.
func (o *Provider) SetName(name ProviderName) {
o.Name = name
}
// GetLabels returns the label of the provider.
func (o Provider) GetLabels() Labels {
return o.Labels
}
// SetLabels sets the labels of the provider.
func (o *Provider) SetLabels(labels []Label) {
o.Labels = labels
}
// Copy copies the provider info.
func (o *Provider) Copy() *Provider {
return &Provider{
Name: o.Name,
Labels: o.Labels.Copy(),
}
}
func (o Provider) Equivalent(a Provider) equivalent.EqualState {
state := equivalent.StateLocalHashEqual(o.Name == a.Name)
return state.Apply(o.Labels.Equivalent(a.Labels))
}
////////////////////////////////////////////////////////////////////////////////
type _time = v1.Time
// Timestamp is time rounded to seconds.
// +k8s:deepcopy-gen=true
type Timestamp struct {
_time `json:",inline"`
}
func NewTimestamp() Timestamp {
return Timestamp{
_time: v1.NewTime(time.Now().UTC().Round(time.Second)),
}
}
func NewTimestampP() *Timestamp {
return &Timestamp{
_time: v1.NewTime(time.Now().UTC().Round(time.Second)),
}
}
func NewTimestampFor(t time.Time) Timestamp {
return Timestamp{
_time: v1.NewTime(t.UTC().Round(time.Second)),
}
}
func NewTimestampPFor(t time.Time) *Timestamp {
return &Timestamp{
_time: v1.NewTime(t.UTC().Round(time.Second)),
}
}
// MarshalJSON implements the json.Marshaler interface.
// The time is a quoted string in RFC 3339 format, with sub-second precision added if present.
func (t Timestamp) MarshalJSON() ([]byte, error) {
if y := t.Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
}
b := make([]byte, 0, len(time.RFC3339)+2)
b = append(b, '"')
b = t.AppendFormat(b, time.RFC3339)
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format.
func (t *Timestamp) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
// Fractional seconds are handled implicitly by Parse.
tt, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
*t = NewTimestampFor(tt)
return err
}
func (t Timestamp) String() string {
return t.Format(time.RFC3339)
}
func (t *Timestamp) Time() time.Time {
return t._time.Time
}
func (t *Timestamp) Equal(o Timestamp) bool {
return t._time.Equal(&o._time)
}
func (t *Timestamp) Add(d time.Duration) Timestamp {
return NewTimestampFor(t._time.Add(d))
}