API(VM): new image FQN (fully-qualified name) field (#165)

This commit is contained in:
Nikolay Edigaryev 2024-04-15 20:14:44 +04:00 committed by GitHub
parent 510a25956f
commit 7fb0a85834
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 1 deletions

View File

@ -132,6 +132,7 @@ func (controller *Controller) updateVM(ctx *gin.Context) responder.Responder {
dbVM.Status = userVM.Status
dbVM.StatusMessage = userVM.StatusMessage
dbVM.ImageFQN = userVM.ImageFQN
if err := txn.SetVM(*dbVM); err != nil {
controller.logger.Errorf("failed to update VM in the DB: %v", err)

View File

@ -39,6 +39,9 @@ type VM struct {
// "tart run" terminations more correctly
stopping atomic.Bool
// Image FQN feature, see https://github.com/cirruslabs/orchard/issues/164
imageFQN atomic.Pointer[string]
err atomic.Pointer[error]
ctx context.Context
@ -147,6 +150,10 @@ func (vm *VM) Started() bool {
return vm.started.Load()
}
func (vm *VM) ImageFQN() *string {
return vm.imageFQN.Load()
}
func (vm *VM) id() string {
return vm.onDiskName.String()
}
@ -171,6 +178,13 @@ func (vm *VM) cloneAndConfigure(ctx context.Context) error {
return err
}
// Image FQN feature, see https://github.com/cirruslabs/orchard/issues/164
fqnRaw, _, err := tart.Tart(ctx, vm.logger, "fqn", vm.Resource.Image)
if err == nil {
fqn := strings.TrimSpace(fqnRaw)
vm.imageFQN.Store(&fqn)
}
if vm.Resource.Memory != 0 {
_, _, err = tart.Tart(ctx, vm.logger, "set", "--memory",
strconv.FormatUint(vm.Resource.Memory, 10), vm.id())

View File

@ -185,7 +185,7 @@ func (worker *Worker) updateWorker(ctx context.Context) error {
return nil
}
//nolint:nestif // nested "if" complexity is tolerable for now
//nolint:nestif,gocognit // nested "if" and cognitive complexity is tolerable for now
func (worker *Worker) syncVMs(ctx context.Context) error {
remoteVMs, err := worker.client.VMs().FindForWorker(ctx, worker.name)
if err != nil {
@ -245,6 +245,12 @@ func (worker *Worker) syncVMs(ctx context.Context) error {
// check if the local VM had already started
// and update the remote VM as accordingly
if vm.Started() {
// Image FQN feature, see https://github.com/cirruslabs/orchard/issues/164
if imageFQN := vm.ImageFQN(); imageFQN != nil {
vmResource.ImageFQN = *imageFQN
}
// Mark the remote VM as started
vmResource.Status = v1.VMStatusRunning
if _, err := worker.client.VMs().Update(ctx, vmResource); err != nil {
return err

View File

@ -53,6 +53,10 @@ type VM struct {
// HostDir is a list of host directories to be mounted to the VM.
HostDirs []HostDir `json:"hostDirs"`
// ImageFQN is a fully qualified name of the Image that it is populated
// by the worker using "tart fqn" command after it had pulled the image.
ImageFQN string `json:"image_fqn"`
Meta
}