k8s

package
v0.6.17 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package k8s provides Kubernetes integration for Virtual MCP Server dynamic mode.

In dynamic mode (outgoingAuth.source: discovered), the vMCP server runs a controller-runtime manager with informers to watch K8s resources dynamically. This enables backends to be added/removed from the MCPGroup without restarting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackendWatcher

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

BackendWatcher wraps a controller-runtime manager for vMCP dynamic mode.

In K8s mode (outgoingAuth.source: discovered), this watcher runs informers that watch for backend changes in the referenced MCPGroup. When backends are added or removed, the watcher updates the DynamicRegistry which triggers cache invalidation via version-based lazy invalidation.

Design Philosophy:

  • Wraps controller-runtime manager for lifecycle management
  • Provides WaitForCacheSync for readiness probe gating
  • Graceful shutdown on context cancellation
  • Single responsibility: watch K8s resources and update registry

Static mode (CLI) skips this entirely - no controller-runtime, no informers.

func NewBackendWatcher

func NewBackendWatcher(
	cfg *rest.Config,
	namespace string,
	groupRef string,
	registry vmcp.DynamicRegistry,
) (*BackendWatcher, error)

NewBackendWatcher creates a new backend watcher for vMCP dynamic mode.

This initializes a controller-runtime manager configured to watch resources in the specified namespace. The watcher will monitor the referenced MCPGroup and update the DynamicRegistry when backends are added or removed.

Parameters:

  • cfg: Kubernetes REST config (typically from in-cluster config)
  • namespace: Namespace to watch for resources
  • groupRef: MCPGroup reference in "namespace/name" format
  • registry: DynamicRegistry to update when backends change

Returns:

  • *BackendWatcher: Configured watcher ready to Start()
  • error: Configuration or initialization errors

Example:

restConfig, _ := rest.InClusterConfig()
registry := vmcp.NewDynamicRegistry(initialBackends)
watcher, err := k8s.NewBackendWatcher(restConfig, "default", "default/my-group", registry)
if err != nil {
    return err
}
go watcher.Start(ctx)
if !watcher.WaitForCacheSync(ctx) {
    return fmt.Errorf("cache sync failed")
}

func (*BackendWatcher) Start

func (w *BackendWatcher) Start(ctx context.Context) error

Start starts the controller-runtime manager and blocks until context is cancelled.

This method runs informers that watch for backend changes in the MCPGroup. It's designed to run in a background goroutine and will gracefully shutdown when the context is cancelled.

Design Notes:

  • Blocks until context cancellation (controller-runtime pattern)
  • Graceful shutdown on context cancel
  • Safe to call only once (subsequent calls will error)

Example:

go func() {
    if err := watcher.Start(ctx); err != nil {
        logger.Errorf("BackendWatcher stopped with error: %v", err)
    }
}()

func (*BackendWatcher) WaitForCacheSync

func (w *BackendWatcher) WaitForCacheSync(ctx context.Context) bool

WaitForCacheSync waits for the watcher's informer caches to sync.

This is used by the /readyz endpoint to gate readiness until the watcher has populated its caches. This ensures the vMCP server doesn't serve requests until it has an accurate view of backends.

Parameters:

  • ctx: Context with optional timeout for the wait operation

Returns:

  • bool: true if caches synced successfully, false on timeout or error

Design Notes:

  • Non-blocking if watcher not started (returns false)
  • Respects context timeout (e.g., 5-second readiness probe timeout)
  • Safe to call multiple times (idempotent)

Example (readiness probe):

func (s *Server) handleReadiness(w http.ResponseWriter, r *http.Request) {
    if s.backendWatcher != nil {
        ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
        defer cancel()
        if !s.backendWatcher.WaitForCacheSync(ctx) {
            w.WriteHeader(http.StatusServiceUnavailable)
            return
        }
    }
    w.WriteHeader(http.StatusOK)
}

Jump to

Keyboard shortcuts

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