Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing labels on CVMI resources #873

Merged
merged 1 commit into from
Jan 30, 2025
Merged
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
37 changes: 37 additions & 0 deletions controllers/contentlibrary/utils/controller_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (r *Reconciler) ReconcileNormal(
}()

if err := r.setUpVMIFromCLItem(
ctx,
cliObj,
*cliSpec,
*cliStatus,
Expand Down Expand Up @@ -384,6 +385,7 @@ func (r *Reconciler) ReconcileNormal(
// setUpVMIFromCLItem sets up the VirtualMachineImage fields that
// are retrievable from the given ContentLibraryItem resource.
func (r *Reconciler) setUpVMIFromCLItem(
ctx context.Context,
cliObj client.Object,
cliSpec imgregv1a1.ContentLibraryItemSpec,
cliStatus imgregv1a1.ContentLibraryItemStatus,
Expand All @@ -407,6 +409,41 @@ func (r *Reconciler) setUpVMIFromCLItem(
Name: cliObj.GetName(),
}

if cliObj.GetNamespace() == "" {
var (
cliLabels = cliObj.GetLabels()
vmiLabels = vmiObj.GetLabels()
labelKeyPrefix = TKGServiceTypeLabelKeyPrefix
)

if pkgcfg.FromContext(ctx).Features.TKGMultipleCL {
labelKeyPrefix = MultipleCLServiceTypeLabelKeyPrefix

// Remove any labels on the VMI object that have a matching
// prefix do not also exist on the CLI resource.
for k := range vmiLabels {
if strings.HasPrefix(k, labelKeyPrefix) {
if _, ok := cliLabels[k]; !ok {
delete(vmiLabels, k)
}
}
}
}

// Copy the labels from the CLI object that match the given prefix
// to the VMI resource.
for k := range cliLabels {
if strings.HasPrefix(k, labelKeyPrefix) {
if vmiLabels == nil {
vmiLabels = map[string]string{}
}
vmiLabels[k] = ""
}
}

vmiObj.SetLabels(vmiLabels)
}

vmiStatus.Name = cliStatus.Name
vmiStatus.ProviderItemID = string(cliSpec.UUID)
vmiStatus.Type = string(cliStatus.Type)
Expand Down
190 changes: 189 additions & 1 deletion controllers/contentlibrary/utils/controller_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,87 @@ var _ = Describe("Reconcile",
})
})

When("Library item has TKG labels", func() {
var cliLabels map[string]string

BeforeEach(func() {
cliLabels = cliObj.GetLabels()
if cliLabels == nil {
cliLabels = map[string]string{}
}
})

When("Multiple Content Library feature is disabled", func() {
BeforeEach(func() {
pkgcfg.SetContext(ctx, func(config *pkgcfg.Config) {
config.Features.TKGMultipleCL = false
})

cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"2"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"2"] = ""

cliObj.SetLabels(cliLabels)
})

It("should not copy the feature or non-feature labels to the vmi", func() {
_, err := reconciler.Reconcile(context.Background(), req)
Expect(err).ToNot(HaveOccurred())

vmiObj, _, _ := getVMI(ctx, req.Namespace, vmiName)
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "2"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "2"))
})
})
When("Multiple Content Library feature is enabled", func() {
BeforeEach(func() {
pkgcfg.SetContext(ctx, func(config *pkgcfg.Config) {
config.Features.TKGMultipleCL = true
})

cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"2"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"2"] = ""

cliObj.SetLabels(cliLabels)
})

JustBeforeEach(func() {
vmiObj := newVMI(
ctx,
req.Namespace,
vmiName,
vmopv1.VirtualMachineImageStatus{
ProviderContentVersion: "stale",
Firmware: "should-be-updated",
})
vmiLabels := vmiObj.GetLabels()
if vmiLabels == nil {
vmiLabels = map[string]string{}
}
vmiLabels[utils.TKGServiceTypeLabelKeyPrefix+"3"] = ""
vmiLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"3"] = ""
vmiObj.SetLabels(vmiLabels)
Expect(ctx.Client.Update(ctx, vmiObj)).To(Succeed())
})

It("should not copy the feature or non-feature labels to the vmi", func() {
_, err := reconciler.Reconcile(context.Background(), req)
Expect(err).ToNot(HaveOccurred())

vmiObj, _, _ := getVMI(ctx, req.Namespace, vmiName)
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "2"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "2"))
})
})
})

When("Library item resource is not security compliant", func() {

BeforeEach(func() {
Expand Down Expand Up @@ -402,6 +483,111 @@ var _ = Describe("Reconcile",
})

Context("ReconcileNormal", func() {

When("Library item has TKG labels", func() {
var cliLabels map[string]string

BeforeEach(func() {
cliLabels = cliObj.GetLabels()
if cliLabels == nil {
cliLabels = map[string]string{}
}
})

When("Multiple Content Library feature is disabled", func() {
BeforeEach(func() {
pkgcfg.SetContext(ctx, func(config *pkgcfg.Config) {
config.Features.TKGMultipleCL = false
})

cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"2"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"2"] = ""

cliObj.SetLabels(cliLabels)
})

JustBeforeEach(func() {
vmiObj := newVMI(
ctx,
req.Namespace,
vmiName,
vmopv1.VirtualMachineImageStatus{
ProviderContentVersion: "stale",
Firmware: "should-be-updated",
})
vmiLabels := vmiObj.GetLabels()
if vmiLabels == nil {
vmiLabels = map[string]string{}
}
vmiLabels[utils.TKGServiceTypeLabelKeyPrefix+"3"] = ""
vmiLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"3"] = ""
vmiObj.SetLabels(vmiLabels)
Expect(ctx.Client.Update(ctx, vmiObj)).To(Succeed())
})

It("should copy the non-feature labels to the vmi", func() {
_, err := reconciler.Reconcile(context.Background(), req)
Expect(err).ToNot(HaveOccurred())

vmiObj, _, _ := getVMI(ctx, req.Namespace, vmiName)
Expect(vmiObj.GetLabels()).To(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).To(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "2"))
Expect(vmiObj.GetLabels()).To(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "3"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "2"))
Expect(vmiObj.GetLabels()).To(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "3"))
})
})
When("Multiple Content Library feature is enabled", func() {
BeforeEach(func() {
pkgcfg.SetContext(ctx, func(config *pkgcfg.Config) {
config.Features.TKGMultipleCL = true
})

cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.TKGServiceTypeLabelKeyPrefix+"2"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"1"] = ""
cliLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"2"] = ""

cliObj.SetLabels(cliLabels)
})

JustBeforeEach(func() {
vmiObj := newVMI(
ctx,
req.Namespace,
vmiName,
vmopv1.VirtualMachineImageStatus{
ProviderContentVersion: "stale",
Firmware: "should-be-updated",
})
vmiLabels := vmiObj.GetLabels()
if vmiLabels == nil {
vmiLabels = map[string]string{}
}
vmiLabels[utils.TKGServiceTypeLabelKeyPrefix+"3"] = ""
vmiLabels[utils.MultipleCLServiceTypeLabelKeyPrefix+"3"] = ""
vmiObj.SetLabels(vmiLabels)
Expect(ctx.Client.Update(ctx, vmiObj)).To(Succeed())
})

It("should copy the feature labels to the vmi", func() {
_, err := reconciler.Reconcile(context.Background(), req)
Expect(err).ToNot(HaveOccurred())

vmiObj, _, _ := getVMI(ctx, req.Namespace, vmiName)
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "2"))
Expect(vmiObj.GetLabels()).To(HaveKey(utils.TKGServiceTypeLabelKeyPrefix + "3"))
Expect(vmiObj.GetLabels()).To(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "1"))
Expect(vmiObj.GetLabels()).To(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "2"))
Expect(vmiObj.GetLabels()).ToNot(HaveKey(utils.MultipleCLServiceTypeLabelKeyPrefix + "3"))
})
})
})

When("Library item resource is ready and security complaint", func() {
JustBeforeEach(func() {
// The dummy library item should meet these requirements.
Expand Down Expand Up @@ -525,7 +711,7 @@ func getVMI(
func newVMI(
ctx *builder.UnitTestContextForController,
namespace, name string,
status vmopv1.VirtualMachineImageStatus) {
status vmopv1.VirtualMachineImageStatus) client.Object {

spec := vmopv1.VirtualMachineImageSpec{
ProviderRef: &common.LocalObjectRef{
Expand All @@ -543,6 +729,7 @@ func newVMI(
Status: status,
}
ExpectWithOffset(1, ctx.Client.Create(ctx, &o)).To(Succeed())
return &o
}

o := vmopv1.ClusterVirtualMachineImage{
Expand All @@ -553,6 +740,7 @@ func newVMI(
Status: status,
}
ExpectWithOffset(1, ctx.Client.Create(ctx, &o)).To(Succeed())
return &o
}

func assertVMImageFromCLItem(
Expand Down