Skip to content

Commit 5313c66

Browse files
authored
auto enable gateway api support (#592)
auto enable gateway API support Signed-off-by: Alice-Lilith <[email protected]>
1 parent 5df148c commit 5313c66

File tree

7 files changed

+85
-60
lines changed

7 files changed

+85
-60
lines changed

cmd/agent/main.go

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func cmd() *cobra.Command {
111111

112112
// feature flags
113113
c.Flags().BoolVar(&opts.enableFeatureIngress, "enable-feature-ingress", true, "Enables the Ingress controller")
114-
c.Flags().BoolVar(&opts.enableFeatureGateway, "enable-feature-gateway", false, "Enables the Gateway controller")
114+
c.Flags().BoolVar(&opts.enableFeatureGateway, "enable-feature-gateway", true, "When true, enables support for Gateway API if the CRDs are detected. When false, Gateway API support will not be enabled")
115115
c.Flags().BoolVar(&opts.disableGatewayReferenceGrants, "disable-reference-grants", false, "Opts-out of requiring ReferenceGrants for cross namespace references in Gateway API config")
116116
c.Flags().BoolVar(&opts.enableFeatureBindings, "enable-feature-bindings", false, "Enables the Endpoint Bindings controller")
117117

@@ -147,56 +147,55 @@ func runController(ctx context.Context, opts managerOpts) error {
147147
}
148148

149149
// shared features between Ingress and Gateway (tunnels)
150-
if opts.enableFeatureIngress || opts.enableFeatureGateway {
151-
var comments tunneldriver.TunnelDriverComments
152-
if opts.enableFeatureGateway {
153-
comments = tunneldriver.TunnelDriverComments{
154-
Gateway: "gateway-api",
155-
}
156-
}
157150

158-
rootCAs := "trusted"
159-
if opts.rootCAs != "" {
160-
rootCAs = opts.rootCAs
151+
var comments tunneldriver.TunnelDriverComments
152+
if opts.enableFeatureGateway {
153+
comments = tunneldriver.TunnelDriverComments{
154+
Gateway: "gateway-api",
161155
}
156+
}
162157

163-
td, err := tunneldriver.New(ctx, ctrl.Log.WithName("drivers").WithName("tunnel"),
164-
tunneldriver.TunnelDriverOpts{
165-
ServerAddr: opts.serverAddr,
166-
Region: opts.region,
167-
RootCAs: rootCAs,
168-
Comments: &comments,
169-
},
170-
)
171-
172-
if err != nil {
173-
return fmt.Errorf("unable to create tunnel driver: %w", err)
174-
}
158+
rootCAs := "trusted"
159+
if opts.rootCAs != "" {
160+
rootCAs = opts.rootCAs
161+
}
175162

176-
// register healthcheck for tunnel driver
177-
healthcheck.RegisterHealthChecker(td)
178-
179-
if err = (&agentcontroller.TunnelReconciler{
180-
Client: mgr.GetClient(),
181-
Log: ctrl.Log.WithName("controllers").WithName("tunnel"),
182-
Scheme: mgr.GetScheme(),
183-
Recorder: mgr.GetEventRecorderFor("tunnel-controller"),
184-
TunnelDriver: td,
185-
}).SetupWithManager(mgr); err != nil {
186-
setupLog.Error(err, "unable to create controller", "controller", "Tunnel")
187-
os.Exit(1)
188-
}
163+
td, err := tunneldriver.New(ctx, ctrl.Log.WithName("drivers").WithName("tunnel"),
164+
tunneldriver.TunnelDriverOpts{
165+
ServerAddr: opts.serverAddr,
166+
Region: opts.region,
167+
RootCAs: rootCAs,
168+
Comments: &comments,
169+
},
170+
)
189171

190-
if err = (&agentcontroller.AgentEndpointReconciler{
191-
Client: mgr.GetClient(),
192-
Log: ctrl.Log.WithName("controllers").WithName("agentendpoint"),
193-
Scheme: mgr.GetScheme(),
194-
Recorder: mgr.GetEventRecorderFor("agentendpoint-controller"),
195-
TunnelDriver: td,
196-
}).SetupWithManager(mgr); err != nil {
197-
setupLog.Error(err, "unable to create controller", "controller", "AgentEndpoint")
198-
os.Exit(1)
199-
}
172+
if err != nil {
173+
return fmt.Errorf("unable to create tunnel driver: %w", err)
174+
}
175+
176+
// register healthcheck for tunnel driver
177+
healthcheck.RegisterHealthChecker(td)
178+
179+
if err = (&agentcontroller.TunnelReconciler{
180+
Client: mgr.GetClient(),
181+
Log: ctrl.Log.WithName("controllers").WithName("tunnel"),
182+
Scheme: mgr.GetScheme(),
183+
Recorder: mgr.GetEventRecorderFor("tunnel-controller"),
184+
TunnelDriver: td,
185+
}).SetupWithManager(mgr); err != nil {
186+
setupLog.Error(err, "unable to create controller", "controller", "Tunnel")
187+
os.Exit(1)
188+
}
189+
190+
if err = (&agentcontroller.AgentEndpointReconciler{
191+
Client: mgr.GetClient(),
192+
Log: ctrl.Log.WithName("controllers").WithName("agentendpoint"),
193+
Scheme: mgr.GetScheme(),
194+
Recorder: mgr.GetEventRecorderFor("agentendpoint-controller"),
195+
TunnelDriver: td,
196+
}).SetupWithManager(mgr); err != nil {
197+
setupLog.Error(err, "unable to create controller", "controller", "AgentEndpoint")
198+
os.Exit(1)
200199
}
201200

202201
// register healthchecks

cmd/api/main.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2929
// to ensure that exec-entrypoint and run can make use of them.
3030

31+
"k8s.io/client-go/discovery"
3132
_ "k8s.io/client-go/plugin/pkg/client/auth"
3233
"k8s.io/client-go/rest"
3334
"k8s.io/utils/ptr"
@@ -76,8 +77,8 @@ var (
7677

7778
func init() {
7879
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
79-
utilruntime.Must(gatewayv1beta1.Install(scheme))
8080
utilruntime.Must(gatewayv1.Install(scheme))
81+
utilruntime.Must(gatewayv1beta1.Install(scheme))
8182
utilruntime.Must(ingressv1alpha1.AddToScheme(scheme))
8283
utilruntime.Must(ngrokv1alpha1.AddToScheme(scheme))
8384
utilruntime.Must(bindingsv1alpha1.AddToScheme(scheme))
@@ -164,7 +165,7 @@ func cmd() *cobra.Command {
164165

165166
// feature flags
166167
c.Flags().BoolVar(&opts.enableFeatureIngress, "enable-feature-ingress", true, "Enables the Ingress controller")
167-
c.Flags().BoolVar(&opts.enableFeatureGateway, "enable-feature-gateway", false, "Enables the Gateway controller")
168+
c.Flags().BoolVar(&opts.enableFeatureGateway, "enable-feature-gateway", true, "When true, enables support for Gateway API if the CRDs are detected. When false, Gateway API support will not be enabled")
168169
c.Flags().BoolVar(&opts.disableGatewayReferenceGrants, "disable-reference-grants", false, "Opts-out of requiring ReferenceGrants for cross namespace references in Gateway API config")
169170
c.Flags().BoolVar(&opts.enableFeatureBindings, "enable-feature-bindings", false, "Enables the Endpoint Bindings controller")
170171
c.Flags().StringSliceVar(&opts.bindings.endpointSelectors, "bindings-endpoint-selectors", []string{"true"}, "Endpoint Selectors for Endpoint Bindings")
@@ -194,6 +195,31 @@ func startOperator(ctx context.Context, opts managerOpts) error {
194195
return fmt.Errorf("unable to create k8s client: %w", err)
195196
}
196197

198+
// Unless we are fully opting-out of GWAPI support, check if the CRDs are installed. If not, disable GWAPI support
199+
if opts.enableFeatureGateway {
200+
discoveryClient, err := discovery.NewDiscoveryClientForConfig(k8sConfig)
201+
if err != nil {
202+
return fmt.Errorf("unable to create discovery client: %w", err)
203+
}
204+
205+
apiGroupList, err := discoveryClient.ServerGroups()
206+
if err != nil {
207+
return fmt.Errorf("unable to list server groups: %w", err)
208+
}
209+
210+
gatewayAPIGroupInstalled := false
211+
for _, group := range apiGroupList.Groups {
212+
if group.Name == "gateway.networking.k8s.io" {
213+
gatewayAPIGroupInstalled = true
214+
break
215+
}
216+
}
217+
if !gatewayAPIGroupInstalled {
218+
setupLog.Info("Gateway API CRDs not detected, Gateway feature set will be disabled")
219+
opts.enableFeatureGateway = false
220+
}
221+
}
222+
197223
var ok bool
198224
opts.namespace, ok = os.LookupEnv("POD_NAMESPACE")
199225
if !ok {

helm/ngrok-operator/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ To uninstall the chart:
143143

144144
### Kubernetes Gateway feature configuration
145145

146-
| Name | Description | Value |
147-
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------- |
148-
| `useExperimentalGatewayApi` | DEPRECATED: Use gateway.enabled instead | |
149-
| `gateway.enabled` | When true, enable the Gateway controller | `false` |
150-
| `gateway.disableReferenceGrants` | When true, disables required ReferenceGrants for cross-namespace references. Does nothing when gateway.enabled is false | `false` |
146+
| Name | Description | Value |
147+
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | ------- |
148+
| `useExperimentalGatewayApi` | DEPRECATED: Use gateway.enabled instead | |
149+
| `gateway.enabled` | When true, Gateway API support will be enabled if the CRDs are detected. When false, Gateway API support will never be enabled | `true` |
150+
| `gateway.disableReferenceGrants` | When true, disables required ReferenceGrants for cross-namespace references. Does nothing when gateway.enabled is false | `false` |
151151

152152
### Kubernetes Bindings feature configuration
153153

helm/ngrok-operator/tests/__snapshot__/controller-deployment_test.yaml.snap

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helm/ngrok-operator/tests/agent/__snapshot__/deployment_test.yaml.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helm/ngrok-operator/values.schema.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helm/ngrok-operator/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ agent:
278278
## @section Kubernetes Gateway feature configuration
279279
##
280280
## @extra useExperimentalGatewayApi DEPRECATED: Use gateway.enabled instead
281-
## @param gateway.enabled When true, enable the Gateway controller
281+
## @param gateway.enabled When true, Gateway API support will be enabled if the CRDs are detected. When false, Gateway API support will never be enabled
282282
## @param gateway.disableReferenceGrants When true, disables required ReferenceGrants for cross-namespace references. Does nothing when gateway.enabled is false
283283
##
284284
gateway:
285-
enabled: false
285+
enabled: true # Enabled by default if the Gateway API CRDs are detected
286286
disableReferenceGrants: false
287287

288288
##

0 commit comments

Comments
 (0)