Skip to content

Commit

Permalink
fix: routes with conflicting services
Browse files Browse the repository at this point in the history
Gateway routes with conflicting services in their backend don't break
config build.

Signed-off-by: Mattia Lavacca <lavacca.mattia@gmail.com>
  • Loading branch information
mlavacca committed Sep 27, 2022
1 parent 11a3226 commit c0846ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions internal/dataplane/parser/ingressrules.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package parser

import (
"fmt"
"strings"

"github.com/kong/go-kong/kong"
Expand Down Expand Up @@ -52,15 +51,8 @@ func (ir *ingressRules) populateServices(log logrus.FieldLogger, s store.Storer)
// and all the annotations in use across all services (when applicable).
k8sServices, seenAnnotations := getK8sServicesForBackends(log, s, service.Namespace, service.Backends)

// if the Kubernetes services have been deemed invalid, no need to continue
// they will all be dropped until the problem has been rectified.
if !servicesAllUseTheSameKongAnnotations(log, k8sServices, seenAnnotations) {
return fmt.Errorf("the Kubernetes Services %v cannot have different sets of konghq.com annotations. "+
"These Services are used in the same Gateway Route BackendRef together to create the Kong Service %s"+
"and must use the same Kong annotations", k8sServices, *service.Name)
}

for _, k8sService := range k8sServices {
servicesToAdd := filterServicesNotUsingTheSameKongAnnotations(log, k8sServices, seenAnnotations)
for _, k8sService := range servicesToAdd {
// at this point we know the Kubernetes service itself is valid and can be
// used for traffic, so cache it amongst the kong Services k8s services.
service.K8sServices[k8sService.Name] = k8sService
Expand Down Expand Up @@ -186,12 +178,13 @@ func getK8sServicesForBackends(
return k8sServices, seenAnnotationsForK8sServices
}

func servicesAllUseTheSameKongAnnotations(
func filterServicesNotUsingTheSameKongAnnotations(
log logrus.FieldLogger,
services []*corev1.Service,
annotations map[string]string,
) bool {
) []*corev1.Service {
match := true
servicesToReturn := make([]*corev1.Service, 0)
for _, service := range services {
// all services grouped together via backends must have identical annotations
// to avoid unexpected routing behaviors.
Expand Down Expand Up @@ -222,8 +215,12 @@ func servicesAllUseTheSameKongAnnotations(
"between them must be set to the same value", k, len(services))
match = false
}

if match {
servicesToReturn = append(servicesToReturn, service)
}
}
}

return match
return servicesToReturn
}
2 changes: 1 addition & 1 deletion internal/dataplane/parser/ingressrules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func Test_doK8sServicesMatchAnnotations(t *testing.T) {
stdout := new(bytes.Buffer)
logger := logrus.New()
logger.SetOutput(stdout)
assert.Equal(t, tt.expected, servicesAllUseTheSameKongAnnotations(logger, tt.services, tt.annotations))
assert.Equal(t, tt.expected, filterServicesNotUsingTheSameKongAnnotations(logger, tt.services, tt.annotations))
for _, expectedLogEntry := range tt.expectedLogEntries {
assert.Contains(t, stdout.String(), expectedLogEntry)
}
Expand Down

0 comments on commit c0846ee

Please sign in to comment.