1
0
Fork 0
mirror of https://github.com/imjasonh/git-k8s synced 2026-07-18 06:55:48 +00:00

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
This commit is contained in:
Claude 2026-02-28 04:39:00 +00:00
parent 9d5cd83734
commit 0cb9e4aa1d
No known key found for this signature in database
11 changed files with 166 additions and 409 deletions

View file

@ -3,9 +3,11 @@ package sync
import (
"context"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"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"
@ -14,6 +16,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"
)
// Reconciler implements the reconcile logic for GitRepoSync.
@ -26,13 +29,14 @@ type Reconciler struct {
func (r *Reconciler) Reconcile(ctx context.Context, key string) error {
logger := logging.FromContext(ctx)
namespace, name, err := splitKey(key)
if err != nil {
return err
}
namespace, name := internal.SplitKey(key)
// Fetch the GitRepoSync resource.
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)
}
@ -208,15 +212,6 @@ func (r *Reconciler) updateSyncStatus(ctx context.Context, syncObj *gitv1alpha1.
return err
}
func splitKey(key string) (string, string, error) {
for i := range key {
if key[i] == '/' {
return key[:i], key[i+1:], nil
}
}
return "", key, nil
}
// Ensure Reconciler implements the reconciler interface.
var _ reconciler.LeaderAware = (*Reconciler)(nil)
@ -226,4 +221,3 @@ func (r *Reconciler) Promote(bkt reconciler.Bucket, enq func(reconciler.Bucket,
func (r *Reconciler) Demote(bkt reconciler.Bucket) {
}

View file

@ -1,54 +1 @@
package sync
import (
"testing"
)
func TestSplitKey(t *testing.T) {
tests := []struct {
key string
wantNS string
wantName string
}{
{
key: "default/my-sync",
wantNS: "default",
wantName: "my-sync",
},
{
key: "git-system/sync-controller",
wantNS: "git-system",
wantName: "sync-controller",
},
{
key: "just-name",
wantNS: "",
wantName: "just-name",
},
{
key: "",
wantNS: "",
wantName: "",
},
{
key: "ns/name/extra",
wantNS: "ns",
wantName: "name/extra",
},
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
ns, name, err := splitKey(tt.key)
if err != nil {
t.Fatalf("splitKey(%q) error = %v", tt.key, err)
}
if ns != tt.wantNS {
t.Errorf("namespace = %q, want %q", ns, tt.wantNS)
}
if name != tt.wantName {
t.Errorf("name = %q, want %q", name, tt.wantName)
}
})
}
}