Skip to content

Commit 511599c

Browse files
committed
load and store cluster path in sync context
On-behalf-of: @SAP [email protected]
1 parent dca5fbe commit 511599c

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ require (
7070
github.com/mattn/go-isatty v0.0.20 // indirect
7171
github.com/mitchellh/copystructure v1.2.0 // indirect
7272
github.com/mitchellh/go-homedir v1.1.0 // indirect
73-
github.com/mitchellh/mapstructure v1.3.3 // indirect
73+
github.com/mitchellh/mapstructure v1.4.1 // indirect
7474
github.com/mitchellh/reflectwalk v1.0.2 // indirect
7575
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7676
github.com/modern-go/reflect2 v1.0.2 // indirect

Diff for: go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
229229
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
230230
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
231231
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
232-
github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
233-
github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
232+
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
233+
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
234234
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
235235
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
236236
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

Diff for: internal/controller/sync/controller.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"fmt"
2222
"time"
2323

24+
kcpcore "github.com/kcp-dev/kcp/sdk/apis/core"
25+
kcpdevcorev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
2426
"github.com/kcp-dev/logicalcluster/v3"
2527
"go.uber.org/zap"
2628

@@ -31,6 +33,7 @@ import (
3133
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
3234

3335
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
36+
"k8s.io/apimachinery/pkg/types"
3437
"k8s.io/utils/ptr"
3538
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
3639
"sigs.k8s.io/controller-runtime/pkg/cluster"
@@ -52,6 +55,7 @@ type Reconciler struct {
5255
log *zap.SugaredLogger
5356
syncer *sync.ResourceSyncer
5457
remoteDummy *unstructured.Unstructured
58+
pubRes *syncagentv1alpha1.PublishedResource
5559
}
5660

5761
// Create creates a new controller and importantly does *not* add it to the manager,
@@ -99,6 +103,7 @@ func Create(
99103
log: log,
100104
remoteDummy: remoteDummy,
101105
syncer: syncer,
106+
pubRes: pubRes,
102107
}
103108

104109
ctrlOptions := controller.Options{
@@ -152,8 +157,22 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
152157
return reconcile.Result{}, nil
153158
}
154159

160+
syncContext := sync.NewContext(ctx, wsCtx)
161+
162+
// if desired, fetch the cluster path as well (some downstream service providers might make use of it,
163+
// but since it requires an additional permission claim, it's optional)
164+
if r.pubRes.Spec.EnableClusterPaths {
165+
lc := &kcpdevcorev1alpha1.LogicalCluster{}
166+
if err := r.vwClient.Get(wsCtx, types.NamespacedName{Name: kcpdevcorev1alpha1.LogicalClusterName}, lc); err != nil {
167+
return reconcile.Result{}, fmt.Errorf("failed to retrieve remote logicalcluster: %w", err)
168+
}
169+
170+
path := lc.Annotations[kcpcore.LogicalClusterPathAnnotationKey]
171+
syncContext = syncContext.WithClusterPath(logicalcluster.NewPath(path))
172+
}
173+
155174
// sync main object
156-
requeue, err := r.syncer.Process(sync.NewContext(ctx, wsCtx), remoteObj)
175+
requeue, err := r.syncer.Process(syncContext, remoteObj)
157176
if err != nil {
158177
return reconcile.Result{}, err
159178
}

Diff for: internal/controller/syncmanager/lifecycle/cluster.go

+9
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ import (
2424
"regexp"
2525
"strings"
2626

27+
kcpdevcorev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
2728
"github.com/kcp-dev/logicalcluster/v3"
2829
"go.uber.org/zap"
2930

3031
"k8s.io/apimachinery/pkg/api/meta"
32+
"k8s.io/apimachinery/pkg/runtime"
3133
"k8s.io/client-go/rest"
3234
"sigs.k8s.io/controller-runtime/pkg/cache"
3335
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -130,7 +132,14 @@ func NewCluster(address string, baseRestConfig *rest.Config) (*Cluster, error) {
130132
return newClusterAwareRoundTripper(rt)
131133
})
132134

135+
scheme := runtime.NewScheme()
136+
137+
if err := kcpdevcorev1alpha1.AddToScheme(scheme); err != nil {
138+
return nil, fmt.Errorf("failed to register scheme %s: %w", kcpdevcorev1alpha1.SchemeGroupVersion, err)
139+
}
140+
133141
clusterObj, err := cluster.New(config, func(o *cluster.Options) {
142+
o.Scheme = scheme
134143
o.NewCache = kcp.NewClusterAwareCache
135144
o.NewAPIReader = kcp.NewClusterAwareAPIReader
136145
o.NewClient = kcp.NewClusterAwareClient

Diff for: internal/sync/context.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package sync
1919
import (
2020
"context"
2121

22+
"github.com/kcp-dev/logicalcluster/v3"
2223
"sigs.k8s.io/controller-runtime/pkg/kontext"
2324
)
2425

2526
type Context struct {
26-
clusterName string
27+
clusterName logicalcluster.Name
28+
clusterPath logicalcluster.Path
2729
local context.Context
2830
remote context.Context
2931
}
@@ -35,8 +37,17 @@ func NewContext(local, remote context.Context) Context {
3537
}
3638

3739
return Context{
38-
clusterName: string(clusterName),
40+
clusterName: clusterName,
3941
local: local,
4042
remote: remote,
4143
}
4244
}
45+
46+
func (c *Context) WithClusterPath(path logicalcluster.Path) Context {
47+
return Context{
48+
clusterName: c.clusterName,
49+
clusterPath: path,
50+
local: c.local,
51+
remote: c.remote,
52+
}
53+
}

Diff for: internal/sync/context_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewContext(t *testing.T) {
3131

3232
combinedCtx := NewContext(context.Background(), ctx)
3333

34-
if combinedCtx.clusterName != clusterName.String() {
34+
if combinedCtx.clusterName != clusterName {
3535
t.Fatalf("Expected function to recognize the cluster name in the context, but got %q", combinedCtx.clusterName)
3636
}
3737
}

0 commit comments

Comments
 (0)