-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store drain status in SriovNetworkNodeState
- Loading branch information
Showing
6 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters