Skip to content

Commit 6aab6ba

Browse files
Merge pull request kubernetes-sigs#1 from fabianvf/cluster/cache
????
2 parents 19cf5db + 1cae937 commit 6aab6ba

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

examples/kcp/main.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121
"fmt"
2222
"os"
2323

24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
"k8s.io/apimachinery/pkg/runtime"
27+
2528
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2629
ctrl "sigs.k8s.io/controller-runtime"
2730
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
@@ -44,18 +47,41 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
4447
log := log.FromContext(ctx).WithValues("chaospod", req.NamespacedName)
4548
log.V(1).Info("reconciling chaos pod")
4649

47-
log.Info(fmt.Sprintf("%+v\n\n%+v\n", ctx, req))
50+
fmt.Println("***************************************")
51+
fmt.Println(req.ClusterName)
52+
fmt.Println("***************************************")
53+
// log.Info(fmt.Sprintf("%+v\n\n%+v\n", ctx, req))
4854

49-
var chaospod api.ChaosPod
55+
chaospod := api.ChaosPod{ObjectMeta: metav1.ObjectMeta{ClusterName: req.ClusterName}}
5056
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil {
5157
log.Error(err, "unable to get chaosctl")
5258
return ctrl.Result{}, err
5359
}
5460

61+
cm := &corev1.ConfigMap{
62+
TypeMeta: metav1.TypeMeta{
63+
APIVersion: "v1",
64+
Kind: "ConfigMap",
65+
},
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: "test-cm",
68+
Namespace: "default",
69+
ClusterName: req.ClusterName,
70+
},
71+
Data: map[string]string{
72+
"test-key": "test-value",
73+
},
74+
}
75+
if err := r.Create(ctx, cm); err != nil {
76+
log.Error(err, "unable to create configmap")
77+
return ctrl.Result{}, err
78+
}
79+
5580
return ctrl.Result{}, nil
5681
}
5782

5883
func main() {
84+
5985
ctrl.SetLogger(zap.New())
6086

6187
cfg := ctrl.GetConfigOrDie()
@@ -73,6 +99,11 @@ func main() {
7399
setupLog.Error(err, "unable to add scheme")
74100
os.Exit(1)
75101
}
102+
err = corev1.AddToScheme(mgr.GetScheme())
103+
if err != nil {
104+
setupLog.Error(err, "unable to add scheme")
105+
os.Exit(1)
106+
}
76107

77108
err = ctrl.NewControllerManagedBy(mgr).
78109
For(&api.ChaosPod{}).

pkg/cache/clusterscoped_cache.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@ func MultiClusterCacheBuilder(clusterNames []string) NewCacheFunc {
5050
caches := map[string]Cache{}
5151

5252
// create aglobal cache for * scope
53-
gCache, err := New(config, opts)
53+
globalConfig := *config
54+
globalConfig.Host = globalConfig.Host + "/clusters/*"
55+
gCache, err := New(&globalConfig, opts)
5456
if err != nil {
5557
return nil, fmt.Errorf("error creating global cache %v", err)
5658
}
5759

5860
for _, cs := range clusterNames {
61+
scopedConfig := *config
62+
scopedConfig.Host = config.Host + "/clusters/" + cs
5963
opts.ClusterName = cs
60-
c, err := New(config, opts)
64+
c, err := New(&scopedConfig, opts)
6165
if err != nil {
6266
return nil, err
6367
}

pkg/cache/internal/cache_reader.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime/schema"
3030
"k8s.io/apimachinery/pkg/selection"
3131
"k8s.io/client-go/tools/cache"
32+
"k8s.io/client-go/tools/clusters"
3233

3334
"sigs.k8s.io/controller-runtime/pkg/client"
3435
)
@@ -61,8 +62,11 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Ob
6162
if c.scopeName == apimeta.RESTScopeNameRoot {
6263
key.Namespace = ""
6364
}
64-
storeKey := objectKeyToStoreKey(key, out.GetClusterName())
65+
key.Name = clusters.ToClusterAwareKey(out.GetClusterName(), key.Name)
66+
67+
storeKey := objectKeyToStoreKey(key)
6568
fmt.Println("storeKey", storeKey)
69+
fmt.Println("c.Indexer.ListKeys()", c.indexer.ListKeys())
6670

6771
// Lookup the object from the indexer cache
6872
obj, exists, err := c.indexer.GetByKey(storeKey)
@@ -184,11 +188,11 @@ func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...cli
184188
// It's akin to MetaNamespaceKeyFunc. It's separate from
185189
// String to allow keeping the key format easily in sync with
186190
// MetaNamespaceKeyFunc.
187-
func objectKeyToStoreKey(k client.ObjectKey, clusterName string) string {
191+
func objectKeyToStoreKey(k client.ObjectKey) string {
188192
if k.Namespace == "" {
189-
return k.Name + clusterName
193+
return k.Name
190194
}
191-
return k.Namespace + "/" + k.Name + "/" + clusterName
195+
return k.Namespace + "/" + k.Name
192196
}
193197

194198
// requiresExactMatch checks if the given field selector is of the form `k=v` or `k==v`.

0 commit comments

Comments
 (0)