Documentation
¶
Overview ¶
Package features provides CRD detection and feature flags for optional Gateway API resources, enabling graceful degradation when supporting resources are unavailable.
cfgate assumes Gateway and HTTPRoute are part of the required Gateway API installation. This package detects optional CRDs that refine behavior.
Detected CRDs ¶
The package checks for ReferenceGrant (v1beta1/standard), which cfgate uses for cross-namespace secret and policy target validation.
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.HasReferenceGrantSupport() {
controllerBuilder = controllerBuilder.Watches(
&gatewayv1b1.ReferenceGrant{},
handler.EnqueueRequestsFromMapFunc(r.findPoliciesForReferenceGrant),
)
}
Nil Safety ¶
All FeatureGates checks include nil guards to support testing without feature detection:
if r.FeatureGates != nil && r.FeatureGates.HasReferenceGrantSupport() {
// 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" httpRouteAvailable=true ...
Missing optional features are logged at V(1) with install hints:
// V(1): "ReferenceGrant CRD not found, cross-namespace references disabled" // requiredVersion=v1beta1 installHint="Install Gateway API standard channel CRDs"
Index ¶
Constants ¶
const (
// GatewayAPIGroup is the API group for Gateway API resources.
GatewayAPIGroup = "gateway.networking.k8s.io"
)
Gateway API group constant.
const (
// ReferenceGrantResource is the plural resource name for ReferenceGrant.
ReferenceGrantResource = "referencegrants"
)
Gateway API resource names (plural form used in API discovery).
const (
// V1Beta1 is the standard channel version.
V1Beta1 = "v1beta1"
)
Gateway API version constants.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FeatureGates ¶
type FeatureGates struct {
// 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) HasReferenceGrantSupport ¶
func (g *FeatureGates) HasReferenceGrantSupport() bool
HasReferenceGrantSupport returns true if ReferenceGrant 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 route kinds exposed by the current product.