Skip to content

Commit

Permalink
Handle nil error when port not defined in service with Gateway API. H…
Browse files Browse the repository at this point in the history
…andle no method is specified in Route.
  • Loading branch information
battlebyte committed Feb 29, 2024
1 parent 0190a88 commit 3f3c9cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 32 additions & 6 deletions kong2kic/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,21 @@ func populateKICIngressesWithGatewayAPI(content *file.Content, kicContent *KICCo
})

// add service details to HTTPBackendRef
portNumber := k8sgwapiv1.PortNumber(*service.Port)
backendRef := k8sgwapiv1.BackendRef{
BackendObjectReference: k8sgwapiv1.BackendObjectReference{
Name: k8sgwapiv1.ObjectName(*service.Name),
Port: &portNumber,
},
var backendRef k8sgwapiv1.BackendRef
if service.Port != nil {
portNumber := k8sgwapiv1.PortNumber(*service.Port)
backendRef = k8sgwapiv1.BackendRef{
BackendObjectReference: k8sgwapiv1.BackendObjectReference{
Name: k8sgwapiv1.ObjectName(*service.Name),
Port: &portNumber,
},
}
} else {
backendRef = k8sgwapiv1.BackendRef{
BackendObjectReference: k8sgwapiv1.BackendObjectReference{
Name: k8sgwapiv1.ObjectName(*service.Name),
},
}
}

var httpHeaderMatch []k8sgwapiv1.HTTPHeaderMatch
Expand Down Expand Up @@ -413,6 +422,23 @@ func populateKICIngressesWithGatewayAPI(content *file.Content, kicContent *KICCo
httpPathMatch.Value = path
}

// if no method is specified, add a httpRouteRule to the httpRoute with headers and path
if route.Methods == nil {
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, k8sgwapiv1.HTTPRouteRule{
Matches: []k8sgwapiv1.HTTPRouteMatch{
{
Path: &httpPathMatch,
Headers: httpHeaderMatch,
},
},
BackendRefs: []k8sgwapiv1.HTTPBackendRef{
{
BackendRef: backendRef,
},
},
})
}

for _, method := range route.Methods {
httpMethod := k8sgwapiv1.HTTPMethod(*method)
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, k8sgwapiv1.HTTPRouteRule{
Expand Down
4 changes: 2 additions & 2 deletions kong2kic/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func populateKICUpstreamPolicy(
return
}

k8sservice.ObjectMeta.Annotations["konghq.com/upstream-policy"] = kongUpstreamPolicy.ObjectMeta.Name

// Find the upstream (if any) whose name matches the service host and copy the upstream
// into kongUpstreamPolicy. Append the kongUpstreamPolicy to kicContent.KongUpstreamPolicies.
found := false
for _, upstream := range content.Upstreams {
if upstream.Name != nil && strings.EqualFold(*upstream.Name, *service.Host) {
found = true
// add an annotation to the k8sservice to link this kongUpstreamPolicy to it
k8sservice.ObjectMeta.Annotations["konghq.com/upstream-policy"] = kongUpstreamPolicy.ObjectMeta.Name
var threshold int
if upstream.Healthchecks != nil && upstream.Healthchecks.Threshold != nil {
threshold = int(*upstream.Healthchecks.Threshold)
Expand Down

0 comments on commit 3f3c9cf

Please sign in to comment.