Skip to content

Commit

Permalink
APIGOV-26727 - fix issues found in testing from testing (#53)
Browse files Browse the repository at this point in the history
* fix issues from testing

* stop deleting agent resource on stop

* remove commented code
  • Loading branch information
jcollins-axway authored Dec 11, 2023
1 parent 8077354 commit ff8c0fe
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 65 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ module github.com/Axway/agents-kong
go 1.18

require (
github.com/Axway/agent-sdk v1.1.69
github.com/Axway/agent-sdk v1.1.70-0.20231207222513-e111e500a79c
github.com/elastic/beats/v7 v7.17.15
github.com/google/uuid v1.3.1
github.com/kong/go-kong v0.47.0
github.com/mitchellh/mapstructure v1.5.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.16.0
)

require (
Expand Down Expand Up @@ -124,6 +123,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/urso/diag v0.0.0-20200210123136-21b3cc8eb797 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/Axway/agent-sdk v1.1.69 h1:k9YHhoXfGoZtBw6hJf3TrRNPdmLTY07eXewY5Odxx+M=
github.com/Axway/agent-sdk v1.1.69/go.mod h1:Iuv9KlWksVTbTKdfs4bKVYMDc33ZTLYoHt572z2CbbI=
github.com/Axway/agent-sdk v1.1.70-0.20231207222513-e111e500a79c h1:Ps/20eSKr6DcmSumAf/yE/KkffrofExw3D/e3lMtoO8=
github.com/Axway/agent-sdk v1.1.70-0.20231207222513-e111e500a79c/go.mod h1:Iuv9KlWksVTbTKdfs4bKVYMDc33ZTLYoHt572z2CbbI=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest/autorest v0.11.12/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
Expand Down
46 changes: 29 additions & 17 deletions pkg/discovery/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,18 @@ func (gc *Client) processSingleKongService(ctx context.Context, service *klib.Se
log.Warn("no spec found")
return nil
}
oasSpec := &Openapi{
spec: string(kongServiceSpec),
}

// parse the spec file that was found and get the spec processor
spec := apic.NewSpecResourceParser(kongServiceSpec, "")
spec.Parse()

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

func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, service *klib.Service, spec *Openapi) {
func (gc *Client) specPreparation(ctx context.Context, route *klib.Route, service *klib.Service, spec apic.SpecProcessor) {
log := gc.logger.WithField(common.AttrRouteID, *route.ID).
WithField(common.AttrServiceID, *service.ID)

Expand Down Expand Up @@ -225,11 +226,11 @@ func (gc *Client) processKongAPI(
ctx context.Context,
route *klib.Route,
service *klib.Service,
oasSpec *Openapi,
spec apic.SpecProcessor,
endpoints []apic.EndpointDefinition,
apiPlugins map[string]*klib.Plugin,
) (*apic.ServiceBody, error) {
kongAPI := newKongAPI(route, service, oasSpec, endpoints)
kongAPI := newKongAPI(route, service, spec, endpoints)
isAlreadyPublished, checksum := isPublished(&kongAPI, gc.cache)
// If true, then the api is published and there were no changes detected
if isAlreadyPublished {
Expand Down Expand Up @@ -266,18 +267,24 @@ func (gc *Client) processKongAPI(
func newKongAPI(
route *klib.Route,
service *klib.Service,
oasSpec *Openapi,
spec apic.SpecProcessor,
endpoints []apic.EndpointDefinition,
) KongAPI {
// strip any security from spec if it is an oas spec
resType := spec.GetResourceType()
if resType == apic.Oas2 || resType == apic.Oas3 {
spec.(apic.OasSpecProcessor).StripSpecAuth()
}

return KongAPI{
id: *service.Name,
name: *service.Name,
description: oasSpec.Description(),
version: oasSpec.Version(),
description: spec.GetDescription(),
version: spec.GetVersion(),
url: *service.Host,
resourceType: oasSpec.ResourceType(),
resourceType: resType,
documentation: []byte(*service.Name),
swaggerSpec: []byte(oasSpec.spec),
spec: spec.GetSpecBytes(),
endpoints: endpoints,
stage: *route.Name,
}
Expand All @@ -295,9 +302,9 @@ func (ka *KongAPI) buildServiceBody() (apic.ServiceBody, error) {
"GatewayType": "Kong API Gateway",
}

return apic.NewServiceBodyBuilder().
builder := apic.NewServiceBodyBuilder().
SetAPIName(ka.name).
SetAPISpec(ka.swaggerSpec).
SetAPISpec(ka.spec).
SetAPIUpdateSeverity(ka.apiUpdateSeverity).
SetDescription(ka.description).
SetDocumentation(ka.documentation).
Expand All @@ -308,15 +315,20 @@ func (ka *KongAPI) buildServiceBody() (apic.ServiceBody, error) {
SetServiceAgentDetails(util.MapStringStringToMapStringInterface(ka.agentDetails)).
SetServiceAttribute(serviceAttributes).
SetStage(ka.stage).
SetStageDescriptor("Route").
SetState(apic.PublishedStatus).
SetStatus(apic.PublishedStatus).
SetTags(tags).
SetTitle(ka.name).
SetURL(ka.url).
SetVersion(ka.version).
SetServiceEndpoints(ka.endpoints).
SetAccessRequestDefinitionName(ka.ard, false).
SetCredentialRequestDefinitions(ka.crds).Build()
SetServiceEndpoints(ka.endpoints)

if len(ka.crds) > 0 {
return builder.SetAccessRequestDefinitionName(ka.ard, false).
SetCredentialRequestDefinitions(ka.crds).Build()
}
return builder.SetAuthPolicy(apic.Passthrough).Build()
}

// makeChecksum generates a makeChecksum for the api for change detection
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/gateway/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Client struct {
}

type KongAPI struct {
swaggerSpec []byte
spec []byte
id string
name string
description string
Expand Down
36 changes: 0 additions & 36 deletions pkg/discovery/gateway/openapi.go

This file was deleted.

6 changes: 6 additions & 0 deletions pkg/discovery/main/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (

func main() {
os.Setenv("AGENTFEATURES_VERSIONCHECKER", "false")

// update to set the default pattern for kong discovery
pattern := os.Getenv("CENTRAL_APISERVICEREVISIONPATTERN")
if pattern == "" {
os.Setenv("CENTRAL_APISERVICEREVISIONPATTERN", "{{.APIServiceName}} - {{.Date:YYYY/MM/DD}} - r {{.Revision}}")
}
if err := discovery.DiscoveryCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
7 changes: 0 additions & 7 deletions pkg/traceability/beater/beater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"sync"

"github.com/elastic/beats/v7/libbeat/beat"
Expand Down Expand Up @@ -110,12 +109,6 @@ func (b *httpLogBeater) shutdownHandler() {
// publish the metrics and usage
b.logger.Info("publishing cached metrics and usage")
metric.GetMetricCollector().ShutdownPublish()

// remove the agent resource in k8s clusters
pod_name := os.Getenv("POD_NAME")
if pod_name != "" {
agent.GetCentralClient().DeleteResourceInstance(agent.GetAgentResource())
}
}

// Stop stops kong_traceability_agent.
Expand Down

0 comments on commit ff8c0fe

Please sign in to comment.