clusterregistry

package
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

Multicluster Technical Documents

This documents not only describe the behavior of clusterregistry, it also describes the technical framework which can help the chaos mesh developers to develop the multicluster application.

A clusterregistry will manage all controllers watching a remote cluster. The construction of these controllers (managers) will be managed by fx. The main process of constructing a controller manage is nearly the same with the main one. The only difference is that we'll need to provide a new RestConfig, and Populate the client to allow others to use its client.

The RemoteClusterRegistry provides three methods: Spawn, Stop and WithClient. Spawn allows you to setup a new controller-manager watching resources inside the remote cluster, and Stop allows you to stop a running controller-manager. WithClient enables the developer to get a client to operate in the remote cluster.

For more details about these three functions, please read the documents of them.

Bootstrap Process

You'll need a *rest.Config to start a controller manager inside remote cluster. This config is used to setup client, watch changes... This client will also be used by any other called WithClient, so make sure it has enough priviledges.

With this *rest.Config, the Spawn method will construct a new fx App, which is nearly the same with the main one. The difference between this fx App and the main one is that it only adds reconciler which is needed by multicluster, and it doesn't start webhook, doesn't listen on the metrics.

Except ctrl.Option and *rest.Config, all other arguments used by manager are provided by the same one in the provider. Only the default client is passed inside. If you need more different client, it won't be too complicated to add them to this construction process.

It also passes a cancelable context to the run function. This context is used to stop the controller.

Stop Process

The stop / cancel is managed directly by the fx lifecycle. We added a stop hook to cancel the context used by the controller manager when we are stopping the fx app.

Register Reconciler

If you need to register a reconciler for a remote cluster, add a new fx.Invoke to the construction of cluster (in the Spawn method), register the new reconciler to the manager inside that function.

See /controllers/multicluster/remotepodreconciler/fx.go for an example.

Use the remote cluster client in manage cluster reconciler

To manage the resources in the remote cluster, you may need to get a client of remote cluster. With a ClusterRegistry, it would be really easy!

err := r.registry.WithClient(obj.Name, func(c client.Client) error {
    return c.Create(ctx, &corev1.Pod{
        ObjectMeta: metav1.ObjectMeta{
            Name:      "hello-world",
            Namespace: "default",
        },
        Spec: corev1.PodSpec{
            Containers: []corev1.Container{
                {
                    Image:           "docker/whalesay",
                    ImagePullPolicy: corev1.PullIfNotPresent,
                    Name:            "hello-world",
                    Command:         []string{"cowsay", "Hello World"},
                },
            },
        },
    })
})
if err != nil {
    if !k8sError.IsAlreadyExists(err) {
        r.Log.Error(err, "fail to create pod")
    }

}

Use the manage cluster client in remote cluster reconciler

ClusterRegistry provides a client.Client annotated with name:"manage-client" to allow remote cluster reconciler operates on the manage cluster. See /controllers/multicluster/remotepodreconciler/fx.go for an example.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyExist = errors.New("controllers of cluster has already been started")
	ErrNotExist     = errors.New("controllers of cluster doesn't exist")
)

Functions

This section is empty.

Types

type RemoteClusterRegistry

type RemoteClusterRegistry struct {
	// contains filtered or unexported fields
}

RemoteClusterRegistry will manage all controllers running on a remote cluster. The construction of these controllers (managers) will be managed by `fx`. The main process of constructing a controller manage is nearly the same with the main one. The only difference is that we'll need to provide a new `RestConfig`, and `Populate` the client to allow others to use its client.

func New

func New(logger logr.Logger, client client.Client) *RemoteClusterRegistry

func (*RemoteClusterRegistry) Spawn

func (r *RemoteClusterRegistry) Spawn(name string, config *rest.Config) error

Spawn starts the controller-manager and watches the remote cluster

func (*RemoteClusterRegistry) Stop

func (r *RemoteClusterRegistry) Stop(ctx context.Context, name string) error

Stop stops the running controller-manager which watches the remote cluster.

func (*RemoteClusterRegistry) WithClient

func (r *RemoteClusterRegistry) WithClient(name string, f func(c client.Client) error) error

WithClient enables developer getting a client of remote cluster to operate inside the remote cluster.

TODO: add more kinds of client, like `no-cache` into this registry, if they are needed

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL