Skip to content

Commit

Permalink
Handle nil error when port not defined in service with Gateway API. (#…
Browse files Browse the repository at this point in the history
…1230)

* Handle nil error when port not defined in service with Gateway API. Handle no method is specified in Route.

* Simplify backenRef in route.go
  • Loading branch information
battlebyte committed Mar 1, 2024
1 parent 0190a88 commit ce8628e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
24 changes: 22 additions & 2 deletions kong2kic/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,16 @@ 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,
},
}
if service.Port != nil {
portNumber := k8sgwapiv1.PortNumber(*service.Port)
backendRef.Port = &portNumber
}

var httpHeaderMatch []k8sgwapiv1.HTTPHeaderMatch
headerMatchExact := k8sgwapiv1.HeaderMatchExact
Expand Down Expand Up @@ -413,6 +416,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 ce8628e

Please sign in to comment.