Skip to content

Commit

Permalink
use multiple routes as stages
Browse files Browse the repository at this point in the history
  • Loading branch information
dgghinea committed Nov 28, 2023
1 parent 4b2285c commit 5595da4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
58 changes: 28 additions & 30 deletions pkg/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,28 @@ func (gc *Client) processSingleKongService(ctx context.Context, service *klib.Se
log.WithError(err).Errorf("failed to get routes for service")
return err
}
kongServiceSpec, err := gc.kongClient.GetSpecForService(ctx, service)
if err != nil {
log.WithError(err).Errorf("failed to get spec for service")
return err
}

// don't publish an empty spec
if kongServiceSpec == nil {
log.Warn("no spec found")
return nil
}
oasSpec := &Openapi{
spec: string(kongServiceSpec),
}

for _, route := range routes {
gc.specPreparation(ctx, route, service)
gc.specPreparation(ctx, route, service, oasSpec)
}
return nil
}

func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, service *klib.Service) {
func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, service *klib.Service, spec *Openapi) {
log := gc.logger.WithField(common.AttrRouteID, *route.ID).
WithField(common.AttrServiceID, *service.ID)
proxyHost := gc.kongGatewayCfg.Proxy.Host
Expand All @@ -133,21 +147,8 @@ func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, servic
return
}

kongServiceSpec, err := gc.kongClient.GetSpecForService(ctx, service, route)
if err != nil {
return
}
// don't publish an empty spec
if kongServiceSpec == nil {
log.Warn("no spec found")
return
}
oasSpec := Openapi{
spec: string(kongServiceSpec),
}

endpoints := gc.processKongRoute(proxyHost, oasSpec.BasePath(), route, httpPort, httpsPort)
serviceBody, err := gc.processKongAPI(ctx, *route.ID, service, oasSpec, endpoints, apiPlugins)
endpoints := gc.processKongRoute(proxyHost, route, httpPort, httpsPort)
serviceBody, err := gc.processKongAPI(ctx, route, service, spec, endpoints, apiPlugins)
if err != nil {
log.WithError(err).Error("failed to process kong API")
return
Expand All @@ -166,7 +167,7 @@ func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, servic
log.Info("Successfully published to central")
}

func (gc *Client) processKongRoute(defaultHost string, basePath string, route *klib.Route, httpPort, httpsPort int) []apic.EndpointDefinition {
func (gc *Client) processKongRoute(defaultHost string, route *klib.Route, httpPort, httpsPort int) []apic.EndpointDefinition {
var endpoints []apic.EndpointDefinition
if route == nil {
return endpoints
Expand All @@ -183,15 +184,11 @@ func (gc *Client) processKongRoute(defaultHost string, basePath string, route *k
port = httpsPort
}

routingBasePath := *path
if *route.StripPath {
routingBasePath = routingBasePath + basePath
}
endpoint := apic.EndpointDefinition{
Host: *host,
Port: int32(port),
Protocol: *protocol,
BasePath: routingBasePath,
BasePath: *path,
}
endpoints = append(endpoints, endpoint)
}
Expand All @@ -203,13 +200,13 @@ func (gc *Client) processKongRoute(defaultHost string, basePath string, route *k

func (gc *Client) processKongAPI(
ctx context.Context,
routeID string,
route *klib.Route,
service *klib.Service,
oasSpec Openapi,
oasSpec *Openapi,
endpoints []apic.EndpointDefinition,
apiPlugins map[string]*klib.Plugin,
) (*apic.ServiceBody, error) {
kongAPI := newKongAPI(routeID, service, oasSpec, endpoints)
kongAPI := newKongAPI(route, service, oasSpec, endpoints)
isAlreadyPublished, checksum := isPublished(&kongAPI, gc.cache)
// If true, then the api is published and there were no changes detected
if isAlreadyPublished {
Expand All @@ -231,7 +228,7 @@ func (gc *Client) processKongAPI(

agentDetails := map[string]string{
common.AttrServiceID: *service.ID,
common.AttrRouteID: routeID,
common.AttrRouteID: *route.ID,
common.AttrChecksum: checksum,
}
kongAPI.agentDetails = agentDetails
Expand All @@ -244,13 +241,13 @@ func (gc *Client) processKongAPI(
}

func newKongAPI(
routeID string,
route *klib.Route,
service *klib.Service,
oasSpec Openapi,
oasSpec *Openapi,
endpoints []apic.EndpointDefinition,
) KongAPI {
return KongAPI{
id: routeID,
id: *route.ID,
name: *service.Name,
description: oasSpec.Description(),
version: oasSpec.Version(),
Expand All @@ -259,6 +256,7 @@ func newKongAPI(
documentation: []byte(*service.Name),
swaggerSpec: []byte(oasSpec.spec),
endpoints: endpoints,
stage: *route.Name,
}
}

Expand Down
18 changes: 8 additions & 10 deletions pkg/kong/kongclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type KongAPIClient interface {

ListServices(ctx context.Context) ([]*klib.Service, error)
ListRoutesForService(ctx context.Context, serviceId string) ([]*klib.Route, error)
GetSpecForService(ctx context.Context, service *klib.Service, route *klib.Route) ([]byte, error)
GetSpecForService(ctx context.Context, service *klib.Service) ([]byte, error)
GetKongPlugins() *Plugins
}

Expand Down Expand Up @@ -106,12 +106,11 @@ func (k KongClient) ListRoutesForService(ctx context.Context, serviceId string)
return routes, err
}

func (k KongClient) GetSpecForService(ctx context.Context, service *klib.Service, route *klib.Route) ([]byte, error) {
log := k.logger.WithField(common.AttrServiceName, *service.Name).
WithField(common.AttrRouteName, *route.Name)
func (k KongClient) GetSpecForService(ctx context.Context, service *klib.Service) ([]byte, error) {
log := k.logger.WithField(common.AttrServiceName, *service.Name)

if k.specLocalPath != "" {
return k.getSpecFromLocal(ctx, service, route)
return k.getSpecFromLocal(ctx, service)
}

if k.devPortalEnabled {
Expand All @@ -132,20 +131,19 @@ func (k KongClient) GetSpecForService(ctx context.Context, service *klib.Service
return k.getSpecFromBackend(ctx, backendURL)
}

func (k KongClient) getSpecFromLocal(ctx context.Context, service *klib.Service, route *klib.Route) ([]byte, error) {
log := k.logger.WithField(common.AttrServiceName, *service.Name).
WithField(common.AttrRouteName, *route.Name)
func (k KongClient) getSpecFromLocal(ctx context.Context, service *klib.Service) ([]byte, error) {
log := k.logger.WithField(common.AttrServiceName, *service.Name)

specTag := ""
for _, tag := range route.Tags {
for _, tag := range service.Tags {
if strings.HasPrefix(*tag, tagPrefix) {
specTag = *tag
break
}
}

if specTag == "" {
log.Error("In order to map local specs to the desired routes, a tag with format 'spec_local_fileName.extension' must be present")
log.Error("in order to map local specs to the desired services, a tag with format 'spec_local_fileName.extension' must be present")
return nil, errors.New("No specification tag found.")
}

Expand Down

0 comments on commit 5595da4

Please sign in to comment.