Multi-cluster with kubebuilder #2729
-
Hello, I'd like to develop an operator multi-cluster ( I mean by that, the operator will manage kubernetes resources in both clusters ), I was wondering how we can achieve something like this with kubebuilder ? How the operator can communicate with the second cluster ? by specifying a kubeconfig ? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
A kubeconfig of the second cluster, and then create a controller-runtime cluster with it, which provides client and cache for the second cluster. But it is still a little laborious if you expect to run those controllers for multiple clusters. |
Beta Was this translation helpful? Give feedback.
-
Hi @HamzaZo, I think you might able to achieve this by setting up a combined kubeconfig with contexts for each of the clusters such as follows:
First, ensure that your kubeconfig has contexts set up for both the clusters. This usually involves merging multiple kubeconfig files or manually adding cluster, user, and context entries.
var mgr, err = ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
// other necessary configurations
})
cluster1Config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{CurrentContext: "context-of-cluster-1"})
client1, err := client.New(cluster1Config, client.Options{Scheme: scheme})
cluster2Config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{CurrentContext: "context-of-cluster-2"})
client2, err := client.New(cluster2Config, client.Options{Scheme: scheme}) Now, within your reconciler, you can decide which client (client1 or client2) to use based on which cluster you want to interact with.
Your reconciler logic will have the intelligence to determine which client to use for each resource reconciliation. Typically, this can be based on certain metadata or spec attributes of the resource. **However, that is a really a good question. Did you try something like the above approach? |
Beta Was this translation helpful? Give feedback.
-
I am closing this one as answered since we no longer interact here. |
Beta Was this translation helpful? Give feedback.
Hi @HamzaZo,
I think you might able to achieve this by setting up a combined kubeconfig with contexts for each of the clusters such as follows:
First, ensure that your kubeconfig has contexts set up for both the clusters. This usually involves merging multiple kubeconfig files or manually adding cluster, user, and context entries.
Set up a single controller-runtime Manager.
When you want your reconciler to …