Skip to content

Commit

Permalink
cleanup secondary vmis of completed primary vmis after 10m (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya authored Sep 28, 2023
1 parent 080efd2 commit afca8b1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tgrun/pkg/runner/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ func CleanUpVMIs() error {
fmt.Printf("Sent logs for successful vmi %s\n", vmi.Name)
}
}
} else if vmi.Status.Phase == kubevirtv1.Succeeded && strings.HasSuffix(vmi.Name, "-initialprimary") {
// cleanup secondary nodes if the primary has been done for 10 minutes

completedAt := time.Time{}
for _, condition := range vmi.Status.PhaseTransitionTimestamps {
if condition.Phase == kubevirtv1.Succeeded {
completedAt = condition.PhaseTransitionTimestamp.Time
}
}
if time.Since(completedAt).Minutes() > 10 {
vmiPrefix := strings.TrimSuffix(vmi.Name, "-initialprimary")
for _, vmi2 := range vmiList.Items {
if strings.HasPrefix(vmi2.Name, vmiPrefix) && vmi.Status.Phase == kubevirtv1.Running {
err := virtClient.VirtualMachineInstance(Namespace).Delete(context.TODO(), vmi2.Name, &metav1.DeleteOptions{})
if err != nil {
fmt.Printf("Failed to delete secondary vmi %s: %v\n", vmi.Name, err)
} else {
fmt.Printf("Delete secondary vmi %s\n", vmi.Name)
}
}
}
}
}

// cleanup VMIs that are not running and have not succeeded after 1.5 hours (cleanupAfterMinutes)
Expand Down

0 comments on commit afca8b1

Please sign in to comment.