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

fix(parser) parse knative.Ingress TLS sections #721

Merged
merged 1 commit into from
Jun 8, 2020
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
34 changes: 29 additions & 5 deletions internal/ingress/controller/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,21 @@ func (p *Parser) Build() (*KongState, error) {
if err != nil {
glog.Errorf("error listing knative Ingresses: %v", err)
}
servicesFromKnative := p.parseKnativeIngressRules(knativeIngresses)
servicesFromKnative, secretToSNIsFromKnative := p.parseKnativeIngressRules(knativeIngresses)

for name, service := range servicesFromKnative {
parsedInfo.ServiceNameToServices[name] = service
}

for secret, snis := range secretToSNIsFromKnative {
var combinedSNIs []string
if snisFromIngress, ok := parsedInfo.SecretNameToSNIs[secret]; ok {
combinedSNIs = append(combinedSNIs, snisFromIngress...)
}
combinedSNIs = append(combinedSNIs, snis...)
parsedInfo.SecretNameToSNIs[secret] = combinedSNIs
}

// populate Kubernetes Service
for key, service := range parsedInfo.ServiceNameToServices {
k8sSvc, err := p.store.GetService(service.Namespace, service.Backend.Name)
Expand Down Expand Up @@ -491,7 +500,19 @@ func processTLSSections(tlsSections []networking.IngressTLS,
}
}

func toNetworkingTLS(tls []configurationv1beta1.IngressTLS) []networking.IngressTLS {
func knativeIngressToNetworkingTLS(tls []knative.IngressTLS) []networking.IngressTLS {
var result []networking.IngressTLS

for _, t := range tls {
result = append(result, networking.IngressTLS{
Hosts: t.Hosts,
SecretName: t.SecretName,
})
}
return result
}

func tcpIngressToNetworkingTLS(tls []configurationv1beta1.IngressTLS) []networking.IngressTLS {
var result []networking.IngressTLS

for _, t := range tls {
Expand All @@ -504,19 +525,22 @@ func toNetworkingTLS(tls []configurationv1beta1.IngressTLS) []networking.Ingress
}

func (p *Parser) parseKnativeIngressRules(
ingressList []*knative.Ingress) map[string]Service {
ingressList []*knative.Ingress) (map[string]Service, map[string][]string) {

sort.SliceStable(ingressList, func(i, j int) bool {
return ingressList[i].CreationTimestamp.Before(
&ingressList[j].CreationTimestamp)
})

services := map[string]Service{}
secretToSNIs := map[string][]string{}

for i := 0; i < len(ingressList); i++ {
ingress := *ingressList[i]
ingressSpec := ingress.Spec

processTLSSections(knativeIngressToNetworkingTLS(ingress.Spec.TLS),
ingress.Namespace, secretToSNIs)
for i, rule := range ingressSpec.Rules {
hosts := rule.Hosts
if rule.HTTP == nil {
Expand Down Expand Up @@ -601,7 +625,7 @@ func (p *Parser) parseKnativeIngressRules(
}
}

return services
return services, secretToSNIs
}

func knativeSelectSplit(splits []knative.IngressBackendSplit) knative.IngressBackendSplit {
Expand Down Expand Up @@ -730,7 +754,7 @@ func (p *Parser) parseIngressRules(
ingress := *tcpIngressList[i]
ingressSpec := ingress.Spec

processTLSSections(toNetworkingTLS(ingressSpec.TLS),
processTLSSections(tcpIngressToNetworkingTLS(ingressSpec.TLS),
ingress.Namespace, secretNameToSNIs)

for i, rule := range ingressSpec.Rules {
Expand Down
82 changes: 78 additions & 4 deletions internal/ingress/controller/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3137,19 +3137,79 @@ func TestParseKnativeIngressRules(t *testing.T) {
},
},
},
// 3
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "foo-namespace",
},
Spec: knative.IngressSpec{
Rules: []knative.IngressRule{
{
Hosts: []string{"my-func.example.com"},
HTTP: &knative.HTTPIngressRuleValue{
Paths: []knative.HTTPIngressPath{
{
Path: "/",
AppendHeaders: map[string]string{
"foo": "bar",
},
Splits: []knative.IngressBackendSplit{
{
IngressBackend: knative.IngressBackend{
ServiceNamespace: "bar-ns",
ServiceName: "bar-svc",
ServicePort: intstr.FromInt(42),
},
Percent: 20,
},
{
IngressBackend: knative.IngressBackend{
ServiceNamespace: "foo-ns",
ServiceName: "foo-svc",
ServicePort: intstr.FromInt(42),
},
Percent: 100,
},
},
},
},
},
},
},
TLS: []knative.IngressTLS{
{
Hosts: []string{
"foo.example.com",
"foo1.example.com",
},
SecretName: "foo-secret",
},
{
Hosts: []string{
"bar.example.com",
"bar1.example.com",
},
SecretName: "bar-secret",
},
},
},
},
}
t.Run("no ingress returns empty info", func(t *testing.T) {
parsedInfo := p.parseKnativeIngressRules([]*knative.Ingress{})
parsedInfo, secretToSNIs := p.parseKnativeIngressRules([]*knative.Ingress{})
assert.Equal(map[string]Service{}, parsedInfo)
assert.Equal(map[string][]string{}, secretToSNIs)
})
t.Run("empty ingress returns empty info", func(t *testing.T) {
parsedInfo := p.parseKnativeIngressRules([]*knative.Ingress{
parsedInfo, secretToSNIs := p.parseKnativeIngressRules([]*knative.Ingress{
ingressList[0],
})
assert.Equal(map[string]Service{}, parsedInfo)
assert.Equal(map[string][]string{}, secretToSNIs)
})
t.Run("basic knative Ingress resource is parsed", func(t *testing.T) {
parsedInfo := p.parseKnativeIngressRules([]*knative.Ingress{
parsedInfo, secretToSNIs := p.parseKnativeIngressRules([]*knative.Ingress{
ingressList[1],
})
assert.Equal(1, len(parsedInfo))
Expand Down Expand Up @@ -3182,9 +3242,21 @@ func TestParseKnativeIngressRules(t *testing.T) {
},
},
}, svc.Plugins[0])

assert.Equal(map[string][]string{}, secretToSNIs)
})
t.Run("knative TLS section is correctly parsed", func(t *testing.T) {
_, secretToSNIs := p.parseKnativeIngressRules([]*knative.Ingress{
ingressList[3],
})

assert.Equal(map[string][]string{
"foo-namespace/bar-secret": {"bar.example.com", "bar1.example.com"},
"foo-namespace/foo-secret": {"foo.example.com", "foo1.example.com"},
}, secretToSNIs)
})
t.Run("split knative Ingress resource chooses the highest split", func(t *testing.T) {
parsedInfo := p.parseKnativeIngressRules([]*knative.Ingress{
parsedInfo, secretToSNIs := p.parseKnativeIngressRules([]*knative.Ingress{
ingressList[2],
})
assert.Equal(1, len(parsedInfo))
Expand Down Expand Up @@ -3217,6 +3289,8 @@ func TestParseKnativeIngressRules(t *testing.T) {
},
},
}, svc.Plugins[0])

assert.Equal(map[string][]string{}, secretToSNIs)
})
}

Expand Down