Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/deckhouse/virtualization-controller/pkg/common/object"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
)

Expand All @@ -44,14 +45,16 @@ func NewSecretRestorer(client client.Client) *SecretRestorer {
}

func (r SecretRestorer) Store(ctx context.Context, vm *v1alpha2.VirtualMachine, vmSnapshot *v1alpha2.VirtualMachineSnapshot) (*corev1.Secret, error) {
secretName := supplements.NewGenerator("vms", vmSnapshot.Name, vmSnapshot.Namespace, vmSnapshot.UID).CommonSupplement()

secret := corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: vmSnapshot.Name,
Namespace: vmSnapshot.Namespace,
Name: secretName.Name,
Namespace: secretName.Namespace,
OwnerReferences: []metav1.OwnerReference{
service.MakeOwnerReference(vmSnapshot),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Generator interface {
UploaderTLSSecretForIngress() types.NamespacedName
ImagePullSecret() types.NamespacedName
NetworkPolicy() types.NamespacedName
CommonSupplement() types.NamespacedName

LegacyBounderPod() types.NamespacedName
LegacyImporterPod() types.NamespacedName
Expand All @@ -70,6 +71,7 @@ type Generator interface {
LegacyDVCRAuthSecretForDV() types.NamespacedName
LegacyUploaderTLSSecretForIngress() types.NamespacedName
LegacyImagePullSecret() types.NamespacedName
LegacySnapshotSupplement() types.NamespacedName
}

// Generator calculates names for supplemental resources, e.g. ImporterPod, AuthSecret or CABundleConfigMap.
Expand Down Expand Up @@ -177,6 +179,11 @@ func (g *generator) NetworkPolicy() types.NamespacedName {
return g.generateName(tplCommon, kvalidation.DNS1123SubdomainMaxLength)
}

// CommonSupplement generates name for common supplemental resources with d8v-<prefix>-<name>-<uid> format.
func (g *generator) CommonSupplement() types.NamespacedName {
return g.generateName(tplCommon, kvalidation.DNS1123SubdomainMaxLength)
}

// PersistentVolumeClaim generates name for underlying PersistentVolumeClaim.
// PVC is always one for vmd/vmi, so prefix is used.
func (g *generator) PersistentVolumeClaim() types.NamespacedName {
Expand Down Expand Up @@ -270,3 +277,11 @@ func (g *generator) LegacyDataVolume() types.NamespacedName {
func (g *generator) LegacyPersistentVolumeClaim() types.NamespacedName {
return g.LegacyDataVolume()
}

// LegacySnapshotSupplement generates old format name for snapshot-related resources.
func (g *generator) LegacySnapshotSupplement() types.NamespacedName {
return types.NamespacedName{
Name: g.name,
Namespace: g.namespace,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,18 @@ var _ = Describe("Generator", func() {
Entry("DataVolume", func(g Generator) types.NamespacedName { return g.DataVolume() }, "vi"),
Entry("PersistentVolumeClaim", func(g Generator) types.NamespacedName { return g.PersistentVolumeClaim() }, "vi"),
Entry("NetworkPolicy", func(g Generator) types.NamespacedName { return g.NetworkPolicy() }, "vi"),
Entry("CommonSupplement", func(g Generator) types.NamespacedName { return g.CommonSupplement() }, "vi"),
)

It("should generate legacy snapshot supplement name without prefix or UID", func() {
name := "test-snapshot"
gen = NewGenerator("vms", name, namespace, uid)
result := gen.LegacySnapshotSupplement()

Expect(result.Name).To(Equal(name))
Expect(result.Namespace).To(Equal(namespace))
})

DescribeTable("should truncate long names to respect limits",
func(method func(Generator) types.NamespacedName, maxLength int) {
name := strings.Repeat("very-long-resource-name-", 30)
Expand All @@ -91,6 +101,7 @@ var _ = Describe("Generator", func() {
Entry("DataVolume - 253 limit", func(g Generator) types.NamespacedName { return g.DataVolume() }, kvalidation.DNS1123SubdomainMaxLength),
Entry("PersistentVolumeClaim - 253 limit", func(g Generator) types.NamespacedName { return g.PersistentVolumeClaim() }, kvalidation.DNS1123SubdomainMaxLength),
Entry("NetworkPolicy - 253 limit", func(g Generator) types.NamespacedName { return g.NetworkPolicy() }, kvalidation.DNS1123SubdomainMaxLength),
Entry("CommonSupplement - 253 limit", func(g Generator) types.NamespacedName { return g.CommonSupplement() }, kvalidation.DNS1123SubdomainMaxLength),
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (h DeletionHandler) Handle(ctx context.Context, vdSnapshot *v1alpha2.Virtua
log := logger.FromContext(ctx).With(logger.SlogHandler(deletionHandlerName))

if vdSnapshot.DeletionTimestamp != nil {
vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Name, vdSnapshot.Namespace)
vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Status.VolumeSnapshotName, vdSnapshot.Namespace)
if err != nil {
return reconcile.Result{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
"github.com/deckhouse/virtualization-controller/pkg/logger"
"github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdscondition"
Expand All @@ -58,7 +59,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *v1alpha2.Virtu

defer func() { conditions.SetCondition(cb, &vdSnapshot.Status.Conditions) }()

vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Name, vdSnapshot.Namespace)
vs, err := h.snapshotter.GetVolumeSnapshot(ctx, vdSnapshot.Status.VolumeSnapshotName, vdSnapshot.Namespace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should generate VolumeSnapshotName beforehand to prevent any panics related to VirtualDiskSnapshot status.

if err != nil {
setPhaseConditionToFailed(cb, &vdSnapshot.Status.Phase, err)
return reconcile.Result{}, err
Expand Down Expand Up @@ -316,11 +317,13 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *v1alpha2.Virtu
}
}

vsName := supplements.NewGenerator("vds", vdSnapshot.Name, vdSnapshot.Namespace, vdSnapshot.UID).CommonSupplement()

vs = &vsv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Annotations: anno,
Name: vdSnapshot.Name,
Namespace: vdSnapshot.Namespace,
Name: vsName.Name,
Namespace: vsName.Namespace,
OwnerReferences: []metav1.OwnerReference{
service.MakeOwnerReference(vdSnapshot),
},
Expand Down
Loading