Skip to content

Commit

Permalink
Set Namespace in v1a1 VirtualMachineImage ProviderRef
Browse files Browse the repository at this point in the history
The VirtualMachineImage provided by the namespace scoped
ContentLibraryItem so the Namespace field should have been
set in the ProviderRef.

Populate the namespace field when down converting the image
to v1a1.
  • Loading branch information
bryanv committed Dec 14, 2023
1 parent 70d22b9 commit fe4cd88
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/virtualmachineimage_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ func (dst *VirtualMachineImage) ConvertFrom(srcRaw conversion.Hub) error {
return err
}

if dst.Spec.ProviderRef.Name != "" {
// The Namespace isn't a field in the v1a2 LocalObjectRef so backfill the namespace here.
// The provider is always in the same namespace.
dst.Spec.ProviderRef.Namespace = src.Namespace
}

return nil
}

Expand Down
44 changes: 44 additions & 0 deletions api/v1alpha1/virtualmachineimage_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1_test

import (
"testing"

. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/vmware-tanzu/vm-operator/api/v1alpha1"
nextver "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
nextver_common "github.com/vmware-tanzu/vm-operator/api/v1alpha2/common"
)

func TestVirtualMachineImageConversion(t *testing.T) {
g := NewWithT(t)

t.Run("VirtualMachineImage hub-spoke-hub", func(t *testing.T) {
hub := &nextver.VirtualMachineImage{
ObjectMeta: metav1.ObjectMeta{
Name: "test-image",
Namespace: "my-namespace",
},
Spec: nextver.VirtualMachineImageSpec{
ProviderRef: nextver_common.LocalObjectRef{
APIVersion: "vmware.com/v1",
Kind: "ImageProvider",
Name: "my-image",
},
},
}

spoke := &v1alpha1.VirtualMachineImage{}
g.Expect(spoke.ConvertFrom(hub)).To(Succeed())

g.Expect(spoke.Spec.ProviderRef.APIVersion).To(Equal("vmware.com/v1"))
g.Expect(spoke.Spec.ProviderRef.Kind).To(Equal("ImageProvider"))
g.Expect(spoke.Spec.ProviderRef.Name).To(Equal("my-image"))
g.Expect(spoke.Spec.ProviderRef.Namespace).To(Equal("my-namespace"))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (r *Reconciler) setUpVMIFromCLItem(ctx *context.ContentLibraryItemContext)
APIVersion: clItem.APIVersion,
Kind: clItem.Kind,
Name: clItem.Name,
Namespace: clItem.Namespace,
}
vmi.Status.ImageName = clItem.Status.Name
if clItem.Status.ContentLibraryRef != nil {
Expand Down
1 change: 1 addition & 0 deletions controllers/contentlibrary/v1alpha1/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func GetExpectedVMIFrom(clItem imgregv1a1.ContentLibraryItem,
APIVersion: clItem.APIVersion,
Kind: clItem.Kind,
Name: clItem.Name,
Namespace: clItem.Namespace,
},
},
Status: vmopv1.VirtualMachineImageStatus{
Expand Down

0 comments on commit fe4cd88

Please sign in to comment.