Skip to content

Commit 934d61d

Browse files
committed
image: Change SourceURL to Download
This better reflect the meaning. Also remove the union as there is currently only one option and we may never implement a second one. We retain the ability to add a second one if required.
1 parent 9b741f1 commit 934d61d

File tree

13 files changed

+107
-192
lines changed

13 files changed

+107
-192
lines changed

internal/controllers/image/upload.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,21 @@ func (r *orcImageReconciler) uploadImageContent(ctx context.Context, orcImage *o
8383
return err
8484
}
8585

86-
if content.SourceType != orcv1alpha1.ImageSourceTypeURL {
87-
// Should have been caught by validation
88-
return capoerrors.Terminal(orcv1alpha1.OpenStackConditionReasonInvalidConfiguration, "invalid image source type: "+string(content.SourceType))
89-
}
90-
91-
urlSource := content.SourceURL
92-
93-
if urlSource == nil {
86+
download := content.Download
87+
if download == nil {
9488
// Should have been caught by validation
9589
return capoerrors.Terminal(orcv1alpha1.OpenStackConditionReasonInvalidConfiguration, "image source type URL has no url entry")
9690
}
9791

98-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlSource.URL, http.NoBody)
92+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, download.URL, http.NoBody)
9993
if err != nil {
100-
return fmt.Errorf("error creating request for %s: %w", urlSource.URL, err)
94+
return fmt.Errorf("error creating request for %s: %w", download.URL, err)
10195
}
10296

10397
client := http.Client{}
10498
resp, err := client.Do(req)
10599
if err != nil {
106-
return fmt.Errorf("error requesting %s: %w", urlSource.URL, err)
100+
return fmt.Errorf("error requesting %s: %w", download.URL, err)
107101
}
108102
defer func() {
109103
err = errors.Join(err, resp.Body.Close())
@@ -114,9 +108,9 @@ func (r *orcImageReconciler) uploadImageContent(ctx context.Context, orcImage *o
114108
reader := newReaderWithProgress(resp.Body, r.downloadProgressReporter(ctx, orcImage, glanceImage, resp.ContentLength))
115109

116110
// If the content defines a hash, calculate the hash while downloading and verify it before returning a successful read to glance
117-
if urlSource.DownloadHash != nil {
118-
log.V(4).Info("will verify download hash", "algorithm", urlSource.DownloadHash.Algorithm, "value", urlSource.DownloadHash.Value)
119-
reader, err = newReaderWithHash(reader, urlSource.DownloadHash.Algorithm, r.hashVerifier(ctx, orcImage, urlSource.DownloadHash.Value))
111+
if download.Hash != nil {
112+
log.V(4).Info("will verify download hash", "algorithm", download.Hash.Algorithm, "value", download.Hash.Value)
113+
reader, err = newReaderWithHash(reader, download.Hash.Algorithm, r.hashVerifier(ctx, orcImage, download.Hash.Value))
120114
if err != nil {
121115
return err
122116
}
@@ -127,11 +121,11 @@ func (r *orcImageReconciler) uploadImageContent(ctx context.Context, orcImage *o
127121
reader = bufio.NewReaderSize(reader, transferBufferSizeBytes)
128122

129123
// If the content requires decompression, decompress before sending to glance
130-
if urlSource.Decompress != nil {
131-
log.V(4).Info("will decompress downladed content", "algorithm", *urlSource.Decompress)
132-
reader, err = newReaderWithDecompression(reader, *urlSource.Decompress)
124+
if download.Decompress != nil {
125+
log.V(4).Info("will decompress downladed content", "algorithm", *download.Decompress)
126+
reader, err = newReaderWithDecompression(reader, *download.Decompress)
133127
if err != nil {
134-
return fmt.Errorf("opening %s: %w", urlSource.URL, err)
128+
return fmt.Errorf("opening %s: %w", download.URL, err)
135129
}
136130
}
137131

internal/controllers/image/upload_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ var _ = Describe("Upload tests", Ordered, func() {
192192
Content: &orcv1alpha1.ImageContent{
193193
ContainerFormat: orcv1alpha1.ImageContainerFormatBare,
194194
DiskFormat: orcv1alpha1.ImageDiskFormatRaw,
195-
SourceType: orcv1alpha1.ImageSourceTypeURL,
196-
SourceURL: &orcv1alpha1.ImageContentSourceURL{
197-
URL: "http://" + fileServeAddr + "/" + imageName,
198-
Decompress: opts.compression,
199-
DownloadHash: opts.downloadHash,
195+
Download: &orcv1alpha1.ImageContentSourceDownload{
196+
URL: "http://" + fileServeAddr + "/" + imageName,
197+
Decompress: opts.compression,
198+
Hash: opts.downloadHash,
200199
},
201200
},
202201
},

internal/controllers/image/webdownload.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func (r *orcImageReconciler) canWebDownload(ctx context.Context, orcImage *orcv1
6363
return false, err
6464
}
6565

66-
urlSource := content.SourceURL
67-
if urlSource == nil {
66+
download := content.Download
67+
if download == nil {
6868
debugLog("not type URL")
6969
return false, nil
7070
}
@@ -73,7 +73,7 @@ func (r *orcImageReconciler) canWebDownload(ctx context.Context, orcImage *orcv1
7373
// Glance can be configured to do automatic decompression of imported
7474
// images, but there's no way to determine if it is enabled so it can't
7575
// be safely used.
76-
if urlSource.Decompress != nil {
76+
if download.Decompress != nil {
7777
debugLog("web-download does not support decompression")
7878
return false, nil
7979
}
@@ -89,7 +89,7 @@ func (r *orcImageReconciler) canWebDownload(ctx context.Context, orcImage *orcv1
8989
// determine, if the hash we have doesn't match the algorithm configured
9090
// server-side in glance, which is also not exposed via the API, we
9191
// can't verify it anyway.
92-
if urlSource.DownloadHash != nil {
92+
if download.Hash != nil {
9393
debugLog("web-download does not support hash verification")
9494
return false, nil
9595
}
@@ -125,6 +125,6 @@ func (r *orcImageReconciler) webDownload(ctx context.Context, orcImage *orcv1alp
125125

126126
return imageClient.CreateImport(ctx, glanceImage.ID, &imageimport.CreateOpts{
127127
Name: imageimport.WebDownloadMethod,
128-
URI: content.SourceURL.URL,
128+
URI: content.Download.URL,
129129
})
130130
}

orc/api/v1alpha1/image_types.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,6 @@ const (
214214
ImageCompressionBZ2 ImageCompression = "bz2"
215215
)
216216

217-
// +kubebuilder:validation:Enum:=URL
218-
type ImageContentSourceType string
219-
220-
const (
221-
ImageSourceTypeURL ImageContentSourceType = "URL"
222-
)
223-
224-
// ImageContent specifies the source of image data
225-
// +kubebuilder:validation:XValidation:rule="has(self.sourceType) && self.sourceType == 'URL' ? has(self.sourceURL) : !has(self.sourceURL)",message="sourceURL is required when sourceType is URL, and forbidden otherwise"
226-
// +union
227217
type ImageContent struct {
228218
// ContainerFormat is the format of the image container.
229219
// qcow2 and raw images do not usually have a container. This is specified as "bare", which is also the default.
@@ -239,19 +229,14 @@ type ImageContent struct {
239229
// +kubebuilder:validation:Required
240230
DiskFormat ImageDiskFormat `json:"diskFormat"`
241231

242-
// SourceType is the type of the image content source
243-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="sourceType is immutable"
244-
// +kubebuilder:validation:Required
245-
// +unionDiscriminator
246-
SourceType ImageContentSourceType `json:"sourceType"`
247-
248-
// SourceURL describes how to obtain image data by downloading it from a SourceURL. Must be set if Type is 'url'
249-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="sourceURL is immutable"
232+
// Download describes how to obtain image data by downloading it from a URL.
233+
// Must be set when creating a managed image.
234+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="download is immutable"
250235
// +unionMember
251-
SourceURL *ImageContentSourceURL `json:"sourceURL,omitempty"`
236+
Download *ImageContentSourceDownload `json:"download,omitempty"`
252237
}
253238

254-
type ImageContentSourceURL struct {
239+
type ImageContentSourceDownload struct {
255240
// URL containing image data
256241
// +kubebuilder:validation:Format=uri
257242
// +kubebuilder:validation:Required
@@ -264,14 +249,14 @@ type ImageContentSourceURL struct {
264249
// +optional
265250
Decompress *ImageCompression `json:"decompress,omitempty"`
266251

267-
// DownloadHash is a hash which will be used to verify downloaded data, i.e.
252+
// Hash is a hash which will be used to verify downloaded data, i.e.
268253
// before any decompression. If not specified, no hash verification will be
269-
// performed. Specifying a DownloadHash will disable the use of Glance's
254+
// performed. Specifying a Hash will disable the use of Glance's
270255
// web-download, as web-download cannot currently deterministically verify
271256
// the hash of downloaded content.
272-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="downloadHash is immutable"
257+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="hash is immutable"
273258
// +optional
274-
DownloadHash *ImageHash `json:"downloadHash,omitempty"`
259+
Hash *ImageHash `json:"hash,omitempty"`
275260
}
276261

277262
type ImageHash struct {

orc/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

orc/config/crd/bases/openstack.k-orc.cloud_images.yaml

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,10 @@ spec:
186186
x-kubernetes-validations:
187187
- message: diskFormat is immutable
188188
rule: self == oldSelf
189-
sourceType:
190-
description: SourceType is the type of the image content source
191-
enum:
192-
- URL
193-
type: string
194-
x-kubernetes-validations:
195-
- message: sourceType is immutable
196-
rule: self == oldSelf
197-
sourceURL:
198-
description: SourceURL describes how to obtain image data
199-
by downloading it from a SourceURL. Must be set if Type
200-
is 'url'
189+
download:
190+
description: |-
191+
Download describes how to obtain image data by downloading it from a URL.
192+
Must be set when creating a managed image.
201193
properties:
202194
decompress:
203195
description: |-
@@ -210,11 +202,11 @@ spec:
210202
- gz
211203
- bz2
212204
type: string
213-
downloadHash:
205+
hash:
214206
description: |-
215-
DownloadHash is a hash which will be used to verify downloaded data, i.e.
207+
Hash is a hash which will be used to verify downloaded data, i.e.
216208
before any decompression. If not specified, no hash verification will be
217-
performed. Specifying a DownloadHash will disable the use of Glance's
209+
performed. Specifying a Hash will disable the use of Glance's
218210
web-download, as web-download cannot currently deterministically verify
219211
the hash of downloaded content.
220212
properties:
@@ -240,7 +232,7 @@ spec:
240232
- value
241233
type: object
242234
x-kubernetes-validations:
243-
- message: downloadHash is immutable
235+
- message: hash is immutable
244236
rule: self == oldSelf
245237
url:
246238
description: URL containing image data
@@ -250,19 +242,14 @@ spec:
250242
- url
251243
type: object
252244
x-kubernetes-validations:
253-
- message: sourceURL is immutable
245+
- message: download is immutable
254246
rule: self == oldSelf
255247
required:
256248
- diskFormat
257-
- sourceType
258249
type: object
259250
x-kubernetes-validations:
260251
- message: content is immutable
261252
rule: self == oldSelf
262-
- message: sourceURL is required when sourceType is URL, and forbidden
263-
otherwise
264-
rule: 'has(self.sourceType) && self.sourceType == ''URL'' ? has(self.sourceURL)
265-
: !has(self.sourceURL)'
266253
name:
267254
description: |-
268255
ImageName will be the name of the created Glance image.

0 commit comments

Comments
 (0)