From 511669a8c0ae4d3c4c12ae8be304720babf33f2d Mon Sep 17 00:00:00 2001 From: An Tran Date: Thu, 22 Feb 2024 11:37:13 +1000 Subject: [PATCH] Logs: disable invalid upstream warning when api_backend is null and empty --- CHANGELOG.md | 2 ++ gateway/src/apicast/proxy.lua | 4 +++- spec/proxy_spec.lua | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4df35d515..df6a65622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Replace luafilesystem-ffi with [luafilesystem](https://github.com/lunarmodules/luafilesystem) [PR #1445](https://github.com/3scale/APIcast/pull/1445) [THREESCALE-10662](https://issues.redhat.com/browse/THREESCALE-10662) +- Fix "Upstream cannot be null" error in APIcast logs [PR #1449](https://github.com/3scale/APIcast/pull/1449) [THREESCALE-5225](https://issues.redhat.com/browse/THREESCALE-5225) + ### Added - Detect number of CPU shares when running on Cgroups V2 [PR #1410](https://github.com/3scale/apicast/pull/1410) [THREESCALE-10167](https://issues.redhat.com/browse/THREESCALE-10167) diff --git a/gateway/src/apicast/proxy.lua b/gateway/src/apicast/proxy.lua index 2c6168a0d..a2197b4ec 100644 --- a/gateway/src/apicast/proxy.lua +++ b/gateway/src/apicast/proxy.lua @@ -14,6 +14,7 @@ local Usage = require('apicast.usage') local errors = require('apicast.errors') local Upstream = require('apicast.upstream') local escape = require("resty.http.uri_escape") +local cjson = require('cjson') local assert = assert local type = type @@ -175,7 +176,8 @@ function _M.get_upstream(service, context) -- Due to API as a product, the api_backend is no longer needed because this -- can be handled by routing policy - if not service.api_backend then + local api_backend = service.api_backend + if not api_backend or api_backend == cjson.null or api_backend == '' then return nil, nil end diff --git a/spec/proxy_spec.lua b/spec/proxy_spec.lua index e99a114e6..4f6211a00 100644 --- a/spec/proxy_spec.lua +++ b/spec/proxy_spec.lua @@ -1,5 +1,6 @@ local http_ng_response = require('resty.http_ng.response') local lrucache = require('resty.lrucache') +local cjson = require('cjson') local configuration_store = require 'apicast.configuration_store' local Service = require 'apicast.configuration.service' @@ -64,6 +65,18 @@ describe('Proxy', function() assert.falsy(err) end) + it("on no api_backend return empty string and no error", function() + local upstream, err = get_upstream({api_backend = ''}) + assert.falsy(upstream) + assert.falsy(err) + end) + + it("on no api_backend return null and no error", function() + local upstream, err = get_upstream({api_backend = cjson.null}) + assert.falsy(upstream) + assert.falsy(err) + end) + end) describe('.authorize', function()