-
Notifications
You must be signed in to change notification settings - Fork 1
/
component.go
289 lines (254 loc) · 7.11 KB
/
component.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Gardener contributors.
//
// SPDX-License-Identifier: Apache-2.0
package ocm
import (
"fmt"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/compatattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/finalizer"
)
// options contains the options for creating a component archive.
type options struct {
provider string
providerLabels metav1.Labels
labels metav1.Labels
repositoryURL string
username string
token string
}
// ComponentOption is a function that configures a component options.
type ComponentOption func(*options)
// Component contains the information for managing a component.
// It is used to create a component archive, add resources and transfer the component
// to a repository.
type Component struct {
// Context is the context used for creating the component.
Context ocm.Context
// Name is the name of the component.
Name string
// Version is the version of the component.
Version string
// access is the component access.
access ocm.ComponentAccess
options
}
// WithProvider configures the provider of the component.
func WithProvider(provider string) ComponentOption {
return func(o *options) {
o.provider = provider
}
}
// WithProviderLabels configures the provider labels of the component.
func WithProviderLabels(labels metav1.Labels) ComponentOption {
return func(o *options) {
o.providerLabels = labels
}
}
// WithLabels configures the labels of the component.
func WithLabels(labels metav1.Labels) ComponentOption {
return func(o *options) {
o.labels = labels
}
}
// WithRepositoryURL configures the repository url of the component.
func WithRepositoryURL(repositoryURL string) ComponentOption {
return func(o *options) {
o.repositoryURL = repositoryURL
}
}
// WithUsername configures the username of the component.
func WithUsername(username string) ComponentOption {
return func(o *options) {
o.username = username
}
}
// WithToken configures the token of the component.
func WithToken(token string) ComponentOption {
return func(o *options) {
o.token = token
}
}
// NewComponent creates a new component.
func NewComponent(ctx ocm.Context, name, version string, opts ...ComponentOption) (*Component, error) {
options := &options{}
for _, opt := range opts {
opt(options)
}
c := &Component{
Context: ctx,
Name: name,
Version: version,
options: *options,
}
if err := c.configureCredentials(); err != nil {
return nil, err
}
return c, nil
}
// ResourceOptions contains the options for adding a resource to a component archive.
type ResourceOptions struct {
name string
path string
typ string
inputType string
version string
image string
componentName string
skipDigest bool
}
// ResourceOption is a function that configures a resource options.
type ResourceOption func(*ResourceOptions)
// WithComponentName configures the component name of the resource to be added.
func WithComponentName(component string) ResourceOption {
return func(o *ResourceOptions) {
o.componentName = component
}
}
// WithResourceImage configures the image of the resource.
func WithResourceImage(image string) ResourceOption {
return func(o *ResourceOptions) {
o.image = image
}
}
// WithResourceName configures the name of the resource.
func WithResourceName(name string) ResourceOption {
return func(o *ResourceOptions) {
o.name = name
}
}
// WithResourcePath configures the path of the resource.
func WithResourcePath(path string) ResourceOption {
return func(o *ResourceOptions) {
o.path = path
}
}
// WithResourceType configures the type of the resource.
func WithResourceType(typ string) ResourceOption {
return func(o *ResourceOptions) {
o.typ = typ
}
}
// WithResourceInputType configures the input type of the resource.
func WithResourceInputType(typ string) ResourceOption {
return func(o *ResourceOptions) {
o.inputType = typ
}
}
// WithResourceVersion configures the version of the resource.
func WithResourceVersion(version string) ResourceOption {
return func(o *ResourceOptions) {
o.version = version
}
}
// WithSkipVerify adds an option to skip the digest calculation of this resource.
func WithSkipVerify(skip bool) ResourceOption {
return func(o *ResourceOptions) {
o.skipDigest = skip
}
}
// AddResource adds a resource to a component archive.
// It accepts options for configuring the resource.
// The resource type can be one of the following:
// - file
// - ociImage
// - componentReference
func (c *Component) AddResource(opts ...ResourceOption) (rerr error) {
var finalize finalizer.Finalizer
defer finalize.FinalizeWithErrorPropagation(&rerr)
resOpt := &ResourceOptions{}
for _, opt := range opts {
opt(resOpt)
}
if resOpt.name == "" {
return fmt.Errorf("resource name must be set")
}
cv, err := c.access.LookupVersion(c.Version)
if err != nil {
if err != nil {
return err
}
}
defer finalize.Close(cv)
switch resOpt.typ {
case "file":
if resOpt.path == "" {
return fmt.Errorf("resource path must be set")
}
o := &addFileOpts{
name: resOpt.name,
path: resOpt.path,
version: resOpt.version,
}
if err := fileHandler(cv, c.Context, o); err != nil {
return err
}
case "ociImage":
o := &addImageOpts{
name: resOpt.name,
image: resOpt.image,
version: resOpt.version,
skipDigest: resOpt.skipDigest,
}
if err := imageHandler(cv, o); err != nil {
return err
}
case "componentReference":
o := &addReferenceOpts{
name: resOpt.name,
version: resOpt.version,
component: resOpt.componentName,
}
if err := referenceHandler(cv, o); err != nil {
return err
}
default:
return fmt.Errorf("unsupported resource type: %s", resOpt.typ)
}
if err := c.access.AddVersion(cv); err != nil {
return err
}
return nil
}
// AddToCTF adds a component to a ctf repository.
func (c *Component) AddToCTF(repo ocm.Repository) (rerr error) {
var finalize finalizer.Finalizer
defer finalize.FinalizeWithErrorPropagation(&rerr)
var err error
c.access, err = repo.LookupComponent(c.Name)
if err != nil {
return err
}
cv, err := c.access.LookupVersion(c.Version)
if err != nil {
cv, err = c.access.NewVersion(c.Version)
if err != nil {
return fmt.Errorf("failed to create component version %s: %w", c.Version, err)
}
}
finalize.Close(cv)
desc := cv.GetDescriptor()
desc.Name = c.Name
desc.Version = c.Version
desc.Provider.Name = metav1.ProviderName(c.provider)
desc.Provider.Labels = c.providerLabels
desc.Labels = c.labels
if !compatattr.Get(c.Context) {
desc.CreationTime = metav1.NewTimestampP()
}
err = compdesc.Validate(desc)
if err != nil {
return fmt.Errorf("cannot add component to ctf: %s", err)
}
err = c.access.AddVersion(cv)
if err != nil {
return fmt.Errorf("cannot add component to ctf: %s", err)
}
return nil
}
// Close closes the component access.
func (c *Component) Close() error {
return c.access.Close()
}