-
Notifications
You must be signed in to change notification settings - Fork 20
/
type.go
120 lines (102 loc) · 3.17 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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package ocireg
import (
"fmt"
"net/url"
"strings"
"github.com/containerd/containerd/reference"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/oci/cpi"
"github.com/open-component-model/ocm/pkg/runtime"
)
const (
LegacyType = "ociRegistry"
Type = "OCIRegistry"
TypeV1 = Type + runtime.VersionSeparator + "v1"
ShortType = "oci"
ShortTypeV1 = ShortType + runtime.VersionSeparator + "v1"
)
func init() {
cpi.RegisterRepositoryType(LegacyType, cpi.NewRepositoryType(LegacyType, &RepositorySpec{}))
cpi.RegisterRepositoryType(Type, cpi.NewRepositoryType(Type, &RepositorySpec{}))
cpi.RegisterRepositoryType(TypeV1, cpi.NewRepositoryType(TypeV1, &RepositorySpec{}))
cpi.RegisterRepositoryType(ShortType, cpi.NewRepositoryType(ShortType, &RepositorySpec{}))
cpi.RegisterRepositoryType(ShortTypeV1, cpi.NewRepositoryType(ShortTypeV1, &RepositorySpec{}))
}
// Is checks the kind.
func Is(spec cpi.RepositorySpec) bool {
return spec != nil && spec.GetKind() == Type || spec.GetKind() == LegacyType
}
func IsKind(k string) bool {
return k == Type || k == LegacyType
}
// RepositorySpec describes an OCI registry interface backed by an oci registry.
type RepositorySpec struct {
runtime.ObjectVersionedType `json:",inline"`
// BaseURL is the base url of the repository to resolve artifacts.
BaseURL string `json:"baseUrl"`
LegacyTypes *bool `json:"legacyTypes,omitempty"`
}
var _ cpi.RepositorySpec = (*RepositorySpec)(nil)
// NewRepositorySpec creates a new RepositorySpec.
func NewRepositorySpec(baseURL string) *RepositorySpec {
return &RepositorySpec{
ObjectVersionedType: runtime.NewVersionedObjectType(Type),
BaseURL: baseURL,
}
}
func NewLegacyRepositorySpec(baseURL string) *RepositorySpec {
return &RepositorySpec{
ObjectVersionedType: runtime.NewVersionedObjectType(LegacyType),
BaseURL: baseURL,
}
}
func (a *RepositorySpec) GetType() string {
return Type
}
func (a *RepositorySpec) Name() string {
return a.BaseURL
}
func (a *RepositorySpec) UniformRepositorySpec() *cpi.UniformRepositorySpec {
return cpi.UniformRepositorySpecForHostURL(Type, a.BaseURL)
}
func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentials) (cpi.Repository, error) {
var u *url.URL
info := &RepositoryInfo{}
legacy := false
ref, err := reference.Parse(a.BaseURL)
if err == nil {
u, err = url.Parse("https://" + ref.Locator)
if err != nil {
return nil, err
}
info.Locator = ref.Locator
if ref.Object != "" {
return nil, fmt.Errorf("invalid repository locator %q", a.BaseURL)
}
} else {
u, err = url.Parse(a.BaseURL)
if err != nil {
return nil, err
}
info.Locator = u.Host
}
if a.LegacyTypes != nil {
legacy = *a.LegacyTypes
} else {
idx := strings.Index(info.Locator, "/")
host := info.Locator
if idx > 0 {
host = info.Locator[:idx]
}
if host == "docker.io" {
legacy = true
}
}
info.Scheme = u.Scheme
info.Creds = creds
info.Legacy = legacy
return NewRepository(ctx, a, info)
}