Execute performs a backup and checks the backup status
( ctx context.Context, crudClient client.Client, scheme *runtime.Scheme, namespace, backupFile string, onlyTargetStandbys bool, timeoutSeconds int, )
| 137 | |
| 138 | // Execute performs a backup and checks the backup status |
| 139 | func Execute( |
| 140 | ctx context.Context, |
| 141 | crudClient client.Client, |
| 142 | scheme *runtime.Scheme, |
| 143 | namespace, |
| 144 | backupFile string, |
| 145 | onlyTargetStandbys bool, |
| 146 | timeoutSeconds int, |
| 147 | ) *apiv1.Backup { |
| 148 | backupName, err := yaml.GetResourceNameFromYAML(scheme, backupFile) |
| 149 | gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 150 | gomega.Eventually(func() error { |
| 151 | _, stderr, err := run.Unchecked("kubectl apply -n " + namespace + " -f " + backupFile) |
| 152 | if err != nil { |
| 153 | return fmt.Errorf("could not create backup.\nStdErr: %v\nError: %v", stderr, err) |
| 154 | } |
| 155 | return nil |
| 156 | }, 60, objects.PollingTime).Should(gomega.Succeed()) |
| 157 | backupNamespacedName := types.NamespacedName{ |
| 158 | Namespace: namespace, |
| 159 | Name: backupName, |
| 160 | } |
| 161 | backup := &apiv1.Backup{} |
| 162 | // Verifying backup status |
| 163 | gomega.Eventually(func() (apiv1.BackupPhase, error) { |
| 164 | err = crudClient.Get(ctx, backupNamespacedName, backup) |
| 165 | return backup.Status.Phase, err |
| 166 | }, timeoutSeconds).Should(gomega.BeEquivalentTo(apiv1.BackupPhaseCompleted)) |
| 167 | gomega.Eventually(func() (string, error) { |
| 168 | err = crudClient.Get(ctx, backupNamespacedName, backup) |
| 169 | if err != nil { |
| 170 | return "", err |
| 171 | } |
| 172 | backupStatus := backup.GetStatus() |
| 173 | return backupStatus.BeginLSN, err |
| 174 | }, timeoutSeconds).ShouldNot(gomega.BeEmpty()) |
| 175 | |
| 176 | var cluster *apiv1.Cluster |
| 177 | gomega.Eventually(func() error { |
| 178 | var err error |
| 179 | cluster, err = clusterutils.Get(ctx, crudClient, namespace, backup.Spec.Cluster.Name) |
| 180 | return err |
| 181 | }, timeoutSeconds).ShouldNot(gomega.HaveOccurred()) |
| 182 | |
| 183 | backupStatus := backup.GetStatus() |
| 184 | if cluster.Spec.Backup != nil { |
| 185 | backupTarget := cluster.Spec.Backup.Target |
| 186 | if backup.Spec.Target != "" { |
| 187 | backupTarget = backup.Spec.Target |
| 188 | } |
| 189 | switch backupTarget { |
| 190 | case apiv1.BackupTargetPrimary, "": |
| 191 | gomega.Expect(backupStatus.InstanceID.PodName).To(gomega.BeEquivalentTo(cluster.Status.TargetPrimary)) |
| 192 | case apiv1.BackupTargetStandby: |
| 193 | gomega.Expect(backupStatus.InstanceID.PodName).To(gomega.BeElementOf(cluster.Status.InstanceNames)) |
| 194 | if onlyTargetStandbys { |
| 195 | gomega.Expect(backupStatus.InstanceID.PodName).NotTo(gomega.Equal(cluster.Status.TargetPrimary)) |
| 196 | } |