Skip to content

Commit e587482

Browse files
committed
Plumb keyfunc through
1 parent b2eebc2 commit e587482

File tree

12 files changed

+88
-592
lines changed

12 files changed

+88
-592
lines changed

examples/kcp/main.go

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2019 The Kubernetes Authors.
2+
Copyright 2022 The KCP Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -22,18 +22,21 @@ import (
2222
"os"
2323

2424
corev1 "k8s.io/api/core/v1"
25-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
"k8s.io/apimachinery/pkg/runtime"
2726

27+
kcpcache "github.com/kcp-dev/apimachinery/pkg/cache"
2828
kcpclient "github.com/kcp-dev/apimachinery/pkg/client"
29+
"github.com/kcp-dev/logicalcluster"
2930

3031
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3132
"k8s.io/client-go/rest"
3233
ctrl "sigs.k8s.io/controller-runtime"
33-
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
34+
3435
"sigs.k8s.io/controller-runtime/pkg/cache"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
37+
"sigs.k8s.io/controller-runtime/pkg/cluster"
3638
"sigs.k8s.io/controller-runtime/pkg/controller"
39+
3740
"sigs.k8s.io/controller-runtime/pkg/kcp"
3841
"sigs.k8s.io/controller-runtime/pkg/log"
3942
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -50,40 +53,16 @@ type reconciler struct {
5053
}
5154

5255
func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
53-
log := log.FromContext(ctx).WithValues("chaospod", req.ObjectKey)
54-
log.V(1).Info("reconciling chaos pod")
55-
56-
fmt.Println("***************************************")
57-
fmt.Println(req.ObjectKey.Cluster)
58-
fmt.Println(ctx.Value("clusterName"))
59-
fmt.Println("***************************************")
60-
// log.Info(fmt.Sprintf("%+v\n\n%+v\n", ctx, req))
61-
62-
var chaospod api.ChaosPod
63-
if err := r.Get(ctx, req.ObjectKey, &chaospod); err != nil {
64-
log.Error(err, "unable to get chaosctl")
65-
return ctrl.Result{}, err
66-
}
56+
log := log.FromContext(ctx).WithValues("cluster", req.ObjectKey.Cluster.String())
6757

68-
cm := &corev1.ConfigMap{
69-
TypeMeta: metav1.TypeMeta{
70-
APIVersion: "v1",
71-
Kind: "ConfigMap",
72-
},
73-
ObjectMeta: metav1.ObjectMeta{
74-
Name: "test-cm",
75-
Namespace: "default",
76-
// ClusterName: req.ClusterName,
77-
},
78-
Data: map[string]string{
79-
"test-key": "test-value",
80-
},
81-
}
82-
if err := r.Create(ctx, cm); err != nil {
83-
log.Info("CM creation failed", err)
84-
// log.Error(err, "this is fine")
85-
return ctrl.Result{}, nil
58+
ctx = kcpclient.WithCluster(ctx, req.ObjectKey.Cluster)
59+
60+
var configmap corev1.ConfigMap
61+
if err := r.Get(ctx, req.ObjectKey, &configmap); err != nil {
62+
log.Error(err, "unable to get configmap")
63+
return ctrl.Result{}, err
8664
}
65+
log.Info("Retrieved configmap")
8766

8867
return ctrl.Result{}, nil
8968
}
@@ -93,6 +72,7 @@ func main() {
9372
ctrl.SetLogger(zap.New())
9473

9574
cfg := ctrl.GetConfigOrDie()
75+
fmt.Println(cfg.Host)
9676
httpClient, err := rest.HTTPClientFor(cfg)
9777
if err != nil {
9878
setupLog.Error(err, "unable to build http client")
@@ -101,21 +81,23 @@ func main() {
10181
clusterRoundTripper := kcpclient.NewClusterRoundTripper(httpClient.Transport)
10282
httpClient.Transport = clusterRoundTripper
10383
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
104-
NewCache: cache.BuilderWithOptions(
105-
cache.Options{
106-
HTTPClient: httpClient,
107-
})})
84+
LeaderElection: false,
85+
NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
86+
c := rest.CopyConfig(config)
87+
c.Host += "/clusters/*"
88+
opts.KeyFunction = kcpcache.ClusterAwareKeyFunc
89+
return cache.New(c, opts)
90+
},
91+
NewClient: func(cache cache.Cache, config *rest.Config, opts client.Options, uncachedObjects ...client.Object) (client.Client, error) {
92+
opts.HTTPClient = httpClient
93+
return cluster.DefaultNewClient(cache, config, opts, uncachedObjects...)
94+
},
95+
})
10896
if err != nil {
10997
setupLog.Error(err, "unable to start manager")
11098
os.Exit(1)
11199
}
112100

113-
// in a real controller, we'd create a new scheme for this
114-
err = api.AddToScheme(mgr.GetScheme())
115-
if err != nil {
116-
setupLog.Error(err, "unable to add scheme")
117-
os.Exit(1)
118-
}
119101
err = corev1.AddToScheme(mgr.GetScheme())
120102
if err != nil {
121103
setupLog.Error(err, "unable to add scheme")
@@ -131,13 +113,13 @@ func main() {
131113
setupLog.Error(err, "unable to set up individual controller")
132114
os.Exit(1)
133115
}
134-
if err := c.Watch(&source.Kind{Type: &api.ChaosPod{}}, &kcp.EnqueueRequestForObject{}); err != nil {
135-
setupLog.Error(err, "unable to watch chaospods")
116+
if err := c.Watch(&source.Kind{Type: &corev1.ConfigMap{}}, &kcp.EnqueueRequestForObject{}); err != nil {
117+
setupLog.Error(err, "unable to watch configmaps")
136118
os.Exit(1)
137119
}
138120

139121
setupLog.Info("starting manager")
140-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
122+
if err := mgr.Start(kcpclient.WithCluster(ctrl.SetupSignalHandler(), logicalcluster.Wildcard)); err != nil {
141123
setupLog.Error(err, "problem running manager")
142124
os.Exit(1)
143125
}

examples/kcp/pkg/groupversion_info.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

examples/kcp/pkg/resource.go

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)