Skip to content

Commit 96e48eb

Browse files
committed
add specific cluster and start individual cache
Signed-off-by: Karol Szwaj <[email protected]> On-behalf-of: @SAP [email protected]
1 parent 4a7e48a commit 96e48eb

File tree

2 files changed

+160
-2
lines changed

2 files changed

+160
-2
lines changed

initializingworkspaces/cluster.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
Copyright 2025 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package initializingworkspaces
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"net/http"
23+
"time"
24+
25+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
26+
27+
"k8s.io/apimachinery/pkg/api/meta"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/client-go/rest"
30+
"k8s.io/client-go/tools/record"
31+
"k8s.io/klog/v2"
32+
"sigs.k8s.io/controller-runtime/pkg/cache"
33+
"sigs.k8s.io/controller-runtime/pkg/client"
34+
"sigs.k8s.io/controller-runtime/pkg/cluster"
35+
36+
"github.com/kcp-dev/logicalcluster/v3"
37+
)
38+
39+
var _ cluster.Cluster = &specificCluster{}
40+
41+
type specificCluster struct {
42+
clusterName logicalcluster.Name
43+
44+
scheme *runtime.Scheme
45+
config *rest.Config
46+
httpClient *http.Client
47+
client client.Client
48+
mapper meta.RESTMapper
49+
cache cache.Cache
50+
}
51+
52+
// New method to create a cluster with specific URL
53+
func (p *Provider) createSpecificCluster(clusterName logicalcluster.Name, scheme *runtime.Scheme) (cluster.Cluster, error) {
54+
specificConfig := rest.CopyConfig(p.config)
55+
specificConfig.Host = fmt.Sprintf("%s/clusters/%s", specificConfig.Host, clusterName)
56+
57+
cli, err := client.New(specificConfig, client.Options{Scheme: scheme})
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
httpClient, err := rest.HTTPClientFor(specificConfig)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
mapper, err := apiutil.NewDynamicRESTMapper(specificConfig, httpClient)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
specificCache, err := cache.New(specificConfig, cache.Options{
73+
Scheme: scheme,
74+
Mapper: mapper,
75+
})
76+
if err != nil {
77+
return nil, fmt.Errorf("failed to create cache: %w", err)
78+
}
79+
80+
// Return the custom cluster implementation
81+
return &specificCluster{
82+
clusterName: clusterName,
83+
config: specificConfig,
84+
scheme: scheme,
85+
client: cli, // Direct client
86+
httpClient: httpClient,
87+
mapper: mapper, // Dedicated mapper
88+
cache: specificCache,
89+
}, nil
90+
}
91+
92+
// GetHTTPClient returns the HTTP client scoped to the cluster.
93+
func (c *specificCluster) GetHTTPClient() *http.Client {
94+
return c.httpClient
95+
}
96+
97+
// GetConfig returns the rest.Config scoped to the cluster.
98+
func (c *specificCluster) GetConfig() *rest.Config {
99+
return c.config
100+
}
101+
102+
// GetScheme returns the scheme scoped to the cluster.
103+
func (c *specificCluster) GetScheme() *runtime.Scheme {
104+
return c.scheme
105+
}
106+
107+
// GetFieldIndexer returns a FieldIndexer scoped to the cluster.
108+
func (c *specificCluster) GetFieldIndexer() client.FieldIndexer {
109+
return c.cache
110+
}
111+
112+
// GetRESTMapper returns a RESTMapper scoped to the cluster.
113+
func (c *specificCluster) GetRESTMapper() meta.RESTMapper {
114+
return c.mapper
115+
}
116+
117+
// GetCache returns a cache.Cache.
118+
func (c *specificCluster) GetCache() cache.Cache {
119+
return c.cache
120+
}
121+
122+
// GetClient returns a client scoped to the namespace.
123+
func (c *specificCluster) GetClient() client.Client {
124+
return c.client
125+
}
126+
127+
// GetEventRecorderFor returns a new EventRecorder for the provided name.
128+
func (c *specificCluster) GetEventRecorderFor(name string) record.EventRecorder {
129+
panic("implement me")
130+
}
131+
132+
// GetAPIReader returns a reader against the cluster.
133+
func (c *specificCluster) GetAPIReader() client.Reader {
134+
return c.cache
135+
}
136+
137+
// Start starts the cluster.
138+
func (c *specificCluster) Start(ctx context.Context) error {
139+
go func() {
140+
if err := c.cache.Start(ctx); err != nil {
141+
klog.Errorf("Error starting cache for cluster %s: %v", c.clusterName, err)
142+
}
143+
}()
144+
145+
_, cancel := context.WithTimeout(ctx, 30*time.Second)
146+
defer cancel()
147+
148+
return nil
149+
}

initializingworkspaces/provider.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,24 @@ func (p *Provider) handleLogicalClusterEvent(ctx context.Context, mgr mcmanager.
243243
return
244244
}
245245

246-
// create new scoped cluster.
246+
// create new specific cluster.
247247
clusterCtx, cancel := context.WithCancel(ctx)
248-
cl, err := mcpcache.NewScopedCluster(p.config, clusterName, p.cache, p.scheme)
248+
cl, err := p.createSpecificCluster(clusterName, p.scheme)
249249
if err != nil {
250250
p.log.Error(err, "failed to create cluster for initializing workspace", "cluster", clusterName)
251251
cancel()
252252
p.lock.Unlock()
253253
return
254254
}
255+
256+
// Start the cluster's cache and wait for it to sync BEFORE engaging
257+
p.log.Info("starting cluster cache", "cluster", clusterName)
258+
if err := cl.Start(clusterCtx); err != nil {
259+
p.log.Error(err, "failed to start cluster cache", "cluster", clusterName)
260+
cancel()
261+
p.lock.Unlock()
262+
return
263+
}
255264
p.clusters[clusterName] = cl
256265
p.cancelFns[clusterName] = cancel
257266
p.lock.Unlock()

0 commit comments

Comments
 (0)