-
Notifications
You must be signed in to change notification settings - Fork 1.2k
WIP: ✨ Cluster Provider and cluster-aware controllers #2207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -19,6 +19,11 @@ jobs: | |
go-version: '1.20' | ||
cache: false | ||
- uses: actions/checkout@v3 | ||
- name: Set up Go | ||
uses: actions/[email protected] | ||
with: | ||
go-version: "1.20" | ||
check-latest: true | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
|
This file contains hidden or 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 hidden or 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{ | ||
sbueringer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Might it be better if we end up with something like logicalcluster.Name or logical.ClusterName
Also in field names of structs like Builder:
vs.
or if we want something shorter
(Might be good to consistently stick with the same naming, e.g. Cluster is a Cluster, ClusterName is a logicalcluster.Name, mixing it up might lead to a bit of confusion, e.g. with req.Cluster)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should rename the repo to
sigs.k8s.io/logicalcluster
and change the package name tologicalcluster
too. I also likeclusterName
as a short-hand for variables of typelogicalcluster.Name
.@nikhita how would we do a repo rename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative, as suggested in other comments: get rid of the alias altogether and just use string. Am undecided. Have done that now locally in this branch. We lose type-safety, and replace it with naming convention of calling every formally logical cluster name
clusterName
.