Skip to content

Commit

Permalink
feat(controller): add paused resource handling and update manager set…
Browse files Browse the repository at this point in the history
…up (#156)
  • Loading branch information
jds9090 authored Jan 20, 2025
1 parent 82a76bd commit a0c5d3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
28 changes: 20 additions & 8 deletions controllers/kamajicontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/component-base/featuregate"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -74,11 +76,7 @@ func (r *KamajiControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R

return ctrl.Result{}, nil
}
// Handling finalizer for external deployment:
// in case of ExternalClusterReference the remote TCP must be deleted.
if kcp.DeletionTimestamp != nil {
return r.handleDeletion(ctx, kcp)
}

// Retrieving the Cluster information
cluster := capiv1beta1.Cluster{}
cluster.SetName(kcp.GetOwnerReferences()[0].Name)
Expand All @@ -95,6 +93,20 @@ func (r *KamajiControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.R

return ctrl.Result{}, err //nolint:wrapcheck
}

// Return early if the object or Cluster is paused.
if annotations.IsPaused(&cluster, &kcp) {
log.Info("Reconciliation is paused for this object")

return ctrl.Result{}, nil
}

// Handling finalizer for external deployment:
// in case of ExternalClusterReference the remote TCP must be deleted.
if kcp.DeletionTimestamp != nil {
return r.handleDeletion(ctx, kcp)
}

// Extracting conditions, used to update the KamajiControlPlane ones upon the end of the reconciliation.
conditions := kcp.Status.Conditions

Expand Down Expand Up @@ -355,16 +367,16 @@ func (r *KamajiControlPlaneReconciler) updateKamajiControlPlaneStatus(ctx contex
}

// SetupWithManager sets up the controller with the Manager.
func (r *KamajiControlPlaneReconciler) SetupWithManager(mgr ctrl.Manager, channel chan event.GenericEvent) error {
func (r *KamajiControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, channel chan event.GenericEvent) error {
r.client = mgr.GetClient()

ctrlBuilder := ctrl.NewControllerManagedBy(mgr).
For(&kcpv1alpha1.KamajiControlPlane{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(object client.Object) bool {
return len(object.GetOwnerReferences()) > 0
}))).
Owns(&corev1.Secret{}).
WatchesRawSource(source.Channel(channel, &handler.EnqueueRequestForObject{})).
WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles})
WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles}).
WithEventFilter(predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx)))

cs, csErr := kubernetes.NewForConfig(mgr.GetConfig())
if csErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
k8s.io/component-base v0.31.1
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
sigs.k8s.io/cluster-api v1.8.4
sigs.k8s.io/controller-runtime v0.19.0
)
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
k8s.io/apiextensions-apiserver v0.31.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func main() {
ExternalClusterReferenceStore: ecrStore,
FeatureGates: featureGate,
MaxConcurrentReconciles: maxConcurrentReconciles,
}).SetupWithManager(mgr, triggerChannel); err != nil {
}).SetupWithManager(ctx, mgr, triggerChannel); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "KamajiControlPlane")
os.Exit(1)
}
Expand Down

0 comments on commit a0c5d3f

Please sign in to comment.