mirror of
https://github.com/imjasonh/git-k8s
synced 2026-07-18 15:05:45 +00:00
Refactor reconcilers to use Knative ReconcileKind pattern
Move the key-splitting, resource-fetching, and not-found handling into a generic internal.NewReconciler wrapper so that each reconciler only implements ReconcileKind(ctx, *TypedResource) — matching the Knative generated reconciler convention. The controller setup wraps the reconciler with internal.NewReconciler, which bridges Reconcile(ctx, key) to ReconcileKind(ctx, *T). This removes duplicated boilerplate from all three reconcilers (push, sync, resolver), eliminates their Reconcile/LeaderAware methods, and makes splitKey an unexported implementation detail. https://claude.ai/code/session_01Eu3LyxX3G1KCcw4aDzmHvp
This commit is contained in:
parent
f6e819e939
commit
b122fb297f
9 changed files with 106 additions and 96 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/dynamic"
|
||||
|
|
@ -16,6 +17,7 @@ import (
|
|||
|
||||
gitv1alpha1 "github.com/imjasonh/git-k8s/pkg/apis/git/v1alpha1"
|
||||
gitclient "github.com/imjasonh/git-k8s/pkg/client"
|
||||
"github.com/imjasonh/git-k8s/pkg/reconciler/internal"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -52,7 +54,12 @@ func NewController(ctx context.Context, cmw configmap.Watcher) *controller.Impl
|
|||
gitClient: gitClient,
|
||||
}
|
||||
|
||||
impl := controller.NewContext(ctx, r, controller.ControllerOptions{
|
||||
impl := controller.NewContext(ctx, internal.NewReconciler(
|
||||
func(ctx context.Context, namespace, name string) (*gitv1alpha1.GitRepoSync, error) {
|
||||
return gitClient.GitRepoSyncs(namespace).Get(ctx, name, metav1.GetOptions{})
|
||||
},
|
||||
r,
|
||||
), controller.ControllerOptions{
|
||||
WorkQueueName: "GitRepoSyncs",
|
||||
Logger: logger,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -7,16 +7,13 @@ import (
|
|||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/storage/memory"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/dynamic"
|
||||
"knative.dev/pkg/logging"
|
||||
"knative.dev/pkg/reconciler"
|
||||
|
||||
gitv1alpha1 "github.com/imjasonh/git-k8s/pkg/apis/git/v1alpha1"
|
||||
gitclient "github.com/imjasonh/git-k8s/pkg/client"
|
||||
"github.com/imjasonh/git-k8s/pkg/reconciler/internal"
|
||||
)
|
||||
|
||||
// Reconciler implements the reconcile logic for GitRepoSync.
|
||||
|
|
@ -25,21 +22,11 @@ type Reconciler struct {
|
|||
gitClient *gitclient.GitV1alpha1Client
|
||||
}
|
||||
|
||||
// Reconcile implements the controller.Reconciler interface.
|
||||
func (r *Reconciler) Reconcile(ctx context.Context, key string) error {
|
||||
// ReconcileKind processes a single GitRepoSync resource.
|
||||
func (r *Reconciler) ReconcileKind(ctx context.Context, syncObj *gitv1alpha1.GitRepoSync) reconciler.Event {
|
||||
logger := logging.FromContext(ctx)
|
||||
|
||||
namespace, name := internal.SplitKey(key)
|
||||
|
||||
// Fetch the GitRepoSync resource.
|
||||
syncObj, err := r.gitClient.GitRepoSyncs(namespace).Get(ctx, name, metav1.GetOptions{})
|
||||
if apierrors.IsNotFound(err) {
|
||||
logger.Debugf("GitRepoSync %s/%s no longer exists", namespace, name)
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting GitRepoSync %s/%s: %w", namespace, name, err)
|
||||
}
|
||||
namespace := syncObj.Namespace
|
||||
name := syncObj.Name
|
||||
|
||||
// Get the branches for both repos.
|
||||
branchA, err := r.findBranch(ctx, namespace, syncObj.Spec.RepoA.Name, syncObj.Spec.BranchName)
|
||||
|
|
@ -211,13 +198,3 @@ func (r *Reconciler) updateSyncStatus(ctx context.Context, syncObj *gitv1alpha1.
|
|||
_, err := r.gitClient.GitRepoSyncs(syncObj.Namespace).UpdateStatus(ctx, syncObj, metav1.UpdateOptions{})
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure Reconciler implements the reconciler interface.
|
||||
var _ reconciler.LeaderAware = (*Reconciler)(nil)
|
||||
|
||||
func (r *Reconciler) Promote(bkt reconciler.Bucket, enq func(reconciler.Bucket, types.NamespacedName)) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) Demote(bkt reconciler.Bucket) {
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue