Skip to content

Commit

Permalink
GetActiveCDI() should return success if single resource in error state
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Henriksen <[email protected]>
  • Loading branch information
mhenriks committed Feb 22, 2024
1 parent d89ea29 commit 0ad107e
Show file tree
Hide file tree
Showing 6 changed files with 562 additions and 536 deletions.
618 changes: 291 additions & 327 deletions WORKSPACE

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/controller/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/kubevirt.io/controller-lifecycle-operator-sdk/api:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/log:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/log/zap:go_default_library",
],
Expand Down
16 changes: 10 additions & 6 deletions pkg/controller/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,23 @@ func GetActiveCDI(c client.Client) (*cdiv1.CDI, error) {
return nil, err
}

if len(crList.Items) == 0 {
return nil, nil
}

if len(crList.Items) == 1 {
return &crList.Items[0], nil
}

var activeResources []cdiv1.CDI
for _, cr := range crList.Items {
if cr.Status.Phase != sdkapi.PhaseError {
activeResources = append(activeResources, cr)
}
}

if len(activeResources) == 0 {
return nil, nil
}

if len(activeResources) > 1 {
return nil, fmt.Errorf("number of active CDI CRs > 1")
if len(activeResources) != 1 {
return nil, fmt.Errorf("invalid number of active CDI resources: %d", len(activeResources))
}

return &activeResources[0], nil
Expand Down
63 changes: 63 additions & 0 deletions pkg/controller/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
sdkapi "kubevirt.io/controller-lifecycle-operator-sdk/api"
)

var _ = Describe("GetRequestedImageSize", func() {
Expand Down Expand Up @@ -73,6 +74,68 @@ var _ = Describe("GetDefaultStorageClass", func() {
sc, _ := GetDefaultStorageClass(client)
Expect(sc).To(BeNil())
})

Context("GetActiveCDI tests", func() {
createCDI := func(name string, phase sdkapi.Phase) *cdiv1.CDI {
return &cdiv1.CDI{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: cdiv1.CDIStatus{
Status: sdkapi.Status{
Phase: phase,
},
},
}
}

It("Should return nil if no CDI", func() {
client := CreateClient()
cdi, err := GetActiveCDI(client)
Expect(err).ToNot(HaveOccurred())
Expect(cdi).To(BeNil())
})

It("Should return single active", func() {
client := CreateClient(
createCDI("cdi1", sdkapi.PhaseDeployed),
)
cdi, err := GetActiveCDI(client)
Expect(err).ToNot(HaveOccurred())
Expect(cdi).ToNot(BeNil())
})

It("Should return success with single active one error", func() {
client := CreateClient(
createCDI("cdi1", sdkapi.PhaseDeployed),
createCDI("cdi2", sdkapi.PhaseError),
)
cdi, err := GetActiveCDI(client)
Expect(err).ToNot(HaveOccurred())
Expect(cdi).ToNot(BeNil())
Expect(cdi.Name).To(Equal("cdi1"))
})

It("Should return error if multiple CDIs are active", func() {
client := CreateClient(
createCDI("cdi1", sdkapi.PhaseDeployed),
createCDI("cdi2", sdkapi.PhaseDeployed),
)
cdi, err := GetActiveCDI(client)
Expect(err).To(HaveOccurred())
Expect(cdi).To(BeNil())
})

It("Should return error if multiple CDIs are error", func() {
client := CreateClient(
createCDI("cdi1", sdkapi.PhaseError),
createCDI("cdi2", sdkapi.PhaseError),
)
cdi, err := GetActiveCDI(client)
Expect(err).To(HaveOccurred())
Expect(cdi).To(BeNil())
})
})
})

func createPvcNoSize(name, ns string, annotations, labels map[string]string) *v1.PersistentVolumeClaim {
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/cr-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *ReconcileCDI) GetDependantResourcesListObjects() []client.ObjectList {
func (r *ReconcileCDI) IsCreating(_ client.Object) (bool, error) {
configMap, err := r.getConfigMap()
if err != nil {
return true, nil
return false, err
}
return configMap == nil, nil
}
Expand Down
Loading

0 comments on commit 0ad107e

Please sign in to comment.