diff --git a/default_kong_discovery_agent.yml b/default_kong_discovery_agent.yml index 526aede..a7bcde1 100644 --- a/default_kong_discovery_agent.yml +++ b/default_kong_discovery_agent.yml @@ -29,4 +29,4 @@ kong: http: 80 https: 443 specDownloadPaths: [] - specLocalPaths: [] + specLocalPath: diff --git a/pkg/cmd/discovery/cmd.go b/pkg/cmd/discovery/cmd.go index ef10e22..7d245aa 100644 --- a/pkg/cmd/discovery/cmd.go +++ b/pkg/cmd/discovery/cmd.go @@ -20,7 +20,7 @@ const ( cfgKongProxyEpHttp = "kong.proxyEndpointProtocols.http" cfgKongProxyEpHttps = "kong.proxyEndpointProtocols.https" cfgKongSpecDownloadPaths = "kong.specDownloadPaths" - cfgKongSpecLocalPaths = "kong.specLocalPaths" + cfgKongSpecLocalPath = "kong.specLocalPath" ) func init() { @@ -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 @@ -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{ diff --git a/pkg/config/discovery/config.go b/pkg/config/discovery/config.go index b530042..0225aa0 100644 --- a/pkg/config/discovery/config.go +++ b/pkg/config/discovery/config.go @@ -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 diff --git a/pkg/gateway/client.go b/pkg/gateway/client.go index b3f896d..b1e8c05 100644 --- a/pkg/gateway/client.go +++ b/pkg/gateway/client.go @@ -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), diff --git a/pkg/kong/kongclient.go b/pkg/kong/kongclient.go index 08da25a..1632697 100644 --- a/pkg/kong/kongclient.go +++ b/pkg/kong/kongclient.go @@ -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 @@ -51,7 +51,7 @@ type KongClient struct { baseClient DoRequest kongAdminEndpoint string specURLPaths []string - specLocalPaths []string + specLocalPath string clientTimeout time.Duration } @@ -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 } @@ -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) } @@ -122,24 +122,24 @@ 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) { @@ -147,7 +147,7 @@ func (k KongClient) loadSpecFile(specFilePath string) ([]byte, error) { 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) @@ -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) {