Skip to content

Commit

Permalink
feat(api): set tag for dvcr images
Browse files Browse the repository at this point in the history
1. Set uid of resource as tag for dvcr images
2. The DVCR importer now copies only the specified tags of an image, rather than all existing tags.
3. Instead of generating the target image name each time, now use the dvcr image name from the importer pod.

Signed-off-by: Isteb4k <[email protected]>
  • Loading branch information
Isteb4k authored Aug 30, 2024
1 parent 4b147f7 commit cf71a98
Show file tree
Hide file tree
Showing 20 changed files with 1,262 additions and 93 deletions.
2 changes: 1 addition & 1 deletion images/dvcr-artifact/pkg/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (i *Importer) runForDVCRSource(ctx context.Context) error {
durCollector := monitoring.NewDurationCollector()

craneOpts := i.destCraneOptions(ctx)
err := crane.CopyRepository(i.src, i.destImageName, craneOpts...)
err := crane.Copy(i.src, i.destImageName, craneOpts...)
if err != nil {
return fmt.Errorf("error copy repository: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (ds HTTPDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualIma
log.Info("Cleaning up...")
case pod == nil:
cvi.Status.Progress = ds.statService.GetProgress(cvi.GetUID(), pod, cvi.Status.Progress)
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)

envSettings := ds.getEnvSettings(cvi, supgen)
err = ds.importerService.Start(ctx, envSettings, cvi, supgen, datasource.NewCABundleForCVMI(cvi.Spec.DataSource))
Expand Down Expand Up @@ -127,7 +126,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualIma
cvi.Status.CDROM = ds.statService.GetCDROM(pod)
cvi.Status.Format = ds.statService.GetFormat(pod)
cvi.Status.Progress = ds.statService.GetProgress(cvi.GetUID(), pod, cvi.Status.Progress)
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)
cvi.Status.DownloadSpeed = ds.statService.GetDownloadSpeed(cvi.GetUID(), pod)

log.Info("Ready", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
Expand Down Expand Up @@ -163,7 +162,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualIma

cvi.Status.Phase = virtv2.ImageProvisioning
cvi.Status.Progress = ds.statService.GetProgress(cvi.GetUID(), pod, cvi.Status.Progress)
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)
cvi.Status.DownloadSpeed = ds.statService.GetDownloadSpeed(cvi.GetUID(), pod)

log.Info("Provisioning...", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
Expand Down Expand Up @@ -195,7 +194,7 @@ func (ds HTTPDataSource) getEnvSettings(cvi *virtv2.ClusterVirtualImage, supgen
&settings,
ds.dvcrSettings,
supgen,
ds.dvcrSettings.RegistryImageForCVMI(cvi.Name),
ds.dvcrSettings.RegistryImageForCVI(cvi),
)

return &settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Stat interface {
GetFormat(pod *corev1.Pod) string
GetCDROM(pod *corev1.Pod) bool
GetSize(pod *corev1.Pod) virtv2.ImageStatusSize
GetDVCRImageName(pod *corev1.Pod) string
GetDownloadSpeed(ownerUID types.UID, pod *corev1.Pod) *virtv2.StatusSpeed
GetProgress(ownerUID types.UID, pod *corev1.Pod, prevProgress string, opts ...service.GetProgressOption) string
IsUploadStarted(ownerUID types.UID, pod *corev1.Pod) bool
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtu
return false, err
}

cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)

log.Info("Ready", "progress", cvi.Status.Progress, "pod.phase", "nil")

return requeue, nil
Expand Down Expand Up @@ -157,7 +155,7 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtu
cvi.Status.CDROM = dvcrDataSource.IsCDROM()
cvi.Status.Format = dvcrDataSource.GetFormat()
cvi.Status.Progress = "100%"
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)

log.Info("Ready", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
default:
Expand Down Expand Up @@ -186,7 +184,7 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtu
condition.Message = "Import is in the process of provisioning to DVCR."

cvi.Status.Phase = virtv2.ImageProvisioning
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)

log.Info("Ready", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
}
Expand Down Expand Up @@ -240,7 +238,7 @@ func (ds ObjectRefDataSource) getEnvSettings(cvi *virtv2.ClusterVirtualImage, su
&settings,
ds.dvcrSettings,
sup,
ds.dvcrSettings.RegistryImageForCVMI(cvi.Name),
ds.dvcrSettings.RegistryImageForCVI(cvi),
)

return &settings, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtua

log.Info("Cleaning up...")
case pod == nil:
cvi.Status.Progress = "0%"

envSettings := ds.getEnvSettings(cvi, supgen)
err = ds.importerService.Start(ctx, envSettings, cvi, supgen, datasource.NewCABundleForCVMI(cvi.Spec.DataSource))
var requeue bool
Expand All @@ -104,9 +106,6 @@ func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtua
return false, err
}

cvi.Status.Progress = "0%"
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)

log.Info("Create importer pod...", "progress", cvi.Status.Progress, "pod.phase", "nil")

return requeue, nil
Expand Down Expand Up @@ -135,7 +134,7 @@ func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtua
cvi.Status.CDROM = ds.statService.GetCDROM(pod)
cvi.Status.Format = ds.statService.GetFormat(pod)
cvi.Status.Progress = "100%"
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)

log.Info("Ready", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
default:
Expand Down Expand Up @@ -165,7 +164,7 @@ func (ds RegistryDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtua

cvi.Status.Phase = virtv2.ImageProvisioning
cvi.Status.Progress = "0%"
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)

log.Info("Provisioning...", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
}
Expand Down Expand Up @@ -211,7 +210,7 @@ func (ds RegistryDataSource) getEnvSettings(cvi *virtv2.ClusterVirtualImage, sup
&settings,
ds.dvcrSettings,
supgen,
ds.dvcrSettings.RegistryImageForCVMI(cvi.Name),
ds.dvcrSettings.RegistryImageForCVI(cvi),
)

return &settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,14 @@ func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualI

log.Info("Cleaning up...")
case pod == nil || svc == nil || ing == nil:
envSettings := ds.getEnvSettings(supgen)
envSettings := ds.getEnvSettings(cvi, supgen)
err = ds.uploaderService.Start(ctx, envSettings, cvi, supgen, datasource.NewCABundleForCVMI(cvi.Spec.DataSource))
var requeue bool
requeue, err = setPhaseConditionForUploaderStart(&condition, &cvi.Status.Phase, err)
if err != nil {
return false, err
}

cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)

log.Info("Create uploader pod...", "progress", cvi.Status.Progress, "pod.phase", nil)

return requeue, nil
Expand Down Expand Up @@ -141,7 +139,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualI
cvi.Status.CDROM = ds.statService.GetCDROM(pod)
cvi.Status.Format = ds.statService.GetFormat(pod)
cvi.Status.Progress = "100%"
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)
cvi.Status.DownloadSpeed = ds.statService.GetDownloadSpeed(cvi.GetUID(), pod)

log.Info("Ready", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
Expand Down Expand Up @@ -172,7 +170,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualI

cvi.Status.Phase = virtv2.ImageProvisioning
cvi.Status.Progress = ds.statService.GetProgress(cvi.GetUID(), pod, cvi.Status.Progress)
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)
cvi.Status.DownloadSpeed = ds.statService.GetDownloadSpeed(cvi.GetUID(), pod)

err = ds.uploaderService.Protect(ctx, pod, svc, ing)
Expand All @@ -187,7 +185,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, cvi *virtv2.ClusterVirtualI
condition.Message = "Waiting for the user upload."

cvi.Status.Phase = virtv2.ImageWaitForUserUpload
cvi.Status.Target.RegistryURL = ds.dvcrSettings.RegistryImageForCVMI(cvi.Name)
cvi.Status.Target.RegistryURL = ds.statService.GetDVCRImageName(pod)

log.Info("WaitForUserUpload...", "progress", cvi.Status.Progress, "pod.phase", pod.Status.Phase)
}
Expand All @@ -210,14 +208,14 @@ func (ds UploadDataSource) Validate(_ context.Context, _ *virtv2.ClusterVirtualI
return nil
}

func (ds UploadDataSource) getEnvSettings(supgen *supplements.Generator) *uploader.Settings {
func (ds UploadDataSource) getEnvSettings(cvi *virtv2.ClusterVirtualImage, supgen *supplements.Generator) *uploader.Settings {
var settings uploader.Settings

uploader.ApplyDVCRDestinationSettings(
&settings,
ds.dvcrSettings,
supgen,
ds.dvcrSettings.RegistryImageForCVMI(supgen.Name),
ds.dvcrSettings.RegistryImageForCVI(cvi),
)

return &settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/deckhouse/virtualization-controller/pkg/controller/common"
"github.com/deckhouse/virtualization-controller/pkg/controller/ipam"
"github.com/deckhouse/virtualization-controller/pkg/dvcr"
"github.com/deckhouse/virtualization-controller/pkg/imageformat"
"github.com/deckhouse/virtualization-controller/pkg/util"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
Expand Down Expand Up @@ -66,10 +65,9 @@ type HotPlugDeviceSettings struct {

func ApplyVirtualMachineSpec(
kvvm *KVVM, vm *virtv2.VirtualMachine,
vmdByName map[string]*virtv2.VirtualDisk,
vmiByName map[string]*virtv2.VirtualImage,
cvmiByName map[string]*virtv2.ClusterVirtualImage,
dvcrSettings *dvcr.Settings,
vdByName map[string]*virtv2.VirtualDisk,
viByName map[string]*virtv2.VirtualImage,
cviByName map[string]*virtv2.ClusterVirtualImage,
class *virtv2.VirtualMachineClass,
ipAddress string,
) error {
Expand Down Expand Up @@ -124,45 +122,43 @@ func ApplyVirtualMachineSpec(
// Attach ephemeral disk for storage: Kubernetes.
// Attach containerDisk for storage: ContainerRegistry (i.e. image from DVCR).

vmi := vmiByName[bd.Name]
vi := viByName[bd.Name]

name := GenerateVMIDiskName(bd.Name)
switch vmi.Spec.Storage {
switch vi.Spec.Storage {
case virtv2.StorageKubernetes:
// Attach PVC as ephemeral volume: its data will be restored to initial state on VM restart.
if err := kvvm.SetDisk(name, SetDiskOptions{
PersistentVolumeClaim: util.GetPointer(vmi.Status.Target.PersistentVolumeClaim),
PersistentVolumeClaim: util.GetPointer(vi.Status.Target.PersistentVolumeClaim),
IsEphemeral: true,
Serial: name,
BootOrder: bootOrder,
}); err != nil {
return err
}
case virtv2.StorageContainerRegistry:
dvcrImage := dvcrSettings.RegistryImageForVMI(vmi.Name, vmi.Namespace)
if err := kvvm.SetDisk(name, SetDiskOptions{
ContainerDisk: util.GetPointer(dvcrImage),
IsCdrom: imageformat.IsISO(vmi.Status.Format),
ContainerDisk: util.GetPointer(vi.Status.Target.RegistryURL),
IsCdrom: imageformat.IsISO(vi.Status.Format),
Serial: name,
BootOrder: bootOrder,
}); err != nil {
return err
}
default:
return fmt.Errorf("unexpected storage type %q for vi %s. %w", vmi.Spec.Storage, vmi.Name, common.ErrUnknownType)
return fmt.Errorf("unexpected storage type %q for vi %s. %w", vi.Spec.Storage, vi.Name, common.ErrUnknownType)
}
bootOrder++

case virtv2.ClusterImageDevice:
// ClusterVirtualImage is attached as containerDisk.

cvmi := cvmiByName[bd.Name]
cvi := cviByName[bd.Name]

name := GenerateCVMIDiskName(bd.Name)
dvcrImage := dvcrSettings.RegistryImageForCVMI(cvmi.Name)
if err := kvvm.SetDisk(name, SetDiskOptions{
ContainerDisk: util.GetPointer(dvcrImage),
IsCdrom: imageformat.IsISO(cvmi.Status.Format),
ContainerDisk: util.GetPointer(cvi.Status.Target.RegistryURL),
IsCdrom: imageformat.IsISO(cvi.Status.Format),
Serial: name,
BootOrder: bootOrder,
}); err != nil {
Expand All @@ -173,15 +169,15 @@ func ApplyVirtualMachineSpec(
case virtv2.DiskDevice:
// VirtualDisk is attached as a regular disk.

vmd := vmdByName[bd.Name]
vd := vdByName[bd.Name]
// VirtualDisk doesn't have pvc yet: wait for pvc and reconcile again.
if vmd.Status.Target.PersistentVolumeClaim == "" {
if vd.Status.Target.PersistentVolumeClaim == "" {
continue
}

name := GenerateVMDDiskName(bd.Name)
if err := kvvm.SetDisk(name, SetDiskOptions{
PersistentVolumeClaim: util.GetPointer(vmd.Status.Target.PersistentVolumeClaim),
PersistentVolumeClaim: util.GetPointer(vd.Status.Target.PersistentVolumeClaim),
Serial: name,
BootOrder: bootOrder,
}); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,19 @@ func (s StatService) GetImportDuration(pod *corev1.Pod) time.Duration {

return report.Duration
}

func (s StatService) GetDVCRImageName(pod *corev1.Pod) string {
if pod == nil {
return ""
}

for _, container := range pod.Spec.Containers {
for _, envVar := range container.Env {
if envVar.Name == common.ImporterDestinationEndpoint {
return envVar.Value
}
}
}

return ""
}
Loading

0 comments on commit cf71a98

Please sign in to comment.