-
Notifications
You must be signed in to change notification settings - Fork 18
/
type.go
175 lines (146 loc) · 5.28 KB
/
type.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package genericocireg
import (
"encoding/json"
"github.com/sirupsen/logrus"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/oci"
"github.com/open-component-model/ocm/pkg/contexts/oci/repositories/ocireg"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/localblob"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
)
// ComponentNameMapping describes the method that is used to map the "Component Name", "Component Version"-tuples
// to OCI Image References.
type ComponentNameMapping string
const (
Type = ocireg.Type
TypeV1 = ocireg.TypeV1
OCIRegistryURLPathMapping ComponentNameMapping = "urlPath"
OCIRegistryDigestMapping ComponentNameMapping = "sha256-digest"
)
func init() {
cpi.RegisterOCIImplementation(func(ctx oci.Context) (cpi.RepositoryType, error) {
return NewRepositoryType(ctx), nil
})
}
// ComponentRepositoryMeta describes config special for a mapping of
// a component repository to an oci registry.
type ComponentRepositoryMeta struct {
// ComponentNameMapping describes the method that is used to map the "Component Name", "Component Version"-tuples
// to OCI Image References.
ComponentNameMapping ComponentNameMapping `json:"componentNameMapping,omitempty"`
SubPath string `json:"subPath,omitempty"`
}
func NewComponentRepositoryMeta(subPath string, mapping ComponentNameMapping) *ComponentRepositoryMeta {
return &ComponentRepositoryMeta{
ComponentNameMapping: mapping,
SubPath: subPath,
}
}
type RepositoryType struct {
runtime.ObjectVersionedType
ocictx oci.Context
}
var _ cpi.RepositoryType = &RepositoryType{}
// NewRepositoryType creates generic type for any OCI Repository Backend.
func NewRepositoryType(ocictx oci.Context) *RepositoryType {
return &RepositoryType{
ObjectVersionedType: runtime.NewVersionedObjectType("genericOCIRepositoryBackend"),
ocictx: ocictx,
}
}
func (t *RepositoryType) Decode(data []byte, unmarshal runtime.Unmarshaler) (runtime.TypedObject, error) {
ospec, err := t.ocictx.RepositoryTypes().DecodeRepositorySpec(data, unmarshal)
if err != nil {
return nil, err
}
meta := &ComponentRepositoryMeta{}
if unmarshal == nil {
unmarshal = runtime.DefaultYAMLEncoding.Unmarshaler
}
err = unmarshal.Unmarshal(data, meta)
if err != nil {
return nil, errors.Wrapf(err, "cannot unmarshal component repository meta information")
}
return NewRepositorySpec(ospec, meta), nil
}
func (t *RepositoryType) LocalSupportForAccessSpec(ctx cpi.Context, a compdesc.AccessSpec) bool {
name := a.GetKind()
return name == localblob.Type
}
////////////////////////////////////////////////////////////////////////////////
type RepositorySpec struct {
oci.RepositorySpec
ComponentRepositoryMeta
}
var (
_ cpi.RepositorySpec = (*RepositorySpec)(nil)
_ cpi.PrefixProvider = (*RepositorySpec)(nil)
_ cpi.IntermediateRepositorySpecAspect = (*RepositorySpec)(nil)
)
func NewRepositorySpec(spec oci.RepositorySpec, meta *ComponentRepositoryMeta) *RepositorySpec {
return &RepositorySpec{
RepositorySpec: spec,
ComponentRepositoryMeta: *DefaultComponentRepositoryMeta(meta),
}
}
func (a *RepositorySpec) PathPrefix() string {
return a.SubPath
}
func (a *RepositorySpec) IsIntermediate() bool {
if s, ok := a.RepositorySpec.(oci.IntermediateRepositorySpecAspect); ok {
return s.IsIntermediate()
}
return false
}
// TODO: host etc is missing
func (a *RepositorySpec) AsUniformSpec(cpi.Context) cpi.UniformRepositorySpec {
return cpi.UniformRepositorySpec{Type: a.GetKind(), SubPath: a.SubPath}
}
func (u *RepositorySpec) UnmarshalJSON(data []byte) error {
logrus.Debugf("unmarshal generic ocireg spec %s\n", string(data))
ocispec := &oci.GenericRepositorySpec{}
if err := json.Unmarshal(data, ocispec); err != nil {
return err
}
compmeta := &ComponentRepositoryMeta{}
if err := json.Unmarshal(data, ocispec); err != nil {
return err
}
u.RepositorySpec = ocispec
u.ComponentRepositoryMeta = *compmeta
return nil
}
// MarshalJSON implements a custom json unmarshal method for a unstructured type.
func (u RepositorySpec) MarshalJSON() ([]byte, error) {
ocispec, err := runtime.ToUnstructuredTypedObject(u.RepositorySpec)
if err != nil {
return nil, err
}
compmeta, err := runtime.ToUnstructuredObject(u.ComponentRepositoryMeta)
if err != nil {
return nil, err
}
return json.Marshal(compmeta.FlatMerge(ocispec.Object))
}
func (s *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentials) (cpi.Repository, error) {
r, err := s.RepositorySpec.Repository(ctx.OCIContext(), creds)
if err != nil {
return nil, err
}
return NewRepository(ctx, &s.ComponentRepositoryMeta, r)
}
func DefaultComponentRepositoryMeta(meta *ComponentRepositoryMeta) *ComponentRepositoryMeta {
if meta == nil {
meta = &ComponentRepositoryMeta{}
}
if meta.ComponentNameMapping == "" {
meta.ComponentNameMapping = OCIRegistryURLPathMapping
}
return meta
}