Skip to content

Commit

Permalink
fix(parser) populate Service port in services (#4503)
Browse files Browse the repository at this point in the history
* fix(parser) populate Service port in services

When generating Kong services, use the Service port from the Ingress
backend instead of the default HTTP port.

This is a cosmetic change. These ports are not actually used by the
routing engine, as all KIC-generated services use upstreams and the
ports associated with their targets (which are the target ports on the
Service's Endpoints).

* chore(parser) update golden tests
  • Loading branch information
rainest committed Aug 15, 2023
1 parent 688acf8 commit e02d8ee
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ Adding a new version? You'll need three changes:

## Unreleased

Nothing yet.
### Fixed

- Display Service ports on generated Kong services, instead of a static default
value. This change is cosmetic only.
[#4503](https://github.com/Kong/kubernetes-ingress-controller/pull/4503)

## [2.11.0]

Expand Down
18 changes: 12 additions & 6 deletions internal/dataplane/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/blang/semver/v4"
"github.com/kong/go-kong/kong"
"github.com/samber/lo"
"github.com/samber/mo"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -212,6 +213,12 @@ func (p *Parser) BuildKongConfig() KongConfigBuildingResult {

// add the routes and services to the state
var result kongstate.KongState

// generate Upstreams and Targets from service defs
// update ServiceNameToServices with resolved ports (translating any name references to their number, as Kong
// services require a number)
result.Upstreams, ingressRules.ServiceNameToServices = p.getUpstreams(ingressRules.ServiceNameToServices)

for key, service := range ingressRules.ServiceNameToServices {
// if the service doesn't need to be skipped, then add it to the
// list of services.
Expand All @@ -220,9 +227,6 @@ func (p *Parser) BuildKongConfig() KongConfigBuildingResult {
}
}

// generate Upstreams and Targets from service defs
result.Upstreams = p.getUpstreams(ingressRules.ServiceNameToServices)

// merge KongIngress with Routes, Services and Upstream
result.FillOverrides(p.logger, p.storer)

Expand Down Expand Up @@ -379,11 +383,11 @@ func findPort(svc *corev1.Service, wantPort kongstate.PortDef) (*corev1.ServiceP
return nil, fmt.Errorf("no suitable port found")
}

func (p *Parser) getUpstreams(serviceMap map[string]kongstate.Service) []kongstate.Upstream {
func (p *Parser) getUpstreams(serviceMap map[string]kongstate.Service) ([]kongstate.Upstream, map[string]kongstate.Service) {
upstreamDedup := make(map[string]struct{}, len(serviceMap))
var empty struct{}
upstreams := make([]kongstate.Upstream, 0, len(serviceMap))
for _, service := range serviceMap {
for serviceName, service := range serviceMap {
// the name of the Upstream for a service must match the service.Host
// as the Gateway's internal DNS resolve mechanisms will fail to properly
// resolve the host otherwise.
Expand Down Expand Up @@ -420,6 +424,8 @@ func (p *Parser) getUpstreams(serviceMap map[string]kongstate.Service) []kongsta
)
continue
}
service.Port = lo.ToPtr(int(port.Port))
serviceMap[serviceName] = service

// get the new targets for this backend service
newTargets := getServiceEndpoints(p.logger, p.storer, k8sService, port)
Expand Down Expand Up @@ -477,7 +483,7 @@ func (p *Parser) getUpstreams(serviceMap map[string]kongstate.Service) []kongsta
upstreamDedup[name] = empty
}
}
return upstreams
return upstreams, serviceMap
}

func getCertFromSecret(secret *corev1.Secret) (string, string, error) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e02d8ee

Please sign in to comment.