mirror of
https://github.com/imjasonh/git-k8s
synced 2026-07-13 08:57:00 +00:00
Add pkg/workspace.Manager that provides Acquire/Release semantics for Git repos. When GIT_CACHE_DIR is set and a GitRepository has spec.cache.enabled, controllers use on-disk bare clones with incremental fetch; otherwise they fall back to the existing in-memory clone behavior. Key changes: - New pkg/workspace package with Manager, Workspace, GC, Deepen, and context-based injection (WithManager/GetManager) - GitRepository API gains spec.cache (CacheConfig) and status.cache (CacheStatus) with deepcopy and CRD schema updates - Push, sync, and resolver reconcilers now use workspace.Acquire/Release instead of inline memory.NewStorage + git.CloneContext - Shallow clone by default; sync/resolver deepen as needed for merge-base - New Prometheus metrics: workspace_acquire_duration_seconds, workspace_cache_hit_total, workspace_cache_miss_total - Deployment manifests include commented-out PVC configuration - Comprehensive unit tests for workspace package (21 tests) - E2E tests for cache-free and cache-enabled push flows https://claude.ai/code/session_01NULGxaCLPMT1yDVEAsffc5
91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package resolver
|
|
|
|
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"
|
|
"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"
|
|
"github.com/imjasonh/git-k8s/pkg/workspace"
|
|
)
|
|
|
|
var repoSyncGVR = schema.GroupVersionResource{
|
|
Group: gitv1alpha1.GroupName,
|
|
Version: gitv1alpha1.Version,
|
|
Resource: "gitreposyncs",
|
|
}
|
|
|
|
// NewController creates a new controller for resolving conflicted GitRepoSyncs.
|
|
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)
|
|
syncInformer := factory.ForResource(repoSyncGVR)
|
|
|
|
r := &Reconciler{
|
|
dynamicClient: dynClient,
|
|
gitClient: gitClient,
|
|
workspaces: workspace.GetManager(ctx),
|
|
}
|
|
|
|
impl := controller.NewContext(ctx, internal.NewReconciler(
|
|
"resolver",
|
|
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,
|
|
})
|
|
|
|
logger.Info("Setting up event handlers for GitConflictResolver")
|
|
|
|
// Only watch GitRepoSync resources that are in the Conflicted phase.
|
|
syncInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
|
|
FilterFunc: func(obj interface{}) bool {
|
|
u, ok := obj.(*unstructured.Unstructured)
|
|
if !ok {
|
|
return false
|
|
}
|
|
phase, found, err := unstructured.NestedString(u.Object, "status", "phase")
|
|
if err != nil || !found {
|
|
return false
|
|
}
|
|
return phase == string(gitv1alpha1.SyncPhaseConflicted)
|
|
},
|
|
Handler: 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
|
|
}
|