Skip to content

Commit

Permalink
fix: use SNO check to disable node-removal controller
Browse files Browse the repository at this point in the history
node-removal controller wouldn't be necessary at the end of the lifecycle of SNO nodes as the entire cluster would go down

Signed-off-by: jakobmoellerdev <[email protected]>
  • Loading branch information
jakobmoellerdev committed Aug 4, 2023
1 parent 6353580 commit 3982605
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ func main() {
}
setupLog.Info("Watching namespace", "Namespace", operatorNamespace)

leaderElectionResolver, err := cluster.NewLeaderElectionResolver(ctrl.GetConfigOrDie(), scheme, enableLeaderElection, operatorNamespace)
setupClient, err := client.New(ctrl.GetConfigOrDie(), client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "unable to initialize setup client for pre-manager startup checks")
os.Exit(1)
}
snoCheck := cluster.NewMasterSNOCheck(setupClient)
leaderElectionResolver, err := cluster.NewLeaderElectionResolver(snoCheck, enableLeaderElection, operatorNamespace)
if err != nil {
setupLog.Error(err, "unable to setup leader election")
os.Exit(1)
Expand Down Expand Up @@ -130,12 +136,15 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "LVMCluster")
os.Exit(1)
}
// TODO only run when not in SNO
if err = (&controllers.NodeRemovalController{
Client: mgr.GetClient(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "NodeRemovalControlelr")
os.Exit(1)

if !snoCheck.IsSNO(context.Background()) {
setupLog.Info("starting node-removal controller to observe node removal in MultiNode")
if err = (&controllers.NodeRemovalController{
Client: mgr.GetClient(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "NodeRemovalControlelr")
os.Exit(1)
}
}

if err = mgr.GetFieldIndexer().IndexField(context.Background(), &lvmv1alpha1.LVMVolumeGroupNodeStatus{}, "metadata.name", func(object client.Object) []string {
Expand Down
31 changes: 6 additions & 25 deletions pkg/cluster/leaderelection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ package cluster

import (
"context"
"fmt"
configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/library-go/pkg/config/leaderelection"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
log "sigs.k8s.io/controller-runtime/pkg/log"
)

Expand All @@ -25,45 +19,32 @@ type LeaderElectionResolver interface {
// on the amount of master nodes discovered in the cluster. If there is exactly one control-plane/master node,
// the returned LeaderElectionResolver settings are optimized for SNO deployments.
func NewLeaderElectionResolver(
config *rest.Config,
scheme *runtime.Scheme,
snoCheck SNOCheck,
enableLeaderElection bool,
operatorNamespace string,
) (LeaderElectionResolver, error) {
leaderElectionClient, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("cannot create leader election client: %w", err)
}

defaultElectionConfig := leaderelection.LeaderElectionDefaulting(configv1.LeaderElection{
Disable: !enableLeaderElection,
}, operatorNamespace, "1136b8a6.topolvm.io")

return &nodeLookupSNOLeaderElection{
clnt: leaderElectionClient,
snoCheck: snoCheck,
defaultElectionConfig: defaultElectionConfig,
}, nil
}

type nodeLookupSNOLeaderElection struct {
clnt client.Client
snoCheck SNOCheck
defaultElectionConfig configv1.LeaderElection
}

func (le *nodeLookupSNOLeaderElection) Resolve(ctx context.Context) (configv1.LeaderElection, error) {
logger := log.FromContext(ctx)
nodes := &corev1.NodeList{}
if err := le.clnt.List(context.Background(), nodes, client.MatchingLabels{
ControlPlaneIDLabel: "",
}); err != nil {
logger.Error(err, "unable to retrieve nodes for SNO check with lease configuration")
os.Exit(1)
}
if len(nodes.Items) != 1 {
if !le.snoCheck.IsSNO(ctx) {
logger.Info("Using default Multi-Node leader election settings optimized for high-availability")
return le.defaultElectionConfig, nil
}
logger.Info("Overwriting defaults with SNO leader election config as only a single node was discovered",
"node", nodes.Items[0].GetName())
logger.Info("Overwriting defaults with SNO leader election config as only a single node was discovered")
config := leaderelection.LeaderElectionSNOConfig(le.defaultElectionConfig)
logger.Info("leader election config setup succeeded",
"retry-period", config.RetryPeriod,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/leaderelection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Test_nodeLookupSNOLeaderElection_Resolve(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
clnt := fake.NewClientBuilder().WithObjects(tt.nodes...).Build()
le := &nodeLookupSNOLeaderElection{
clnt: clnt,
snoCheck: NewMasterSNOCheck(clnt),
defaultElectionConfig: leaderelection.LeaderElectionDefaulting(configv1.LeaderElection{},
"test", "test-leader-id"),
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/cluster/sno.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cluster

import (
"context"
corev1 "k8s.io/api/core/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

type SNOCheck interface {
IsSNO(ctx context.Context) bool
}

func NewMasterSNOCheck(clnt client.Client) SNOCheck {
return &masterSNOCheck{clnt: clnt}
}

type masterSNOCheck struct {
clnt client.Client
}

func (chk *masterSNOCheck) IsSNO(ctx context.Context) bool {
logger := log.FromContext(ctx)
nodes := &corev1.NodeList{}
if err := chk.clnt.List(context.Background(), nodes, client.MatchingLabels{
ControlPlaneIDLabel: "",
}); err != nil {
logger.Error(err, "unable to retrieve nodes for SNO check with lease configuration")
os.Exit(1)
}
return nodes.Items != nil && len(nodes.Items) == 1
}

0 comments on commit 3982605

Please sign in to comment.