Skip to content

Commit 42af02a

Browse files
committed
Tidy code, fix panic, update for plus manifest
1 parent 96269ae commit 42af02a

File tree

10 files changed

+152
-81
lines changed

10 files changed

+152
-81
lines changed

.yamllint.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ rules:
3232
ignore: |
3333
deploy/manifests/nginx-gateway.yaml
3434
deploy/manifests/crds
35-
examples/backend-tls/secure-app.yaml
3635
key-duplicates: enable
3736
key-ordering: disable
3837
line-length:

deploy/manifests/nginx-plus-gateway.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ rules:
3232
- namespaces
3333
- services
3434
- secrets
35+
- configmaps
3536
verbs:
3637
- list
3738
- watch
@@ -76,6 +77,7 @@ rules:
7677
- gateways
7778
- httproutes
7879
- referencegrants
80+
- backendtlspolicies
7981
verbs:
8082
- list
8183
- watch
@@ -85,6 +87,7 @@ rules:
8587
- httproutes/status
8688
- gateways/status
8789
- gatewayclasses/status
90+
- backendtlspolicies/status
8891
verbs:
8992
- update
9093
- apiGroups:

internal/mode/static/build_statuses_test.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -616,32 +616,58 @@ func TestBuildGatewayStatuses(t *testing.T) {
616616
}
617617

618618
func TestBuildBackendTLSPolicyStatuses(t *testing.T) {
619-
getBackendTLSPolicy := func(
620-
name string,
621-
valid bool,
622-
ignored bool,
623-
isReferenced bool,
624-
conditions []conditions.Condition,
625-
) *graph.BackendTLSPolicy {
619+
type policyCfg struct {
620+
Name string
621+
Conditions []conditions.Condition
622+
Valid bool
623+
Ignored bool
624+
IsReferenced bool
625+
}
626+
627+
getBackendTLSPolicy := func(policyCfg policyCfg) *graph.BackendTLSPolicy {
626628
return &graph.BackendTLSPolicy{
627629
Source: &v1alpha2.BackendTLSPolicy{
628630
ObjectMeta: metav1.ObjectMeta{
629631
Namespace: "test",
630-
Name: name,
632+
Name: policyCfg.Name,
631633
Generation: 1,
632634
},
633635
},
634-
Valid: valid,
635-
Ignored: ignored,
636-
IsReferenced: isReferenced,
637-
Conditions: conditions,
636+
Valid: policyCfg.Valid,
637+
Ignored: policyCfg.Ignored,
638+
IsReferenced: policyCfg.IsReferenced,
639+
Conditions: policyCfg.Conditions,
638640
Gateway: types.NamespacedName{Name: "gateway", Namespace: "test"},
639641
}
640642
}
641643

642644
attachedConds := []conditions.Condition{staticConds.NewBackendTLSPolicyAccepted()}
643645
invalidConds := []conditions.Condition{staticConds.NewBackendTLSPolicyInvalid("invalid backendTLSPolicy")}
644646

647+
validPolicyCfg := policyCfg{
648+
Name: "valid-bt",
649+
Valid: true,
650+
IsReferenced: true,
651+
Conditions: attachedConds,
652+
}
653+
654+
invalidPolicyCfg := policyCfg{
655+
Name: "invalid-bt",
656+
IsReferenced: true,
657+
Conditions: invalidConds,
658+
}
659+
660+
ignoredPolicyCfg := policyCfg{
661+
Name: "ignored-bt",
662+
Ignored: true,
663+
IsReferenced: true,
664+
}
665+
666+
notReferencedPolicyCfg := policyCfg{
667+
Name: "not-referenced",
668+
Valid: true,
669+
}
670+
645671
tests := []struct {
646672
backendTLSPolicies map[types.NamespacedName]*graph.BackendTLSPolicy
647673
expected status.BackendTLSPolicyStatuses
@@ -654,7 +680,7 @@ func TestBuildBackendTLSPolicyStatuses(t *testing.T) {
654680
{
655681
name: "valid backendTLSPolicy",
656682
backendTLSPolicies: map[types.NamespacedName]*graph.BackendTLSPolicy{
657-
{Namespace: "test", Name: "valid-bt"}: getBackendTLSPolicy("valid-bt", true, false, true, attachedConds),
683+
{Namespace: "test", Name: "valid-bt"}: getBackendTLSPolicy(validPolicyCfg),
658684
},
659685
expected: status.BackendTLSPolicyStatuses{
660686
{Namespace: "test", Name: "valid-bt"}: {
@@ -670,7 +696,7 @@ func TestBuildBackendTLSPolicyStatuses(t *testing.T) {
670696
{
671697
name: "invalid backendTLSPolicy",
672698
backendTLSPolicies: map[types.NamespacedName]*graph.BackendTLSPolicy{
673-
{Namespace: "test", Name: "invalid-bt"}: getBackendTLSPolicy("invalid-bt", false, false, true, invalidConds),
699+
{Namespace: "test", Name: "invalid-bt"}: getBackendTLSPolicy(invalidPolicyCfg),
674700
},
675701
expected: status.BackendTLSPolicyStatuses{
676702
{Namespace: "test", Name: "invalid-bt"}: {
@@ -686,16 +712,16 @@ func TestBuildBackendTLSPolicyStatuses(t *testing.T) {
686712
{
687713
name: "ignored or not referenced backendTLSPolicies",
688714
backendTLSPolicies: map[types.NamespacedName]*graph.BackendTLSPolicy{
689-
{Namespace: "test", Name: "ignored-bt"}: getBackendTLSPolicy("ignored-bt", false, true, true, nil),
690-
{Namespace: "test", Name: "not-referenced"}: getBackendTLSPolicy("not-referenced", true, false, false, nil),
715+
{Namespace: "test", Name: "ignored-bt"}: getBackendTLSPolicy(ignoredPolicyCfg),
716+
{Namespace: "test", Name: "not-referenced"}: getBackendTLSPolicy(notReferencedPolicyCfg),
691717
},
692718
expected: status.BackendTLSPolicyStatuses{},
693719
},
694720
{
695721
name: "mix valid and ignored backendTLSPolicies",
696722
backendTLSPolicies: map[types.NamespacedName]*graph.BackendTLSPolicy{
697-
{Namespace: "test", Name: "ignored-bt"}: getBackendTLSPolicy("ignored-bt", false, true, true, nil),
698-
{Namespace: "test", Name: "valid-bt"}: getBackendTLSPolicy("valid-bt", true, false, true, attachedConds),
723+
{Namespace: "test", Name: "ignored-bt"}: getBackendTLSPolicy(ignoredPolicyCfg),
724+
{Namespace: "test", Name: "valid-bt"}: getBackendTLSPolicy(validPolicyCfg),
699725
},
700726
expected: status.BackendTLSPolicyStatuses{
701727
{Namespace: "test", Name: "valid-bt"}: {

internal/mode/static/nginx/config/servers.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,21 @@ func updateLocationsForFilters(
258258
proxyPass := createProxyPass(
259259
matchRule.BackendGroup,
260260
matchRule.Filters.RequestURLRewrite,
261-
buildLocations[i].ProxySSLVerify != nil,
261+
generateProtocolString(buildLocations[i].ProxySSLVerify),
262262
)
263263
buildLocations[i].ProxyPass = proxyPass
264264
}
265265

266266
return buildLocations
267267
}
268268

269+
func generateProtocolString(ssl *http.ProxySSLVerify) string {
270+
if ssl != nil {
271+
return "https"
272+
}
273+
return "http"
274+
}
275+
269276
func createProxyTLSFromBackends(backends []dataplane.Backend) *http.ProxySSLVerify {
270277
if len(backends) == 0 {
271278
return nil
@@ -467,18 +474,13 @@ func isPathOnlyMatch(match dataplane.Match) bool {
467474
func createProxyPass(
468475
backendGroup dataplane.BackendGroup,
469476
filter *dataplane.HTTPURLRewriteFilter,
470-
enableTLS bool,
477+
protocol string,
471478
) string {
472479
var requestURI string
473480
if filter == nil || filter.Path == nil {
474481
requestURI = "$request_uri"
475482
}
476483

477-
protocol := "http"
478-
if enableTLS {
479-
protocol = "https"
480-
}
481-
482484
backendName := backendGroupName(backendGroup)
483485
if backendGroupNeedsSplit(backendGroup) {
484486
return protocol + "://$" + convertStringToSafeVariableName(backendName) + requestURI

internal/mode/static/nginx/config/servers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ func TestCreateProxyPass(t *testing.T) {
17141714
}
17151715

17161716
for _, tc := range tests {
1717-
result := createProxyPass(tc.grp, tc.rewrite, false)
1717+
result := createProxyPass(tc.grp, tc.rewrite, generateProtocolString(nil))
17181718
g.Expect(result).To(Equal(tc.expected))
17191719
}
17201720
}

internal/mode/static/state/change_processor_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,6 @@ var _ = Describe("ChangeProcessor", func() {
18351835
},
18361836
}
18371837
btlsUpdated = btls.DeepCopy()
1838-
btlsUpdated.Generation++
18391838
})
18401839
// Changing change - a change that makes processor.Process() report changed
18411840
// Non-changing change - a change that doesn't do that

internal/mode/static/state/conditions/conditions.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,6 @@ const (
6666
RouteMessageFailedNginxReload = GatewayMessageFailedNginxReload + ". NGINX may still be configured " +
6767
"for this HTTPRoute. However, future updates to this resource will not be configured until the Gateway " +
6868
"is programmed again"
69-
70-
// BackendTLSPolicyConditionAttached is the condition type indicating the BackendTLS policy is valid and attached to
71-
// the Gateway.
72-
BackendTLSPolicyConditionAttached BackendTLSPolicyConditionType = "Attached"
73-
74-
// BackendTLSPolicyReasonAttached is the condition reason for the BackendTLSPolicy Attached condition.
75-
BackendTLSPolicyReasonAttached BackendTLSPolicyConditionReason = "BackendTLSPolicyAttached"
76-
77-
// BackendTLSPolicyConditionValid is the condition type indicating whether the BackendTLS policy is valid.
78-
BackendTLSPolicyConditionValid BackendTLSPolicyConditionType = "Valid"
79-
80-
// BackendTLSPolicyReasonInvalid is the condition reason for the BackendTLSPolicy Valid condition being False.
81-
BackendTLSPolicyReasonInvalid BackendTLSPolicyConditionReason = "BackendTLSPolicyInvalid"
8269
)
8370

8471
// NewTODO returns a Condition that can be used as a placeholder for a condition that is not yet implemented.

internal/mode/static/state/graph/backend_tls_policy.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,26 @@ func processBackendTLSPolicies(
3737
ctlrName string,
3838
gateway *Gateway,
3939
) map[types.NamespacedName]*BackendTLSPolicy {
40-
if len(backendTLSPolicies) == 0 {
40+
if len(backendTLSPolicies) == 0 || gateway == nil {
4141
return nil
4242
}
43+
4344
processedBackendTLSPolicies := make(map[types.NamespacedName]*BackendTLSPolicy, len(backendTLSPolicies))
4445
for nsname, backendTLSPolicy := range backendTLSPolicies {
45-
valid, ignored, caCertRef, conds := validateBackendTLSPolicy(
46+
var caCertRef types.NamespacedName
47+
valid, ignored, conds := validateBackendTLSPolicy(
4648
backendTLSPolicy,
4749
configMapResolver,
4850
ctlrName,
4951
gateway,
5052
)
5153

54+
if valid && !ignored && backendTLSPolicy.Spec.TLS.CACertRefs != nil {
55+
caCertRef = types.NamespacedName{
56+
Namespace: backendTLSPolicy.Namespace, Name: string(backendTLSPolicy.Spec.TLS.CACertRefs[0].Name),
57+
}
58+
}
59+
5260
processedBackendTLSPolicies[nsname] = &BackendTLSPolicy{
5361
Source: backendTLSPolicy,
5462
Valid: valid,
@@ -69,11 +77,9 @@ func validateBackendTLSPolicy(
6977
configMapResolver *configMapResolver,
7078
ctlrName string,
7179
gateway *Gateway,
72-
) (bool, bool, types.NamespacedName, []conditions.Condition) {
73-
var conds []conditions.Condition
74-
valid := true
75-
ignored := false
76-
var caCertRef types.NamespacedName
80+
) (valid, ignored bool, conds []conditions.Condition) {
81+
valid = true
82+
ignored = false
7783
if err := validateAncestorMaxCount(backendTLSPolicy, ctlrName, gateway); err != nil {
7884
valid = false
7985
ignored = true
@@ -87,10 +93,6 @@ func validateBackendTLSPolicy(
8793
valid = false
8894
conds = append(conds, staticConds.NewBackendTLSPolicyInvalid(
8995
fmt.Sprintf("invalid CACertRef: %s", err.Error())))
90-
} else {
91-
caCertRef = types.NamespacedName{
92-
Namespace: backendTLSPolicy.Namespace, Name: string(backendTLSPolicy.Spec.TLS.CACertRefs[0].Name),
93-
}
9496
}
9597
} else if backendTLSPolicy.Spec.TLS.WellKnownCACerts != nil {
9698
if err := validateBackendTLSWellKnownCACerts(backendTLSPolicy); err != nil {
@@ -102,7 +104,7 @@ func validateBackendTLSPolicy(
102104
valid = false
103105
conds = append(conds, staticConds.NewBackendTLSPolicyInvalid("CACertRefs and WellKnownCACerts are both nil"))
104106
}
105-
return valid, ignored, caCertRef, conds
107+
return valid, ignored, conds
106108
}
107109

108110
func validateAncestorMaxCount(backendTLSPolicy *v1alpha2.BackendTLSPolicy, ctlrName string, gateway *Gateway) error {

0 commit comments

Comments
 (0)