|
| 1 | +package evacuators |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + v1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/client-go/kubernetes" |
| 13 | + |
| 14 | + log "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | +// KubeVirtEvacuator implements Evacuator interface, managing KubeVirt instances |
| 18 | +type KubeVirtEvacuator struct { |
| 19 | + client *kubernetes.Clientset // Kubernetes client object inherited from the caller |
| 20 | + nodeID string // Kubernetes Node ID |
| 21 | + errors []error // Errors created by the evacuation threads |
| 22 | + |
| 23 | + sleepingTime time.Duration // Time in seconds between each VM state change |
| 24 | + timeoutCounter int // Number of times checking each VM state change |
| 25 | + |
| 26 | + mutex sync.Mutex // Used to start non-threadsafe commands |
| 27 | +} |
| 28 | + |
| 29 | +// NewKubeVirtEvacuator is the constructor |
| 30 | +func NewKubeVirtEvacuator(nodeID string, client *kubernetes.Clientset) (*KubeVirtEvacuator, error) { |
| 31 | + var result KubeVirtEvacuator |
| 32 | + var err error |
| 33 | + |
| 34 | + if client == nil { |
| 35 | + err = fmt.Errorf("NewKubeVirtEvacuator: the given clientset is nil") |
| 36 | + } |
| 37 | + |
| 38 | + if len(nodeID) == 0 { |
| 39 | + err = fmt.Errorf("NewKubeVirtEvacuator: the given nodeID is empty") |
| 40 | + } |
| 41 | + |
| 42 | + result.nodeID = nodeID |
| 43 | + result.client = client |
| 44 | + result.sleepingTime = 30 |
| 45 | + result.timeoutCounter = 40 |
| 46 | + |
| 47 | + return &result, err |
| 48 | +} |
| 49 | + |
| 50 | +// Evacuate start the live migration process of the hosted virtual instances |
| 51 | +func (k *KubeVirtEvacuator) Evacuate() (err error) { |
| 52 | + log.Infof("Evacuate: migration configuration is %v retries every %v", k.timeoutCounter, k.sleepingTime*time.Second) |
| 53 | + |
| 54 | + vms, err := k.getVMRunningOnNode() |
| 55 | + |
| 56 | + if err == nil { |
| 57 | + k.startAsyncEvacuation(vms) |
| 58 | + |
| 59 | + for { |
| 60 | + if k.timeoutCounter == 0 { |
| 61 | + err = fmt.Errorf("Evacuate: timeout exceeded") |
| 62 | + break |
| 63 | + } |
| 64 | + |
| 65 | + log.Infof("EvacuateVM: %v retries left. %v remaining instances on the node", k.timeoutCounter, vms.Size()) |
| 66 | + |
| 67 | + vms, err = k.getVMRunningOnNode() |
| 68 | + if err != nil { |
| 69 | + err = fmt.Errorf("%v errors occured", len(k.errors)) |
| 70 | + break |
| 71 | + } |
| 72 | + |
| 73 | + if vms.Size() == 0 { |
| 74 | + log.Info("Evacuate: Completed.") |
| 75 | + break |
| 76 | + } |
| 77 | + |
| 78 | + k.countDown() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return err |
| 83 | +} |
| 84 | + |
| 85 | +// startAsyncEvacuation starts one evacuateVM fonction per VM |
| 86 | +func (k *KubeVirtEvacuator) startAsyncEvacuation(vms *v1.PodList) { |
| 87 | + for _, vm := range vms.Items { |
| 88 | + go k.evacuateVM(&vm) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// countDown counts down the timer |
| 93 | +func (k *KubeVirtEvacuator) countDown() { |
| 94 | + time.Sleep(k.sleepingTime * time.Second) |
| 95 | + k.timeoutCounter = k.timeoutCounter - 1 |
| 96 | +} |
| 97 | + |
| 98 | +// getVMRunningOnNode gets the virt-launcher pods running on the node |
| 99 | +func (k *KubeVirtEvacuator) getVMRunningOnNode() (*v1.PodList, error) { |
| 100 | + labelSelector := "kubevirt.io=virt-launcher" |
| 101 | + fieldSelector := fmt.Sprintf("spec.nodeName=%s", k.nodeID) |
| 102 | + |
| 103 | + return k.client.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{ |
| 104 | + LabelSelector: labelSelector, |
| 105 | + FieldSelector: fieldSelector, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +// evacuateVM starts and monitors the migration of the given virtual instance |
| 110 | +func (k *KubeVirtEvacuator) evacuateVM(vm *v1.Pod) { |
| 111 | + var newNode string |
| 112 | + var err error |
| 113 | + |
| 114 | + vmName := vm.Labels["kubevirt.io/vm"] |
| 115 | + |
| 116 | + if len(vmName) > 2 { |
| 117 | + logPrefix := fmt.Sprintf("evacuateVM: %s (ns: %s, pod %s).", vmName, vm.Namespace, vm.Name) |
| 118 | + shellCommand := exec.Command("/usr/bin/virtctl", "migrate", vmName, "-n", vm.Namespace) |
| 119 | + |
| 120 | + log.Infof("%s Evacuating from %s", logPrefix, k.nodeID) |
| 121 | + |
| 122 | + k.execCommand(shellCommand) |
| 123 | + if err != nil { |
| 124 | + err = fmt.Errorf("%s %v", logPrefix, err) |
| 125 | + } else { |
| 126 | + for { |
| 127 | + newNode, err = k.getNodeOfVM(vmName) |
| 128 | + if err != nil { |
| 129 | + break |
| 130 | + } |
| 131 | + |
| 132 | + if k.checkMigrationCompletion(logPrefix, newNode) { |
| 133 | + time.Sleep(k.sleepingTime * time.Second) |
| 134 | + } else { |
| 135 | + break |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + k.appendError(err) |
| 141 | + } else { |
| 142 | + log.Infof("given pod %s (ns %s) has an empty VM name. Skipping", vm.Name, vm.Namespace) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// checkMigrationCompletion return true if the migration is completed |
| 147 | +func (k *KubeVirtEvacuator) checkMigrationCompletion(logPrefix, newNode string) (result bool) { |
| 148 | + if k.nodeID == newNode { |
| 149 | + log.Infof("%s Still on %v", logPrefix, newNode) |
| 150 | + } else { |
| 151 | + log.Infof("%s Completed.", logPrefix) |
| 152 | + result = true |
| 153 | + } |
| 154 | + |
| 155 | + return result |
| 156 | +} |
| 157 | + |
| 158 | +// appendError append the given error to the internal errors array in a threadsafe way |
| 159 | +func (k *KubeVirtEvacuator) appendError(err error) { |
| 160 | + if err != nil { |
| 161 | + k.mutex.Lock() |
| 162 | + k.errors = append(k.errors, err) // TODO: is append threadsafe? |
| 163 | + k.mutex.Unlock() |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// execCommand starts the given command in a threadsafe way |
| 168 | +func (k *KubeVirtEvacuator) execCommand(command *exec.Cmd) (err error) { |
| 169 | + k.mutex.Lock() |
| 170 | + defer k.mutex.Unlock() |
| 171 | + |
| 172 | + return command.Run() |
| 173 | +} |
| 174 | + |
| 175 | +// getNodeOfVM provides the node ID hosting the given virtual instance |
| 176 | +func (k *KubeVirtEvacuator) getNodeOfVM(vmName string) (result string, err error) { |
| 177 | + var podList *v1.PodList |
| 178 | + |
| 179 | + if len(vmName) == 0 { |
| 180 | + err = fmt.Errorf("getNodeOfVM: the given VM name is empty") |
| 181 | + } else { |
| 182 | + labelSelector := fmt.Sprintf("kubevirt.io/vm=%s", vmName) |
| 183 | + |
| 184 | + podList, err = k.client.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{ |
| 185 | + LabelSelector: labelSelector, |
| 186 | + }) |
| 187 | + } |
| 188 | + |
| 189 | + if err == nil && podList != nil { |
| 190 | + result = podList.Items[0].Spec.NodeName |
| 191 | + } |
| 192 | + |
| 193 | + return result, err |
| 194 | +} |
0 commit comments