Skip to content

Commit

Permalink
ensure machine config and machine config pool operations are toggled …
Browse files Browse the repository at this point in the history
…off when interacting with hypershift clusters
  • Loading branch information
relyt0925 committed Aug 22, 2022
1 parent a6bee60 commit 0c0a221
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
14 changes: 14 additions & 0 deletions cmd/sriov-network-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

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 @@ -127,6 +129,17 @@ func runStartCmd(cmd *cobra.Command, args []string) {
snclient := snclientset.NewForConfigOrDie(config)
kubeclient := kubernetes.NewForConfigOrDie(config)
mcclient := mcclientset.NewForConfigOrDie(config)
isHypershift := false
if utils.ClusterType == utils.ClusterTypeOpenshift {
infraClient, err := client.New(config, client.Options{})
if err != nil {
panic(err)
}
isHypershift, err = utils.IsHypershiftCluster(infraClient)
if err != nil {
panic(err)
}
}

config.Timeout = 5 * time.Second
writerclient := snclientset.NewForConfigOrDie(config)
Expand Down Expand Up @@ -179,6 +192,7 @@ func runStartCmd(cmd *cobra.Command, args []string) {
snclient,
kubeclient,
mcclient,
isHypershift,
exitCh,
stopCh,
syncCh,
Expand Down
20 changes: 16 additions & 4 deletions controllers/sriovnetworkpoolconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,33 @@ func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ct
}
}
if utils.ClusterType == utils.ClusterTypeOpenshift {
if err = r.syncOvsHardwareOffloadMachineConfigs(instance, false); err != nil {
isHypershift, err := utils.IsHypershiftCluster(r.Client)
if err != nil {
return reconcile.Result{}, err
}
if !isHypershift {
if err = r.syncOvsHardwareOffloadMachineConfigs(instance, false); err != nil {
return reconcile.Result{}, err
}
}
}
} else {
// The object is being deleted
if sriovnetworkv1.StringInArray(sriovnetworkv1.POOLCONFIGFINALIZERNAME, instance.ObjectMeta.Finalizers) {
// our finalizer is present, so lets handle any external dependency
logger.Info("delete SriovNetworkPoolConfig CR", "Namespace", instance.Namespace, "Name", instance.Name)
if utils.ClusterType == utils.ClusterTypeOpenshift {
if err = r.syncOvsHardwareOffloadMachineConfigs(instance, true); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
isHypershift, err := utils.IsHypershiftCluster(r.Client)
if err != nil {
return reconcile.Result{}, err
}
if !isHypershift {
if err = r.syncOvsHardwareOffloadMachineConfigs(instance, true); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return reconcile.Result{}, err
}
}
}
// remove our finalizer from the list and update it.
var found bool
Expand Down
3 changes: 3 additions & 0 deletions deploy/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ rules:
- apiGroups: ["machineconfiguration.openshift.io"]
resources: ["*"]
verbs: ["*"]
- apiGroups: [ "config.openshift.io" ]
resources: [ "infrastructures" ]
verbs: [ "get", "list", "watch" ]
3 changes: 3 additions & 0 deletions deployment/sriov-network-operator/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ rules:
- apiGroups: [""]
resources: ["pods/eviction"]
verbs: ["create"]
- apiGroups: [ "config.openshift.io" ]
resources: [ "infrastructures" ]
verbs: [ "get", "list", "watch" ]
31 changes: 17 additions & 14 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type Daemon struct {
workqueue workqueue.RateLimitingInterface

mcpName string

isHypershift bool
}

const (
Expand Down Expand Up @@ -129,23 +131,25 @@ func New(
client snclientset.Interface,
kubeClient *kubernetes.Clientset,
mcClient *mcclientset.Clientset,
isHypershift bool,
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,
exitCh: exitCh,
stopCh: stopCh,
syncCh: syncCh,
refreshCh: refreshCh,
nodeState: &sriovnetworkv1.SriovNetworkNodeState{},
name: nodeName,
platform: platformType,
client: client,
kubeClient: kubeClient,
mcClient: mcClient,
isHypershift: isHypershift,
exitCh: exitCh,
stopCh: stopCh,
syncCh: syncCh,
refreshCh: refreshCh,
nodeState: &sriovnetworkv1.SriovNetworkNodeState{},
drainer: &drain.Helper{
Client: kubeClient,
Force: true,
Expand Down Expand Up @@ -477,12 +481,11 @@ func (dn *Daemon) nodeStateSyncHandler(generation int64) error {
}
}
}
if utils.ClusterType == utils.ClusterTypeOpenshift {
if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.isHypershift {
if err = dn.getNodeMachinePool(); err != nil {
return err
}
}

if reqDrain {
if !dn.isNodeDraining() {
if !dn.disableDrain {
Expand All @@ -495,7 +498,7 @@ func (dn *Daemon) nodeStateSyncHandler(generation int64) error {
<-done
}

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

if utils.ClusterType == utils.ClusterTypeOpenshift {
if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.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 {
Expand Down
7 changes: 5 additions & 2 deletions pkg/utils/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

"k8s.io/apimachinery/pkg/api/errors"

"github.com/golang/glog"

configv1 "github.com/openshift/api/config/v1"
Expand Down Expand Up @@ -62,11 +64,12 @@ func IsHypershiftCluster(c client.Client) (bool, error) {
func openshiftExternalControlPlaneStatus(c client.Client) (bool, error) {
infra := &configv1.Infrastructure{}
err := c.Get(context.TODO(), types.NamespacedName{Name: infraResourceName}, infra)
if err != nil {
if err != nil && !errors.IsNotFound(err) {
return false, err
}
// for nil will assume it's not hypershift cluster to aid in integration testing
if infra == nil {
return false, fmt.Errorf("getting resource Infrastructure (name: %s) succeeded but object was nil", infraResourceName)
return false, nil
}
return infra.Status.ControlPlaneTopology == configv1.ExternalTopologyMode, nil
}

0 comments on commit 0c0a221

Please sign in to comment.