implementing reconciler
This commit is contained in:
@@ -4,6 +4,18 @@ kind: ClusterRole
|
|||||||
metadata:
|
metadata:
|
||||||
name: manager-role
|
name: manager-role
|
||||||
rules:
|
rules:
|
||||||
|
- apiGroups:
|
||||||
|
- apps
|
||||||
|
resources:
|
||||||
|
- deployments
|
||||||
|
verbs:
|
||||||
|
- create
|
||||||
|
- delete
|
||||||
|
- get
|
||||||
|
- list
|
||||||
|
- patch
|
||||||
|
- update
|
||||||
|
- watch
|
||||||
- apiGroups:
|
- apiGroups:
|
||||||
- schedule.rs
|
- schedule.rs
|
||||||
resources:
|
resources:
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
@@ -27,6 +29,10 @@ import (
|
|||||||
schedulev1 "github.com/baschno/tdset-operator/api/v1"
|
schedulev1 "github.com/baschno/tdset-operator/api/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultReconciliationInterval = 5
|
||||||
|
)
|
||||||
|
|
||||||
// TDSetReconciler reconciles a TDSet object
|
// TDSetReconciler reconciles a TDSet object
|
||||||
type TDSetReconciler struct {
|
type TDSetReconciler struct {
|
||||||
client.Client
|
client.Client
|
||||||
@@ -36,6 +42,7 @@ type TDSetReconciler struct {
|
|||||||
// +kubebuilder:rbac:groups=schedule.rs,resources=tdsets,verbs=get;list;watch;create;update;patch;delete
|
// +kubebuilder:rbac:groups=schedule.rs,resources=tdsets,verbs=get;list;watch;create;update;patch;delete
|
||||||
// +kubebuilder:rbac:groups=schedule.rs,resources=tdsets/status,verbs=get;update;patch
|
// +kubebuilder:rbac:groups=schedule.rs,resources=tdsets/status,verbs=get;update;patch
|
||||||
// +kubebuilder:rbac:groups=schedule.rs,resources=tdsets/finalizers,verbs=update
|
// +kubebuilder:rbac:groups=schedule.rs,resources=tdsets/finalizers,verbs=update
|
||||||
|
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
|
||||||
|
|
||||||
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
||||||
// move the current state of the cluster closer to the desired state.
|
// move the current state of the cluster closer to the desired state.
|
||||||
@@ -47,13 +54,60 @@ type TDSetReconciler struct {
|
|||||||
// For more details, check Reconcile and its Result here:
|
// For more details, check Reconcile and its Result here:
|
||||||
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/reconcile
|
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/reconcile
|
||||||
func (r *TDSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
func (r *TDSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||||
_ = log.FromContext(ctx)
|
log := log.FromContext(ctx)
|
||||||
|
|
||||||
log.Info("Starting reconciliation")
|
log.Info("Starting reconciliation")
|
||||||
// TODO(user): your logic here
|
|
||||||
|
tdSet := &schedulev1.TDSet{}
|
||||||
|
|
||||||
|
// Get TDSet
|
||||||
|
err := r.GetTDSet(ctx, req, tdSet)
|
||||||
|
if err != nil {
|
||||||
|
if apierrors.IsNotFound(err) {
|
||||||
|
log.Info("TDSet not found - ignoring since object must be deleted")
|
||||||
|
|
||||||
return ctrl.Result{}, nil
|
return ctrl.Result{}, nil
|
||||||
}
|
}
|
||||||
|
log.Error(err, "Failed to get TDSet")
|
||||||
|
return ctrl.Result{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to set initial status
|
||||||
|
err = r.SetInitialCondition(ctx, req, tdSet)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err, "Failed to set initial condition")
|
||||||
|
return ctrl.Result{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Delete finalizer
|
||||||
|
|
||||||
|
// Deployment if not exist
|
||||||
|
ok, err := r.DeploymentIfNotExist(ctx, req, tdSet)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err, "Failed to check deployment for TDSet")
|
||||||
|
return ctrl.Result{}, err
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
return ctrl.Result{RequeueAfter: time.Minute}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update deployment replica if mismatched
|
||||||
|
err = r.UpdateDeploymentReplica(ctx, req, tdSet)
|
||||||
|
if err != nil {
|
||||||
|
log.Log.Error(err, "Failed to update deployment replica for TDSet")
|
||||||
|
return ctrl.Result{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
interval := DefaultReconciliationInterval
|
||||||
|
if tdSet.Spec.IntervalMint != 0 {
|
||||||
|
interval = int(tdSet.Spec.IntervalMint)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Reconciliation done", "RequeueAfter", interval)
|
||||||
|
|
||||||
|
return ctrl.Result{RequeueAfter: time.Duration(time.Minute * time.Duration(interval))}, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// SetupWithManager sets up the controller with the Manager.
|
// SetupWithManager sets up the controller with the Manager.
|
||||||
func (r *TDSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
func (r *TDSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user