Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -31,6 +31,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 @@ -45,14 +46,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 Down Expand Up @@ -177,6 +178,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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ 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"),
)

DescribeTable("should truncate long names to respect limits",
Expand All @@ -91,6 +92,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,11 @@ 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)
if vdSnapshot.Status.VolumeSnapshotName == "" {
vdSnapshot.Status.VolumeSnapshotName = supplements.NewGenerator("vds", vdSnapshot.Name, vdSnapshot.Namespace, vdSnapshot.UID).CommonSupplement().Name
}

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 @@ -333,7 +338,7 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vdSnapshot *v1alpha2.Virtu
vs = &vsv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Annotations: anno,
Name: vdSnapshot.Name,
Name: vdSnapshot.Status.VolumeSnapshotName,
Namespace: vdSnapshot.Namespace,
OwnerReferences: []metav1.OwnerReference{
service.MakeOwnerReference(vdSnapshot),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s CreatePersistentVolumeClaimStep) buildPVC(vi *v1alpha2.VirtualImage, vs
DataSource: &corev1.TypedLocalObjectReference{
APIGroup: ptr.To(vs.GroupVersionKind().Group),
Kind: vs.Kind,
Name: vi.Spec.DataSource.ObjectRef.Name,
Name: vs.Name,
},
}

Expand Down
Loading