|
| 1 | +/* |
| 2 | +Copyright 2026 Flant JSC |
| 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 | + |
| 17 | +// Package precreatedcvi provides suite-level lifecycle (bootstrap and cleanup) for |
| 18 | +// precreated ClusterVirtualImages used by e2e tests. |
| 19 | +package precreatedcvi |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + |
| 30 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 31 | + "github.com/deckhouse/virtualization/test/e2e/internal/config" |
| 32 | + "github.com/deckhouse/virtualization/test/e2e/internal/framework" |
| 33 | + "github.com/deckhouse/virtualization/test/e2e/internal/object" |
| 34 | + "github.com/deckhouse/virtualization/test/e2e/internal/util" |
| 35 | +) |
| 36 | + |
| 37 | +const labelKey = "v12n-e2e-precreated" |
| 38 | + |
| 39 | +// PrecreatedCVIManager runs bootstrap and cleanup of precreated CVIs for the e2e suite. |
| 40 | +// The list of CVIs is loaded once during Bootstrap and reused in Cleanup. |
| 41 | +type PrecreatedCVIManager struct { |
| 42 | + cvis []*v1alpha2.ClusterVirtualImage |
| 43 | +} |
| 44 | + |
| 45 | +// NewPrecreatedCVIManager returns a new precreated CVI manager. |
| 46 | +func NewPrecreatedCVIManager() *PrecreatedCVIManager { |
| 47 | + return &PrecreatedCVIManager{} |
| 48 | +} |
| 49 | + |
| 50 | +// Bootstrap creates or reuses precreated CVIs in the cluster, then waits until all are ready. |
| 51 | +// Call once from SynchronizedBeforeSuite (process 1). |
| 52 | +func (m *PrecreatedCVIManager) Bootstrap(ctx context.Context) { |
| 53 | + GinkgoHelper() |
| 54 | + |
| 55 | + m.cvis = object.PrecreatedClusterVirtualImages() |
| 56 | + |
| 57 | + for _, cvi := range m.cvis { |
| 58 | + By(fmt.Sprintf("Create or reuse precreated CVI %q in the cluster", cvi.Name)) |
| 59 | + created, err := m.createOrReuse(ctx, cvi) |
| 60 | + Expect(err).NotTo(HaveOccurred()) |
| 61 | + if created { |
| 62 | + By(fmt.Sprintf("Precreated CVI %q has been created", cvi.Name)) |
| 63 | + } else { |
| 64 | + By(fmt.Sprintf("Precreated CVI %q already exists and will be reused", cvi.Name)) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + By("Wait until all precreated CVIs are ready") |
| 69 | + util.UntilObjectPhase(string(v1alpha2.ImageReady), framework.LongTimeout, m.cvisAsObjects()...) |
| 70 | + |
| 71 | + for _, cvi := range m.cvis { |
| 72 | + By(fmt.Sprintf("Precreated CVI %q is ready", cvi.Name)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Cleanup deletes precreated CVIs when both POST_CLEANUP and PRECREATED_CVI_CLEANUP allow it. |
| 77 | +// Call from SynchronizedAfterSuite (process 1). Uses the same CVI list as Bootstrap; if Bootstrap |
| 78 | +// was not run, the list is loaded from object so that cleanup can still run. |
| 79 | +func (m *PrecreatedCVIManager) Cleanup(ctx context.Context) { |
| 80 | + GinkgoHelper() |
| 81 | + |
| 82 | + if !config.IsCleanUpNeeded() { |
| 83 | + return |
| 84 | + } |
| 85 | + if !config.IsPrecreatedCVICleanupNeeded() { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + if len(m.cvis) == 0 { |
| 90 | + m.cvis = object.PrecreatedClusterVirtualImages() |
| 91 | + } |
| 92 | + |
| 93 | + f := framework.NewFramework("") |
| 94 | + err := f.Delete(ctx, m.cvisAsObjects()...) |
| 95 | + Expect(err).NotTo(HaveOccurred(), "Failed to delete precreated CVIs") |
| 96 | +} |
| 97 | + |
| 98 | +func (m *PrecreatedCVIManager) createOrReuse(ctx context.Context, cvi *v1alpha2.ClusterVirtualImage) (bool, error) { |
| 99 | + applyLabel(cvi) |
| 100 | + |
| 101 | + err := framework.GetClients().GenericClient().Create(ctx, cvi) |
| 102 | + if err == nil { |
| 103 | + return true, nil |
| 104 | + } |
| 105 | + if !k8serrors.IsAlreadyExists(err) { |
| 106 | + return false, err |
| 107 | + } |
| 108 | + return false, framework.GetClients().GenericClient().Get(ctx, crclient.ObjectKeyFromObject(cvi), cvi) |
| 109 | +} |
| 110 | + |
| 111 | +func (m *PrecreatedCVIManager) cvisAsObjects() []crclient.Object { |
| 112 | + objs := make([]crclient.Object, 0, len(m.cvis)) |
| 113 | + for _, cvi := range m.cvis { |
| 114 | + objs = append(objs, cvi) |
| 115 | + } |
| 116 | + return objs |
| 117 | +} |
| 118 | + |
| 119 | +func applyLabel(cvi *v1alpha2.ClusterVirtualImage) { |
| 120 | + labels := cvi.GetLabels() |
| 121 | + if labels == nil { |
| 122 | + labels = make(map[string]string) |
| 123 | + } |
| 124 | + labels[labelKey] = "true" |
| 125 | + cvi.SetLabels(labels) |
| 126 | +} |
0 commit comments