|
| 1 | +/* |
| 2 | +Copyright the Velero contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package bslmgmt |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/google/uuid" |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + appsv1api "k8s.io/api/apps/v1" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" |
| 32 | + . "github.com/vmware-tanzu/velero/test" |
| 33 | + . "github.com/vmware-tanzu/velero/test/util/k8s" |
| 34 | + . "github.com/vmware-tanzu/velero/test/util/kibishii" |
| 35 | + . "github.com/vmware-tanzu/velero/test/util/velero" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + startupValidationTestNs = "bsl-startup-validation" |
| 40 | +) |
| 41 | + |
| 42 | +// BackupRepositoryStartupValidation tests that backup repositories are validated |
| 43 | +// against BSL configuration on Velero startup and invalidated if configuration has changed |
| 44 | +func BackupRepositoryStartupValidation() { |
| 45 | + var ( |
| 46 | + veleroCfg VeleroConfig |
| 47 | + ) |
| 48 | + veleroCfg = VeleroCfg |
| 49 | + veleroCfg.UseVolumeSnapshots = false |
| 50 | + veleroCfg.UseNodeAgent = true |
| 51 | + |
| 52 | + BeforeEach(func() { |
| 53 | + var err error |
| 54 | + flag.Parse() |
| 55 | + UUIDgen, err = uuid.NewRandom() |
| 56 | + Expect(err).To(Succeed()) |
| 57 | + if InstallVelero { |
| 58 | + Expect(PrepareVelero(context.Background(), "BSL Startup Validation", veleroCfg)).To(Succeed()) |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + AfterEach(func() { |
| 63 | + if CurrentSpecReport().Failed() && veleroCfg.FailFast { |
| 64 | + fmt.Println("Test case failed and fail fast is enabled. Skip resource clean up.") |
| 65 | + } else { |
| 66 | + By("Clean backups after test", func() { |
| 67 | + veleroCfg.ClientToInstallVelero = veleroCfg.DefaultClient |
| 68 | + DeleteAllBackups(context.Background(), &veleroCfg) |
| 69 | + }) |
| 70 | + By(fmt.Sprintf("Delete sample workload namespace %s", startupValidationTestNs), func() { |
| 71 | + Expect(DeleteNamespace(context.Background(), *veleroCfg.ClientToInstallVelero, startupValidationTestNs, |
| 72 | + true)).To(Succeed(), fmt.Sprintf("failed to delete the namespace %q", |
| 73 | + startupValidationTestNs)) |
| 74 | + }) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + When("BSL configuration changes while Velero is not running", func() { |
| 79 | + It("Should invalidate backup repositories on startup", func() { |
| 80 | + oneHourTimeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*60) |
| 81 | + defer ctxCancel() |
| 82 | + |
| 83 | + backupName := "backup-" + UUIDgen.String() |
| 84 | + backupLocation := "default" |
| 85 | + |
| 86 | + By("Create namespace for sample workload", func() { |
| 87 | + Expect(CreateNamespace(oneHourTimeout, *veleroCfg.ClientToInstallVelero, startupValidationTestNs)).To(Succeed()) |
| 88 | + }) |
| 89 | + |
| 90 | + By("Deploy sample workload of Kibishii", func() { |
| 91 | + Expect(KibishiiPrepareBeforeBackup( |
| 92 | + oneHourTimeout, |
| 93 | + *veleroCfg.ClientToInstallVelero, |
| 94 | + veleroCfg.CloudProvider, |
| 95 | + startupValidationTestNs, |
| 96 | + veleroCfg.RegistryCredentialFile, |
| 97 | + veleroCfg.Features, |
| 98 | + veleroCfg.KibishiiDirectory, |
| 99 | + DefaultKibishiiData, |
| 100 | + veleroCfg.ImageRegistryProxy, |
| 101 | + veleroCfg.WorkerOS, |
| 102 | + )).To(Succeed()) |
| 103 | + }) |
| 104 | + |
| 105 | + var BackupCfg BackupConfig |
| 106 | + BackupCfg.BackupName = backupName |
| 107 | + BackupCfg.Namespace = startupValidationTestNs |
| 108 | + BackupCfg.BackupLocation = backupLocation |
| 109 | + BackupCfg.UseVolumeSnapshots = false |
| 110 | + BackupCfg.DefaultVolumesToFsBackup = true |
| 111 | + |
| 112 | + By("Backup sample workload to establish BackupRepository", func() { |
| 113 | + Expect(VeleroBackupNamespace(oneHourTimeout, veleroCfg.VeleroCLI, |
| 114 | + veleroCfg.VeleroNamespace, BackupCfg)).To(Succeed(), func() string { |
| 115 | + RunDebug(context.Background(), veleroCfg.VeleroCLI, veleroCfg.VeleroNamespace, BackupCfg.BackupName, "") |
| 116 | + return "Fail to backup workload" |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + By("Verify backup completed successfully", func() { |
| 121 | + Expect(WaitForBackupToBeCreated(context.Background(), backupName, 10*time.Minute, &veleroCfg)).To(Succeed()) |
| 122 | + }) |
| 123 | + |
| 124 | + By("Verify BackupRepository is created and ready", func() { |
| 125 | + Expect(BackupRepositoriesCountShouldBe(context.Background(), |
| 126 | + veleroCfg.VeleroNamespace, startupValidationTestNs+"-"+backupLocation, 1)).To(Succeed()) |
| 127 | + |
| 128 | + // Get the BackupRepository and verify it's ready |
| 129 | + repoList := &velerov1api.BackupRepositoryList{} |
| 130 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.List(oneHourTimeout, repoList, |
| 131 | + client.InNamespace(veleroCfg.VeleroNamespace))).To(Succeed()) |
| 132 | + |
| 133 | + var targetRepo *velerov1api.BackupRepository |
| 134 | + for i := range repoList.Items { |
| 135 | + if repoList.Items[i].Spec.VolumeNamespace == startupValidationTestNs { |
| 136 | + targetRepo = &repoList.Items[i] |
| 137 | + break |
| 138 | + } |
| 139 | + } |
| 140 | + Expect(targetRepo).NotTo(BeNil(), "BackupRepository not found") |
| 141 | + Expect(targetRepo.Status.Phase).To(Equal(velerov1api.BackupRepositoryPhaseReady)) |
| 142 | + }) |
| 143 | + |
| 144 | + // Store original BSL configuration |
| 145 | + originalBSL := &velerov1api.BackupStorageLocation{} |
| 146 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 147 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: backupLocation}, |
| 148 | + originalBSL)).To(Succeed()) |
| 149 | + |
| 150 | + originalBucket := "" |
| 151 | + originalPrefix := "" |
| 152 | + if originalBSL.Spec.StorageType.ObjectStorage != nil { |
| 153 | + originalBucket = originalBSL.Spec.StorageType.ObjectStorage.Bucket |
| 154 | + originalPrefix = originalBSL.Spec.StorageType.ObjectStorage.Prefix |
| 155 | + } |
| 156 | + |
| 157 | + By("Scale down Velero deployment to simulate shutdown", func() { |
| 158 | + deployment := &appsv1api.Deployment{} |
| 159 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 160 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: "velero"}, |
| 161 | + deployment)).To(Succeed()) |
| 162 | + |
| 163 | + zero := int32(0) |
| 164 | + deployment.Spec.Replicas = &zero |
| 165 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Update(oneHourTimeout, |
| 166 | + deployment)).To(Succeed()) |
| 167 | + |
| 168 | + // Wait for deployment to scale down |
| 169 | + Eventually(func() int32 { |
| 170 | + d := &appsv1api.Deployment{} |
| 171 | + err := veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 172 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: "velero"}, |
| 173 | + d) |
| 174 | + if err != nil { |
| 175 | + return -1 |
| 176 | + } |
| 177 | + return d.Status.ReadyReplicas |
| 178 | + }, 2*time.Minute, 5*time.Second).Should(Equal(int32(0))) |
| 179 | + }) |
| 180 | + |
| 181 | + By("Modify BSL configuration (change prefix)", func() { |
| 182 | + bsl := &velerov1api.BackupStorageLocation{} |
| 183 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 184 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: backupLocation}, |
| 185 | + bsl)).To(Succeed()) |
| 186 | + |
| 187 | + if bsl.Spec.StorageType.ObjectStorage != nil { |
| 188 | + fmt.Printf("Original BSL prefix: %s\n", bsl.Spec.StorageType.ObjectStorage.Prefix) |
| 189 | + // Change the prefix to trigger validation failure |
| 190 | + bsl.Spec.StorageType.ObjectStorage.Prefix = originalPrefix + "-modified" |
| 191 | + fmt.Printf("Modified BSL prefix: %s\n", bsl.Spec.StorageType.ObjectStorage.Prefix) |
| 192 | + } |
| 193 | + |
| 194 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Update(oneHourTimeout, |
| 195 | + bsl)).To(Succeed()) |
| 196 | + fmt.Println("BSL configuration successfully modified") |
| 197 | + }) |
| 198 | + |
| 199 | + By("Scale up Velero deployment to simulate startup", func() { |
| 200 | + deployment := &appsv1api.Deployment{} |
| 201 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 202 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: "velero"}, |
| 203 | + deployment)).To(Succeed()) |
| 204 | + |
| 205 | + one := int32(1) |
| 206 | + deployment.Spec.Replicas = &one |
| 207 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Update(oneHourTimeout, |
| 208 | + deployment)).To(Succeed()) |
| 209 | + |
| 210 | + // Wait for deployment to scale up |
| 211 | + Eventually(func() int32 { |
| 212 | + d := &appsv1api.Deployment{} |
| 213 | + err := veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 214 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: "velero"}, |
| 215 | + d) |
| 216 | + if err != nil { |
| 217 | + return -1 |
| 218 | + } |
| 219 | + return d.Status.ReadyReplicas |
| 220 | + }, 2*time.Minute, 5*time.Second).Should(Equal(int32(1))) |
| 221 | + |
| 222 | + // Wait a bit for controller to start |
| 223 | + time.Sleep(5 * time.Second) |
| 224 | + |
| 225 | + // Force a reconciliation by adding/updating a label on the repository |
| 226 | + // This ensures the startup validation runs |
| 227 | + repoList := &velerov1api.BackupRepositoryList{} |
| 228 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.List(oneHourTimeout, repoList, |
| 229 | + client.InNamespace(veleroCfg.VeleroNamespace))).To(Succeed()) |
| 230 | + |
| 231 | + foundRepo := false |
| 232 | + for i := range repoList.Items { |
| 233 | + if repoList.Items[i].Spec.VolumeNamespace == startupValidationTestNs { |
| 234 | + repo := &repoList.Items[i] |
| 235 | + fmt.Printf("Found repository %s to update, current annotations: %+v\n", repo.Name, repo.Annotations) |
| 236 | + if repo.Labels == nil { |
| 237 | + repo.Labels = make(map[string]string) |
| 238 | + } |
| 239 | + repo.Labels["test-trigger"] = "reconcile" |
| 240 | + err := veleroCfg.ClientToInstallVelero.Kubebuilder.Update(oneHourTimeout, repo) |
| 241 | + Expect(err).NotTo(HaveOccurred(), "Failed to update repository to trigger reconciliation") |
| 242 | + fmt.Printf("Successfully updated repository %s with trigger label\n", repo.Name) |
| 243 | + foundRepo = true |
| 244 | + break |
| 245 | + } |
| 246 | + } |
| 247 | + Expect(foundRepo).To(BeTrue(), "No repository found for namespace " + startupValidationTestNs) |
| 248 | + |
| 249 | + // Give controller time to run startup validation |
| 250 | + // The validation now runs synchronously on first reconciliation |
| 251 | + time.Sleep(10 * time.Second) |
| 252 | + }) |
| 253 | + |
| 254 | + By("Verify BackupRepository is invalidated with correct message", func() { |
| 255 | + Eventually(func() string { |
| 256 | + repoList := &velerov1api.BackupRepositoryList{} |
| 257 | + err := veleroCfg.ClientToInstallVelero.Kubebuilder.List(oneHourTimeout, repoList, |
| 258 | + client.InNamespace(veleroCfg.VeleroNamespace)) |
| 259 | + if err != nil { |
| 260 | + return fmt.Sprintf("Error listing repos: %v", err) |
| 261 | + } |
| 262 | + |
| 263 | + for _, repo := range repoList.Items { |
| 264 | + if repo.Spec.VolumeNamespace == startupValidationTestNs { |
| 265 | + // Log the current status for debugging |
| 266 | + fmt.Printf("Repository %s: Phase=%s, Message=%s\n", |
| 267 | + repo.Name, repo.Status.Phase, repo.Status.Message) |
| 268 | + if repo.Status.Message != "" { |
| 269 | + return repo.Status.Message |
| 270 | + } |
| 271 | + return fmt.Sprintf("Repository found but no message, phase: %s", repo.Status.Phase) |
| 272 | + } |
| 273 | + } |
| 274 | + return "Repository not found for namespace " + startupValidationTestNs |
| 275 | + }, 3*time.Minute, 10*time.Second).Should(ContainSubstring("BSL configuration changed while Velero was not running")) |
| 276 | + }) |
| 277 | + |
| 278 | + By("Restore original BSL configuration", func() { |
| 279 | + bsl := &velerov1api.BackupStorageLocation{} |
| 280 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Get(oneHourTimeout, |
| 281 | + types.NamespacedName{Namespace: veleroCfg.VeleroNamespace, Name: backupLocation}, |
| 282 | + bsl)).To(Succeed()) |
| 283 | + |
| 284 | + if bsl.Spec.StorageType.ObjectStorage != nil { |
| 285 | + bsl.Spec.StorageType.ObjectStorage.Bucket = originalBucket |
| 286 | + bsl.Spec.StorageType.ObjectStorage.Prefix = originalPrefix |
| 287 | + } |
| 288 | + |
| 289 | + Expect(veleroCfg.ClientToInstallVelero.Kubebuilder.Update(oneHourTimeout, |
| 290 | + bsl)).To(Succeed()) |
| 291 | + }) |
| 292 | + |
| 293 | + By("Verify BackupRepository recovers to Ready state", func() { |
| 294 | + Eventually(func() velerov1api.BackupRepositoryPhase { |
| 295 | + repoList := &velerov1api.BackupRepositoryList{} |
| 296 | + err := veleroCfg.ClientToInstallVelero.Kubebuilder.List(oneHourTimeout, repoList, |
| 297 | + client.InNamespace(veleroCfg.VeleroNamespace)) |
| 298 | + if err != nil { |
| 299 | + return "" |
| 300 | + } |
| 301 | + |
| 302 | + for _, repo := range repoList.Items { |
| 303 | + if repo.Spec.VolumeNamespace == startupValidationTestNs { |
| 304 | + return repo.Status.Phase |
| 305 | + } |
| 306 | + } |
| 307 | + return "" |
| 308 | + }, 5*time.Minute, 10*time.Second).Should(Equal(velerov1api.BackupRepositoryPhaseReady)) |
| 309 | + }) |
| 310 | + |
| 311 | + fmt.Printf("|| EXPECTED || - Backup repository startup validation test completed successfully\n") |
| 312 | + }) |
| 313 | + }) |
| 314 | +} |
0 commit comments