diff --git a/cli/pkg/kubectl/base/kubeconfig.go b/cli/pkg/kubectl/base/kubeconfig.go index 948e518ad..fe54a39c3 100644 --- a/cli/pkg/kubectl/base/kubeconfig.go +++ b/cli/pkg/kubectl/base/kubeconfig.go @@ -29,6 +29,8 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/retry" "k8s.io/klog/v2" + + kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2" ) func ParseRemoteKubeconfig(kubeconfig []byte) (host string, ns string, err error) { @@ -96,6 +98,9 @@ func EnsureKubeconfigSecret(ctx context.Context, kubeconfig, name string, client ObjectMeta: v1.ObjectMeta{ Namespace: "kube-bind", GenerateName: "kubeconfig-", + Labels: map[string]string{ + kubebindv1alpha2.LabelProviderKubeconfig: "true", + }, }, Data: map[string][]byte{ "kubeconfig": []byte(kubeconfig), @@ -129,6 +134,11 @@ func EnsureKubeconfigSecret(ctx context.Context, kubeconfig, name string, client return errors.NewAlreadyExists(corev1.Resource("secret"), secret.Name) } secret.Data["kubeconfig"] = []byte(kubeconfig) + // Ensure label is set on existing secrets + if secret.Labels == nil { + secret.Labels = make(map[string]string) + } + secret.Labels[kubebindv1alpha2.LabelProviderKubeconfig] = "true" if _, err := client.CoreV1().Secrets("kube-bind").Update(ctx, secret, v1.UpdateOptions{}); err != nil { return err } diff --git a/pkg/konnector/konnector_controller.go b/pkg/konnector/konnector_controller.go index 2405f27dd..c4ac1e402 100644 --- a/pkg/konnector/konnector_controller.go +++ b/pkg/konnector/konnector_controller.go @@ -19,6 +19,7 @@ package konnector import ( "context" "fmt" + "strings" "time" corev1 "k8s.io/api/core/v1" @@ -231,6 +232,14 @@ func (c *Controller) enqueueSecret(logger klog.Logger, obj any) { return } + // Check if this is a provider kubeconfig secret (for early heartbeat) + secret, ok := obj.(*corev1.Secret) + if ok && secret.Labels[kubebindv1alpha2.LabelProviderKubeconfig] == "true" { + // Queue it as a heartbeat secret with a special prefix + logger.V(2).Info("queueing heartbeat secret", "key", key) + c.queue.Add("__heartbeat_secret__" + key) + } + bindings, err := c.serviceBindingIndexer.ByIndex(indexers.ByServiceBindingKubeconfigSecret, fmt.Sprintf("%s/%s", ns, name)) if err != nil { runtime.HandleError(err) @@ -302,7 +311,33 @@ func (c *Controller) processNextWorkItem(ctx context.Context) bool { return true } +const heartbeatSecretPrefix = "__heartbeat_secret__" + func (c *Controller) process(ctx context.Context, key string) error { + logger := klog.FromContext(ctx) + + // Handle heartbeat secret keys (for early heartbeat without APIServiceBinding) + if strings.HasPrefix(key, heartbeatSecretPrefix) { + secretKey := strings.TrimPrefix(key, heartbeatSecretPrefix) + ns, name, err := cache.SplitMetaNamespaceKey(secretKey) + if err != nil { + runtime.HandleError(err) + return nil + } + + secret, err := c.secretLister.Secrets(ns).Get(name) + if err != nil { + if errors.IsNotFound(err) { + logger.V(2).Info("heartbeat secret not found, skipping", "secret", secretKey) + return nil + } + return err + } + + // Start controller for this secret + return c.reconciler.startClusterControllerForSecret(ctx, secret) + } + _, name, err := cache.SplitMetaNamespaceKey(key) if err != nil { runtime.HandleError(err) diff --git a/pkg/konnector/konnector_reconcile.go b/pkg/konnector/konnector_reconcile.go index 4f68cbfaa..a2781f448 100644 --- a/pkg/konnector/konnector_reconcile.go +++ b/pkg/konnector/konnector_reconcile.go @@ -34,18 +34,134 @@ type startable interface { Start(ctx context.Context) } +// reconciler manages cluster controllers for provider connections. +// +// Controller Sharing Model: +// ------------------------- +// Multiple APIServiceBindings can share the same cluster controller if they use the same +// kubeconfig (i.e., connect to the same provider namespace). This is tracked via controllerContext: +// +// r.controllers map: +// "__heartbeat__kubeconfig-xyz" -> controllerContext{kubeconfig: "...", serviceBindings: ["__heartbeat__kubeconfig-xyz", "binding-a", "binding-b"]} +// "binding-a" -> (same controllerContext as above) +// "binding-b" -> (same controllerContext as above) +// +// Flow when APIServiceBinding arrives using same kubeconfig as existing heartbeat controller: +// 1. reconcile() is called with the new binding +// 2. It extracts kubeconfig from binding.Spec.KubeconfigSecretRef +// 3. It loops through r.controllers looking for matching kubeconfig (lines 174-182) +// 4. Finds the existing controllerContext (created by startClusterControllerForSecret) +// 5. Points r.controllers[binding.Name] to the SAME controllerContext +// 6. Adds binding.Name to controllerContext.serviceBindings set +// 7. No new controller is started - the existing one handles everything type reconciler struct { lock sync.RWMutex - controllers map[string]*controllerContext // by service binding name + controllers map[string]*controllerContext // keyed by binding name or synthetic heartbeat key newClusterController func(consumerSecretRefKey, providerNamespace string, reconcileServiceBinding func(binding *kubebindv1alpha2.APIServiceBinding) bool, providerConfig *rest.Config) (startable, error) getSecret func(ns, name string) (*corev1.Secret, error) } +// controllerContext tracks a running cluster controller and the bindings it serves. +// Multiple map entries in reconciler.controllers can point to the same controllerContext +// when they share the same kubeconfig. type controllerContext struct { - kubeconfig string - cancel func() - serviceBindings sets.Set[string] // when this is empty, the Controller should be stopped by closing the context + kubeconfig string // the kubeconfig content - used to match bindings to controllers + cancel func() // cancels the controller's context when no bindings remain + serviceBindings sets.Set[string] // all binding names (including synthetic heartbeat keys) using this controller +} + +// startClusterControllerForSecret starts a cluster controller for the given kubeconfig secret. +// This enables heartbeat reporting immediately without requiring an APIServiceBinding. +// +// The controller is registered with a synthetic key "__heartbeat__". +// When an APIServiceBinding later arrives using the same kubeconfig, reconcile() will: +// 1. Find this existing controller by matching kubeconfig content +// 2. Add the binding to the same controllerContext.serviceBindings set +// 3. Point r.controllers[binding.Name] to the same controllerContext +// 4. NOT start a new controller (reuses existing one) +func (r *reconciler) startClusterControllerForSecret(ctx context.Context, secret *corev1.Secret) error { + logger := klog.FromContext(ctx) + + kubeconfig := string(secret.Data["kubeconfig"]) + if kubeconfig == "" { + logger.V(2).Info("secret does not contain kubeconfig", "secret", secret.Namespace+"/"+secret.Name) + return nil + } + + r.lock.Lock() + defer r.lock.Unlock() + + // Check if we already have a controller for this kubeconfig + for _, ctrlContext := range r.controllers { + if ctrlContext.kubeconfig == kubeconfig { + logger.V(2).Info("cluster controller already exists for secret", "secret", secret.Namespace+"/"+secret.Name) + return nil + } + } + + // Extract which namespace this kubeconfig points to + cfg, err := clientcmd.Load([]byte(kubeconfig)) + if err != nil { + logger.Error(err, "invalid kubeconfig in secret", "namespace", secret.Namespace, "name", secret.Name) + return nil // nothing we can do here + } + kubeContext, found := cfg.Contexts[cfg.CurrentContext] + if !found { + logger.Error(err, "kubeconfig in secret does not have a current context", "namespace", secret.Namespace, "name", secret.Name) + return nil + } + if kubeContext.Namespace == "" { + logger.Error(err, "kubeconfig in secret does not have a namespace set for the current context", "namespace", secret.Namespace, "name", secret.Name) + return nil + } + providerNamespace := kubeContext.Namespace + providerConfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfig)) + if err != nil { + logger.Error(err, "invalid kubeconfig in secret", "namespace", secret.Namespace, "name", secret.Name) + return nil + } + + // Use the secret name as a synthetic binding key for tracking. + // This key is used to track the controller context, but the controller + // will also handle real APIServiceBindings that use the same kubeconfig. + syntheticKey := "__heartbeat__" + secret.Name + + ctrlCtx, cancel := context.WithCancel(ctx) + ctrlContext := &controllerContext{ + kubeconfig: kubeconfig, + cancel: cancel, + serviceBindings: sets.New(syntheticKey), + } + r.controllers[syntheticKey] = ctrlContext + + // Create and start the cluster controller. + // The reconcileServiceBinding function checks if a binding should be processed + // by this controller. We check if the binding is in our serviceBindings set, + // which will include both the synthetic heartbeat key and any real bindings + // that get added later when they use the same kubeconfig. + logger.Info("starting cluster controller for early heartbeat", "secret", secret.Namespace+"/"+secret.Name, "providerNamespace", providerNamespace) + ctrl, err := r.newClusterController( + secret.Namespace+"/"+secret.Name, + providerNamespace, + func(svcBinding *kubebindv1alpha2.APIServiceBinding) bool { + r.lock.RLock() + defer r.lock.RUnlock() + // Check if this binding is registered with this controller context + return ctrlContext.serviceBindings.Has(svcBinding.Name) + }, + providerConfig, + ) + if err != nil { + logger.Error(err, "failed to start cluster controller for heartbeat") + cancel() + delete(r.controllers, syntheticKey) + return err + } + + go ctrl.Start(ctrlCtx) + + return nil } func (r *reconciler) reconcile(ctx context.Context, binding *kubebindv1alpha2.APIServiceBinding) error { @@ -83,11 +199,18 @@ func (r *reconciler) reconcile(ctx context.Context, binding *kubebindv1alpha2.AP return nil } - // find existing with new kubeconfig + // Find existing controller with the same kubeconfig. + // This handles the case where: + // 1. A heartbeat controller was started from a labeled secret (via startClusterControllerForSecret) + // 2. An APIServiceBinding now arrives that uses the same kubeconfig + // 3. Instead of starting a duplicate controller, we reuse the existing one + // + // The binding is added to the existing controllerContext's serviceBindings set, + // and r.controllers[binding.Name] points to the same controllerContext. for _, ctrlContext := range r.controllers { if ctrlContext.kubeconfig == kubeconfig { - // add to it - logger.V(2).Info("adding to existing Controller", "secret", ref.Namespace+"/"+ref.Name) + // Reuse existing controller - no new controller started + logger.V(2).Info("adding binding to existing Controller", "secret", ref.Namespace+"/"+ref.Name) r.controllers[binding.Name] = ctrlContext ctrlContext.serviceBindings.Insert(binding.Name) return nil diff --git a/sdk/apis/kubebind/v1alpha2/types.go b/sdk/apis/kubebind/v1alpha2/types.go index 8ef71cbb4..636765abf 100644 --- a/sdk/apis/kubebind/v1alpha2/types.go +++ b/sdk/apis/kubebind/v1alpha2/types.go @@ -21,4 +21,9 @@ const ( OIDCProviderTypeEmbedded = "embedded" // OIDCProviderTypeExternal represents an external OIDC provider managed outside of kube-bind. OIDCProviderTypeExternal = "external" + + // LabelProviderKubeconfig identifies secrets containing provider kubeconfig. + // Secrets with this label are used by konnector to start heartbeat reporting + // immediately at startup, even without APIServiceBinding resources. + LabelProviderKubeconfig = "kube-bind.io/provider-kubeconfig" )