1
0
Fork 0
mirror of https://github.com/imjasonh/git-k8s synced 2026-07-10 15:41:54 +00:00
git-k8s/pkg/reconciler/repowatcher/controller.go
Claude ee281430b6
Add repo-watcher-controller for polling remote Git refs
Introduces a fourth controller that watches GitRepository resources and
polls their remotes via git ls-remote on a configurable per-repo interval
(spec.pollInterval, defaulting to 30s). It auto-creates, updates, and
deletes GitBranch CRDs to reflect the actual state of the remote,
enabling fast detection of external pushes to trigger downstream sync.

Key changes:
- Add PollInterval field to GitRepositorySpec
- New pkg/reconciler/repowatcher with controller and reconciler
- New cmd/repo-watcher-controller binary
- CRD manifest updated for pollInterval
- Deployment manifest for the new controller
- Unit tests for ref filtering, naming, and poll interval logic
- CLAUDE.md updated with new controller docs and Go module workaround

https://claude.ai/code/session_01P7xfBqARU5DFS8QJ4uDPVj
2026-02-28 14:09:58 +00:00

78 lines
2.1 KiB
Go

package repowatcher
import (
"context"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/logging"
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 repoGVR = schema.GroupVersionResource{
Group: gitv1alpha1.GroupName,
Version: gitv1alpha1.Version,
Resource: "gitrepositories",
}
// NewController creates a new controller for watching GitRepository remotes.
func NewController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
cfg := injection.GetConfig(ctx)
dynClient, err := dynamic.NewForConfig(cfg)
if err != nil {
logger.Fatalf("Error building dynamic client: %v", err)
}
gitClient := gitclient.NewFromDynamic(dynClient)
factory := dynamicinformer.NewDynamicSharedInformerFactory(dynClient, 30*time.Second)
repoInformer := factory.ForResource(repoGVR)
r := &Reconciler{
dynamicClient: dynClient,
gitClient: gitClient,
defaultInterval: DefaultPollInterval,
lsRemote: defaultLsRemote,
}
impl := controller.NewContext(ctx, internal.NewReconciler(
func(ctx context.Context, namespace, name string) (*gitv1alpha1.GitRepository, error) {
return gitClient.GitRepositories(namespace).Get(ctx, name, metav1.GetOptions{})
},
r,
), controller.ControllerOptions{
WorkQueueName: "RepoWatcher",
Logger: logger,
})
// Give the reconciler access to the impl for re-enqueue.
r.SetImpl(impl)
logger.Info("Setting up event handlers for repo-watcher")
repoInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
impl.Enqueue(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
impl.Enqueue(newObj)
},
})
factory.Start(ctx.Done())
factory.WaitForCacheSync(ctx.Done())
return impl
}