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

Commit d828cd2

Browse files
committed
refactor(CNV-47155): make VM_NAMESPACE env var optionally
Make VM_NAMESPACE environment variable optionally and instead use parameter that is passed by the user if needed. Signed-off-by: Ben Oukhanov <[email protected]>
1 parent 14f2e12 commit d828cd2

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

README.md

Lines changed: 3 additions & 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, Image Destination, 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 Namespace, 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):
@@ -24,6 +24,8 @@ Deploy `kubevirt-disk-uploader` within the same namespace as the Virtual Machine
2424
kubectl apply -f kubevirt-disk-uploader.yaml -n $VM_NAMESPACE
2525
```
2626

27+
**Note**: If both `VM_NAMESPACE` and `--vmnamespace` argument are set, `VM_NAMESPACE` will be used.
28+
2729
## KubeVirt Documentation
2830

2931
Read more about the used API at [KubeVirt Export API](https://kubevirt.io/user-guide/operations/export_api).

examples/kubevirt-disk-uploader-tekton.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ spec:
182182
secretKeyRef:
183183
name: kubevirt-disk-uploader-credentials-tekton
184184
key: registryPassword
185+
- name: VM_NAMESPACE
186+
valueFrom:
187+
fieldRef:
188+
fieldPath: metadata.namespace
185189
command: ["/usr/local/bin/kubevirt-disk-uploader"]
186190
args:
187191
- "--vmname"

kubevirt-disk-uploader.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ spec:
4949
secretKeyRef:
5050
name: kubevirt-disk-uploader-credentials
5151
key: registryPassword
52+
- name: VM_NAMESPACE
53+
valueFrom:
54+
fieldRef:
55+
fieldPath: metadata.namespace
5256
command: ["/usr/local/bin/kubevirt-disk-uploader"]
5357
# args: ["--vmname", "example-vm", "--volumename", "datavolumedisk", "--imagedestination", "quay.io/boukhano/example-vm-exported:latest", "--enablevirtsysprep", "false", "--pushtimeout", "120"]
5458
resources:

main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,27 @@ const (
3131
diskPathConverted string = "./tmp/disk.qcow2"
3232
)
3333

34-
func applyVirtualMachineExport(vmName string) error {
34+
func applyVirtualMachineExport(vmNamespace, vmName string) error {
3535
log.Println("Applying VirtualMachineExport object...")
3636

3737
client, err := kubecli.GetKubevirtClient()
3838
if err != nil {
3939
return err
4040
}
4141

42-
vmNamespace := os.Getenv("VM_NAMESPACE")
42+
env := os.Getenv("VM_NAMESPACE")
43+
if env != "" {
44+
vmNamespace = env
45+
}
46+
4347
if vmNamespace == "" {
44-
return fmt.Errorf("VM namespace is not defined. Set VM_NAMESPACE.")
48+
return fmt.Errorf("VM namespace is not defined. Set VM_NAMESPACE or parameter.")
4549
}
4650

4751
vmExport := &v1beta1.VirtualMachineExport{
4852
ObjectMeta: metav1.ObjectMeta{
49-
Name: vmName,
53+
Name: vmName,
54+
Namespace: vmNamespace,
5055
},
5156
Spec: v1beta1.VirtualMachineExportSpec{
5257
Source: corev1.TypedLocalObjectReference{
@@ -175,8 +180,8 @@ func pushContainerDisk(image v1.Image, imageDestination string, pushTimeout int)
175180
return nil
176181
}
177182

178-
func run(vmName, volumeName, imageDestination, enableVirtSysprep string, pushTimeout int) error {
179-
if err := applyVirtualMachineExport(vmName); err != nil {
183+
func run(vmNamespace, vmName, volumeName, imageDestination, enableVirtSysprep string, pushTimeout int) error {
184+
if err := applyVirtualMachineExport(vmNamespace, vmName); err != nil {
180185
return err
181186
}
182187

@@ -205,6 +210,7 @@ func run(vmName, volumeName, imageDestination, enableVirtSysprep string, pushTim
205210
}
206211

207212
func main() {
213+
var vmNamespace string
208214
var vmName string
209215
var volumeName string
210216
var imageDestination string
@@ -217,14 +223,15 @@ func main() {
217223
Run: func(cmd *cobra.Command, args []string) {
218224
log.Println("Extracts disk and uploads it to a container registry...")
219225

220-
if err := run(vmName, volumeName, imageDestination, enableVirtSysprep, pushTimeout); err != nil {
226+
if err := run(vmNamespace, vmName, volumeName, imageDestination, enableVirtSysprep, pushTimeout); err != nil {
221227
log.Panicln(err)
222228
}
223229

224230
log.Println("Succesfully extracted disk image and uploaded it in a new container image to container registry.")
225231
},
226232
}
227233

234+
command.Flags().StringVar(&vmNamespace, "vmnamespace", "", "namespace of the virtual machine")
228235
command.Flags().StringVar(&vmName, "vmname", "", "name of the virtual machine")
229236
command.Flags().StringVar(&volumeName, "volumename", "", "volume name of the virtual machine")
230237
command.Flags().StringVar(&imageDestination, "imagedestination", "", "destination of the image in container registry")

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ spec:
4545
secretKeyRef:
4646
name: kubevirt-disk-uploader-credentials-tekton
4747
key: registryPassword
48+
- name: VM_NAMESPACE
49+
valueFrom:
50+
fieldRef:
51+
fieldPath: metadata.namespace
4852
command: ["/usr/local/bin/kubevirt-disk-uploader"]
4953
args:
5054
- "--vmname"

0 commit comments

Comments
 (0)