Skip to content

Commit

Permalink
Store drain status in SriovNetworkNodeState
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ne committed Aug 22, 2023
1 parent ef50104 commit 807c34d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/v1/sriovnetworknodestate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type VirtualFunction struct {
// SriovNetworkNodeStateStatus defines the observed state of SriovNetworkNodeState
type SriovNetworkNodeStateStatus struct {
Interfaces InterfaceExts `json:"interfaces,omitempty"`
DrainStatus string `json:"drainStatus,omitempty"`
SyncStatus string `json:"syncStatus,omitempty"`
LastSyncError string `json:"lastSyncError,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ spec:
description: SriovNetworkNodeStateStatus defines the observed state of
SriovNetworkNodeState
properties:
drainStatus:
type: string
interfaces:
items:
properties:
Expand Down
69 changes: 69 additions & 0 deletions controllers/migration_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package controllers

import (
"context"
"fmt"
v1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type MigrationReconciler struct {
client.Client
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch;update;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (mr *MigrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
req.Namespace = namespace
reqLogger := log.FromContext(ctx).WithValues("drain", req.NamespacedName)
reqLogger.Info("Reconciling annotation migration")

node := &corev1.Node{}
err := mr.Get(ctx, types.NamespacedName{
Name: req.Name}, node)
if err != nil {
reqLogger.Error(err, "Error occurred on GET SriovOperatorConfig request from API server.")
return reconcile.Result{}, err
}

if anno, ok := node.Annotations[constants.NodeDrainAnnotation]; ok {
nodeState := &v1.SriovNetworkNodeState{}
err := mr.Get(ctx, types.NamespacedName{
Name: node.Name, Namespace: namespace}, nodeState)
if err != nil {
reqLogger.Error(err, "Error occurred on GET SriovNetworkNodeState request from API server.")
return reconcile.Result{}, err
}
patch := []byte(fmt.Sprintf(`{"status":{"drainStatus":"%s"}}`, anno))
err = mr.Client.Patch(context.TODO(), nodeState, client.RawPatch(types.StrategicMergePatchType, patch))
if err != nil {
reqLogger.Error(err, "Error occurred on SriovNetworkNodeState update.")
return reconcile.Result{}, err
}

}

return reconcile.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (mr *MigrationReconciler) SetupWithManager(mgr ctrl.Manager) error {
nodePredicates := builder.WithPredicates(DrainAnnotationPredicate{})

return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Node{}, nodePredicates).
Complete(mr)
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ spec:
description: SriovNetworkNodeStateStatus defines the observed state of
SriovNetworkNodeState
properties:
drainStatus:
type: string
interfaces:
items:
properties:
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "DrainReconciler")
os.Exit(1)
}
if err = (&controllers.MigrationReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MigrationReconciler")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

// Create a default SriovNetworkNodePolicy
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func openshiftControlPlaneTopologyStatus(c client.Client) (configv1.TopologyMode
}

func NodeHasAnnotation(node corev1.Node, annoKey string, value string) bool {
// Check if node already contains annotation
// Check if node already contains annotation with specified value
if anno, ok := node.Annotations[annoKey]; ok && (anno == value) {
return true
}
Expand Down

0 comments on commit 807c34d

Please sign in to comment.