Skip to content

Commit

Permalink
Adding vmiList to track VMs that need to be restarted
Browse files Browse the repository at this point in the history
and a stop channel for the job to wait until vmiList
is empty

Signed-off-by: prnaraya <[email protected]>
  • Loading branch information
prnaraya committed Jun 8, 2023
1 parent 7233349 commit 9cb856c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
48 changes: 39 additions & 9 deletions informers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mass_machine_type_transition

import (
"context"
"fmt"
"strings"
"time"
Expand All @@ -13,7 +14,11 @@ import (
"kubevirt.io/client-go/kubecli"
)

var needsRestart = 0
// using this as a const allows us to easily modify the program to update if a newer version is released
// we generally want to be updating the machine types to the most recent version
const latestVersion = "rhel9.2.0"

var vmiList []string{}

func getVirtCli() (kubecli.KubevirtClient, error) {
clientConfig, err := kubecli.GetKubevirtClientConfig()
Expand Down Expand Up @@ -49,9 +54,12 @@ func updateMachineTypes(virtCli kubecli.KubevirtClient) error {
machineType := vm.Spec.Template.Spec.Domain.Machine.Type
machineTypeSubstrings := strings.Split(machineType, "-")
version := machineTypeSubstrings[2]
if len(machineTypeSubstrings != 3) {
return nil
}

if strings.Contains(version, "rhel") && version < "rhel9.0.0" {
machineTypeSubstrings[2] = "rhel9.0.0"
machineTypeSubstrings[2] = latestVersion
machineType = strings.Join(machineTypeSubstrings, "-")
updateMachineType := fmt.Sprintf(`{"spec": {"template": {"spec": {"domain": {"machine": {"type": "%s"}}}}}}`, machineType)

Expand All @@ -62,17 +70,25 @@ func updateMachineTypes(virtCli kubecli.KubevirtClient) error {

// add label to running VMs that a restart is required for change to take place
if vm.Status.Ready {
addWarningLabel(virtCli, &vm)
err = addWarningLabel(virtCli, &vm)
if err != nil {
return err
}
}
}
}
return nil
}

func addWarningLabel (virtCli kubecli.KubevirtClient, vm *k6tv1.VirtualMachine) {
func addWarningLabel (virtCli kubecli.KubevirtClient, vm *k6tv1.VirtualMachine) error {
addLabel := fmt.Sprint(`{"metadata": {"labels": {"restart-vm-required": "true"}}}}}`)
virtCli.VirtualMachine(vm.Namespace).Patch(vm.Name, types.StrategicMergePatchType, []byte(addLabel), &k8sv1.PatchOptions{})
needsRestart++
virtCli.VirtualMachineInstance(vm.Namespace).Patch(context.Background(), vm.Name, types.StrategicMergePatchType, []byte(addLabel), &k8sv1.PatchOptions{})
if err != nil {
return err
}
vmiList = append(vmiList, vm.Name)

return nil
}

func removeWarningLabel(obj interface{}) {
Expand All @@ -86,7 +102,21 @@ func removeWarningLabel(obj interface{}) {
return
}

removeLabel := fmt.Sprint(`[{"op": "remove", "path": "metadata/labels/restart-vm-required"}]`)
virtCli.VirtualMachine(vmi.Namespace).Patch(vmi.Name, types.StrategicMergePatchType, []byte(removeLabel), &k8sv1.PatchOptions{})
needsRestart--
//check if deleted VMI is in list of VMIs that need to be restarted
vmiIndex = searchVMIList(vmi.Name)
if vmiIndex == -1 {
return
}

// remove deleted VMI from list
vmiList = append(vmiList[:vmiIndex], vmiList[vmiIndex+1:]
}

func searchVMIList(vmiName string) int {
for i, element := range vmiList {
if element == vmiName {
return i
}
}
return -1
}
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ func main() {

updateMachineTypes(virtCli)

// for now this uses the number of vms to be restarted to determine
// when to exit the job rather than seeing if the actual label exists
// on any VMs before exiting the job

for (needsRestart > 0) {
}
// need to implement probably a chan of some kind to inform the
// program of when to exit after waiting for all VMs to be
// rebooted

os.Exit(0)
}

0 comments on commit 9cb856c

Please sign in to comment.