Skip to content

Commit

Permalink
update support
Browse files Browse the repository at this point in the history
  • Loading branch information
relyt0925 committed Sep 19, 2022
1 parent a3590b0 commit c5de82b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
13 changes: 9 additions & 4 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,21 @@ func runStartCmd(cmd *cobra.Command, args []string) {
snclient := snclientset.NewForConfigOrDie(config)
kubeclient := kubernetes.NewForConfigOrDie(config)
mcclient := mcclientset.NewForConfigOrDie(config)
isHypershift := false
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)
isHypershift, err := utils.IsExternalControlPlaneCluster(infraClient)
if err != nil {
panic(err)
}
if isHypershift {
openshiftFlavor = utils.OpenshiftFlavorHypershift
}
}

config.Timeout = 5 * time.Second
Expand Down Expand Up @@ -196,8 +199,10 @@ func runStartCmd(cmd *cobra.Command, args []string) {
startOpts.nodeName,
snclient,
kubeclient,
mcclient,
isHypershift,
utils.OpenshiftContext{
McClient: mcclient,
OpenshiftFlavor: openshiftFlavor,
},
exitCh,
stopCh,
syncCh,
Expand Down
53 changes: 26 additions & 27 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/golang/glog"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
daemonconsts "github.com/openshift/machine-config-operator/pkg/daemon/constants"
mcclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
mcfginformers "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions"
"golang.org/x/time/rate"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -70,7 +69,7 @@ type Daemon struct {
// kubeClient allows interaction with Kubernetes, including the node we are running on.
kubeClient kubernetes.Interface

mcClient mcclientset.Interface
openshiftContext utils.OpenshiftContext

nodeState *sriovnetworkv1.SriovNetworkNodeState

Expand Down Expand Up @@ -101,8 +100,6 @@ type Daemon struct {
workqueue workqueue.RateLimitingInterface

mcpName string

isHypershift bool
}

const (
Expand Down Expand Up @@ -136,26 +133,24 @@ func New(
nodeName string,
client snclientset.Interface,
kubeClient kubernetes.Interface,
mcClient mcclientset.Interface,
isHypershift bool,
openshiftContext utils.OpenshiftContext,
exitCh chan<- error,
stopCh <-chan struct{},
syncCh <-chan struct{},
refreshCh chan<- Message,
platformType utils.PlatformType,
) *Daemon {
return &Daemon{
name: nodeName,
platform: platformType,
client: client,
kubeClient: kubeClient,
mcClient: mcClient,
isHypershift: isHypershift,
exitCh: exitCh,
stopCh: stopCh,
syncCh: syncCh,
refreshCh: refreshCh,
nodeState: &sriovnetworkv1.SriovNetworkNodeState{},
name: nodeName,
platform: platformType,
client: client,
kubeClient: kubeClient,
openshiftContext: openshiftContext,
exitCh: exitCh,
stopCh: stopCh,
syncCh: syncCh,
refreshCh: refreshCh,
nodeState: &sriovnetworkv1.SriovNetworkNodeState{},
drainer: &drain.Helper{
Client: kubeClient,
Force: true,
Expand Down Expand Up @@ -209,7 +204,11 @@ func (dn *Daemon) tryCreateUdevRuleWrapper() error {

// Run the config daemon
func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error {
glog.V(0).Infof("Run(): start daemon. isHypershift: %t", dn.isHypershift)
if utils.ClusterType == utils.ClusterTypeOpenshift {
glog.V(0).Infof("Run(): start daemon. openshiftFlavor: %s", dn.openshiftContext.OpenshiftFlavor)
} else {
glog.V(0).Infof("Run(): start daemon.")
}
// Only watch own SriovNetworkNodeState CR
defer utilruntime.HandleCrash()
defer dn.workqueue.ShutDown()
Expand Down Expand Up @@ -491,7 +490,7 @@ func (dn *Daemon) nodeStateSyncHandler() error {
}
}
}
if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.isHypershift {
if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() {
if err = dn.getNodeMachinePool(); err != nil {
return err
}
Expand All @@ -508,7 +507,7 @@ func (dn *Daemon) nodeStateSyncHandler() error {
<-done
}

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

if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.isHypershift {
if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() {
glog.Infof("completeDrain(): resume MCP %s", dn.mcpName)
pausePatch := []byte("{\"spec\":{\"paused\":false}}")
if _, err := dn.mcClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}); err != nil {
if _, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}); err != nil {
glog.Errorf("completeDrain(): failed to resume MCP %s: %v", dn.mcpName, err)
return err
}
Expand Down Expand Up @@ -733,7 +732,7 @@ func (dn *Daemon) getNodeMachinePool() error {
glog.Errorf("getNodeMachinePool(): Failed to find the the desiredConfig Annotation")
return fmt.Errorf("getNodeMachinePool(): Failed to find the the desiredConfig Annotation")
}
mc, err := dn.mcClient.MachineconfigurationV1().MachineConfigs().Get(context.TODO(), desiredConfig, metav1.GetOptions{})
mc, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigs().Get(context.TODO(), desiredConfig, metav1.GetOptions{})
if err != nil {
glog.Errorf("getNodeMachinePool(): Failed to get the desired Machine Config: %v", err)
return err
Expand Down Expand Up @@ -803,7 +802,7 @@ func (dn *Daemon) pauseMCP() error {
glog.Info("pauseMCP(): pausing MCP")
var err error

mcpInformerFactory := mcfginformers.NewSharedInformerFactory(dn.mcClient,
mcpInformerFactory := mcfginformers.NewSharedInformerFactory(dn.openshiftContext.McClient,
time.Second*30,
)
mcpInformer := mcpInformerFactory.Machineconfiguration().V1().MachineConfigPools().Informer()
Expand All @@ -818,7 +817,7 @@ func (dn *Daemon) pauseMCP() error {
return
}
// Always get the latest object
newMcp, err := dn.mcClient.MachineconfigurationV1().MachineConfigPools().Get(ctx, dn.mcpName, metav1.GetOptions{})
newMcp, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Get(ctx, dn.mcpName, metav1.GetOptions{})
if err != nil {
glog.V(2).Infof("pauseMCP(): Failed to get MCP %s: %v", dn.mcpName, err)
return
Expand All @@ -838,7 +837,7 @@ func (dn *Daemon) pauseMCP() error {
}
glog.Infof("pauseMCP(): pause MCP %s", dn.mcpName)
pausePatch := []byte("{\"spec\":{\"paused\":true}}")
_, err = dn.mcClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{})
_, err = dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{})
if err != nil {
glog.V(2).Infof("pauseMCP(): Failed to pause MCP %s: %v", dn.mcpName, err)
return
Expand All @@ -854,7 +853,7 @@ func (dn *Daemon) pauseMCP() error {
if paused {
glog.Infof("pauseMCP(): MCP is processing, resume MCP %s", dn.mcpName)
pausePatch := []byte("{\"spec\":{\"paused\":false}}")
_, err = dn.mcClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{})
_, err = dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{})
if err != nil {
glog.V(2).Infof("pauseMCP(): fail to resume MCP %s: %v", dn.mcpName, err)
return
Expand Down
6 changes: 4 additions & 2 deletions pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ var _ = Describe("Config Daemon", func() {
sut = New("test-node",
client,
kubeClient,
mcClient,
false,
utils.OpenshiftContext{
McClient: mcClient,
OpenshiftFlavor: utils.OpenshiftFlavorDefault,
},
exitCh,
stopCh,
syncCh,
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/openshift_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import mcclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"

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

const (
// Hypershift flavor of openshift: https://github.com/openshift/hypershift
OpenshiftFlavorHypershift OpenshiftFlavor = "hypershift"
OpenshiftFlavorDefault OpenshiftFlavor = "default"
)

// OpenshiftContext contains metadata and structs utilized to interact with Openshift clusters
type OpenshiftContext struct {
// McClient is a client for MachineConfigs in an Openshift environment
McClient mcclientset.Interface
// OpenshiftFlavor holds metadata about the type of Openshift environment the operator is in.
OpenshiftFlavor OpenshiftFlavor
}

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

0 comments on commit c5de82b

Please sign in to comment.