Skip to content

Commit

Permalink
Switch IngressTLS helper from equality to contains
Browse files Browse the repository at this point in the history
  • Loading branch information
ReToCode committed Nov 23, 2023
1 parent 44caafa commit 839bec7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/networking/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const (
VisibilityLabelKey = PublicGroupName + "/visibility"

// CertificateTypeLabelKey is the label to indicate the type of Knative certificate
// used for Knative Serving encryption functionality.
// used for Knative Serving encryption functionality. Corresponding values are defined in config.CertificateType.
CertificateTypeLabelKey = PublicGroupName + "/certificate-type"
)

Expand Down
24 changes: 14 additions & 10 deletions pkg/apis/networking/v1alpha1/ingress_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ limitations under the License.
package v1alpha1

import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"slices"
)

// GetIngressTLSForVisibility returns a list of `Spec.TLS` where the `Hosts` field matches
// to `Spec.Rules.Hosts` and where the Rules have the defined ingress visibility.
// GetIngressTLSForVisibility returns a list of `Spec.TLS` where each host in the `Rules.Hosts` field is
// present in `Spec.TLS.Hosts` and where the Rules have the defined ingress visibility.
// This method can be used in net-* implementations to select the correct `IngressTLS` entries
// for cluster-local and cluster-external gateways/listeners.
func (i *Ingress) GetIngressTLSForVisibility(visibility IngressVisibility) []IngressTLS {
Expand All @@ -32,12 +31,17 @@ func (i *Ingress) GetIngressTLSForVisibility(visibility IngressVisibility) []Ing
return ingressTLS
}

for _, r := range i.Spec.Rules {
if r.Visibility == visibility {
for _, t := range i.Spec.TLS {
// Check if hosts slices are equal ignoring the order
if cmp.Diff(r.Hosts, t.Hosts, cmpopts.SortSlices(func(a, b string) bool { return a < b })) == "" {
ingressTLS = append(ingressTLS, t)
for _, rule := range i.Spec.Rules {
if rule.Visibility == visibility {
for _, tls := range i.Spec.TLS {
containsAllRulesHosts := true
for _, h := range rule.Hosts {
if !slices.Contains(tls.Hosts, h) {
containsAllRulesHosts = false
}
}
if containsAllRulesHosts {
ingressTLS = append(ingressTLS, tls)
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/apis/networking/v1alpha1/ingress_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ func TestGetIngressTLSForVisibility(t *testing.T) {
},
},
want: make([]IngressTLS, 0),
}, {
name: "matching entries with additional hosts in TLS block",
visibility: IngressVisibilityClusterLocal,
ingress: &Ingress{
Spec: IngressSpec{
Rules: []IngressRule{
{
Hosts: []string{"expected"},
Visibility: IngressVisibilityClusterLocal,
},
{
Hosts: []string{"other", "entries"},
Visibility: IngressVisibilityExternalIP,
},
},
TLS: []IngressTLS{
{Hosts: []string{"expected", "additional"}},
},
},
},
want: []IngressTLS{
{Hosts: []string{"expected", "additional"}},
},
}}

for _, test := range tests {
Expand Down

0 comments on commit 839bec7

Please sign in to comment.