1
0
Fork 0
mirror of https://github.com/imjasonh/git-k8s synced 2026-07-12 00:19:47 +00:00
git-k8s/pkg/reconciler/push/controller.go
Claude 0cb9e4aa1d
Fix multiple code review issues: dedup splitKey, fix merge bug, remove dead code
- Extract triplicated splitKey into shared pkg/reconciler/internal package
  using strings.Cut and removing the unnecessary error return
- Fix resolver merge bug: attemptMerge was using treeA.Hash as the merge
  commit tree, silently dropping all of branch B's changes. Now builds a
  proper merged tree that applies B's non-conflicting changes onto A's tree
- Replace hand-rolled nestedFieldNoCopy/unstructuredNestedStringMap with
  k8s.io/apimachinery's unstructured.NestedStringMap stdlib equivalent
- Hoist branch listing out of per-refspec loop in updateBranches to avoid
  redundant API calls
- Add not-found handling in all three reconcilers so deleted resources
  don't cause infinite requeue loops
- Fix client.Get() to use checked type assertion with a clear panic message
  instead of an opaque nil-pointer dereference
- Remove dead code: unused addDefaultingFuncs wrapper, unused context
  injection in push controller setup

https://claude.ai/code/session_01634JRpHEoM9FzuBjZ7qHcY
2026-02-28 04:39:00 +00:00

70 lines
1.8 KiB
Go

package push
import (
"context"
"time"
"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"
)
var pushTransactionGVR = schema.GroupVersionResource{
Group: gitv1alpha1.GroupName,
Version: gitv1alpha1.Version,
Resource: "gitpushtransactions",
}
// NewController creates a new controller for GitPushTransaction resources.
func NewController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
// Get dynamic client from the injected config.
cfg := injection.GetConfig(ctx)
dynClient, err := dynamic.NewForConfig(cfg)
if err != nil {
logger.Fatalf("Error building dynamic client: %v", err)
}
gitClient := gitclient.NewFromDynamic(dynClient)
// Create dynamic informer for GitPushTransaction.
factory := dynamicinformer.NewDynamicSharedInformerFactory(dynClient, 30*time.Second)
transactionInformer := factory.ForResource(pushTransactionGVR)
r := &Reconciler{
dynamicClient: dynClient,
gitClient: gitClient,
}
impl := controller.NewContext(ctx, r, controller.ControllerOptions{
WorkQueueName: "GitPushTransactions",
Logger: logger,
})
logger.Info("Setting up event handlers for GitPushTransaction")
transactionInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
impl.Enqueue(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
impl.Enqueue(newObj)
},
})
// Start the informer factory.
factory.Start(ctx.Done())
factory.WaitForCacheSync(ctx.Done())
return impl
}