• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubevirt / hyperconverged-cluster-operator / 16152346836

08 Jul 2025 07:16PM UTC coverage: 75.231% (-0.2%) from 75.476%
16152346836

push

github

web-flow
Refactor operands (#3615)

* split the operands package to three new packages

Split the operands package into 3 packages:
* the OperandHandler logic moved to the new operatorhandler package
* the resource specific handler logic moved to the new handlers package
* all the operands/handlers infrastructure logic reminds in the operands
  package

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>

* update webhook code with the new package names

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>

* update e2e tests with the new package names

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>

* update main with the new package names

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>

---------

Signed-off-by: Nahshon Unna-Tsameret <nunnatsa@redhat.com>

506 of 565 new or added lines in 24 files covered. (89.56%)

1 existing line in 1 file now uncovered.

6515 of 8660 relevant lines covered (75.23%)

1.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.6
/controllers/operands/conditionalHandler.go
1
package operands
2

3
import (
4
        objectreferencesv1 "github.com/openshift/custom-resource-status/objectreferences/v1"
5
        apierrors "k8s.io/apimachinery/pkg/api/errors"
6
        "k8s.io/client-go/tools/reference"
7
        "sigs.k8s.io/controller-runtime/pkg/client"
8

9
        "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1"
10
        "github.com/kubevirt/hyperconverged-cluster-operator/controllers/common"
11
        hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
12
)
13

14
type ConditionFunc func(hc *v1beta1.HyperConverged) bool
15
type GetCRWithNameFunc func(hc *v1beta1.HyperConverged) client.Object
16

17
// ConditionalHandler is an operand handler that only deploy the operand CR if a given shouldDeploy function returns true.
18
// If not, it makes sure the CR is deleted.
19
type ConditionalHandler struct {
20
        operand       *GenericOperand
21
        shouldDeploy  ConditionFunc
22
        getCRWithName GetCRWithNameFunc
23
}
24

25
func NewConditionalHandler(operand *GenericOperand, shouldDeploy ConditionFunc, getCRWithName GetCRWithNameFunc) *ConditionalHandler {
3✔
26
        return &ConditionalHandler{
3✔
27
                operand:       operand,
3✔
28
                shouldDeploy:  shouldDeploy,
3✔
29
                getCRWithName: getCRWithName,
3✔
30
        }
3✔
31
}
3✔
32

33
func (ch *ConditionalHandler) Ensure(req *common.HcoRequest) *EnsureResult {
3✔
34
        if ch.shouldDeploy(req.Instance) {
4✔
35
                return ch.operand.Ensure(req)
1✔
36
        }
1✔
37
        return ch.ensureDeleted(req)
3✔
38
}
39

40
func (ch *ConditionalHandler) Reset() {
1✔
41
        ch.operand.Reset()
1✔
42
}
1✔
43

44
func (ch *ConditionalHandler) ensureDeleted(req *common.HcoRequest) *EnsureResult {
3✔
45
        cr := ch.getCRWithName(req.Instance)
3✔
46
        res := NewEnsureResult(req.Instance)
3✔
47
        res.SetName(cr.GetName())
3✔
48

3✔
49
        // hcoutil.EnsureDeleted does check that the CR exists before removing it. But it also writes a log message each
3✔
50
        // time it happens, i.e. for every reconcile loop. Assuming the client cache is up-to-date, we can safely get it here
3✔
51
        // with no meaningful performance cost.
3✔
52
        err := ch.operand.Get(req.Ctx, client.ObjectKeyFromObject(cr), cr)
3✔
53
        if err != nil {
6✔
54
                if !apierrors.IsNotFound(err) {
3✔
55
                        return res.Error(err)
×
56
                }
×
57
        } else {
1✔
58
                deleted, err := hcoutil.EnsureDeleted(req.Ctx, ch.operand.Client, cr, req.Instance.Name, req.Logger, false, false, true)
1✔
59
                if err != nil {
1✔
60
                        return res.Error(err)
×
61
                }
×
62

63
                if deleted {
2✔
64
                        res.SetDeleted()
1✔
65
                        objectRef, err := reference.GetReference(ch.operand.Scheme, cr)
1✔
66
                        if err != nil {
1✔
67
                                return res.Error(err)
×
68
                        }
×
69

70
                        if err = objectreferencesv1.RemoveObjectReference(&req.Instance.Status.RelatedObjects, *objectRef); err != nil {
1✔
71
                                return res.Error(err)
×
72
                        }
×
73
                        req.StatusDirty = true
1✔
74
                }
75
        }
76

77
        return res.SetUpgradeDone(req.ComponentUpgradeInProgress)
3✔
78
}
79

NEW
80
func (ch *ConditionalHandler) GetFullCr(hc *v1beta1.HyperConverged) (client.Object, error) {
×
NEW
81
        return ch.operand.GetFullCr(hc)
×
UNCOV
82
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc