Skip to content

Commit 045d7da

Browse files
author
Kate Osborn
committed
Watch UpstreamSettingsPolicies and translate into datplane config
1 parent eabc8c9 commit 045d7da

File tree

27 files changed

+1975
-195
lines changed

27 files changed

+1975
-195
lines changed

charts/nginx-gateway-fabric/templates/clusterrole.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ rules:
104104
- nginxproxies
105105
- clientsettingspolicies
106106
- observabilitypolicies
107+
- upstreamsettingspolicies
107108
{{- if .Values.nginxGateway.snippetsFilters.enable }}
108109
- snippetsfilters
109110
{{- end }}
@@ -116,6 +117,7 @@ rules:
116117
- nginxgateways/status
117118
- clientsettingspolicies/status
118119
- observabilitypolicies/status
120+
- upstreamsettingspolicies/status
119121
{{- if .Values.nginxGateway.snippetsFilters.enable }}
120122
- snippetsfilters/status
121123
{{- end }}

config/crd/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ resources:
55
- bases/gateway.nginx.org_nginxgateways.yaml
66
- bases/gateway.nginx.org_nginxproxies.yaml
77
- bases/gateway.nginx.org_observabilitypolicies.yaml
8+
- bases/gateway.nginx.org_snippetsfilters.yaml
9+
- bases/gateway.nginx.org_upstreamsettingspolicies.yaml

deploy/crds.yaml

Lines changed: 633 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: HTTPRoute
3+
metadata:
4+
name: coffee
5+
spec:
6+
parentRefs:
7+
- name: gateway
8+
sectionName: http
9+
hostnames:
10+
- "cafe.example.com"
11+
rules:
12+
- matches:
13+
- path:
14+
type: PathPrefix
15+
value: /coffee
16+
backendRefs:
17+
- name: coffee
18+
port: 80
19+
---
20+
apiVersion: gateway.networking.k8s.io/v1
21+
kind: HTTPRoute
22+
metadata:
23+
name: tea
24+
spec:
25+
parentRefs:
26+
- name: gateway
27+
sectionName: http
28+
hostnames:
29+
- "cafe.example.com"
30+
rules:
31+
- matches:
32+
- path:
33+
type: Exact
34+
value: /tea
35+
backendRefs:
36+
- name: tea
37+
port: 80
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: coffee
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: coffee
10+
template:
11+
metadata:
12+
labels:
13+
app: coffee
14+
spec:
15+
containers:
16+
- name: coffee
17+
image: nginxdemos/nginx-hello:plain-text
18+
ports:
19+
- containerPort: 8080
20+
---
21+
apiVersion: v1
22+
kind: Service
23+
metadata:
24+
name: coffee
25+
spec:
26+
ports:
27+
- port: 80
28+
targetPort: 8080
29+
protocol: TCP
30+
name: http
31+
selector:
32+
app: coffee
33+
---
34+
apiVersion: apps/v1
35+
kind: Deployment
36+
metadata:
37+
name: tea
38+
spec:
39+
replicas: 1
40+
selector:
41+
matchLabels:
42+
app: tea
43+
template:
44+
metadata:
45+
labels:
46+
app: tea
47+
spec:
48+
containers:
49+
- name: tea
50+
image: nginxdemos/nginx-hello:plain-text
51+
ports:
52+
- containerPort: 8080
53+
---
54+
apiVersion: v1
55+
kind: Service
56+
metadata:
57+
name: tea
58+
spec:
59+
ports:
60+
- port: 80
61+
targetPort: 8080
62+
protocol: TCP
63+
name: http
64+
selector:
65+
app: tea
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: Gateway
3+
metadata:
4+
name: gateway
5+
spec:
6+
gatewayClassName: nginx
7+
listeners:
8+
- name: http
9+
port: 80
10+
protocol: HTTP
11+
hostname: "*.example.com"

examples/upstream-settings-policy/upstream-settings-policy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spec:
77
targetRefs:
88
- group: core
99
kind: Service
10-
name: service
10+
name: coffee
1111
keepAlive:
1212
connections: 32
1313
requests: 1001

internal/framework/kinds/kinds.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111

1212
// Gateway API Kinds.
1313
const (
14-
// Gateway is the Gateway Kind.
14+
// Gateway is the Gateway kind.
1515
Gateway = "Gateway"
16-
// GatewayClass is the GatewayClass Kind.
16+
// GatewayClass is the GatewayClass kind.
1717
GatewayClass = "GatewayClass"
1818
// HTTPRoute is the HTTPRoute kind.
1919
HTTPRoute = "HTTPRoute"
@@ -23,6 +23,12 @@ const (
2323
TLSRoute = "TLSRoute"
2424
)
2525

26+
// Core API Kinds.
27+
const (
28+
// Service is the Service kind.
29+
Service = "Service"
30+
)
31+
2632
// NGINX Gateway Fabric kinds.
2733
const (
2834
// ClientSettingsPolicy is the ClientSettingsPolicy kind.
@@ -33,6 +39,8 @@ const (
3339
NginxProxy = "NginxProxy"
3440
// SnippetsFilter is the SnippetsFilter kind.
3541
SnippetsFilter = "SnippetsFilter"
42+
// UpstreamSettingsPolicy is the UpstreamSettingsPolicy kind.
43+
UpstreamSettingsPolicy = "UpstreamSettingsPolicy"
3644
)
3745

3846
// MustExtractGVK is a function that extracts the GroupVersionKind (GVK) of a client.object.

internal/mode/static/manager.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies"
5353
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies/clientsettings"
5454
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies/observability"
55+
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/policies/upstreamsettings"
5556
ngxvalidation "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/validation"
5657
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/file"
5758
ngxruntime "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/runtime"
@@ -311,6 +312,10 @@ func createPolicyManager(
311312
GVK: mustExtractGVK(&ngfAPI.ObservabilityPolicy{}),
312313
Validator: observability.NewValidator(validator),
313314
},
315+
{
316+
GVK: mustExtractGVK(&ngfAPI.UpstreamSettingsPolicy{}),
317+
Validator: upstreamsettings.NewValidator(validator),
318+
},
314319
}
315320

316321
return policies.NewManager(mustExtractGVK, cfgs...)
@@ -492,6 +497,12 @@ func registerControllers(
492497
controller.WithK8sPredicate(k8spredicate.GenerationChangedPredicate{}),
493498
},
494499
},
500+
{
501+
objectType: &ngfAPI.UpstreamSettingsPolicy{},
502+
options: []controller.Option{
503+
controller.WithK8sPredicate(k8spredicate.GenerationChangedPredicate{}),
504+
},
505+
},
495506
}
496507

497508
if cfg.ExperimentalFeatures {
@@ -728,6 +739,7 @@ func prepareFirstEventBatchPreparerArgs(cfg config.Config) ([]client.Object, []c
728739
&gatewayv1.GRPCRouteList{},
729740
&ngfAPI.ClientSettingsPolicyList{},
730741
&ngfAPI.ObservabilityPolicyList{},
742+
&ngfAPI.UpstreamSettingsPolicyList{},
731743
partialObjectMetadataList,
732744
}
733745

internal/mode/static/manager_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) {
6767
partialObjectMetadataList,
6868
&ngfAPI.ClientSettingsPolicyList{},
6969
&ngfAPI.ObservabilityPolicyList{},
70+
&ngfAPI.UpstreamSettingsPolicyList{},
7071
},
7172
},
7273
{
@@ -96,6 +97,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) {
9697
partialObjectMetadataList,
9798
&ngfAPI.ClientSettingsPolicyList{},
9899
&ngfAPI.ObservabilityPolicyList{},
100+
&ngfAPI.UpstreamSettingsPolicyList{},
99101
},
100102
},
101103
{
@@ -128,6 +130,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) {
128130
&gatewayv1.GRPCRouteList{},
129131
&ngfAPI.ClientSettingsPolicyList{},
130132
&ngfAPI.ObservabilityPolicyList{},
133+
&ngfAPI.UpstreamSettingsPolicyList{},
131134
},
132135
},
133136
{
@@ -158,6 +161,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) {
158161
&ngfAPI.ClientSettingsPolicyList{},
159162
&ngfAPI.ObservabilityPolicyList{},
160163
&ngfAPI.SnippetsFilterList{},
164+
&ngfAPI.UpstreamSettingsPolicyList{},
161165
},
162166
},
163167
{
@@ -191,6 +195,7 @@ func TestPrepareFirstEventBatchPreparerArgs(t *testing.T) {
191195
&ngfAPI.ClientSettingsPolicyList{},
192196
&ngfAPI.ObservabilityPolicyList{},
193197
&ngfAPI.SnippetsFilterList{},
198+
&ngfAPI.UpstreamSettingsPolicyList{},
194199
},
195200
},
196201
}

internal/mode/static/nginx/config/policies/clientsettings/validator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ func (v *Validator) Validate(policy policies.Policy, _ *policies.GlobalSettings)
3030

3131
targetRefPath := field.NewPath("spec").Child("targetRef")
3232
supportedKinds := []gatewayv1.Kind{kinds.Gateway, kinds.HTTPRoute, kinds.GRPCRoute}
33-
if err := policies.ValidateTargetRef(csp.Spec.TargetRef, targetRefPath, supportedKinds); err != nil {
33+
supportedGroups := []gatewayv1.Group{gatewayv1.GroupName}
34+
35+
if err := policies.ValidateTargetRef(csp.Spec.TargetRef, targetRefPath, supportedGroups, supportedKinds); err != nil {
3436
return []conditions.Condition{staticConds.NewPolicyInvalid(err.Error())}
3537
}
3638

internal/mode/static/nginx/config/policies/observability/validator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ func (v *Validator) Validate(
4545

4646
targetRefPath := field.NewPath("spec").Child("targetRefs")
4747
supportedKinds := []gatewayv1.Kind{kinds.HTTPRoute, kinds.GRPCRoute}
48+
supportedGroups := []gatewayv1.Group{gatewayv1.GroupName}
49+
4850
for _, ref := range obs.Spec.TargetRefs {
49-
if err := policies.ValidateTargetRef(ref, targetRefPath, supportedKinds); err != nil {
51+
if err := policies.ValidateTargetRef(ref, targetRefPath, supportedGroups, supportedKinds); err != nil {
5052
return []conditions.Condition{staticConds.NewPolicyInvalid(err.Error())}
5153
}
5254
}

internal/mode/static/nginx/config/policies/policy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,26 @@ type Policy interface {
2424
// GlobalSettings contains global settings from the current state of the graph that may be
2525
// needed for policy validation or generation if certain policies rely on those global settings.
2626
type GlobalSettings struct {
27-
// NginxProxyValid is whether or not the NginxProxy resource is valid.
27+
// NginxProxyValid is whether the NginxProxy resource is valid.
2828
NginxProxyValid bool
29-
// TelemetryEnabled is whether or not telemetry is enabled in the NginxProxy resource.
29+
// TelemetryEnabled is whether telemetry is enabled in the NginxProxy resource.
3030
TelemetryEnabled bool
3131
}
3232

3333
// ValidateTargetRef validates a policy's targetRef for the proper group and kind.
3434
func ValidateTargetRef(
3535
ref v1alpha2.LocalPolicyTargetReference,
3636
basePath *field.Path,
37+
groups []gatewayv1.Group,
3738
supportedKinds []gatewayv1.Kind,
3839
) error {
39-
if ref.Group != gatewayv1.GroupName {
40+
if !slices.Contains(groups, ref.Group) {
4041
path := basePath.Child("group")
4142

4243
return field.NotSupported(
4344
path,
4445
ref.Group,
45-
[]string{gatewayv1.GroupName},
46+
groups,
4647
)
4748
}
4849

0 commit comments

Comments
 (0)