Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ jobs:
go-version: '1.20'
cache: false
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3.5.0
with:
go-version: "1.20"
check-latest: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
75 changes: 75 additions & 0 deletions examples/fleet/go.mod
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
)
627 changes: 627 additions & 0 deletions examples/fleet/go.sum

Large diffs are not rendered by default.

194 changes: 194 additions & 0 deletions examples/fleet/main.go
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) {

Copy link
Copy Markdown
Member

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:

	cluster          cluster.Cluster
	logicalName      logical.Name

vs.

	cluster          cluster.Cluster
	logicalClusterName      logicalcluster.Name

or if we want something shorter

	cluster          cluster.Cluster
	clusterName      logicalcluster.Name

(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)

Copy link
Copy Markdown

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 to logicalcluster too. I also like clusterName as a short-hand for variables of type logicalcluster.Name.

@nikhita how would we do a repo rename?

Copy link
Copy Markdown

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.

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{
Comment thread
sbueringer marked this conversation as resolved.
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
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrC
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/logical-cluster v0.0.1-alpha.0 h1:vigMG0I1fgDVn0hsTOeZB55AmplXC7D4iLa60qeyX70=
sigs.k8s.io/logical-cluster v0.0.1-alpha.0/go.mod h1:7YymTkuUFI+tkwCRPMsk+TiyBQiPDKRArxVAAGpezZI=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
Expand Down
Loading