Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions examples/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
gwv1 "sigs.k8s.io/gateway-api/apis/v1"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kgateway-dev/kgateway/v2/internal/kgateway/ir"
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/plugins"
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/setup"
"github.com/kgateway-dev/kgateway/v2/pkg/deployer"
sdk "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk"
collections "github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/collections"
"github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/reporter"
Expand Down Expand Up @@ -238,10 +235,6 @@ func pluginFactory(ctx context.Context, commoncol *collections.CommonCollections
}
}

func extraGatewayParametersFactory(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters {
return make([]deployer.ExtraGatewayParameters, 0)
}

func main() {
// TODO: move setup.StartGGv2 from internal to public.
// Start Kgateway and provide our plugin.
Expand All @@ -251,7 +244,6 @@ func main() {

setup, _ := setup.New(
setup.WithExtraPlugins(pluginFactory),
setup.ExtraGatewayParameters(extraGatewayParametersFactory),
)
setup.Start(context.Background())
}
22 changes: 11 additions & 11 deletions internal/kgateway/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ type GatewayConfig struct {
AdditionalGatewayClasses map[string]*deployer.GatewayClassInfo
}

type ExtraGatewayParametersFunc func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters
type HelmValuesGeneratorOverrideFunc func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator

func NewBaseGatewayController(ctx context.Context, cfg GatewayConfig, extraGatewayParameters ExtraGatewayParametersFunc) error {
func NewBaseGatewayController(ctx context.Context, cfg GatewayConfig, helmValuesGeneratorOverride HelmValuesGeneratorOverrideFunc) error {
log := log.FromContext(ctx)
log.V(5).Info("starting gateway controller", "controllerName", cfg.ControllerName)

Expand All @@ -90,7 +90,7 @@ func NewBaseGatewayController(ctx context.Context, cfg GatewayConfig, extraGatew
customEvents: make(chan event.TypedGenericEvent[ir.Gateway], 1024),
metricsName: "gatewayclass",
},
extraGatewayParameters: extraGatewayParameters,
helmValuesGeneratorOverride: helmValuesGeneratorOverride,
}

return run(
Expand All @@ -110,7 +110,7 @@ type InferencePoolConfig struct {
func NewBaseInferencePoolController(ctx context.Context,
poolCfg *InferencePoolConfig,
gwCfg *GatewayConfig,
extraGatewayParameters func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters) error {
helmValuesGeneratorOverride func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator) error {
log := log.FromContext(ctx)
log.V(5).Info("starting inferencepool controller", "controllerName", poolCfg.ControllerName)

Expand All @@ -124,7 +124,7 @@ func NewBaseInferencePoolController(ctx context.Context,
customEvents: make(chan event.TypedGenericEvent[ir.Gateway], 1024),
metricsName: "gatewayclass-inferencepool",
},
extraGatewayParameters: extraGatewayParameters,
helmValuesGeneratorOverride: helmValuesGeneratorOverride,
}

return run(ctx, controllerBuilder.watchInferencePool)
Expand All @@ -140,10 +140,10 @@ func run(ctx context.Context, funcs ...func(ctx context.Context) error) error {
}

type controllerBuilder struct {
cfg GatewayConfig
poolCfg *InferencePoolConfig
reconciler *controllerReconciler
extraGatewayParameters func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters
cfg GatewayConfig
poolCfg *InferencePoolConfig
reconciler *controllerReconciler
helmValuesGeneratorOverride func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator
}

func (c *controllerBuilder) addIndexes(ctx context.Context) error {
Expand Down Expand Up @@ -197,8 +197,8 @@ func (c *controllerBuilder) watchGw(ctx context.Context) error {
}

gwParams := internaldeployer.NewGatewayParameters(c.cfg.Mgr.GetClient(), inputs)
if c.extraGatewayParameters != nil {
gwParams.WithExtraGatewayParameters(c.extraGatewayParameters(c.cfg.Mgr.GetClient(), inputs)...)
if c.helmValuesGeneratorOverride != nil {
gwParams.WithHelmValuesGeneratorOverride(c.helmValuesGeneratorOverride(c.cfg.Mgr.GetClient(), inputs))
}

discoveryNamespaceFilterPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool {
Expand Down
14 changes: 7 additions & 7 deletions internal/kgateway/controller/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ type StartConfig struct {
RestConfig *rest.Config
// ExtensionsFactory is the factory function which will return an extensions.K8sGatewayExtensions
// This is responsible for producing the extension points that this controller requires
ExtraPlugins func(ctx context.Context, commoncol *collections.CommonCollections, mergeSettingsJSON string) []sdk.Plugin
ExtraAgwPlugins func(ctx context.Context, agw *agwplugins.AgwCollections) []agwplugins.AgwPlugin
ExtraGatewayParameters func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters
Client istiokube.Client
Validator validator.Validator
ExtraPlugins func(ctx context.Context, commoncol *collections.CommonCollections, mergeSettingsJSON string) []sdk.Plugin
ExtraAgwPlugins func(ctx context.Context, agw *agwplugins.AgwCollections) []agwplugins.AgwPlugin
HelmValuesGeneratorOverride func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator
Client istiokube.Client
Validator validator.Validator

AgwCollections *agwplugins.AgwCollections
CommonCollections *collections.CommonCollections
Expand Down Expand Up @@ -327,7 +327,7 @@ func (c *ControllerBuilder) Build(ctx context.Context) error {
}

setupLog.Info("creating base gateway controller")
if err := NewBaseGatewayController(ctx, gwCfg, c.cfg.ExtraGatewayParameters); err != nil {
if err := NewBaseGatewayController(ctx, gwCfg, c.cfg.HelmValuesGeneratorOverride); err != nil {
setupLog.Error(err, "unable to create gateway controller")
return err
}
Expand All @@ -349,7 +349,7 @@ func (c *ControllerBuilder) Build(ctx context.Context) error {
if !globalSettings.EnableAgentgateway {
setupLog.Info("using inference extension without agentgateway is deprecated in v2.1 and will not be supported in v2.2.")
}
if err := NewBaseInferencePoolController(ctx, poolCfg, &gwCfg, c.cfg.ExtraGatewayParameters); err != nil {
if err := NewBaseInferencePoolController(ctx, poolCfg, &gwCfg, c.cfg.HelmValuesGeneratorOverride); err != nil {
setupLog.Error(err, "unable to create inferencepool controller")
return err
}
Expand Down
92 changes: 9 additions & 83 deletions internal/kgateway/deployer/gateway_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,23 @@ func NewGatewayParameters(cli client.Client, inputs *deployer.Inputs) *GatewayPa
cli: cli,
inputs: inputs,
knownGWParameters: []client.Object{&v1alpha1.GatewayParameters{}}, // always include default GatewayParameters
extraHVGenerators: make(map[schema.GroupKind]deployer.HelmValuesGenerator),
}
Copy link

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed extraHVGenerators map initialization suggests this field was removed from the struct, but the knownGWParameters field logic may need to be updated to handle the simplified override pattern consistently.

Copilot uses AI. Check for mistakes.
}

type GatewayParameters struct {
cli client.Client
inputs *deployer.Inputs
extraHVGenerators map[schema.GroupKind]deployer.HelmValuesGenerator
knownGWParameters []client.Object
cli client.Client
inputs *deployer.Inputs
helmValuesGeneratorOverride deployer.HelmValuesGenerator
knownGWParameters []client.Object
}

type kGatewayParameters struct {
cli client.Client
inputs *deployer.Inputs
}

func (gp *GatewayParameters) WithExtraGatewayParameters(params ...deployer.ExtraGatewayParameters) *GatewayParameters {
for _, p := range params {
key := schema.GroupKind{Group: p.Group, Kind: p.Kind}
if _, ok := gp.extraHVGenerators[key]; ok {
panic(fmt.Sprintf("key already exists in the map: %v", key))
}
gp.knownGWParameters = append(gp.knownGWParameters, p.Object)
gp.extraHVGenerators[key] = p.Generator
}
func (gp *GatewayParameters) WithHelmValuesGeneratorOverride(generator deployer.HelmValuesGenerator) *GatewayParameters {
gp.helmValuesGeneratorOverride = generator
return gp
}

Expand Down Expand Up @@ -102,38 +94,12 @@ func (gp *GatewayParameters) getHelmValuesGenerator(ctx context.Context, obj cli
return nil, fmt.Errorf("expected a Gateway resource, got %s", obj.GetObjectKind().GroupVersionKind().String())
}

ref, err := gp.getGatewayParametersGK(ctx, gw)
if err != nil {
return nil, err
}

if g, ok := gp.extraHVGenerators[ref]; ok {
slog.Debug("using custom HelmValuesGenerator for Gateway",
if gp.helmValuesGeneratorOverride != nil {
slog.Debug("using override HelmValuesGenerator for Gateway",
"gateway_name", gw.GetName(),
"gateway_namespace", gw.GetNamespace(),
)
return g, nil
}

// Before falling back to built-in defaults, check if ExtraGatewayParameters
// can handle this gateway class specifically
gwc, err := getGatewayClassFromGateway(ctx, gp.cli, gw)
if err == nil {
gatewayClassName := string(gwc.GetName())

// Try to find ExtraGatewayParameters for this specific gateway class
// This allows overriding built-in defaults for specific gateway classes
fallbackRef := schema.GroupKind{
Group: "gateway.class.kgateway.dev",
Kind: gatewayClassName,
}
if g, ok := gp.extraHVGenerators[fallbackRef]; ok {
slog.Debug("using ExtraGatewayParameters fallback for gateway class",
"gateway_name", gw.GetName(),
"gateway_class_name", gatewayClassName,
)
return g, nil
}
return gp.helmValuesGeneratorOverride, nil
}

slog.Debug("using default HelmValuesGenerator for Gateway",
Expand All @@ -143,46 +109,6 @@ func (gp *GatewayParameters) getHelmValuesGenerator(ctx context.Context, obj cli
return newKGatewayParameters(gp.cli, gp.inputs), nil
}

func (gp *GatewayParameters) getGatewayParametersGK(ctx context.Context, gw *api.Gateway) (schema.GroupKind, error) {
// attempt to get the GatewayParameters name from the Gateway. If we can't find it,
// we'll check for the default GWP for the GatewayClass.
if gw.Spec.Infrastructure == nil || gw.Spec.Infrastructure.ParametersRef == nil {
slog.Debug("no GatewayParameters found for Gateway, using default",
"gateway_name", gw.GetName(),
"gateway_namespace", gw.GetNamespace(),
)
return gp.getDefaultGatewayParametersGK(ctx, gw)
}

return schema.GroupKind{
Group: string(gw.Spec.Infrastructure.ParametersRef.Group),
Kind: string(gw.Spec.Infrastructure.ParametersRef.Kind),
},
nil
}

func (gp *GatewayParameters) getDefaultGatewayParametersGK(ctx context.Context, gw *api.Gateway) (schema.GroupKind, error) {
gwc, err := getGatewayClassFromGateway(ctx, gp.cli, gw)
if err != nil {
return schema.GroupKind{}, err
}

if gwc.Spec.ParametersRef != nil {
return schema.GroupKind{
Group: string(gwc.Spec.ParametersRef.Group),
Kind: string(gwc.Spec.ParametersRef.Kind),
},
nil
}

// For gateways without explicit parametersRef, use a default GroupKind
// that ExtraGatewayParameters can register for based on gateway class name
return schema.GroupKind{
Group: "default.gateway.kgateway.dev",
Kind: string(gwc.GetName()), // Use gateway class name as Kind
}, nil
}

func newKGatewayParameters(cli client.Client, inputs *deployer.Inputs) *kGatewayParameters {
return &kGatewayParameters{cli: cli, inputs: inputs}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/kgateway/deployer/gateway_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestIsSelfManagedWithExtendedGatewayParameters(t *testing.T) {
}

gwp := NewGatewayParameters(newFakeClientWithObjs(gwc, gwParams, extraGwParams), defaultInputs(t, gwc, gw)).
WithExtraGatewayParameters(deployer.ExtraGatewayParameters{Group: "v1", Kind: "ConfigMap", Object: extraGwParams, Generator: &testHelmValuesGenerator{}})
WithHelmValuesGeneratorOverride(&testHelmValuesGenerator{})
selfManaged, err := gwp.IsSelfManaged(context.Background(), gw)
assert.NoError(t, err)
assert.True(t, selfManaged)
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestShouldUseExtendedGatewayParameters(t *testing.T) {
}

gwp := NewGatewayParameters(newFakeClientWithObjs(gwc, gwParams, extraGwParams), defaultInputs(t, gwc, gw)).
WithExtraGatewayParameters(deployer.ExtraGatewayParameters{Group: "v1", Kind: "ConfigMap", Object: extraGwParams, Generator: &testHelmValuesGenerator{}})
WithHelmValuesGeneratorOverride(&testHelmValuesGenerator{})
vals, err := gwp.GetValues(context.Background(), gw)

assert.NoError(t, err)
Expand Down
38 changes: 19 additions & 19 deletions internal/kgateway/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func WithLeaderElectionID(id string) func(*setup) {
}
}

func ExtraGatewayParameters(extraGatewayParameters func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters) func(*setup) {
func WithHelmValuesGeneratorOverride(helmValuesGeneratorOverride func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator) func(*setup) {
return func(s *setup) {
s.extraGatewayParameters = extraGatewayParameters
s.helmValuesGeneratorOverride = helmValuesGeneratorOverride
}
}

Expand Down Expand Up @@ -167,20 +167,20 @@ func WithExtraAgwPolicyStatusHandlers(handlers map[string]agwplugins.AgwPolicySt
}

type setup struct {
gatewayControllerName string
agwControllerName string
gatewayClassName string
waypointClassName string
agentgatewayClassName string
additionalGatewayClasses map[string]*deployer.GatewayClassInfo
extraPlugins func(ctx context.Context, commoncol *collections.CommonCollections, mergeSettingsJSON string) []sdk.Plugin
extraAgwPlugins func(ctx context.Context, agw *agwplugins.AgwCollections) []agwplugins.AgwPlugin
extraGatewayParameters func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters
extraXDSCallbacks xdsserver.Callbacks
xdsListener net.Listener
agwXdsListener net.Listener
restConfig *rest.Config
ctrlMgrOptionsInitFunc func(context.Context) *ctrl.Options
gatewayControllerName string
agwControllerName string
gatewayClassName string
waypointClassName string
agentgatewayClassName string
additionalGatewayClasses map[string]*deployer.GatewayClassInfo
extraPlugins func(ctx context.Context, commoncol *collections.CommonCollections, mergeSettingsJSON string) []sdk.Plugin
extraAgwPlugins func(ctx context.Context, agw *agwplugins.AgwCollections) []agwplugins.AgwPlugin
helmValuesGeneratorOverride func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator
extraXDSCallbacks xdsserver.Callbacks
xdsListener net.Listener
agwXdsListener net.Listener
restConfig *rest.Config
ctrlMgrOptionsInitFunc func(context.Context) *ctrl.Options
// extra controller manager config, like adding registering additional controllers
extraManagerConfig []func(ctx context.Context, mgr manager.Manager, objectFilter kubetypes.DynamicObjectFilter) error
krtDebugger *krt.DebugHandler
Expand Down Expand Up @@ -351,7 +351,7 @@ func (s *setup) Start(ctx context.Context) error {
ctx, mgr, s.gatewayControllerName, s.agwControllerName, s.gatewayClassName, s.waypointClassName,
s.agentgatewayClassName, s.additionalGatewayClasses, setupOpts, s.restConfig,
istioClient, commoncol, agwCollections, uccBuilder, s.extraPlugins, s.extraAgwPlugins,
s.extraGatewayParameters,
s.helmValuesGeneratorOverride,
s.validator,
s.extraAgwPolicyStatusHandlers,
)
Expand Down Expand Up @@ -385,7 +385,7 @@ func BuildKgatewayWithConfig(
uccBuilder krtcollections.UniquelyConnectedClientsBulider,
extraPlugins func(ctx context.Context, commoncol *collections.CommonCollections, mergeSettingsJSON string) []sdk.Plugin,
extraAgwPlugins func(ctx context.Context, agw *agwplugins.AgwCollections) []agwplugins.AgwPlugin,
extraGatewayParameters func(cli client.Client, inputs *deployer.Inputs) []deployer.ExtraGatewayParameters,
helmValuesGeneratorOverride func(cli client.Client, inputs *deployer.Inputs) deployer.HelmValuesGenerator,
validator validator.Validator,
extraAgwPolicyStatusHandlers map[string]agwplugins.AgwPolicyStatusSyncHandler,
) error {
Expand All @@ -411,7 +411,7 @@ func BuildKgatewayWithConfig(
AdditionalGatewayClasses: additionalGatewayClasses,
ExtraPlugins: extraPlugins,
ExtraAgwPlugins: extraAgwPlugins,
ExtraGatewayParameters: extraGatewayParameters,
HelmValuesGeneratorOverride: helmValuesGeneratorOverride,
RestConfig: restConfig,
SetupOpts: setupOpts,
Client: kubeClient,
Expand Down
8 changes: 0 additions & 8 deletions pkg/deployer/gateway_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kgateway-dev/kgateway/v2/api/v1alpha1"
"github.com/kgateway-dev/kgateway/v2/pkg/pluginsdk/collections"
Expand All @@ -27,13 +26,6 @@ type Inputs struct {
AgentgatewayClassName string
}

type ExtraGatewayParameters struct {
Group string
Kind string
Object client.Object
Generator HelmValuesGenerator
}

// UpdateSecurityContexts updates the security contexts in the gateway parameters.
// It applies the floating user ID if it is set and adds the sysctl to allow the privileged ports if the gateway uses them.
func UpdateSecurityContexts(cfg *v1alpha1.KubernetesProxyConfig, ports []HelmPort) {
Expand Down
Loading
Loading