Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gateway api BackendTLSPolicy #6119

Merged
Merged
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
9 changes: 9 additions & 0 deletions changelogs/unreleased/6119-flawedmatrix-major.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Support for Gateway API BackendTLSPolicy

The BackendTLSPolicy CRD can now be used with HTTPRoute to configure a Contour gateway to connect to a backend Service with TLS. This will give users the ability to use Gateway API to configure their routes to securely connect to backends that use TLS with Contour.

The BackendTLSPolicy spec requires you to specify a `targetRef`, which can currently only be a Kubernetes Service within the same namespace as the BackendTLSPolicy. The targetRef is what Service should be watched to apply the BackendTLSPolicy to. A `SectionName` can also be configured to the port name of a Service to reference a specific section of the Service.

The spec also requires you to specify `caCertRefs`, which can either be a ConfigMap or Secret with a `ca.crt` key in the data map containing a PEM-encoded TLS certificate. The CA certificates referenced will be configured to be used by the gateway to perform TLS to the backend Service. You will also need to specify a `Hostname`, which will be used to configure the SNI the gateway will use for the connection.

See Gateway API's [GEP-1897](https://gateway-api.sigs.k8s.io/geps/gep-1897) for the proposal for BackendTLSPolicy.
44 changes: 40 additions & 4 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
serve.Flag("debug", "Enable debug logging.").Short('d').BoolVar(&ctx.Config.Debug)
serve.Flag("debug-http-address", "Address the debug http endpoint will bind to.").PlaceHolder("<ipaddr>").StringVar(&ctx.debugAddr)
serve.Flag("debug-http-port", "Port the debug http endpoint will bind to.").PlaceHolder("<port>").IntVar(&ctx.debugPort)
serve.Flag("disable-feature", "Do not start an informer for the specified resources.").PlaceHolder("<extensionservices,tlsroutes,grpcroutes,tcproutes>").EnumsVar(&ctx.disabledFeatures, "extensionservices", "tlsroutes", "grpcroutes", "tcproutes")
serve.Flag("disable-feature", "Do not start an informer for the specified resources.").PlaceHolder("<extensionservices,tlsroutes,grpcroutes,tcproutes,backendtlspolicies>").EnumsVar(&ctx.disabledFeatures, "extensionservices", "tlsroutes", "grpcroutes", "tcproutes", "backendtlspolicies")
serve.Flag("disable-leader-election", "Disable leader election mechanism.").BoolVar(&ctx.LeaderElection.Disable)

serve.Flag("envoy-http-access-log", "Envoy HTTP access log.").PlaceHolder("/path/to/file").StringVar(&ctx.httpAccessLog)
Expand Down Expand Up @@ -260,6 +260,28 @@
return secret, nil
},
},
&corev1.ConfigMap{}: {
Transform: func(obj any) (any, error) {
configMap, ok := obj.(*corev1.ConfigMap)
// TransformFunc should handle the tombstone of type cache.DeletedFinalStateUnknown
if !ok {
return obj, nil
}

Check warning on line 269 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L264-L269

Added lines #L264 - L269 were not covered by tests

// Keep ConfigMaps that have the ca.crt key because they may be necessary
if _, ok := configMap.Data[dag.CACertificateKey]; ok {
return obj, nil
}

Check warning on line 274 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L272-L274

Added lines #L272 - L274 were not covered by tests

// Other types of ConfigMaps will never be referred to, so we can remove all data.
// Last-applied-configuration annotation might contain a copy of the complete data.
configMap.Data = map[string]string{}
configMap.SetManagedFields(nil)
configMap.SetAnnotations(nil)

return configMap, nil

Check warning on line 282 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L278-L282

Added lines #L278 - L282 were not covered by tests
},
},
},
// DefaultTransform is called for objects that do not have a TransformByObject function.
DefaultTransform: func(obj any) (any, error) {
Expand Down Expand Up @@ -1053,9 +1075,10 @@

// Some features may be disabled.
features := map[string]struct{}{
"tlsroutes": {},
"grpcroutes": {},
"tcproutes": {},
"tlsroutes": {},
"grpcroutes": {},
"tcproutes": {},

Check warning on line 1080 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L1078-L1080

Added lines #L1078 - L1080 were not covered by tests
"backendtlspolicies": {},
}
for _, f := range s.ctx.disabledFeatures {
delete(features, f)
Expand Down Expand Up @@ -1087,6 +1110,18 @@
}
}

// Create and register the BackendTLSPolicy controller with the manager.
if _, enabled := features["backendtlspolicies"]; enabled {
// Inform on ConfigMap if BackendTLSPolicy is enabled
if err := s.informOnResource(&corev1.ConfigMap{}, eventHandler); err != nil {
skriss marked this conversation as resolved.
Show resolved Hide resolved
skriss marked this conversation as resolved.
Show resolved Hide resolved
s.log.WithError(err).WithField("resource", "configmaps").Fatal("failed to create informer")

Check warning on line 1117 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L1114-L1117

Added lines #L1114 - L1117 were not covered by tests
}

if err := controller.RegisterBackendTLSPolicyController(s.log.WithField("context", "backendtlspolicy-controller"), mgr, eventHandler); err != nil {
skriss marked this conversation as resolved.
Show resolved Hide resolved
s.log.WithError(err).Fatal("failed to create backendtlspolicy-controller")
}
}

Check warning on line 1124 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L1121-L1124

Added lines #L1121 - L1124 were not covered by tests
// Inform on ReferenceGrants.
if err := s.informOnResource(&gatewayapi_v1beta1.ReferenceGrant{}, eventHandler); err != nil {
s.log.WithError(err).WithField("resource", "referencegrants").Fatal("failed to create informer")
Expand Down Expand Up @@ -1232,6 +1267,7 @@
PerConnectionBufferLimitBytes: dbc.perConnectionBufferLimitBytes,
SetSourceMetadataOnRoutes: true,
GlobalCircuitBreakerDefaults: dbc.globalCircuitBreakerDefaults,
UpstreamTLS: dbc.upstreamTLS,
})
}

Expand Down
3 changes: 3 additions & 0 deletions examples/contour/02-role-contour.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- namespaces
- secrets
Expand All @@ -29,6 +30,7 @@ rules:
- apiGroups:
- gateway.networking.k8s.io
resources:
- backendtlspolicies
- gatewayclasses
- gateways
- grpcroutes
Expand All @@ -43,6 +45,7 @@ rules:
- apiGroups:
- gateway.networking.k8s.io
resources:
- backendtlspolicies/status
- gatewayclasses/status
- gateways/status
- grpcroutes/status
Expand Down
29 changes: 16 additions & 13 deletions examples/gateway-provisioner/01-roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- namespaces
- secrets
Expand Down Expand Up @@ -70,15 +71,7 @@ rules:
- apiGroups:
- gateway.networking.k8s.io
resources:
- gatewayclasses
- gateways
verbs:
- get
- list
- watch
- apiGroups:
- gateway.networking.k8s.io
resources:
- backendtlspolicies
- gatewayclasses
- gateways
- grpcroutes
Expand All @@ -93,19 +86,29 @@ rules:
- apiGroups:
- gateway.networking.k8s.io
resources:
- backendtlspolicies/status
- gatewayclasses/status
- gateways/status
- grpcroutes/status
- httproutes/status
- tcproutes/status
- tlsroutes/status
verbs:
- update
- apiGroups:
- gateway.networking.k8s.io
resources:
- gatewayclasses
- gateways
verbs:
- get
- list
- watch
- apiGroups:
- gateway.networking.k8s.io
resources:
- gatewayclasses/status
- gateways/status
- grpcroutes/status
- httproutes/status
- tcproutes/status
- tlsroutes/status
verbs:
- update
- apiGroups:
Expand Down
Loading
Loading