Skip to content

Commit a78cd29

Browse files
committed
Improve Capabilities object
Improve the new Capabilities struct to better encapsulate cluster capabilities. - add NewCapabilties function to return the filled Capabilities struct - made both IsOnOpenshift and HasMachineAPI private under cluster.capabilities file - Access to the respective values is only allowed via the Capabilities struct's fields Signed-off-by: Carlo Lobrano <[email protected]>
1 parent 8b772a9 commit a78cd29

File tree

9 files changed

+70
-69
lines changed

9 files changed

+70
-69
lines changed

controllers/cluster/capabilities.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,56 @@ import (
66
"github.com/pkg/errors"
77

88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/runtime/schema"
10+
"k8s.io/client-go/discovery"
911
"k8s.io/client-go/rest"
1012

1113
v1 "github.com/openshift/api/config/v1"
1214
configv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
1315
)
1416

1517
type Capabilities struct {
16-
HasEtcdPDBQuorum, HasMachineAPI bool
18+
IsOnOpenshift, HasMachineAPI bool
19+
}
20+
21+
func NewCapabilities(config *rest.Config) (Capabilities, error) {
22+
var err error
23+
var onOpenshift, machineAPI bool
24+
25+
if onOpenshift, err = isOnOpenshift(config); err != nil {
26+
return Capabilities{}, errors.Wrap(err, "failed to check if cluster is on OpenShift")
27+
} else if onOpenshift {
28+
if machineAPI, err = hasMachineAPICapability(config); err != nil {
29+
return Capabilities{}, errors.Wrap(err, "failed to check machine API capability")
30+
}
31+
}
32+
33+
return Capabilities{
34+
IsOnOpenshift: onOpenshift,
35+
HasMachineAPI: machineAPI,
36+
}, nil
37+
}
38+
39+
// IsOnOpenshift returns true if the cluster has the openshift config group
40+
func isOnOpenshift(config *rest.Config) (bool, error) {
41+
dc, err := discovery.NewDiscoveryClientForConfig(config)
42+
if err != nil {
43+
return false, err
44+
}
45+
apiGroups, err := dc.ServerGroups()
46+
kind := schema.GroupVersionKind{Group: "config.openshift.io", Version: "v1", Kind: "ClusterVersion"}
47+
for _, apiGroup := range apiGroups.Groups {
48+
for _, supportedVersion := range apiGroup.Versions {
49+
if supportedVersion.GroupVersion == kind.GroupVersion().String() {
50+
return true, nil
51+
}
52+
}
53+
}
54+
return false, nil
1755
}
1856

1957
// HasMachineAPICapability returns true if the cluster has the MachineAPI Enabled
20-
func HasMachineAPICapability(config *rest.Config) (bool, error) {
58+
func hasMachineAPICapability(config *rest.Config) (bool, error) {
2159
configV1Client, err := configv1.NewForConfig(config)
2260
if err != nil {
2361
return false, errors.Wrapf(err, "failed to create cluster version client")

controllers/cluster/upgrade_checker.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313

1414
v1 "github.com/openshift/api/config/v1"
1515
clusterversion "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
16-
17-
"github.com/medik8s/node-healthcheck-operator/controllers/utils"
1816
)
1917

2018
var unsupportedUpgradeCheckerErr = errors.New(
@@ -66,12 +64,8 @@ func (n *noopClusterUpgradeStatusChecker) Check() (bool, error) {
6664

6765
// NewClusterUpgradeStatusChecker will return some implementation of a checker or err in case it can't
6866
// reliably detect which implementation to use.
69-
func NewClusterUpgradeStatusChecker(mgr manager.Manager) (UpgradeChecker, error) {
70-
openshift, err := utils.IsOnOpenshift(mgr.GetConfig())
71-
if err != nil {
72-
return nil, err
73-
}
74-
if !openshift {
67+
func NewClusterUpgradeStatusChecker(mgr manager.Manager, caps Capabilities) (UpgradeChecker, error) {
68+
if !caps.IsOnOpenshift {
7569
return &noopClusterUpgradeStatusChecker{}, nil
7670
}
7771
checker, err := newOpenshiftClusterUpgradeChecker(mgr)

controllers/console/plugin.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
"github.com/openshift/api/console/v1alpha1"
3131

32-
"github.com/medik8s/node-healthcheck-operator/controllers/utils"
32+
"github.com/medik8s/node-healthcheck-operator/controllers/cluster"
3333
)
3434

3535
const (
@@ -50,9 +50,11 @@ const (
5050
func CreateOrUpdatePlugin(ctx context.Context, cl client.Client, config *rest.Config, namespace string, log logr.Logger) error {
5151

5252
// check if we are on OCP
53-
if isOpenshift, err := utils.IsOnOpenshift(config); err != nil {
54-
return errors.Wrap(err, "failed to check if we are on Openshift")
55-
} else if !isOpenshift {
53+
clusterCapabilities, err := cluster.NewCapabilities(config)
54+
if err != nil {
55+
return errors.Wrap(err, "failed to check cluster capabilities")
56+
}
57+
if !clusterCapabilities.IsOnOpenshift {
5658
log.Info("we are not on Openshift, skipping console plugin activation")
5759
return nil
5860
}

controllers/machinehealthcheck_controller_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
machinev1 "github.com/openshift/api/machine/v1beta1"
3535

36+
"github.com/medik8s/node-healthcheck-operator/controllers/cluster"
3637
"github.com/medik8s/node-healthcheck-operator/controllers/featuregates"
3738
"github.com/medik8s/node-healthcheck-operator/controllers/mhc"
3839
"github.com/medik8s/node-healthcheck-operator/controllers/resources"
@@ -2386,7 +2387,8 @@ func newFakeReconcilerWithCustomRecorder(recorder record.EventRecorder, initObje
23862387
WithRuntimeObjects(initObjects...).
23872388
WithStatusSubresource(&machinev1.MachineHealthCheck{}).
23882389
Build()
2389-
mhcChecker, _ := mhc.NewMHCChecker(k8sManager, false, nil)
2390+
caps := cluster.Capabilities{IsOnOpenshift: false, HasMachineAPI: false}
2391+
mhcChecker, _ := mhc.NewMHCChecker(k8sManager, caps, nil)
23902392
return &MachineHealthCheckReconciler{
23912393
Client: fakeClient,
23922394
Recorder: recorder,

controllers/mhc/checker.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"sigs.k8s.io/controller-runtime/pkg/manager"
1212

1313
"github.com/openshift/api/machine/v1beta1"
14+
15+
"github.com/medik8s/node-healthcheck-operator/controllers/cluster"
1416
)
1517

1618
// NodeConditionTerminating is the node condition type used by the termination handler MHC
@@ -25,9 +27,9 @@ type Checker interface {
2527
}
2628

2729
// NewMHCChecker creates a new Checker
28-
func NewMHCChecker(mgr manager.Manager, hasMachineAPI bool, mhcEvents chan<- event.GenericEvent) (Checker, error) {
30+
func NewMHCChecker(mgr manager.Manager, caps cluster.Capabilities, mhcEvents chan<- event.GenericEvent) (Checker, error) {
2931

30-
if !hasMachineAPI {
32+
if !caps.HasMachineAPI {
3133
return DummyChecker{}, nil
3234
}
3335

controllers/nodehealthcheck_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ func (r *NodeHealthCheckReconciler) isControlPlaneRemediationAllowed(ctx context
683683
}
684684

685685
// no ongoing control plane remediation, check etcd quorum
686-
if !r.Capabilities.HasEtcdPDBQuorum {
686+
if !r.Capabilities.IsOnOpenshift {
687+
// etcd quorum PDB is only installed in OpenShift
687688
return true, nil
688689
}
689690
var allowed bool

controllers/suite_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ var _ = BeforeSuite(func() {
169169
Upgrading: false,
170170
}
171171

172-
mhcChecker, err := mhc.NewMHCChecker(k8sManager, false, nil)
172+
caps := cluster.Capabilities{IsOnOpenshift: true, HasMachineAPI: true}
173+
174+
mhcChecker, err := mhc.NewMHCChecker(k8sManager, caps, nil)
173175
Expect(err).NotTo(HaveOccurred())
174176

175177
os.Setenv("DEPLOYMENT_NAMESPACE", DeploymentNamespace)
@@ -197,7 +199,7 @@ var _ = BeforeSuite(func() {
197199
ClusterUpgradeStatusChecker: upgradeChecker,
198200
MHCChecker: mhcChecker,
199201
MHCEvents: mhcEvents,
200-
Capabilities: cluster.Capabilities{HasEtcdPDBQuorum: true, HasMachineAPI: true},
202+
Capabilities: caps,
201203
}).SetupWithManager(k8sManager)
202204
Expect(err).NotTo(HaveOccurred())
203205

controllers/utils/utils.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111

1212
v1 "k8s.io/api/core/v1"
1313
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14-
"k8s.io/apimachinery/pkg/runtime/schema"
15-
"k8s.io/client-go/discovery"
16-
"k8s.io/client-go/rest"
1714
"k8s.io/client-go/tools/cache"
1815
"sigs.k8s.io/controller-runtime/pkg/client"
1916

@@ -45,24 +42,6 @@ func GetDeploymentNamespace() (string, error) {
4542
return ns, nil
4643
}
4744

48-
// IsOnOpenshift returns true if the cluster has the openshift config group
49-
func IsOnOpenshift(config *rest.Config) (bool, error) {
50-
dc, err := discovery.NewDiscoveryClientForConfig(config)
51-
if err != nil {
52-
return false, err
53-
}
54-
apiGroups, err := dc.ServerGroups()
55-
kind := schema.GroupVersionKind{Group: "config.openshift.io", Version: "v1", Kind: "ClusterVersion"}
56-
for _, apiGroup := range apiGroups.Groups {
57-
for _, supportedVersion := range apiGroup.Versions {
58-
if supportedVersion.GroupVersion == kind.GroupVersion().String() {
59-
return true, nil
60-
}
61-
}
62-
}
63-
return false, nil
64-
}
65-
6645
// GetLogWithNHC return a logger with NHC namespace and name
6746
func GetLogWithNHC(log logr.Logger, nhc *v1alpha1.NodeHealthCheck) logr.Logger {
6847
return log.WithValues("NodeHealthCheck name", nhc.Name)

main.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import (
5252
"github.com/medik8s/node-healthcheck-operator/controllers/featuregates"
5353
"github.com/medik8s/node-healthcheck-operator/controllers/initializer"
5454
"github.com/medik8s/node-healthcheck-operator/controllers/mhc"
55-
"github.com/medik8s/node-healthcheck-operator/controllers/utils"
5655
"github.com/medik8s/node-healthcheck-operator/metrics"
5756
"github.com/medik8s/node-healthcheck-operator/version"
5857
)
@@ -131,34 +130,22 @@ func main() {
131130
os.Exit(1)
132131
}
133132

134-
upgradeChecker, err := cluster.NewClusterUpgradeStatusChecker(mgr)
133+
caps, err := cluster.NewCapabilities(mgr.GetConfig())
135134
if err != nil {
136-
setupLog.Error(err, "unable initialize cluster upgrade checker")
135+
setupLog.Error(err, "unable to determine cluster capabilities")
137136
os.Exit(1)
138137
}
139138

140-
onOpenshift, err := utils.IsOnOpenshift(mgr.GetConfig())
139+
setupLog.Info("Cluster capabilities", "IsOnOpenshift", caps.IsOnOpenshift, "HasMachineAPI", caps.HasMachineAPI)
140+
141+
upgradeChecker, err := cluster.NewClusterUpgradeStatusChecker(mgr, caps)
141142
if err != nil {
142-
setupLog.Error(err, "failed to check if we run on Openshift")
143+
setupLog.Error(err, "unable initialize cluster upgrade checker")
143144
os.Exit(1)
144145
}
145146

146-
var hasMachineAPI bool
147-
if onOpenshift {
148-
hasMachineAPI, err := cluster.HasMachineAPICapability(mgr.GetConfig())
149-
if err != nil {
150-
setupLog.Error(err, "failed to check if MachineAPI is enabled")
151-
os.Exit(1)
152-
}
153-
if hasMachineAPI {
154-
setupLog.Info("MachineAPI is enabled")
155-
} else {
156-
setupLog.Info("MachineAPI is not enabled")
157-
}
158-
}
159-
160147
mhcEvents := make(chan event.GenericEvent)
161-
mhcChecker, err := mhc.NewMHCChecker(mgr, hasMachineAPI, mhcEvents)
148+
mhcChecker, err := mhc.NewMHCChecker(mgr, caps, mhcEvents)
162149
if err != nil {
163150
setupLog.Error(err, "unable initialize MHC checker")
164151
os.Exit(1)
@@ -168,26 +155,20 @@ func main() {
168155
os.Exit(1)
169156
}
170157

171-
clusterCapabilities := cluster.Capabilities{
172-
// etcd quorum PDB is only installed in OpenShift
173-
HasEtcdPDBQuorum: onOpenshift,
174-
HasMachineAPI: hasMachineAPI,
175-
}
176-
177158
if err := (&controllers.NodeHealthCheckReconciler{
178159
Client: mgr.GetClient(),
179160
Log: ctrl.Log.WithName("controllers").WithName("NodeHealthCheck"),
180161
Recorder: mgr.GetEventRecorderFor("NodeHealthCheck"),
181162
ClusterUpgradeStatusChecker: upgradeChecker,
182163
MHCChecker: mhcChecker,
183-
Capabilities: clusterCapabilities,
164+
Capabilities: caps,
184165
MHCEvents: mhcEvents,
185166
}).SetupWithManager(mgr); err != nil {
186167
setupLog.Error(err, "unable to create controller", "controller", "NodeHealthCheck")
187168
os.Exit(1)
188169
}
189170

190-
if hasMachineAPI {
171+
if caps.HasMachineAPI {
191172
featureGateMHCControllerDisabledEvents := make(chan event.GenericEvent)
192173
featureGateAccessor := featuregates.NewAccessor(mgr.GetConfig(), featureGateMHCControllerDisabledEvents)
193174
if err = mgr.Add(featureGateAccessor); err != nil {

0 commit comments

Comments
 (0)