Skip to content

Commit

Permalink
Make feature gate channel readable from accessor
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Sluiter <[email protected]>
  • Loading branch information
slintes committed Dec 19, 2023
1 parent 3fda0f9 commit b1b0048
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
20 changes: 15 additions & 5 deletions controllers/featuregates/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,23 @@ type Accessor interface {
Start(context.Context) error
// IsMachineAPIOperatorMHCDisabled returns if the Machine API Operator's MachineHealthCheck controller is disabled
IsMachineAPIOperatorMHCDisabled() bool
// GetMHCControllerDisabledEvents returns the event channel for signalling a change
// in the MachineAPIOperatorDisableMachineHealthCheckController feature gate
GetMHCControllerDisabledEvents() <-chan event.GenericEvent
}

type accessor struct {
config *rest.Config
log logr.Logger
featureGateMHCControllerDisabledEvents chan<- event.GenericEvent
featureGateMHCControllerDisabledEvents chan event.GenericEvent
featureGateAccessor featuregates.FeatureGateAccess
isMaoMhcDisabled bool
isMaoMhcDisabledLock *sync.Mutex
}

var _ Accessor = &accessor{}

func NewAccessor(cfg *rest.Config, featureGateMHCControllerDisabledEvents chan<- event.GenericEvent) Accessor {
func NewAccessor(cfg *rest.Config, featureGateMHCControllerDisabledEvents chan event.GenericEvent) Accessor {
return &accessor{
config: cfg,
log: ctrl.Log.WithName("FeatureGateAccessor"),
Expand Down Expand Up @@ -118,6 +121,10 @@ func (fga *accessor) IsMachineAPIOperatorMHCDisabled() bool {
return fga.isMaoMhcDisabled
}

func (fga *accessor) GetMHCControllerDisabledEvents() <-chan event.GenericEvent {
return fga.featureGateMHCControllerDisabledEvents
}

func (fga *accessor) featureGateChanged(fc featuregates.FeatureChange) {
fga.isMaoMhcDisabledLock.Lock()
defer fga.isMaoMhcDisabledLock.Unlock()
Expand Down Expand Up @@ -170,9 +177,12 @@ type FakeAccessor struct {

var _ Accessor = &FakeAccessor{}

func (ds *FakeAccessor) Start(_ context.Context) error {
func (fa *FakeAccessor) Start(_ context.Context) error {
return nil
}
func (ds *FakeAccessor) IsMachineAPIOperatorMHCDisabled() bool {
return ds.IsMaoMhcDisabled
func (fa *FakeAccessor) IsMachineAPIOperatorMHCDisabled() bool {
return fa.IsMaoMhcDisabled
}
func (fa *FakeAccessor) GetMHCControllerDisabledEvents() <-chan event.GenericEvent {
return make(<-chan event.GenericEvent)
}
13 changes: 6 additions & 7 deletions controllers/machinehealthcheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ var (
// MachineHealthCheckReconciler reconciles a MachineHealthCheck object
type MachineHealthCheckReconciler struct {
client.Client
Log logr.Logger
Recorder record.EventRecorder
ClusterUpgradeStatusChecker cluster.UpgradeChecker
MHCChecker mhc.Checker
FeatureGateMHCControllerEvents <-chan event.GenericEvent
FeatureGates featuregates.Accessor
Log logr.Logger
Recorder record.EventRecorder
ClusterUpgradeStatusChecker cluster.UpgradeChecker
MHCChecker mhc.Checker
FeatureGates featuregates.Accessor
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -91,7 +90,7 @@ func (r *MachineHealthCheckReconciler) SetupWithManager(mgr ctrl.Manager) error
handler.EnqueueRequestsFromMapFunc(utils.MHCByMachineMapperFunc(mgr.GetClient(), mgr.GetLogger(), r.FeatureGates)),
)
bldr = bldr.WatchesRawSource(
&source.Channel{Source: r.FeatureGateMHCControllerEvents},
&source.Channel{Source: r.FeatureGates.GetMHCControllerDisabledEvents()},
handler.EnqueueRequestsFromMapFunc(utils.MHCByFeatureGateEventMapperFunc(mgr.GetClient(), mgr.GetLogger(), r.FeatureGates)),
)

Expand Down
10 changes: 4 additions & 6 deletions controllers/machinehealthcheck_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

machinev1 "github.com/openshift/api/machine/v1beta1"
Expand Down Expand Up @@ -2363,11 +2362,10 @@ func newFakeReconcilerWithCustomRecorder(recorder record.EventRecorder, initObje
Build()
mhcChecker, _ := mhc.NewMHCChecker(k8sManager, false, nil)
return &MachineHealthCheckReconciler{
Client: fakeClient,
Recorder: recorder,
ClusterUpgradeStatusChecker: upgradeChecker,
MHCChecker: mhcChecker,
FeatureGateMHCControllerEvents: make(chan event.GenericEvent),
Client: fakeClient,
Recorder: recorder,
ClusterUpgradeStatusChecker: upgradeChecker,
MHCChecker: mhcChecker,
FeatureGates: &featuregates.FakeAccessor{
IsMaoMhcDisabled: true,
},
Expand Down
11 changes: 5 additions & 6 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,11 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())

err = (&MachineHealthCheckReconciler{
Client: k8sManager.GetClient(),
Log: k8sManager.GetLogger().WithName("test reconciler"),
Recorder: k8sManager.GetEventRecorderFor("NodeHealthCheck"),
ClusterUpgradeStatusChecker: upgradeChecker,
MHCChecker: mhcChecker,
FeatureGateMHCControllerEvents: make(chan event.GenericEvent),
Client: k8sManager.GetClient(),
Log: k8sManager.GetLogger().WithName("test reconciler"),
Recorder: k8sManager.GetEventRecorderFor("NodeHealthCheck"),
ClusterUpgradeStatusChecker: upgradeChecker,
MHCChecker: mhcChecker,
FeatureGates: &featuregates.FakeAccessor{
IsMaoMhcDisabled: false,
},
Expand Down
13 changes: 6 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ func main() {
}

if err := (&controllers.MachineHealthCheckReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MachineHealthCheck"),
Recorder: mgr.GetEventRecorderFor("MachineHealthCheck"),
ClusterUpgradeStatusChecker: upgradeChecker,
MHCChecker: mhcChecker,
FeatureGateMHCControllerEvents: featureGateMHCControllerDisabledEvents,
FeatureGates: featureGateAccessor,
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MachineHealthCheck"),
Recorder: mgr.GetEventRecorderFor("MachineHealthCheck"),
ClusterUpgradeStatusChecker: upgradeChecker,
MHCChecker: mhcChecker,
FeatureGates: featureGateAccessor,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MachineHealthCheck")
os.Exit(1)
Expand Down

0 comments on commit b1b0048

Please sign in to comment.