-
Notifications
You must be signed in to change notification settings - Fork 17
/
componentdescriptor.go
706 lines (605 loc) · 17.6 KB
/
componentdescriptor.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package compdesc
import (
"fmt"
"reflect"
"strings"
metav1 "github.com/open-component-model/ocm/v2/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/v2/pkg/errors"
"github.com/open-component-model/ocm/v2/pkg/runtime"
)
const InternalSchemaVersion = "internal"
var NotFound = errors.ErrNotFound()
const KIND_REFERENCE = "component reference"
type (
ObjectMeta = metav1.ObjectMeta
Provider = metav1.Provider
)
const ComponentDescriptorFileName = "component-descriptor.yaml"
// Metadata defines the configured metadata of the component descriptor.
// It is taken from the original serialization format. It can be set
// to define a default serialization version.
type Metadata struct {
ConfiguredVersion string `json:"configuredSchemaVersion"`
}
// ComponentDescriptor defines a versioned component with a source and dependencies.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ComponentDescriptor struct {
// Metadata specifies the schema version of the component.
Metadata Metadata `json:"meta"`
// Spec contains the specification of the component.
ComponentSpec `json:"component"`
// Signatures contains a list of signatures for the ComponentDescriptor
Signatures metav1.Signatures `json:"signatures,omitempty"`
// NestedDigets describe calculated resource digests for aggregated
// component versions, which might not be persisted, but incorporated
// into signatures of the actual component version
NestedDigests metav1.NestedDigests `json:"nestedDigests,omitempty"`
}
func New(name, version string) *ComponentDescriptor {
return DefaultComponent(&ComponentDescriptor{
Metadata: Metadata{
ConfiguredVersion: "v2",
},
ComponentSpec: ComponentSpec{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Version: version,
Provider: metav1.Provider{
Name: "acme",
},
},
},
})
}
// SchemaVersion returns the scheme version configured in the representation.
func (cd *ComponentDescriptor) SchemaVersion() string {
return cd.Metadata.ConfiguredVersion
}
func (cd *ComponentDescriptor) Copy() *ComponentDescriptor {
out := &ComponentDescriptor{
Metadata: cd.Metadata,
ComponentSpec: ComponentSpec{
ObjectMeta: *cd.ObjectMeta.Copy(),
RepositoryContexts: cd.RepositoryContexts.Copy(),
Sources: cd.Sources.Copy(),
References: cd.References.Copy(),
Resources: cd.Resources.Copy(),
},
Signatures: cd.Signatures.Copy(),
NestedDigests: cd.NestedDigests.Copy(),
}
return out
}
func (cd *ComponentDescriptor) Reset() {
cd.Provider.Name = ""
cd.Provider.Labels = nil
cd.Resources = nil
cd.Sources = nil
cd.References = nil
cd.RepositoryContexts = nil
cd.Signatures = nil
cd.Labels = nil
DefaultComponent(cd)
}
// ComponentSpec defines a virtual component with
// a repository context, source and dependencies.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ComponentSpec struct {
metav1.ObjectMeta `json:",inline"`
// RepositoryContexts defines the previous repositories of the component
RepositoryContexts runtime.UnstructuredTypedObjectList `json:"repositoryContexts"`
// Sources defines sources that produced the component
Sources Sources `json:"sources"`
// References references component dependencies that can be resolved in the current context.
References References `json:"componentReferences"`
// Resources defines all resources that are created by the component and by a third party.
Resources Resources `json:"resources"`
}
const (
SystemIdentityName = "name"
SystemIdentityVersion = "version"
)
// ElementMeta defines a object that is uniquely identified by its identity.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ElementMeta struct {
// Name is the context unique name of the object.
Name string `json:"name"`
// Version is the semver version of the object.
Version string `json:"version"`
// ExtraIdentity is the identity of an object.
// An additional label with key "name" ist not allowed
ExtraIdentity metav1.Identity `json:"extraIdentity,omitempty"`
// Labels defines an optional set of additional labels
// describing the object.
// +optional
Labels metav1.Labels `json:"labels,omitempty"`
}
// GetName returns the name of the object.
func (o *ElementMeta) GetName() string {
return o.Name
}
// SetName sets the name of the object.
func (o *ElementMeta) SetName(name string) {
o.Name = name
}
// GetVersion returns the version of the object.
func (o *ElementMeta) GetVersion() string {
return o.Version
}
// SetVersion sets the version of the object.
func (o *ElementMeta) SetVersion(version string) {
o.Version = version
}
// GetLabels returns the label of the object.
func (o *ElementMeta) GetLabels() metav1.Labels {
return o.Labels
}
// SetLabels sets the labels of the object.
func (o *ElementMeta) SetLabels(labels []metav1.Label) {
o.Labels = labels
}
// SetLabel sets a single label to an effective value.
// If the value is no byte slice, it is marshaled.
func (o *ElementMeta) SetLabel(name string, value interface{}) error {
return o.Labels.Set(name, value)
}
// RemoveLabel removes a single label.
func (o *ElementMeta) RemoveLabel(name string) bool {
return o.Labels.Remove(name)
}
// SetExtraIdentity sets the identity of the object.
func (o *ElementMeta) SetExtraIdentity(identity metav1.Identity) {
o.ExtraIdentity = identity
}
// GetIdentity returns the identity of the object.
func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
if accessor != nil {
found := false
l := accessor.Len()
for i := 0; i < l; i++ {
m := accessor.Get(i).GetMeta()
if m.Name == o.Name && m.ExtraIdentity.Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version
break
}
found = true
}
}
}
return identity
}
// GetRawIdentity returns the identity plus version.
func (o *ElementMeta) GetRawIdentity() metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
if o.Version != "" {
identity[SystemIdentityVersion] = o.Version
}
return identity
}
// GetMatchBaseIdentity returns all possible identity attributes for resource matching.
func (o *ElementMeta) GetMatchBaseIdentity() metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
identity[SystemIdentityVersion] = o.Version
return identity
}
// GetIdentityDigest returns the digest of the object's identity.
func (o *ElementMeta) GetIdentityDigest(accessor ElementAccessor) []byte {
return o.GetIdentity(accessor).Digest()
}
func (o *ElementMeta) Copy() *ElementMeta {
if o == nil {
return nil
}
return &ElementMeta{
Name: o.Name,
Version: o.Version,
ExtraIdentity: o.ExtraIdentity.Copy(),
Labels: o.Labels.Copy(),
}
}
// NameAccessor describes a accessor for a named object.
type NameAccessor interface {
// GetName returns the name of the object.
GetName() string
// SetName sets the name of the object.
SetName(name string)
}
// VersionAccessor describes a accessor for a versioned object.
type VersionAccessor interface {
// GetVersion returns the version of the object.
GetVersion() string
// SetVersion sets the version of the object.
SetVersion(version string)
}
// LabelsAccessor describes a accessor for a labeled object.
type LabelsAccessor interface {
// GetLabels returns the labels of the object.
GetLabels() metav1.Labels
// SetLabels sets the labels of the object.
SetLabels(labels []metav1.Label)
}
// ObjectMetaAccessor describes a accessor for named and versioned object.
type ObjectMetaAccessor interface {
NameAccessor
VersionAccessor
LabelsAccessor
}
// ElementMetaAccessor provides generic access an elements meta information.
type ElementMetaAccessor interface {
GetMeta() *ElementMeta
IsEquivalent(ElementMetaAccessor) bool
}
// ElementAccessor provides generic access to list of elements.
type ElementAccessor interface {
Len() int
Get(i int) ElementMetaAccessor
}
// ElementArtifactAccessor provides access to generic artifact information of an element.
type ElementArtifactAccessor interface {
ElementMetaAccessor
GetAccess() AccessSpec
}
// ArtifactAccessor provides generic access to list of artifacts.
// There are resources or sources.
type ArtifactAccessor interface {
Len() int
GetArtifact(i int) ElementArtifactAccessor
}
// ArtifactAccess provides access to a dedicated kind of artifact set
// in the component descriptor (resources or sources).
type ArtifactAccess func(cd *ComponentDescriptor) ArtifactAccessor
// AccessSpec is an abstract specification of an access method
// The outbound object is typicall a runtime.UnstructuredTypedObject.
// Inbound any serializable AccessSpec implementation is possible.
type AccessSpec interface {
runtime.VersionedTypedObject
}
// GenericAccessSpec returns a generic AccessSpec implementation for an unstructured object.
// It can always be used instead of a dedicated access spec implementation. The core
// methods will map these spec into effective ones before an access is returned to the caller.
func GenericAccessSpec(un *runtime.UnstructuredTypedObject) AccessSpec {
return &runtime.UnstructuredVersionedTypedObject{
*un.DeepCopy(),
}
}
// Sources describes a set of source specifications.
type Sources []Source
var _ ElementAccessor = Sources{}
func SourceArtifacts(cd *ComponentDescriptor) ArtifactAccessor {
return cd.Sources
}
func (s Sources) Len() int {
return len(s)
}
func (s Sources) Get(i int) ElementMetaAccessor {
return &s[i]
}
func (s Sources) GetArtifact(i int) ElementArtifactAccessor {
return &s[i]
}
func (s Sources) Copy() Sources {
if s == nil {
return nil
}
out := make(Sources, len(s))
for i, v := range s {
out[i] = *v.Copy()
}
return out
}
// Source is the definition of a component's source.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Source struct {
SourceMeta `json:",inline"`
Access AccessSpec `json:"access"`
}
func (s *Source) GetMeta() *ElementMeta {
return &s.ElementMeta
}
func (r *Source) IsEquivalent(e ElementMetaAccessor) bool {
if o, ok := e.(*Source); !ok {
return false
} else {
if !reflect.DeepEqual(&r.ElementMeta, &o.ElementMeta) {
return false
}
if !reflect.DeepEqual(&r.Access, &o.Access) {
return false
}
return r.Type == o.Type
}
}
func (s *Source) GetAccess() AccessSpec {
return s.Access
}
func (s *Source) Copy() *Source {
return &Source{
SourceMeta: *s.SourceMeta.Copy(),
Access: s.Access,
}
}
// SourceMeta is the definition of the meta data of a source.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type SourceMeta struct {
ElementMeta
// Type describes the type of the object.
Type string `json:"type"`
}
// GetType returns the type of the object.
func (o *SourceMeta) GetType() string {
return o.Type
}
// SetType sets the type of the object.
func (o *SourceMeta) SetType(ttype string) {
o.Type = ttype
}
// Copy copies a source meta.
func (o *SourceMeta) Copy() *SourceMeta {
if o == nil {
return nil
}
return &SourceMeta{
ElementMeta: *o.ElementMeta.Copy(),
Type: o.Type,
}
}
// SourceRef defines a reference to a source
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type SourceRef struct {
// IdentitySelector defines the identity that is used to match a source.
IdentitySelector metav1.StringMap `json:"identitySelector,omitempty"`
// Labels defines an optional set of additional labels
// describing the object.
// +optional
Labels metav1.Labels `json:"labels,omitempty"`
}
// Copy copy a source ref.
func (r *SourceRef) Copy() *SourceRef {
if r == nil {
return nil
}
return &SourceRef{
IdentitySelector: r.IdentitySelector.Copy(),
Labels: r.Labels.Copy(),
}
}
type SourceRefs []SourceRef
// Copy copies a list of source refs.
func (r SourceRefs) Copy() SourceRefs {
if r == nil {
return nil
}
result := make(SourceRefs, len(r))
for i, v := range r {
result[i] = *v.Copy()
}
return result
}
// Resources describes a set of resource specifications.
type Resources []Resource
var _ ElementAccessor = Resources{}
func ResourceArtifacts(cd *ComponentDescriptor) ArtifactAccessor {
return cd.Resources
}
func (r Resources) Len() int {
return len(r)
}
func (r Resources) Get(i int) ElementMetaAccessor {
return &r[i]
}
func (r Resources) GetArtifact(i int) ElementArtifactAccessor {
return &r[i]
}
func (r Resources) Copy() Resources {
if r == nil {
return nil
}
out := make(Resources, len(r))
for i, v := range r {
out[i] = *v.Copy()
}
return out
}
// Resource describes a resource dependency of a component.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Resource struct {
ResourceMeta `json:",inline"`
// Access describes the type specific method to
// access the defined resource.
Access AccessSpec `json:"access"`
}
func (r *Resource) GetMeta() *ElementMeta {
return &r.ElementMeta
}
func (r *Resource) IsEquivalent(e ElementMetaAccessor) bool {
if o, ok := e.(*Resource); !ok {
return false
} else {
if !reflect.DeepEqual(&r.ElementMeta, &o.ElementMeta) {
return false
}
if !reflect.DeepEqual(&r.Access, &o.Access) {
return false
}
return r.Type == o.Type &&
r.Relation == o.Relation &&
reflect.DeepEqual(r.SourceRef, o.SourceRef)
}
}
func (r *Resource) GetAccess() AccessSpec {
return r.Access
}
func (r *Resource) Copy() *Resource {
return &Resource{
ResourceMeta: *r.ResourceMeta.Copy(),
Access: r.Access,
}
}
// ResourceMeta describes the meta data of a resource.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ResourceMeta struct {
ElementMeta `json:",inline"`
// Type describes the type of the object.
Type string `json:"type"`
// Relation describes the relation of the resource to the component.
// Can be a local or external resource
Relation metav1.ResourceRelation `json:"relation,omitempty"`
// SourceRef defines a list of source names.
// These names reference the sources defines in `component.sources`.
SourceRef SourceRefs `json:"srcRef,omitempty"`
// Digest is the optional digest of the referenced resource.
// +optional
Digest *metav1.DigestSpec `json:"digest,omitempty"`
}
// HashEqual indicates whether the digest hash would be equal.
// Excluded: Labels
// Adapt together with version specific hash excludes.
func (o *ResourceMeta) HashEqual(r *ResourceMeta) bool {
if o.Type != r.Type {
return false
}
if o.Relation != r.Relation {
return false
}
if o.ElementMeta.Name != r.ElementMeta.Name {
return false
}
if o.ElementMeta.Version != r.ElementMeta.Version {
return false
}
if o.Digest != nil && !reflect.DeepEqual(o.Digest, r.Digest) {
return false
}
if !reflect.DeepEqual(o.ElementMeta.ExtraIdentity, r.ElementMeta.ExtraIdentity) {
return false
}
return true
}
// GetType returns the type of the object.
func (o *ResourceMeta) GetType() string {
return o.Type
}
// SetType sets the type of the object.
func (o *ResourceMeta) SetType(ttype string) {
o.Type = ttype
}
// Copy copies a resource meta.
func (o *ResourceMeta) Copy() *ResourceMeta {
if o == nil {
return nil
}
r := &ResourceMeta{
ElementMeta: *o.ElementMeta.Copy(),
Type: o.Type,
Relation: o.Relation,
SourceRef: o.SourceRef.Copy(),
Digest: o.Digest.Copy(),
}
return r
}
func NewResourceMeta(name string, typ string, relation metav1.ResourceRelation) *ResourceMeta {
return &ResourceMeta{
ElementMeta: ElementMeta{Name: name},
Type: typ,
Relation: relation,
}
}
type References []ComponentReference
func (r References) Len() int {
return len(r)
}
func (r References) Get(i int) ElementMetaAccessor {
return &r[i]
}
func (r References) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r References) Less(i, j int) bool {
c := strings.Compare(r[i].Name, r[j].Name)
if c != 0 {
return c < 0
}
return strings.Compare(r[i].Version, r[j].Version) < 0
}
func (r References) Copy() References {
if r == nil {
return nil
}
out := make(References, len(r))
for i, v := range r {
out[i] = *v.Copy()
}
return out
}
// ComponentReference describes the reference to another component in the registry.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type ComponentReference struct {
ElementMeta `json:",inline"`
// ComponentName describes the remote name of the referenced object
ComponentName string `json:"componentName"`
// Digest is the optional digest of the referenced component.
// +optional
Digest *metav1.DigestSpec `json:"digest,omitempty"`
}
func NewComponentReference(name, componentName, version string, extraIdentity metav1.Identity) *ComponentReference {
return &ComponentReference{
ElementMeta: ElementMeta{
Name: name,
Version: version,
ExtraIdentity: extraIdentity,
},
ComponentName: componentName,
}
}
func (r ComponentReference) String() string {
return fmt.Sprintf("%s[%s:%s]", r.Name, r.ComponentName, r.Version)
}
func (r *ComponentReference) GetMeta() *ElementMeta {
return &r.ElementMeta
}
func (r *ComponentReference) IsEquivalent(e ElementMetaAccessor) bool {
if o, ok := e.(*ComponentReference); !ok {
return false
} else {
if !reflect.DeepEqual(&r.ElementMeta, &o.ElementMeta) {
return false
}
return r.ComponentName == o.ComponentName
}
}
func (r *ComponentReference) GetComponentName() string {
return r.ComponentName
}
func (r *ComponentReference) Copy() *ComponentReference {
return &ComponentReference{
ElementMeta: *r.ElementMeta.Copy(),
ComponentName: r.ComponentName,
Digest: r.Digest.Copy(),
}
}