1
0
Fork 0
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:
Claude 2026-02-28 05:06:22 +00:00
parent f6e819e939
commit b122fb297f
No known key found for this signature in database
9 changed files with 106 additions and 96 deletions

View file

@ -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 repoSyncGVR = schema.GroupVersionResource{
@ -44,7 +46,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: "GitConflictResolver",
Logger: logger,
})

View file

@ -9,16 +9,13 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"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 resolving conflicted GitRepoSyncs.
@ -27,28 +24,17 @@ 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 that is in the Conflicted phase.
func (r *Reconciler) ReconcileKind(ctx context.Context, syncObj *gitv1alpha1.GitRepoSync) reconciler.Event {
logger := logging.FromContext(ctx)
namespace, name := internal.SplitKey(key)
// Fetch the GitRepoSync.
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
// Only process if still conflicted.
if syncObj.Status.Phase != gitv1alpha1.SyncPhaseConflicted {
return nil
}
logger.Infof("Attempting to resolve conflict for %s/%s", namespace, name)
logger.Infof("Attempting to resolve conflict for %s/%s", namespace, syncObj.Name)
// Get the repo URLs.
repoA, err := r.gitClient.GitRepositories(namespace).Get(ctx, syncObj.Spec.RepoA.Name, metav1.GetOptions{})
@ -348,12 +334,3 @@ func (r *Reconciler) markManualIntervention(ctx context.Context, syncObj *gitv1a
_, err := r.gitClient.GitRepoSyncs(syncObj.Namespace).UpdateStatus(ctx, syncObj, metav1.UpdateOptions{})
return err
}
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) {
}