-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Cluster Provider and cluster-aware controllers
Signed-off-by: Vince Prignano <vincepri@redhat.com>
- Loading branch information
Showing
25 changed files
with
1,923 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module sigs.k8s.io/controller-runtime/examples/fleet | ||
|
||
go 1.19 | ||
|
||
replace sigs.k8s.io/controller-runtime => ../.. | ||
|
||
require ( | ||
k8s.io/api v0.26.1 | ||
k8s.io/apimachinery v0.26.1 | ||
k8s.io/client-go v0.26.1 | ||
k8s.io/klog/v2 v2.90.0 | ||
sigs.k8s.io/controller-runtime v0.0.0-00010101000000-000000000000 | ||
sigs.k8s.io/kind v0.17.0 | ||
sigs.k8s.io/logical-cluster v0.0.1-alpha.0 | ||
) | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.0.0 // indirect | ||
github.com/alessio/shellescape v1.4.1 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/emicklei/go-restful/v3 v3.9.0 // indirect | ||
github.com/evanphx/json-patch/v5 v5.6.0 // indirect | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.5 // indirect | ||
github.com/go-openapi/jsonreference v0.20.0 // indirect | ||
github.com/go-openapi/swag v0.19.14 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/gnostic v0.5.7-v3refs // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/google/gofuzz v1.1.0 // indirect | ||
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect | ||
github.com/google/uuid v1.1.2 // indirect | ||
github.com/imdario/mergo v0.3.6 // indirect | ||
github.com/inconshreveable/mousetrap v1.0.1 // indirect | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/mailru/easyjson v0.7.6 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/pelletier/go-toml v1.9.4 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/prometheus/client_golang v1.14.0 // indirect | ||
github.com/prometheus/client_model v0.3.0 // indirect | ||
github.com/prometheus/common v0.37.0 // indirect | ||
github.com/prometheus/procfs v0.8.0 // indirect | ||
github.com/spf13/cobra v1.6.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/net v0.7.0 // indirect | ||
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect | ||
golang.org/x/sys v0.5.0 // indirect | ||
golang.org/x/term v0.5.0 // indirect | ||
golang.org/x/text v0.7.0 // indirect | ||
golang.org/x/time v0.3.0 // indirect | ||
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/apiextensions-apiserver v0.26.1 // indirect | ||
k8s.io/component-base v0.26.1 // indirect | ||
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect | ||
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect | ||
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/cluster" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
kind "sigs.k8s.io/kind/pkg/cluster" | ||
"sigs.k8s.io/logical-cluster" | ||
) | ||
|
||
func init() { | ||
ctrl.SetLogger(klog.Background()) | ||
} | ||
|
||
func main() { | ||
entryLog := log.Log.WithName("entrypoint") | ||
|
||
testEnv := &envtest.Environment{} | ||
cfg, err := testEnv.Start() | ||
if err != nil { | ||
entryLog.Error(err, "failed to start local environment") | ||
os.Exit(1) | ||
} | ||
defer func() { | ||
if testEnv == nil { | ||
return | ||
} | ||
if err := testEnv.Stop(); err != nil { | ||
entryLog.Error(err, "failed to stop local environment") | ||
os.Exit(1) | ||
} | ||
}() | ||
|
||
// Setup a Manager | ||
entryLog.Info("Setting up manager") | ||
mgr, err := manager.New( | ||
cfg, | ||
manager.Options{}.WithExperimentalClusterProvider(&KindClusterProvider{}), | ||
) | ||
if err != nil { | ||
entryLog.Error(err, "unable to set up overall controller manager") | ||
os.Exit(1) | ||
} | ||
|
||
builder.ControllerManagedBy(mgr). | ||
For(&corev1.Pod{}).Complete(reconcile.Func( | ||
func(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := log.FromContext(ctx) | ||
|
||
cluster, err := mgr.GetCluster(ctx, req.Cluster) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
client := cluster.GetClient() | ||
|
||
// Retrieve the pod from the cluster. | ||
pod := &corev1.Pod{} | ||
if err := client.Get(ctx, req.NamespacedName, pod); err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
log.Info("Reconciling pod", "name", pod.Name, "uuid", pod.UID) | ||
|
||
// Print any annotations that start with fleet. | ||
for k, v := range pod.Labels { | ||
if strings.HasPrefix(k, "fleet-") { | ||
log.Info("Detected fleet annotation!", "key", k, "value", v) | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
}, | ||
)) | ||
|
||
entryLog.Info("Starting manager") | ||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
entryLog.Error(err, "unable to run manager") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// KindClusterProvider is a cluster provider that works with a local Kind instance. | ||
type KindClusterProvider struct{} | ||
|
||
func (k *KindClusterProvider) Get(ctx context.Context, name logical.Name, opts ...cluster.Option) (cluster.Cluster, error) { | ||
provider := kind.NewProvider() | ||
kubeconfig, err := provider.KubeConfig(string(name), false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Parse the kubeconfig into a rest.Config. | ||
cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfig)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cluster.New(cfg, opts...) | ||
} | ||
|
||
func (k *KindClusterProvider) List() ([]logical.Name, error) { | ||
provider := kind.NewProvider() | ||
list, err := provider.List() | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := make([]logical.Name, 0, len(list)) | ||
for _, cluster := range list { | ||
if !strings.HasPrefix(cluster, "fleet-") { | ||
continue | ||
} | ||
res = append(res, logical.Name(cluster)) | ||
} | ||
return res, nil | ||
} | ||
|
||
func (k *KindClusterProvider) Watch() (cluster.Watcher, error) { | ||
return &KindWatcher{ch: make(chan cluster.WatchEvent)}, nil | ||
} | ||
|
||
type KindWatcher struct { | ||
init sync.Once | ||
wg sync.WaitGroup | ||
ch chan cluster.WatchEvent | ||
cancel context.CancelFunc | ||
} | ||
|
||
func (k *KindWatcher) Stop() { | ||
if k.cancel != nil { | ||
k.cancel() | ||
} | ||
k.wg.Wait() | ||
close(k.ch) | ||
} | ||
func (k *KindWatcher) ResultChan() <-chan cluster.WatchEvent { | ||
k.init.Do(func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
k.cancel = cancel | ||
set := sets.New[string]() | ||
k.wg.Add(1) | ||
go func() { | ||
defer k.wg.Done() | ||
for { | ||
select { | ||
case <-time.After(2 * time.Second): | ||
provider := kind.NewProvider() | ||
list, err := provider.List() | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
newSet := sets.New(list...) | ||
// Check for new clusters. | ||
for _, cl := range newSet.Difference(set).UnsortedList() { | ||
if !strings.HasPrefix(cl, "fleet-") { | ||
continue | ||
} | ||
k.ch <- cl.WatchEvent{ | ||
Type: watch.Added, | ||
Name: logical.Name(cl), | ||
} | ||
} | ||
// Check for deleted clusters. | ||
for _, cl := range set.Difference(newSet).UnsortedList() { | ||
if !strings.HasPrefix(cl, "fleet-") { | ||
continue | ||
} | ||
k.ch <- cluster.WatchEvent{ | ||
Type: watch.Deleted, | ||
Name: logical.Name(cl), | ||
} | ||
} | ||
set = newSet | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
}) | ||
return k.ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.