From dcbc69c0cf56ddd6fe3b2fa3e4f7eb9500698791 Mon Sep 17 00:00:00 2001 From: ehila Date: Fri, 7 Jan 2022 09:47:47 -0500 Subject: [PATCH] feat: moved sno logic to utils moved sno logic to use the cluster.go file added check for k8s or openshift environment upkeep re-organize imports Signed-off-by: ehila --- main.go | 30 +++++++++++------------- pkg/leaderelection/leaderelection.go | 34 ++++------------------------ pkg/utils/cluster.go | 29 +++++++++++++++++++++++- 3 files changed, 46 insertions(+), 47 deletions(-) diff --git a/main.go b/main.go index 63d80d99d8..b9f7f64f03 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,6 @@ import ( "k8s.io/client-go/kubernetes" clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" - "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/healthz" @@ -79,7 +78,13 @@ func main() { flag.Parse() restConfig := ctrl.GetConfigOrDie() - le := leaderelection.GetLeaderElectionConfig(restConfig, enableLeaderElection) + kubeClient, err := client.New(restConfig, client.Options{Scheme: scheme}) + if err != nil { + setupLog.Error(err, "couldn't create client") + os.Exit(1) + } + + le := leaderelection.GetLeaderElectionConfig(kubeClient, enableLeaderElection) ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) namespace := os.Getenv("NAMESPACE") @@ -151,14 +156,14 @@ func main() { // +kubebuilder:scaffold:builder // Create a default SriovNetworkNodePolicy - err = createDefaultPolicy(ctrl.GetConfigOrDie()) + err = createDefaultPolicy(kubeClient) if err != nil { setupLog.Error(err, "unable to create default SriovNetworkNodePolicy") os.Exit(1) } // Create default SriovOperatorConfig - err = createDefaultOperatorConfig(ctrl.GetConfigOrDie()) + err = createDefaultOperatorConfig(kubeClient) if err != nil { setupLog.Error(err, "unable to create default SriovOperatorConfig") os.Exit(1) @@ -201,12 +206,8 @@ func initNicIdMap() error { return nil } -func createDefaultPolicy(cfg *rest.Config) error { +func createDefaultPolicy(c client.Client) error { logger := setupLog.WithName("createDefaultPolicy") - c, err := client.New(cfg, client.Options{Scheme: scheme}) - if err != nil { - return fmt.Errorf("Couldn't create client: %v", err) - } policy := &sriovnetworkv1.SriovNetworkNodePolicy{ Spec: sriovnetworkv1.SriovNetworkNodePolicySpec{ NumVfs: 0, @@ -216,7 +217,7 @@ func createDefaultPolicy(cfg *rest.Config) error { } name := "default" namespace := os.Getenv("NAMESPACE") - err = c.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, policy) + err := c.Get(context.TODO(), types.NamespacedName{Name: name, Namespace: namespace}, policy) if err != nil { if errors.IsNotFound(err) { logger.Info("Create a default SriovNetworkNodePolicy") @@ -233,16 +234,11 @@ func createDefaultPolicy(cfg *rest.Config) error { return nil } -func createDefaultOperatorConfig(cfg *rest.Config) error { +func createDefaultOperatorConfig(c client.Client) error { logger := setupLog.WithName("createDefaultOperatorConfig") - c, err := client.New(cfg, client.Options{Scheme: scheme}) - if err != nil { - return fmt.Errorf("Couldn't create client: %v", err) - } - singleNode, err := utils.IsSingleNodeCluster(c) if err != nil { - return fmt.Errorf("Couldn't check the anount of nodes in the cluster") + return fmt.Errorf("Couldn't check the amount of nodes in the cluster") } enableAdmissionController := os.Getenv("ENABLE_ADMISSION_CONTROLLER") == "true" diff --git a/pkg/leaderelection/leaderelection.go b/pkg/leaderelection/leaderelection.go index 7fb22f6106..b0ff21442b 100644 --- a/pkg/leaderelection/leaderelection.go +++ b/pkg/leaderelection/leaderelection.go @@ -1,23 +1,17 @@ package leaderelection import ( - "context" - "fmt" "time" "github.com/golang/glog" - configv1 "github.com/openshift/api/config/v1" - - openshiftcorev1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" - "k8s.io/client-go/rest" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) const ( - infraResourceName = "cluster" - // Defaults follow conventions // https://github.com/openshift/enhancements/blob/master/CONVENTIONS.md#high-availability // Impl Calculations: https://github.com/openshift/library-go/commit/7e7d216ed91c3119800219c9194e5e57113d059a @@ -26,7 +20,7 @@ const ( defaultRetryPeriod = 26 * time.Second ) -func GetLeaderElectionConfig(restClient *rest.Config, enabled bool) (defaultConfig configv1.LeaderElection) { +func GetLeaderElectionConfig(c client.Client, enabled bool) (defaultConfig configv1.LeaderElection) { defaultConfig = configv1.LeaderElection{ Disable: !enabled, LeaseDuration: metav1.Duration{Duration: defaultLeaseDuration}, @@ -35,14 +29,12 @@ func GetLeaderElectionConfig(restClient *rest.Config, enabled bool) (defaultConf } if enabled { - ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*3)) - defer cancel() - infra, err := getClusterInfraStatus(ctx, restClient) + isSno, err := utils.IsSingleNodeCluster(c) if err != nil { glog.Warningf("unable to get cluster infrastructure status, using HA cluster values for leader election: %v", err) return } - if infra != nil && infra.ControlPlaneTopology == configv1.SingleReplicaTopologyMode { + if isSno { return leaderElectionSNOConfig(defaultConfig) } } @@ -59,19 +51,3 @@ func leaderElectionSNOConfig(config configv1.LeaderElection) configv1.LeaderElec ret.RetryPeriod.Duration = 60 * time.Second return ret } - -// Retrieve the cluster status, used to determine if we should use different leader election. -func getClusterInfraStatus(ctx context.Context, restClient *rest.Config) (*configv1.InfrastructureStatus, error) { - client, err := openshiftcorev1.NewForConfig(restClient) - if err != nil { - return nil, err - } - infra, err := client.Infrastructures().Get(ctx, infraResourceName, metav1.GetOptions{}) - if err != nil { - return nil, err - } - if infra == nil { - return nil, fmt.Errorf("getting resource Infrastructure (name: %s) succeeded but object was nil", infraResourceName) - } - return &infra.Status, nil -} diff --git a/pkg/utils/cluster.go b/pkg/utils/cluster.go index 1421e1e62d..77656814f6 100644 --- a/pkg/utils/cluster.go +++ b/pkg/utils/cluster.go @@ -2,14 +2,30 @@ package utils import ( "context" + "fmt" + "os" "github.com/golang/glog" + configv1 "github.com/openshift/api/config/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) +const ( + // default Infrastructure resource name for Openshift + infraResourceName = "cluster" +) + func IsSingleNodeCluster(c client.Client) (bool, error) { + if os.Getenv("CLUSTER_TYPE") == ClusterTypeOpenshift { + return openshiftSNOClusterStatus(c) + } + return k8sSNOClusterStatus(c) +} + +func k8sSNOClusterStatus(c client.Client) (bool, error) { nodeList := &corev1.NodeList{} err := c.List(context.TODO(), nodeList) if err != nil { @@ -21,6 +37,17 @@ func IsSingleNodeCluster(c client.Client) (bool, error) { glog.Infof("IsSingleNodeCluster(): one node found in the cluster") return true, nil } - return false, nil } + +func openshiftSNOClusterStatus(c client.Client) (bool, error) { + infra := &configv1.Infrastructure{} + err := c.Get(context.TODO(), types.NamespacedName{Name: infraResourceName}, infra) + if err != nil { + return false, err + } + if infra == nil { + return false, fmt.Errorf("getting resource Infrastructure (name: %s) succeeded but object was nil", infraResourceName) + } + return infra.Status.ControlPlaneTopology == configv1.SingleReplicaTopologyMode, nil +}