diff --git a/internal/controller/tdset.go b/internal/controller/tdset.go new file mode 100644 index 0000000..6f94dc3 --- /dev/null +++ b/internal/controller/tdset.go @@ -0,0 +1,69 @@ +package controller + +import ( + "context" + + schedulev1 "github.com/baschno/tdset-operator/api/v1" + "k8s.io/apimachinery/pkg/api/meta" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/log" +) + +// ConditionStatus defines TDSet condition status. +type ConditionStatus string + +// Defines TDSet condition status. +const ( + TypeAvailable ConditionStatus = "Available" + TypeProgressing ConditionStatus = "Progressing" + TypeDegraded ConditionStatus = "Degraded" +) + +// GetTDSet gets the TDSet from api server. +func (r *TDSetReconciler) GetTDSet(ctx context.Context, req ctrl.Request, tdSet *schedulev1.TDSet) error { + + err := r.Get(ctx, req.NamespacedName, tdSet) + if err != nil { + return err + } + return nil +} + +// SetInitialCondition sets the status condition of the TDSet to available initially +// when no condition exists yet. +func (r *TDSetReconciler) SetInitialCondition(ctx context.Context, req ctrl.Request, tdSet *schedulev1.TDSet) error { + if tdSet.Status.Conditions != nil || len(tdSet.Status.Conditions) != 0 { + return nil + } + + err := r.SetCondition(ctx, req, tdSet, TypeAvailable, "Starting reconciliation") + + return err +} + +func (r *TDSetReconciler) SetCondition(ctx context.Context, req ctrl.Request, + tdSet *schedulev1.TDSet, condition ConditionStatus, message string) error { + + log := log.FromContext(ctx) + + meta.SetStatusCondition( + &tdSet.Status.Conditions, + metav1.Condition{ + Type: string(condition), + Status: metav1.ConditionUnknown, Reason: "Reconciling", + Message: message, + } + ) + + if err := r.Status().Update(ctx, tdSet); err != nil { + log.Error(err, "failed to update TDSet status condition") + return err + } + + if err := r.Get(ctx, req.NamespacedName, tdSet); err != nil { + log.Error(err, "failed to get updated TDSet after setting condition") + return err + } + + return nil +} diff --git a/internal/controller/tdset_controller.go b/internal/controller/tdset_controller.go index 41c4458..19deb03 100644 --- a/internal/controller/tdset_controller.go +++ b/internal/controller/tdset_controller.go @@ -19,6 +19,7 @@ package controller import ( "context" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -47,7 +48,29 @@ type TDSetReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/reconcile func (r *TDSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - _ = logf.FromContext(ctx) + log := logf.FromContext(ctx) + + log.Info("starting reconciliation of TDSet") + + tdSet := &schedulev1.TDSet{} + + err := r.GetTDSet(ctx, req, tdSet) + if err != nil { + if apierrors.IsNotFound(err) { + log.Info("TDSet resource not found. Ignoring since object must be deleted") + return ctrl.Result{}, nil + } + log.Error(err, "unable to fetch TDSet") + return ctrl.Result{}, err + } + + // set condition status + err = r.SetInitialCondition(ctx, req, tdSet) + + if err != nil { + log.Error(err, "unable to set initial condition for TDSet") + return ctrl.Result{}, err + } // TODO(user): your logic here