Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit a1855d1

Browse files
committed
refactor: use image destination variable
Use image destination that is provided by the user and not constructed from provided variables. Jira-Url: https://issues.redhat.com/browse/CNV-36060 Signed-off-by: Ben Oukhanov <[email protected]>
1 parent a0e29d7 commit a1855d1

File tree

8 files changed

+28
-49
lines changed

8 files changed

+28
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ KubeVirt Disk Uploader -> Download VM Disk -> Build New Container Disk -> Push T
1515
**Prerequisites**
1616

1717
1. Ensure Virtual Machine (VM) is powered off. Data from VM can be exported only when it is not used.
18-
2. Modify [kubevirt-disk-uploader](https://github.com/codingben/kubevirt-disk-uploader/blob/main/kubevirt-disk-uploader.yaml#L58) arguments (VM Name, Volume Name, New Container Disk Name, Enable or Disable System Preparation and Push Timeout).
18+
2. Modify [kubevirt-disk-uploader](https://github.com/codingben/kubevirt-disk-uploader/blob/main/kubevirt-disk-uploader.yaml#L58) arguments (VM Name, Volume Name, Image Destination, Enable or Disable System Preparation and Push Timeout).
1919
3. Modify [kubevirt-disk-uploader-credentials](https://github.com/codingben/kubevirt-disk-uploader/blob/main/kubevirt-disk-uploader.yaml#L65-L74) of the external container registry (Username, Password and Hostname).
2020

2121
Deploy `kubevirt-disk-uploader` within the same namespace as the Virtual Machine (VM):

examples/kubevirt-disk-uploader-tekton.yaml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ type: Opaque
146146
data:
147147
registryUsername: "<REGISTRY_USERNAME>"
148148
registryPassword: "<REGISTRY_PASSWORD>"
149-
registryHostname: "<REGISTRY_HOSTNAME>"
150149
---
151150
apiVersion: tekton.dev/v1
152151
kind: Task
@@ -160,8 +159,8 @@ spec:
160159
- name: VOLUME_NAME
161160
description: The volume name of the virtual machine
162161
type: string
163-
- name: CONTAINER_DISK_NAME
164-
description: The name of the new image
162+
- name: IMAGE_DESTINATION
163+
description: Destination of the image in container registry
165164
type: string
166165
- name: ENABLE_VIRT_SYSPREP
167166
description: Enable or disable preparation of disk image
@@ -183,19 +182,14 @@ spec:
183182
secretKeyRef:
184183
name: kubevirt-disk-uploader-credentials-tekton
185184
key: registryPassword
186-
- name: REGISTRY_HOST
187-
valueFrom:
188-
secretKeyRef:
189-
name: kubevirt-disk-uploader-credentials-tekton
190-
key: registryHostname
191185
command: ["/usr/local/bin/kubevirt-disk-uploader"]
192186
args:
193187
- "--vmname"
194188
- $(params.VM_NAME)
195189
- "--volumename"
196190
- $(params.VOLUME_NAME)
197-
- "--containerdiskname"
198-
- $(params.CONTAINER_DISK_NAME)
191+
- "--imagedestination"
192+
- $(params.IMAGE_DESTINATION)
199193
- "--enablevirtsysprep"
200194
- $(params.ENABLE_VIRT_SYSPREP)
201195
- "--pushtimeout"
@@ -218,8 +212,8 @@ spec:
218212
- name: VOLUME_NAME
219213
description: "Volume name of the virtual machine"
220214
type: string
221-
- name: CONTAINER_DISK_NAME
222-
description: "Name of the new image"
215+
- name: IMAGE_DESTINATION
216+
description: "Destination of the image in container registry"
223217
type: string
224218
- name: ENABLE_VIRT_SYSPREP
225219
description: "Enable or disable preparation of disk image"
@@ -241,8 +235,8 @@ spec:
241235
value: "$(params.VM_NAME)"
242236
- name: VOLUME_NAME
243237
value: "$(params.VOLUME_NAME)"
244-
- name: CONTAINER_DISK_NAME
245-
value: "$(params.CONTAINER_DISK_NAME)"
238+
- name: IMAGE_DESTINATION
239+
value: "$(params.IMAGE_DESTINATION)"
246240
- name: ENABLE_VIRT_SYSPREP
247241
value: "$(params.ENABLE_VIRT_SYSPREP)"
248242
- name: PUSH_TIMEOUT
@@ -265,8 +259,8 @@ spec:
265259
value: example-vm-tekton
266260
- name: VOLUME_NAME
267261
value: datavolumedisk
268-
- name: CONTAINER_DISK_NAME
269-
value: example-vm-tekton-exported:latest
262+
- name: IMAGE_DESTINATION
263+
value: quay.io/boukhano/example-vm-tekton-exported:latest
270264
- name: ENABLE_VIRT_SYSPREP
271265
value: true
272266
- name: PUSH_TIMEOUT

kubevirt-disk-uploader.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ spec:
4949
secretKeyRef:
5050
name: kubevirt-disk-uploader-credentials
5151
key: registryPassword
52-
- name: REGISTRY_HOST
53-
valueFrom:
54-
secretKeyRef:
55-
name: kubevirt-disk-uploader-credentials
56-
key: registryHostname
5752
command: ["/usr/local/bin/kubevirt-disk-uploader"]
58-
# args: ["--vmname", "example-vm", "--volumename", "datavolumedisk", "--containerdiskname", "example-vm-exported:latest", "--enablevirtsysprep", "false", "--pushtimeout", "120"]
53+
# args: ["--vmname", "example-vm", "--volumename", "datavolumedisk", "--imagedestination", "quay.io/boukhano/example-vm-exported:latest", "--enablevirtsysprep", "false", "--pushtimeout", "120"]
5954
resources:
6055
requests:
6156
memory: 3Gi
@@ -71,4 +66,3 @@ type: Opaque
7166
data:
7267
registryUsername: "<REGISTRY_USERNAME>"
7368
registryPassword: "<REGISTRY_PASSWORD>"
74-
registryHostname: "<REGISTRY_HOSTNAME>"

main.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,17 @@ func buildContainerDisk(diskPath string) (v1.Image, error) {
145145
return image, nil
146146
}
147147

148-
func pushContainerDisk(image v1.Image, containerDiskName string, pushTimeout int) error {
148+
func pushContainerDisk(image v1.Image, imageDestination string, pushTimeout int) error {
149149
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(pushTimeout))
150150
defer cancel()
151151

152-
hostName := os.Getenv("REGISTRY_HOST")
153152
userName := os.Getenv("REGISTRY_USERNAME")
154153
password := os.Getenv("REGISTRY_PASSWORD")
155154
auth := &authn.Basic{
156155
Username: userName,
157156
Password: password,
158157
}
159158

160-
imageDestination := fmt.Sprintf("%s/%s/%s", hostName, userName, containerDiskName)
161159
err := crane.Push(image, imageDestination, crane.WithAuth(auth), crane.WithContext(ctx))
162160
if err != nil {
163161
log.Fatalf("Error pushing image: %v", err)
@@ -168,7 +166,7 @@ func pushContainerDisk(image v1.Image, containerDiskName string, pushTimeout int
168166
return nil
169167
}
170168

171-
func run(vmName, volumeName, containerDiskName, enableVirtSysprep string, pushTimeout int) error {
169+
func run(vmName, volumeName, imageDestination, enableVirtSysprep string, pushTimeout int) error {
172170
if err := applyVirtualMachineExport(vmName); err != nil {
173171
return err
174172
}
@@ -194,13 +192,13 @@ func run(vmName, volumeName, containerDiskName, enableVirtSysprep string, pushTi
194192
return err
195193
}
196194

197-
return pushContainerDisk(image, containerDiskName, pushTimeout)
195+
return pushContainerDisk(image, imageDestination, pushTimeout)
198196
}
199197

200198
func main() {
201199
var vmName string
202200
var volumeName string
203-
var containerDiskName string
201+
var imageDestination string
204202
var enableVirtSysprep string
205203
var pushTimeout int
206204

@@ -210,7 +208,7 @@ func main() {
210208
Run: func(cmd *cobra.Command, args []string) {
211209
log.Println("Extracts disk and uploads it to a container registry...")
212210

213-
if err := run(vmName, volumeName, containerDiskName, enableVirtSysprep, pushTimeout); err != nil {
211+
if err := run(vmName, volumeName, imageDestination, enableVirtSysprep, pushTimeout); err != nil {
214212
log.Panicln(err)
215213
}
216214

@@ -220,12 +218,12 @@ func main() {
220218

221219
command.Flags().StringVar(&vmName, "vmname", "", "name of the virtual machine")
222220
command.Flags().StringVar(&volumeName, "volumename", "", "volume name of the virtual machine")
223-
command.Flags().StringVar(&containerDiskName, "containerdiskname", "", "name of the new container image")
221+
command.Flags().StringVar(&imageDestination, "imagedestination", "", "destination of the image in container registry")
224222
command.Flags().StringVar(&enableVirtSysprep, "enablevirtsysprep", "false", "enable or disable virt-sysprep")
225223
command.Flags().IntVar(&pushTimeout, "pushtimeout", 60, "containerdisk push timeout in minutes")
226224
command.MarkFlagRequired("vmname")
227225
command.MarkFlagRequired("volumename")
228-
command.MarkFlagRequired("containerDiskName")
226+
command.MarkFlagRequired("imagedestination")
229227

230228
if err := command.Execute(); err != nil {
231229
log.Println(err)

task/0.5.0/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ kubectl apply -f https://raw.githubusercontent.com/codingben/kubevirt-disk-uploa
1616

1717
- **VM_NAME**: The name of the virtual machine
1818
- **VOLUME_NAME**: The volume name of the virtual machine
19-
- **CONTAINER_DISK_NAME**: The name of the new image
19+
- **IMAGE_DESTINATION**: Destination of the image in container registry
2020
- **ENABLE_VIRT_SYSPREP**: Enable or disable preparation of disk image
2121
- **PUSH_TIMEOUT**: ContainerDisk push timeout in minutes
2222

@@ -36,7 +36,6 @@ type: Opaque
3636
data:
3737
registryUsername: "<REGISTRY_USERNAME>"
3838
registryPassword: "<REGISTRY_PASSWORD>"
39-
registryHostname: "<REGISTRY_HOSTNAME>"
4039
```
4140

4241
TaskRun:
@@ -54,8 +53,8 @@ spec:
5453
value: <VM_NAME_VALUE>
5554
- name: VOLUME_NAME
5655
value: <VOLUME_NAME_VALUE>
57-
- name: CONTAINER_DISK_NAME
58-
value: <CONTAINER_DISK_NAME_VALUE>
56+
- name: IMAGE_DESTINATION
57+
value: <IMAGE_DESTINATION_VALUE>
5958
- name: ENABLE_VIRT_SYSPREP
6059
value: <ENABLE_VIRT_SYSPREP_VALUE>
6160
- name: PUSH_TIMEOUT

task/0.5.0/kubevirt-disk-uploader-task.yaml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ spec:
1919
- name: VOLUME_NAME
2020
description: The volume name of the virtual machine
2121
type: string
22-
- name: CONTAINER_DISK_NAME
23-
description: The name of the new image
22+
- name: IMAGE_DESTINATION
23+
description: Destination of the image in container registry
2424
type: string
2525
- name: ENABLE_VIRT_SYSPREP
2626
description: Enable or disable preparation of disk image
@@ -42,19 +42,14 @@ spec:
4242
secretKeyRef:
4343
name: kubevirt-disk-uploader-credentials-tekton
4444
key: registryPassword
45-
- name: REGISTRY_HOST
46-
valueFrom:
47-
secretKeyRef:
48-
name: kubevirt-disk-uploader-credentials-tekton
49-
key: registryHostname
5045
command: ["/usr/local/bin/kubevirt-disk-uploader"]
5146
args:
5247
- "--vmname"
5348
- $(params.VM_NAME)
5449
- "--volumename"
5550
- $(params.VOLUME_NAME)
56-
- "--containerdiskname"
57-
- $(params.CONTAINER_DISK_NAME)
51+
- "--imagedestination"
52+
- $(params.IMAGE_DESTINATION)
5853
- "--enablevirtsysprep"
5954
- $(params.ENABLE_VIRT_SYSPREP)
6055
- "--pushtimeout"

task/0.5.0/tests/kubevirt-disk-uploader-secret.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ type: Opaque
77
data:
88
registryUsername: "<REGISTRY_USERNAME>"
99
registryPassword: "<REGISTRY_PASSWORD>"
10-
registryHostname: "<REGISTRY_HOSTNAME>"

task/0.5.0/tests/kubevirt-disk-uploader-task-run.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ spec:
1010
value: example-vm-tekton
1111
- name: VOLUME_NAME
1212
value: datavolumedisk
13-
- name: CONTAINER_DISK_NAME
14-
value: example-vm-tekton-exported:latest
13+
- name: IMAGE_DESTINATION
14+
value: quay.io/boukhano/example-vm-tekton-exported:latest
1515
- name: ENABLE_VIRT_SYSPREP
1616
value: false
1717
- name: PUSH_TIMEOUT

0 commit comments

Comments
 (0)