features

package
v0.1.0-alpha.12 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package features provides CRD detection and feature flags for optional Gateway API resources, enabling graceful degradation when experimental CRDs are unavailable.

cfgate supports multiple Gateway API route types, but not all CRDs are required or present in every cluster. This package detects which optional CRDs are installed and exposes feature flags for conditional behavior.

Detected CRDs

The package checks for these optional Gateway API CRDs:

  • TCPRoute (v1alpha2/experimental): TCP proxy support via Cloudflare Spectrum
  • UDPRoute (v1alpha2/experimental): UDP proxy support via Cloudflare Spectrum
  • GRPCRoute (v1/GA): gRPC routing (GA but may not be installed in minimal deployments)
  • ReferenceGrant (v1beta1/standard): Cross-namespace secret/service references

HTTPRoute (v1/GA) is assumed always present since Gateway API is a prerequisite.

Usage

Detect features at manager startup using the discovery client:

dc, err := discovery.NewDiscoveryClientForConfig(mgr.GetConfig())
if err != nil {
    return err
}
gates, err := features.DetectFeatures(dc)
if err != nil {
    return err
}
gates.LogFeatures(setupLog)

Pass FeatureGates to reconcilers that need conditional behavior:

reconciler := &CloudflareAccessPolicyReconciler{
    Client:       mgr.GetClient(),
    FeatureGates: gates,
}

Conditional Watches

Use feature gates to conditionally register watches in SetupWithManager:

if r.FeatureGates != nil && r.FeatureGates.HasGRPCRouteSupport() {
    controllerBuilder = controllerBuilder.Watches(
        &gateway.GRPCRoute{},
        handler.EnqueueRequestsFromMapFunc(r.findPoliciesForGRPCRoute),
    )
}

Defensive Checks

Controllers for optional CRDs should verify the CRD exists before registration:

func (r *TCPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
    if r.FeatureGates != nil && !r.FeatureGates.HasTCPRouteSupport() {
        log.V(1).Info("TCPRoute CRD not found, skipping controller registration")
        return nil
    }
    return ctrl.NewControllerManagedBy(mgr).
        For(&gwapiv1alpha2.TCPRoute{}).
        Complete(r)
}

Nil Safety

All FeatureGates checks include nil guards to support testing without feature detection:

if r.FeatureGates != nil && r.FeatureGates.HasGRPCRouteSupport() {
    // Feature available
}

This allows tests to skip feature gate injection and provides backward compatibility during gradual integration.

Detection Behavior

Detection failures are non-fatal; features default to disabled. This ensures the controller can start even if detection fails for some CRDs. If the API server is unreachable, DetectFeatures returns an error and the manager should fail fast rather than start with incomplete feature state.

CRD availability is detected once at startup and cached for the controller lifetime, since CRDs don't typically change at runtime.

Logging

LogFeatures logs detection results at Info level:

gates.LogFeatures(setupLog)
// Output: "Gateway API feature detection complete" tcpRouteAvailable=true ...

Missing experimental features are logged at V(1) with install hints:

// V(1): "TCPRoute CRD not found, TCP routing disabled"
//       requiredVersion=v1alpha2 installHint="Install Gateway API experimental channel CRDs"

Index

Constants

View Source
const (
	// V1Alpha2 is the experimental channel version.
	V1Alpha2 = "v1alpha2"
	// V1Beta1 is the standard channel version.
	V1Beta1 = "v1beta1"
	// V1 is the GA version.
	V1 = "v1"
)

Gateway API version constants.

View Source
const (
	// TCPRouteResource is the plural resource name for TCPRoute.
	TCPRouteResource = "tcproutes"
	// UDPRouteResource is the plural resource name for UDPRoute.
	UDPRouteResource = "udproutes"
	// GRPCRouteResource is the plural resource name for GRPCRoute.
	GRPCRouteResource = "grpcroutes"
	// ReferenceGrantResource is the plural resource name for ReferenceGrant.
	ReferenceGrantResource = "referencegrants"
)

Gateway API resource names (plural form used in API discovery).

View Source
const (
	// GatewayAPIGroup is the API group for Gateway API resources.
	GatewayAPIGroup = "gateway.networking.k8s.io"
)

Gateway API group constant.

Variables

This section is empty.

Functions

This section is empty.

Types

type FeatureGates

type FeatureGates struct {
	// TCPRouteCRDExists indicates TCPRoute (v1alpha2) is installed.
	// Required for TCP proxy support via Cloudflare Spectrum.
	TCPRouteCRDExists bool

	// UDPRouteCRDExists indicates UDPRoute (v1alpha2) is installed.
	// Required for UDP proxy support via Cloudflare Spectrum.
	UDPRouteCRDExists bool

	// GRPCRouteCRDExists indicates GRPCRoute (v1) is installed.
	// While GA, may not be installed in minimal Gateway API deployments.
	GRPCRouteCRDExists bool

	// ReferenceGrantCRDExists indicates ReferenceGrant (v1beta1) is installed.
	// Required for cross-namespace secret/service references.
	ReferenceGrantCRDExists bool
}

FeatureGates tracks which optional Gateway API CRDs are available. CRD availability is detected once at startup and cached for the controller lifetime (CRDs don't change at runtime in practice).

func DetectFeatures

func DetectFeatures(dc discovery.DiscoveryInterface) (*FeatureGates, error)

DetectFeatures checks for the existence of optional Gateway API CRDs using the discovery client. Results are cached in FeatureGates. Each CRD check is independent; detection failures disable that feature.

func (*FeatureGates) HasGRPCRouteSupport

func (g *FeatureGates) HasGRPCRouteSupport() bool

HasGRPCRouteSupport returns true if GRPCRoute CRD is available.

func (*FeatureGates) HasReferenceGrantSupport

func (g *FeatureGates) HasReferenceGrantSupport() bool

HasReferenceGrantSupport returns true if ReferenceGrant CRD is available.

func (*FeatureGates) HasTCPRouteSupport

func (g *FeatureGates) HasTCPRouteSupport() bool

HasTCPRouteSupport returns true if TCPRoute CRD is available.

func (*FeatureGates) HasUDPRouteSupport

func (g *FeatureGates) HasUDPRouteSupport() bool

HasUDPRouteSupport returns true if UDPRoute CRD is available.

func (*FeatureGates) LogFeatures

func (g *FeatureGates) LogFeatures(log logr.Logger)

LogFeatures logs the detected feature availability at startup. Called once during manager initialization.

func (*FeatureGates) SupportedRouteKinds

func (g *FeatureGates) SupportedRouteKinds() []string

SupportedRouteKinds returns the list of supported route kinds. Always includes HTTPRoute; conditionally includes TCP/UDP/GRPC.

Jump to

Keyboard shortcuts

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