Skip to content

Commit

Permalink
APIGOV-26621 fix PR comms
Browse files Browse the repository at this point in the history
  • Loading branch information
alrosca committed Nov 14, 2023
1 parent 690002c commit a181051
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion default_kong_discovery_agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ kong:
http: 80
https: 443
specDownloadPaths: [<Paths>]
specLocalPaths: [<Paths>]
specLocalPath: <Path>
6 changes: 3 additions & 3 deletions pkg/cmd/discovery/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
cfgKongProxyEpHttp = "kong.proxyEndpointProtocols.http"
cfgKongProxyEpHttps = "kong.proxyEndpointProtocols.https"
cfgKongSpecDownloadPaths = "kong.specDownloadPaths"
cfgKongSpecLocalPaths = "kong.specLocalPaths"
cfgKongSpecLocalPath = "kong.specLocalPath"
)

func init() {
Expand All @@ -42,7 +42,7 @@ func init() {
rootProps.AddIntProperty(cfgKongProxyEpHttp, 80, "The Kong proxy http port")
rootProps.AddIntProperty(cfgKongProxyEpHttps, 443, "The Kong proxy https port")
rootProps.AddStringSliceProperty(cfgKongSpecDownloadPaths, []string{}, "URL paths where the agent will look in for spec files")
rootProps.AddStringSliceProperty(cfgKongSpecLocalPaths, []string{}, "Local paths where the agent will look for spec files")
rootProps.AddStringProperty(cfgKongSpecLocalPath, "", "Local paths where the agent will look for spec files")
}

// Callback that agent will call to process the execution
Expand Down Expand Up @@ -86,7 +86,7 @@ func initConfig(centralConfig corecfg.CentralConfig) (interface{}, error) {
ProxyHttpPort: rootProps.IntPropertyValue(cfgKongProxyEpHttp),
ProxyHttpsPort: rootProps.IntPropertyValue(cfgKongProxyEpHttps),
SpecDownloadPaths: rootProps.StringSlicePropertyValue(cfgKongSpecDownloadPaths),
SpecLocalPaths: rootProps.StringSlicePropertyValue(cfgKongSpecLocalPaths),
SpecLocalPath: rootProps.StringPropertyValue(cfgKongSpecLocalPath),
}

agentConfig = config.AgentConfig{
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type KongGatewayConfig struct {
ProxyHttpPort int `config:"proxyHttpPort"`
ProxyHttpsPort int `config:"proxyHttpsPort"`
SpecDownloadPaths []string `config:"specDownloadPaths"`
SpecLocalPaths []string `config:"specLocalPaths"`
SpecLocalPath string `config:"specLocalPath"`
}

// ValidateCfg - Validates the gateway config
Expand Down
6 changes: 5 additions & 1 deletion pkg/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ func (gc *Client) processSingleKongService(ctx context.Context, service *klib.Se
return err
}

// TODO: check if spec is nil
// don't publish an empty spec
if kongServiceSpec == nil {
log.Debug("no spec found")
return nil
}

oasSpec := Openapi{
spec: string(kongServiceSpec),
Expand Down
38 changes: 19 additions & 19 deletions pkg/kong/kongclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type KongAPIClient interface {
CreateHttpBasic(ctx context.Context, consumerID string, basicAuth *klib.BasicAuth) (*klib.BasicAuth, error)
CreateOauth2(ctx context.Context, consumerID string, oauth2 *klib.Oauth2Credential) (*klib.Oauth2Credential, error)
CreateAuthKey(ctx context.Context, consumerID string, keyAuth *klib.KeyAuth) (*klib.KeyAuth, error)
// Access Request
// Access Request
AddRouteACL(ctx context.Context, routeID, allowedID string) error
RemoveRouteACL(ctx context.Context, routeID, revokedID string) error
AddQuota(ctx context.Context, routeID, allowedID, quotaInterval string, quotaLimit int) error
Expand All @@ -51,7 +51,7 @@ type KongClient struct {
baseClient DoRequest
kongAdminEndpoint string
specURLPaths []string
specLocalPaths []string
specLocalPath string
clientTimeout time.Duration
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func NewKongClient(baseClient *http.Client, kongConfig *config.KongGatewayConfig
baseClient: baseClient,
kongAdminEndpoint: kongConfig.AdminEndpoint,
specURLPaths: kongConfig.SpecDownloadPaths,
specLocalPaths: kongConfig.SpecLocalPaths,
specLocalPath: kongConfig.SpecLocalPath,
clientTimeout: 10 * time.Second,
}, nil
}
Expand All @@ -97,7 +97,7 @@ func (k KongClient) ListRoutesForService(ctx context.Context, serviceId string)
func (k KongClient) GetSpecForService(ctx context.Context, service *klib.Service) ([]byte, error) {
log := k.logger.WithField("serviceID", service.ID).WithField("serviceName", service.Name)

if len(k.specLocalPaths) > 0 {
if k.specLocalPath != "" {
return k.getSpecFromLocal(ctx, service)
}

Expand All @@ -122,32 +122,32 @@ func (k KongClient) getSpecFromLocal(ctx context.Context, service *klib.Service)
for _, tag := range service.Tags {
if strings.HasPrefix(*tag, tagPrefix) {
specTag = *tag
break
}
}

if len(specTag) > 0 {
filename := specTag[len(tagPrefix):]
for _, specPath := range k.specLocalPaths {
specFilePath := path.Join(specPath, filename)
specContent, err := k.loadSpecFile(specFilePath)
if err != nil {
log.WithError(err).Error("failed to get spec from file")
continue
}
return specContent, nil
}
if specTag == "" {
log.Info("no specification tag found")
return nil, nil
}

filename := specTag[len(tagPrefix):]
specFilePath := path.Join(k.specLocalPath, filename)
specContent, err := k.loadSpecFile(specFilePath)
if err != nil {
log.WithError(err).Error("failed to get spec from file")
return nil, err
}

log.Info("no specification tag found")
return []byte{}, nil
return specContent, nil
}

func (k KongClient) loadSpecFile(specFilePath string) ([]byte, error) {
log := k.logger.WithField("specFilePath", specFilePath)

if _, err := os.Stat(specFilePath); os.IsNotExist(err) {
log.Debug("spec file not found")
return []byte{}, nil
return nil, nil
}

data, err := os.ReadFile(specFilePath)
Expand Down Expand Up @@ -178,7 +178,7 @@ func (k KongClient) getSpecFromBackend(ctx context.Context, backendURL string) (
}

k.logger.Info("no spec found")
return []byte{}, nil
return nil, nil
}

func (k KongClient) getSpec(ctx context.Context, endpoint string) ([]byte, error) {
Expand Down

0 comments on commit a181051

Please sign in to comment.