Skip to content

Commit

Permalink
feat: moved sno logic to utils
Browse files Browse the repository at this point in the history
moved sno logic to use the cluster.go file
added check for k8s or openshift environment
upkeep re-organize imports

Signed-off-by: ehila <[email protected]>
  • Loading branch information
eggfoobar committed Jan 10, 2022
1 parent e8a4020 commit dcbc69c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 47 deletions.
30 changes: 13 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand All @@ -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"
Expand Down
34 changes: 5 additions & 29 deletions pkg/leaderelection/leaderelection.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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},
Expand All @@ -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)
}
}
Expand All @@ -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
}
29 changes: 28 additions & 1 deletion pkg/utils/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit dcbc69c

Please sign in to comment.