-
Notifications
You must be signed in to change notification settings - Fork 18
/
repository.go
202 lines (178 loc) · 5.53 KB
/
repository.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
package genericocireg
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"path"
"strings"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/general"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/contexts/oci"
ocicpi "github.com/open-component-model/ocm/pkg/contexts/oci/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi/repocpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/genericocireg/componentmapping"
)
type OCIBasedRepository interface {
OCIRepository() ocicpi.Repository
}
func GetOCIRepository(r cpi.Repository) ocicpi.Repository {
if o, ok := r.(OCIBasedRepository); ok {
return o.OCIRepository()
}
impl, err := repocpi.GetRepositoryImplementation(r)
if err != nil {
return nil
}
if o, ok := impl.(OCIBasedRepository); ok {
return o.OCIRepository()
}
return nil
}
type RepositoryImpl struct {
bridge repocpi.RepositoryBridge
ctx cpi.Context
meta ComponentRepositoryMeta
nonref cpi.Repository
ocirepo oci.Repository
readonly bool
}
var (
_ repocpi.RepositoryImpl = (*RepositoryImpl)(nil)
_ credentials.ConsumerIdentityProvider = (*RepositoryImpl)(nil)
)
func NewRepository(ctxp cpi.ContextProvider, meta *ComponentRepositoryMeta, ocirepo oci.Repository) cpi.Repository {
ctx := datacontext.InternalContextRef(ctxp.OCMContext())
impl := &RepositoryImpl{
ctx: ctx,
meta: *DefaultComponentRepositoryMeta(meta),
ocirepo: ocirepo,
}
return repocpi.NewRepository(impl, "OCM repo[OCI]")
}
func (r *RepositoryImpl) Close() error {
return r.ocirepo.Close()
}
func (r *RepositoryImpl) IsReadOnly() bool {
// TODO: extend OCI to query ReadOnly mode
return r.readonly
}
func (r *RepositoryImpl) SetReadOnly() {
r.readonly = true
}
func (r *RepositoryImpl) SetBridge(base repocpi.RepositoryBridge) {
r.bridge = base
r.nonref = repocpi.NewNoneRefRepositoryView(base)
}
func (r *RepositoryImpl) GetContext() cpi.Context {
return r.ctx
}
func (r *RepositoryImpl) GetConsumerId(uctx ...credentials.UsageContext) credentials.ConsumerIdentity {
prefix := r.meta.SubPath
if c, ok := general.Optional(uctx...).(credentials.StringUsageContext); ok {
prefix = path.Join(prefix, componentmapping.ComponentDescriptorNamespace, c.String())
}
if p, ok := r.ocirepo.(credentials.ConsumerIdentityProvider); ok {
return p.GetConsumerId(credentials.StringUsageContext(prefix))
}
return nil
}
func (r *RepositoryImpl) GetIdentityMatcher() string {
if p, ok := r.ocirepo.(credentials.ConsumerIdentityProvider); ok {
return p.GetIdentityMatcher()
}
return ""
}
func (r *RepositoryImpl) OCIRepository() ocicpi.Repository {
return r.ocirepo
}
func (r *RepositoryImpl) GetSpecification() cpi.RepositorySpec {
return &RepositorySpec{
RepositorySpec: r.ocirepo.GetSpecification(),
ComponentRepositoryMeta: r.meta,
}
}
func (r *RepositoryImpl) ComponentLister() cpi.ComponentLister {
if r.meta.ComponentNameMapping != OCIRegistryURLPathMapping {
return nil
}
lister := r.ocirepo.NamespaceLister()
if lister == nil {
return nil
}
return r
}
func (r *RepositoryImpl) NumComponents(prefix string) (int, error) {
lister := r.ocirepo.NamespaceLister()
if lister == nil {
return -1, errors.ErrNotSupported("component lister")
}
p := path.Join(r.meta.SubPath, componentmapping.ComponentDescriptorNamespace, prefix)
if strings.HasSuffix(prefix, "/") && !strings.HasSuffix(p, "/") {
p += "/"
}
return lister.NumNamespaces(p)
}
func (r *RepositoryImpl) GetComponents(prefix string, closure bool) ([]string, error) {
lister := r.ocirepo.NamespaceLister()
if lister == nil {
return nil, errors.ErrNotSupported("component lister")
}
p := path.Join(r.meta.SubPath, componentmapping.ComponentDescriptorNamespace)
compprefix := len(p) + 1
p = path.Join(p, prefix)
if strings.HasSuffix(prefix, "/") && !strings.HasSuffix(p, "/") {
p += "/"
}
tmp, err := lister.GetNamespaces(p, closure)
if err != nil {
return nil, err
}
result := make([]string, len(tmp))
for i, r := range tmp {
result[i] = r[compprefix:]
}
return result, nil
}
func (r *RepositoryImpl) GetOCIRepository() oci.Repository {
return r.ocirepo
}
func (r *RepositoryImpl) ExistsComponentVersion(name string, version string) (bool, error) {
namespace, err := r.MapComponentNameToNamespace(name)
if err != nil {
return false, err
}
a, err := r.ocirepo.LookupArtifact(namespace, version)
if err != nil {
return false, err
}
defer a.Close()
desc, err := a.Manifest()
if err != nil {
return false, err
}
switch desc.Config.MediaType {
case componentmapping.ComponentDescriptorConfigMimeType,
componentmapping.LegacyComponentDescriptorConfigMimeType,
componentmapping.Legacy2ComponentDescriptorConfigMimeType:
return true, nil
}
return false, nil
}
func (r *RepositoryImpl) LookupComponent(name string) (*repocpi.ComponentAccessInfo, error) {
return newComponentAccess(r, name, true)
}
func (r *RepositoryImpl) MapComponentNameToNamespace(name string) (string, error) {
switch r.meta.ComponentNameMapping {
case OCIRegistryURLPathMapping, "":
return path.Join(r.meta.SubPath, componentmapping.ComponentDescriptorNamespace, name), nil
case OCIRegistryDigestMapping:
h := sha256.New()
_, _ = h.Write([]byte(name))
return path.Join(r.meta.SubPath, hex.EncodeToString(h.Sum(nil))), nil
default:
return "", fmt.Errorf("unknown component name mapping method %s", r.meta.ComponentNameMapping)
}
}