vm-import-controller/pkg/controllers/migration/vmware.go
Volker Theile 29629c75f8
Enhance importer to handle several issue (#42)
- Import OpenStack server by UUID
- Import OpenStack server with upper case characters in its name

The following improvements have been done:
- Sanitize the configured `VirtualMachineName` field, e.g. convert upper case to lower case to make it RFC 1123 compliant.
- Convert UUID to real name for OpenStack imports
- Reduce waiting time to recheck if created VM is running from 5min to 2min
- Rename variable `uuid` to `serverUUID` in the OpenStack client code to do not collide with the imported uuid module
- Improve error and log messages
- Fix typos
- Add comments

Related to: https://github.com/harvester/harvester/issues/6500
Related to: https://github.com/harvester/harvester/issues/6505

Signed-off-by: Volker Theile <vtheile@suse.com>
2025-02-04 07:42:42 +01:00

107 lines
3.4 KiB
Go

package migration
import (
"context"
"fmt"
"time"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/harvester/vm-import-controller/pkg/apis/common"
migration "github.com/harvester/vm-import-controller/pkg/apis/migration.harvesterhci.io/v1beta1"
migrationController "github.com/harvester/vm-import-controller/pkg/generated/controllers/migration.harvesterhci.io/v1beta1"
"github.com/harvester/vm-import-controller/pkg/source/vmware"
"github.com/harvester/vm-import-controller/pkg/util"
)
type vmwareHandler struct {
ctx context.Context
vmware migrationController.VmwareSourceController
secret corecontrollers.SecretController
}
func RegisterVmwareController(ctx context.Context, vc migrationController.VmwareSourceController, secret corecontrollers.SecretController) {
vHandler := &vmwareHandler{
ctx: ctx,
vmware: vc,
secret: secret,
}
vc.OnChange(ctx, "vmware-migration-change", vHandler.OnSourceChange)
}
func (h *vmwareHandler) OnSourceChange(_ string, v *migration.VmwareSource) (*migration.VmwareSource, error) {
if v == nil || v.DeletionTimestamp != nil {
return v, nil
}
logrus.WithFields(logrus.Fields{
"kind": v.Kind,
"name": v.Name,
"namespace": v.Namespace,
}).Info("Reconciling source")
if v.Status.Status != migration.ClusterReady {
secretObj, err := h.secret.Get(v.Spec.Credentials.Namespace, v.Spec.Credentials.Name, metav1.GetOptions{})
if err != nil {
return v, fmt.Errorf("error looking up secret for vmware migration: %v", err)
}
client, err := vmware.NewClient(h.ctx, v.Spec.EndpointAddress, v.Spec.Datacenter, secretObj)
if err != nil {
return v, fmt.Errorf("error generating vmware client for vmware migration '%s': %v", v.Name, err)
}
err = client.Verify()
if err != nil {
logrus.WithFields(logrus.Fields{
"apiVersion": v.APIVersion,
"kind": v.Kind,
"name": v.Name,
"namespace": v.Namespace,
"err": err,
}).Error("failed to verify client for vmware migration")
// unable to find specific datacenter
conds := []common.Condition{
{
Type: migration.ClusterErrorCondition,
Status: v1.ConditionTrue,
LastUpdateTime: metav1.Now().Format(time.RFC3339),
LastTransitionTime: metav1.Now().Format(time.RFC3339),
}, {
Type: migration.ClusterReadyCondition,
Status: v1.ConditionFalse,
LastUpdateTime: metav1.Now().Format(time.RFC3339),
LastTransitionTime: metav1.Now().Format(time.RFC3339),
},
}
v.Status.Conditions = util.MergeConditions(v.Status.Conditions, conds)
v.Status.Status = migration.ClusterNotReady
return h.vmware.UpdateStatus(v)
}
conds := []common.Condition{
{
Type: migration.ClusterReadyCondition,
Status: v1.ConditionTrue,
LastUpdateTime: metav1.Now().Format(time.RFC3339),
LastTransitionTime: metav1.Now().Format(time.RFC3339),
}, {
Type: migration.ClusterErrorCondition,
Status: v1.ConditionFalse,
LastUpdateTime: metav1.Now().Format(time.RFC3339),
LastTransitionTime: metav1.Now().Format(time.RFC3339),
},
}
v.Status.Conditions = util.MergeConditions(v.Status.Conditions, conds)
v.Status.Status = migration.ClusterReady
return h.vmware.UpdateStatus(v)
}
return v, nil
}