Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use openshift_context instead of internal function in util package #409

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
"k8s.io/client-go/util/connrotation"

mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
mcclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"

"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
Expand Down Expand Up @@ -131,22 +128,9 @@ func runStartCmd(cmd *cobra.Command, args []string) {

snclient := snclientset.NewForConfigOrDie(config)
kubeclient := kubernetes.NewForConfigOrDie(config)
mcclient := mcclientset.NewForConfigOrDie(config)
openshiftFlavor := utils.OpenshiftFlavorDefault
if utils.ClusterType == utils.ClusterTypeOpenshift {
infraClient, err := client.New(config, client.Options{
Scheme: scheme.Scheme,
})
if err != nil {
panic(err)
}
isHypershift, err := utils.IsExternalControlPlaneCluster(infraClient)
if err != nil {
panic(err)
}
if isHypershift {
openshiftFlavor = utils.OpenshiftFlavorHypershift
}
openshiftContext, err := utils.NewOpenshiftContext(config, scheme.Scheme)
if err != nil {
panic(err)
}

config.Timeout = 5 * time.Second
Expand Down Expand Up @@ -200,10 +184,7 @@ func runStartCmd(cmd *cobra.Command, args []string) {
startOpts.nodeName,
snclient,
kubeclient,
utils.OpenshiftContext{
McClient: mcclient,
OpenshiftFlavor: openshiftFlavor,
},
openshiftContext,
exitCh,
stopCh,
syncCh,
Expand All @@ -221,7 +202,8 @@ func updateDialer(clientConfig *rest.Config) (func(), error) {
if clientConfig.Transport != nil || clientConfig.Dial != nil {
return nil, fmt.Errorf("there is already a transport or dialer configured")
}
d := connrotation.NewDialer((&net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}).DialContext)
f := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
d := connrotation.NewDialer(f.DialContext)
clientConfig.Dial = d.DialContext
return d.CloseAll, nil
}
10 changes: 5 additions & 5 deletions controllers/sriovnetworkpoolconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// SriovNetworkPoolConfigReconciler reconciles a SriovNetworkPoolConfig object
type SriovNetworkPoolConfigReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
OpenshiftContext *utils.OpenshiftContext
}

//+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovnetworkpoolconfigs,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -43,10 +44,9 @@ type SriovNetworkPoolConfigReconciler struct {
func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx).WithValues("sriovnetworkpoolconfig", req.NamespacedName)
isHypershift := false
if utils.ClusterType == utils.ClusterTypeOpenshift {
var err error
if isHypershift, err = utils.IsExternalControlPlaneCluster(r.Client); err != nil {
return reconcile.Result{}, err
if r.OpenshiftContext.IsOpenshiftCluster() {
if r.OpenshiftContext.IsHypershift() {
isHypershift = true
}
logger = logger.WithValues("isHypershift", isHypershift)
}
Expand Down
14 changes: 8 additions & 6 deletions controllers/sriovoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import (
// SriovOperatorConfigReconciler reconciles a SriovOperatorConfig object
type SriovOperatorConfigReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
OpenshiftContext *utils.OpenshiftContext
}

//+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovoperatorconfigs,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -229,12 +230,13 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc
data.Data["CaBundle"] = os.Getenv("WEBHOOK_CA_BUNDLE")
data.Data["DevMode"] = os.Getenv("DEV_MODE")
data.Data["ImagePullSecrets"] = GetImagePullSecrets()
external, err := utils.IsExternalControlPlaneCluster(r.Client)
if err != nil {
logger.Error(err, "Fail to get control plane topology")
return err

data.Data["ExternalControlPlane"] = false
if r.OpenshiftContext.IsOpenshiftCluster() {
external := r.OpenshiftContext.IsHypershift()
data.Data["ExternalControlPlane"] = external
}
data.Data["ExternalControlPlane"] = external

objs, err := render.RenderDir(path, &data)
if err != nil {
logger.Error(err, "Fail to render webhook manifests")
Expand Down
11 changes: 7 additions & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -113,14 +114,16 @@ var _ = BeforeSuite(func(done Done) {
Expect(err).ToNot(HaveOccurred())

err = (&SriovOperatorConfigReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
OpenshiftContext: &utils.OpenshiftContext{OpenshiftFlavor: utils.OpenshiftFlavorDefault},
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&SriovNetworkPoolConfigReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
OpenshiftContext: &utils.OpenshiftContext{OpenshiftFlavor: utils.OpenshiftFlavorDefault},
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func main() {
os.Exit(1)
}

openshiftContext, err := utils.NewOpenshiftContext(restConfig, scheme)
if err != nil {
setupLog.Error(err, "couldn't create openshift context")
os.Exit(1)
}

le := leaderelection.GetLeaderElectionConfig(kubeClient, enableLeaderElection)

namespace := os.Getenv("NAMESPACE")
Expand Down Expand Up @@ -143,15 +149,17 @@ func main() {
os.Exit(1)
}
if err = (&controllers.SriovOperatorConfigReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OpenshiftContext: openshiftContext,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SriovOperatorConfig")
os.Exit(1)
}
if err = (&controllers.SriovNetworkPoolConfigReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OpenshiftContext: openshiftContext,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SriovNetworkPoolConfig")
os.Exit(1)
Expand Down
10 changes: 5 additions & 5 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Daemon struct {
// kubeClient allows interaction with Kubernetes, including the node we are running on.
kubeClient kubernetes.Interface

openshiftContext utils.OpenshiftContext
openshiftContext *utils.OpenshiftContext

nodeState *sriovnetworkv1.SriovNetworkNodeState

Expand Down Expand Up @@ -133,7 +133,7 @@ func New(
nodeName string,
client snclientset.Interface,
kubeClient kubernetes.Interface,
openshiftContext utils.OpenshiftContext,
openshiftContext *utils.OpenshiftContext,
exitCh chan<- error,
stopCh <-chan struct{},
syncCh <-chan struct{},
Expand Down Expand Up @@ -490,7 +490,7 @@ func (dn *Daemon) nodeStateSyncHandler() error {
}
}
}
if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() {
if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() {
if err = dn.getNodeMachinePool(); err != nil {
return err
}
Expand All @@ -507,7 +507,7 @@ func (dn *Daemon) nodeStateSyncHandler() error {
<-done
}

if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() {
if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() {
glog.Infof("nodeStateSyncHandler(): pause MCP")
if err := dn.pauseMCP(); err != nil {
return err
Expand Down Expand Up @@ -591,7 +591,7 @@ func (dn *Daemon) completeDrain() error {
}
}

if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() {
if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() {
glog.Infof("completeDrain(): resume MCP %s", dn.mcpName)
pausePatch := []byte("{\"spec\":{\"paused\":false}}")
if _, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}); err != nil {
Expand Down
7 changes: 1 addition & 6 deletions pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
fakemcclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned/fake"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakek8s "k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -100,18 +99,14 @@ var _ = Describe("Config Daemon", func() {

kubeClient := fakek8s.NewSimpleClientset(&FakeSupportedNicIDs, &SriovDevicePluginPod)
client := fakesnclientset.NewSimpleClientset()
mcClient := fakemcclientset.NewSimpleClientset()

err = sriovnetworkv1.InitNicIDMap(kubeClient, namespace)
Expect(err).ToNot(HaveOccurred())

sut = New("test-node",
client,
kubeClient,
utils.OpenshiftContext{
McClient: mcClient,
OpenshiftFlavor: utils.OpenshiftFlavorDefault,
},
&utils.OpenshiftContext{IsOpenShiftCluster: false, OpenshiftFlavor: ""},
exitCh,
stopCh,
syncCh,
Expand Down
45 changes: 44 additions & 1 deletion pkg/utils/openshift_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package utils

import mcclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
import (
mcclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// OpenshiftFlavor holds metadata about the type of Openshift environment the operator is in.
type OpenshiftFlavor string
Expand All @@ -16,10 +21,48 @@ const (
type OpenshiftContext struct {
// McClient is a client for MachineConfigs in an Openshift environment
McClient mcclientset.Interface

// IsOpenShiftCluster boolean to point out if the cluster is an OpenShift cluster
IsOpenShiftCluster bool

// OpenshiftFlavor holds metadata about the type of Openshift environment the operator is in.
OpenshiftFlavor OpenshiftFlavor
}

func NewOpenshiftContext(config *rest.Config, scheme *runtime.Scheme) (*OpenshiftContext, error) {
if ClusterType != ClusterTypeOpenshift {
return &OpenshiftContext{nil, false, ""}, nil
}

mcclient, err := mcclientset.NewForConfig(config)
if err != nil {
return nil, err
}

openshiftFlavor := OpenshiftFlavorDefault
infraClient, err := client.New(config, client.Options{
Scheme: scheme,
})
if err != nil {
return nil, err
}

isHypershift, err := IsExternalControlPlaneCluster(infraClient)
if err != nil {
return nil, err
}

if isHypershift {
openshiftFlavor = OpenshiftFlavorHypershift
}

return &OpenshiftContext{mcclient, true, openshiftFlavor}, nil
}

func (c OpenshiftContext) IsOpenshiftCluster() bool {
return c.IsOpenShiftCluster
}

func (c OpenshiftContext) IsHypershift() bool {
return c.OpenshiftFlavor == OpenshiftFlavorHypershift
}