Modify the schedule cases.

* Modify the OrderResource case's verification code.
* Simplify the Periodical case.
* Simplify the InProgess case.
* Prettify the code.
* Replace math/rand with crypto/rand
* Replace PollUnitl with PollUntilContextTimeout

Signed-off-by: Xun Jiang <xun.jiang@broadcom.com>
This commit is contained in:
Xun Jiang
2024-11-21 15:16:50 +08:00
parent 9f0026d7dc
commit 226d50d9cb
10 changed files with 576 additions and 484 deletions
+4 -4
View File
@@ -57,9 +57,9 @@ func CreateConfigMapFromYAMLData(c clientset.Interface, yamlData, cmName, namesp
}
// WaitForConfigMapComplete uses c to wait for completions to complete for the Job jobName in namespace ns.
func WaitForConfigMapComplete(c clientset.Interface, ns, configmapName string) error {
func WaitForConfigMapComplete(c clientset.Interface, ns, cmName string) error {
return wait.Poll(PollInterval, PollTimeout, func() (bool, error) {
_, err := c.CoreV1().ConfigMaps(ns).Get(context.TODO(), configmapName, metav1.GetOptions{})
_, err := c.CoreV1().ConfigMaps(ns).Get(context.TODO(), cmName, metav1.GetOptions{})
if err != nil {
return false, err
}
@@ -67,13 +67,13 @@ func WaitForConfigMapComplete(c clientset.Interface, ns, configmapName string) e
})
}
func GetConfigmap(c clientset.Interface, ns, secretName string) (*v1.ConfigMap, error) {
func GetConfigMap(c clientset.Interface, ns, secretName string) (*v1.ConfigMap, error) {
return c.CoreV1().ConfigMaps(ns).Get(context.TODO(), secretName, metav1.GetOptions{})
}
func DeleteConfigMap(c clientset.Interface, ns, name string) error {
if err := c.CoreV1().ConfigMaps(ns).Delete(context.TODO(), name, metav1.DeleteOptions{}); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to delete configmap in namespace %q", ns))
return errors.Wrap(err, fmt.Sprintf("failed to delete ConfigMap in namespace %q", ns))
}
return nil
}
+1 -1
View File
@@ -250,7 +250,7 @@ func cleanVSpherePluginConfig(c clientset.Interface, ns, secretName, configMapNa
}
//clear configmap
_, err = k8s.GetConfigmap(c, ns, configMapName)
_, err = k8s.GetConfigMap(c, ns, configMapName)
if err == nil {
if err := k8s.WaitForConfigmapDelete(c, ns, configMapName); err != nil {
return errors.WithMessagef(err, "Failed to clear up vsphere plugin configmap in %s namespace", ns)
+30 -46
View File
@@ -37,6 +37,7 @@ import (
"github.com/pkg/errors"
"golang.org/x/exp/slices"
"golang.org/x/mod/semver"
"k8s.io/apimachinery/pkg/labels"
ver "k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -334,7 +335,7 @@ func checkRestorePhase(ctx context.Context, veleroCLI string, veleroNamespace st
func checkSchedulePhase(ctx context.Context, veleroCLI, veleroNamespace, scheduleName string) error {
return wait.PollImmediate(time.Second*5, time.Minute*2, func() (bool, error) {
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "schedule", "get", scheduleName, "-ojson")
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "schedule", "get", scheduleName, "-o", "json")
jsonBuf, err := common.CMDExecWithOutput(checkCMD)
if err != nil {
return false, err
@@ -354,7 +355,7 @@ func checkSchedulePhase(ctx context.Context, veleroCLI, veleroNamespace, schedul
}
func checkSchedulePause(ctx context.Context, veleroCLI, veleroNamespace, scheduleName string, pause bool) error {
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "schedule", "get", scheduleName, "-ojson")
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "schedule", "get", scheduleName, "-o", "json")
jsonBuf, err := common.CMDExecWithOutput(checkCMD)
if err != nil {
return err
@@ -372,7 +373,7 @@ func checkSchedulePause(ctx context.Context, veleroCLI, veleroNamespace, schedul
return nil
}
func CheckScheduleWithResourceOrder(ctx context.Context, veleroCLI, veleroNamespace, scheduleName string, order map[string]string) error {
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "schedule", "get", scheduleName, "-ojson")
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "schedule", "get", scheduleName, "-o", "json")
jsonBuf, err := common.CMDExecWithOutput(checkCMD)
if err != nil {
return err
@@ -393,8 +394,8 @@ func CheckScheduleWithResourceOrder(ctx context.Context, veleroCLI, veleroNamesp
}
}
func CheckBackupWithResourceOrder(ctx context.Context, veleroCLI, veleroNamespace, backupName string, order map[string]string) error {
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "get", "backup", backupName, "-ojson")
func CheckBackupWithResourceOrder(ctx context.Context, veleroCLI, veleroNamespace, backupName string, orderResources map[string]string) error {
checkCMD := exec.CommandContext(ctx, veleroCLI, "--namespace", veleroNamespace, "get", "backup", backupName, "-o", "json")
jsonBuf, err := common.CMDExecWithOutput(checkCMD)
if err != nil {
return err
@@ -407,10 +408,10 @@ func CheckBackupWithResourceOrder(ctx context.Context, veleroCLI, veleroNamespac
if backup.Status.Phase != velerov1api.BackupPhaseCompleted {
return errors.Errorf("Unexpected restore phase got %s, expecting %s", backup.Status.Phase, velerov1api.BackupPhaseCompleted)
}
if reflect.DeepEqual(backup.Spec.OrderedResources, order) {
if reflect.DeepEqual(backup.Spec.OrderedResources, orderResources) {
return nil
} else {
return fmt.Errorf("resource order %v set in backup command is not equal with order %v stored in backup cr", order, backup.Spec.OrderedResources)
return fmt.Errorf("resource order %v set in backup command is not equal with order %v stored in backup cr", orderResources, backup.Spec.OrderedResources)
}
}
@@ -452,7 +453,7 @@ func VeleroBackupNamespace(ctx context.Context, veleroCLI, veleroNamespace strin
args = append(args, "--snapshot-volumes=false")
} // if "--snapshot-volumes" is not provide, snapshot should be taken as default behavior.
} else { // DefaultVolumesToFsBackup is false
// Althrough DefaultVolumesToFsBackup is false, but probably DefaultVolumesToFsBackup
// Although DefaultVolumesToFsBackup is false, but probably DefaultVolumesToFsBackup
// was set to true in installation CLI in snapshot volume test, so set DefaultVolumesToFsBackup
// to false specifically to make sure volume snapshot was taken
if backupCfg.UseVolumeSnapshots {
@@ -462,7 +463,7 @@ func VeleroBackupNamespace(ctx context.Context, veleroCLI, veleroNamespace strin
args = append(args, "--default-volumes-to-fs-backup=false")
}
}
// Also Althrough DefaultVolumesToFsBackup is false, but probably DefaultVolumesToFsBackup
// Although DefaultVolumesToFsBackup is false, but probably DefaultVolumesToFsBackup
// was set to true in installation CLI in FS volume backup test, so do nothing here, no DefaultVolumesToFsBackup
// appear in backup CLI
}
@@ -1181,46 +1182,29 @@ func GetLatestSuccessBackupsFromBSL(ctx context.Context, veleroCLI, bslName stri
return backups[0], nil
}
func GetScheduledBackupsCreationTime(ctx context.Context, veleroCLI, bslName, scheduleName string) ([]string, error) {
var creationTimes []string
backups, err := GetBackupsCreationTime(ctx, veleroCLI, bslName)
if err != nil {
return nil, err
}
for _, b := range backups {
if strings.Contains(b, scheduleName) {
creationTimes = append(creationTimes, b)
}
}
return creationTimes, nil
}
func GetBackupsCreationTime(ctx context.Context, veleroCLI, bslName string) ([]string, error) {
args1 := []string{"get", "backups"}
createdTime := "$1,\",\" $5,$6,$7,$8"
if strings.TrimSpace(bslName) != "" {
args1 = append(args1, "-l", "velero.io/storage-location="+bslName)
}
cmds := []*common.OsCommandLine{}
func GetBackupsForSchedule(
ctx context.Context,
client kbclient.Client,
scheduleName string,
namespace string,
) ([]velerov1api.Backup, error) {
backupList := new(velerov1api.BackupList)
cmd := &common.OsCommandLine{
Cmd: veleroCLI,
Args: args1,
if err := client.List(
ctx,
backupList,
&kbclient.ListOptions{
Namespace: namespace,
LabelSelector: labels.SelectorFromSet(map[string]string{
velerov1api.ScheduleNameLabel: scheduleName,
}),
},
); err != nil {
return nil, fmt.Errorf("failed to list backup in %s namespace for schedule %s: %s",
namespace, scheduleName, err.Error())
}
cmds = append(cmds, cmd)
cmd = &common.OsCommandLine{
Cmd: "awk",
Args: []string{"{print " + createdTime + "}"},
}
cmds = append(cmds, cmd)
cmd = &common.OsCommandLine{
Cmd: "tail",
Args: []string{"-n", "+2"},
}
cmds = append(cmds, cmd)
return common.GetListByCmdPipes(ctx, cmds)
return backupList.Items, nil
}
func GetAllBackups(ctx context.Context, veleroCLI string) ([]string, error) {