mirror of
https://github.com/imjasonh/git-k8s
synced 2026-07-16 02:15:41 +00:00
Critical production-readiness improvements: - Prometheus metrics: reconcile count/latency per controller, Git operation duration (clone/push/ls-remote) via pkg/metrics with /metrics on :9090 - Health probes: leverage Knative's built-in health server on :8080, add readiness/liveness probes to all four deployment manifests - Git operation timeouts: 5-minute context deadline on all git.CloneContext and PushContext calls to prevent indefinite blocking on large repos - Unit tests: new ReconcileKind-level tests for push (pending→failed on missing repo, auth error paths), sync (branch-not-found, LastSyncTime), resolver (missing repo, partial hashes, empty phase), and repowatcher (ls-remote errors, empty remote, unchanged branches) https://claude.ai/code/session_01PaXbaSqhVEqj97kpY4v6rt
79 lines
2.1 KiB
Go
79 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(
|
|
"repowatcher",
|
|
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
|
|
}
|