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

Commit

Permalink
refactor(CNV-43601): organize project into packages
Browse files Browse the repository at this point in the history
Organize project code into packages. This improves
readability, maintainability and testability.

Signed-off-by: Ben Oukhanov <[email protected]>
  • Loading branch information
codingben committed Sep 30, 2024
1 parent 8bd16be commit 9f03527
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 417 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ FROM golang:1.22 as builder
WORKDIR /app
COPY go.mod go.sum ./
COPY vendor/ ./vendor
COPY main.go .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o kubevirt-disk-uploader .
COPY cmd/ ./cmd
COPY pkg/ ./pkg
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o kubevirt-disk-uploader ./cmd

FROM quay.io/fedora/fedora-minimal:39

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ KubeVirt Disk Uploader -> Download VM Disk -> Build New Container Disk -> Push T
Deploy `kubevirt-disk-uploader` within the same namespace as the Virtual Machine (VM):

```
kubectl apply -f kubevirt-disk-uploader.yaml -n $VM_NAMESPACE
kubectl apply -f kubevirt-disk-uploader.yaml -n $POD_NAMESPACE
```

**Note**: If both `VM_NAMESPACE` and `--vmnamespace` argument are set, `VM_NAMESPACE` will be used.
**Note**: If both `POD_NAMESPACE` and `--vmnamespace` argument are set, `POD_NAMESPACE` will be used.

## KubeVirt Documentation

Expand Down
130 changes: 130 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"log"
"os"

"github.com/codingben/kubevirt-disk-uploader/pkg/certificate"
"github.com/codingben/kubevirt-disk-uploader/pkg/disk"
"github.com/codingben/kubevirt-disk-uploader/pkg/image"
"github.com/codingben/kubevirt-disk-uploader/pkg/secrets"
"github.com/codingben/kubevirt-disk-uploader/pkg/vmexport"

cobra "github.com/spf13/cobra"
kubecli "kubevirt.io/client-go/kubecli"
)

const (
diskPath string = "./tmp/disk.qcow2"
certificatePath string = "./tmp/tls.crt"
kvExportTokenHeader string = "x-kubevirt-export-token"
)

func run(client kubecli.KubevirtClient, vmNamespace, vmName, volumeName, imageDestination string, pushTimeout int) error {
log.Printf("Creating a new Secret '%s/%s' object...", vmNamespace, vmName)

if err := secrets.CreateVirtualMachineExportSecret(client, vmNamespace, vmName); err != nil {
return err
}

log.Printf("Creating a new VirtualMachineExport '%s/%s' object...", vmNamespace, vmName)

if err := vmexport.CreateVirtualMachineExport(client, vmNamespace, vmName); err != nil {
return err
}

log.Println("Waiting for VirtualMachineExport status to be ready...")

if err := vmexport.WaitUntilVirtualMachineExportReady(client, vmNamespace, vmName); err != nil {
return err
}

log.Println("Getting raw disk URL from the VirtualMachineExport object status...")

rawDiskUrl, err := vmexport.GetRawDiskUrlFromVolumes(client, vmNamespace, vmName, volumeName)
if err != nil {
return err
}

log.Println("Creating TLS certificate file from the VirtualMachineExport object status...")

certificateData, err := certificate.GetCertificateFromVirtualMachineExport(client, vmNamespace, vmName)
if err != nil {
return err
}

if err := certificate.CreateCertificateFile(certificatePath, certificateData); err != nil {
return err
}

log.Println("Getting export token from the Secret object...")

kvExportToken, err := secrets.GetTokenFromVirtualMachineExportSecret(client, vmNamespace, vmName)
if err != nil {
return err
}

log.Println("Downloading disk image from the VirtualMachineExport server...")

if err := disk.DownloadDiskImageFromURL(rawDiskUrl, kvExportTokenHeader, kvExportToken, certificatePath, diskPath); err != nil {
return err
}

log.Println("Building a new container image...")

containerImage, err := image.Build(diskPath)
if err != nil {
return err
}

log.Println("Pushing new container image to the container registry...")

if err := image.Push(containerImage, imageDestination, pushTimeout); err != nil {
return err
}

log.Println("Successfully uploaded to the container registry.")
return nil
}

func main() {
var vmNamespace string
var vmName string
var volumeName string
var imageDestination string
var pushTimeout int

var command = &cobra.Command{
Use: "kubevirt-disk-uploader",
Short: "Extracts disk and uploads it to a container registry",
Run: func(cmd *cobra.Command, args []string) {
client, err := kubecli.GetKubevirtClient()
if err != nil {
log.Panicln(err)
}

namespace := os.Getenv("POD_NAMESPACE")
if namespace != "" {
vmNamespace = namespace
}

if err := run(client, namespace, vmName, volumeName, imageDestination, pushTimeout); err != nil {
log.Panicln(err)
}
},
}

command.Flags().StringVar(&vmNamespace, "vmnamespace", "", "namespace of the virtual machine")
command.Flags().StringVar(&vmName, "vmname", "", "name of the virtual machine")
command.Flags().StringVar(&volumeName, "volumename", "", "volume name of the virtual machine")
command.Flags().StringVar(&imageDestination, "imagedestination", "", "destination of the image in container registry")
command.Flags().IntVar(&pushTimeout, "pushtimeout", 60, "containerdisk push timeout in minutes")
command.MarkFlagRequired("vmname")
command.MarkFlagRequired("volumename")
command.MarkFlagRequired("imagedestination")

if err := command.Execute(); err != nil {
log.Println(err)
os.Exit(1)
}
}
21 changes: 14 additions & 7 deletions examples/kubevirt-disk-uploader-tekton.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "create"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
Expand Down Expand Up @@ -153,7 +156,7 @@ metadata:
name: kubevirt-disk-uploader-task
spec:
params:
- name: VM_NAME
- name: POD_NAME
description: The name of the virtual machine
type: string
- name: VOLUME_NAME
Expand All @@ -179,14 +182,18 @@ spec:
secretKeyRef:
name: kubevirt-disk-uploader-credentials-tekton
key: secretKey
- name: VM_NAMESPACE
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
command: ["/usr/local/bin/kubevirt-disk-uploader"]
args:
- "--vmname"
- $(params.VM_NAME)
- $(params.POD_NAME)
- "--volumename"
- $(params.VOLUME_NAME)
- "--imagedestination"
Expand All @@ -205,7 +212,7 @@ metadata:
name: kubevirt-disk-uploader-pipeline
spec:
params:
- name: VM_NAME
- name: POD_NAME
description: "Name of the virtual machine"
type: string
- name: VOLUME_NAME
Expand All @@ -227,8 +234,8 @@ spec:
runAfter:
- deploy-example-vm
params:
- name: VM_NAME
value: "$(params.VM_NAME)"
- name: POD_NAME
value: "$(params.POD_NAME)"
- name: VOLUME_NAME
value: "$(params.VOLUME_NAME)"
- name: IMAGE_DESTINATION
Expand All @@ -249,7 +256,7 @@ spec:
pipelineRef:
name: kubevirt-disk-uploader-pipeline
params:
- name: VM_NAME
- name: POD_NAME
value: example-vm-tekton
- name: VOLUME_NAME
value: example-dv-tekton
Expand Down
4 changes: 2 additions & 2 deletions kubevirt-disk-uploader.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ spec:
secretKeyRef:
name: kubevirt-disk-uploader-credentials
key: secretKey
- name: VM_NAMESPACE
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: VM_NAME
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
Expand Down
Loading

0 comments on commit 9f03527

Please sign in to comment.