Skip to content

Commit

Permalink
fix: support upstream_id & consumer with grpc (#3387)
Browse files Browse the repository at this point in the history
Fix #1646.
  • Loading branch information
spacewander authored Jan 21, 2021
1 parent 64029e0 commit c030783
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 68 deletions.
74 changes: 6 additions & 68 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,6 @@ function _M.http_access_phase()
core.log.info("matched route: ",
core.json.delay_encode(api_ctx.matched_route, true))

if route.value.service_protocol == "grpc" then
return ngx.exec("@grpc_pass")
end

local enable_websocket = route.value.enable_websocket
if route.value.service_id then
local service = service_fetch(route.value.service_id)
Expand Down Expand Up @@ -523,6 +519,11 @@ function _M.http_access_phase()

set_upstream_host(api_ctx)

if route.value.service_protocol == "grpc" then
ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
return ngx.exec("@grpc_pass")
end

if api_ctx.dubbo_proxy_enabled then
ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
return ngx.exec("@dubbo_pass")
Expand All @@ -536,70 +537,7 @@ end


function _M.grpc_access_phase()
local ngx_ctx = ngx.ctx
local api_ctx = ngx_ctx.api_ctx

if not api_ctx then
api_ctx = core.tablepool.fetch("api_ctx", 0, 32)
ngx_ctx.api_ctx = api_ctx
end

core.ctx.set_vars_meta(api_ctx)

router.router_http.match(api_ctx)

core.log.info("route: ",
core.json.delay_encode(api_ctx.matched_route, true))

local route = api_ctx.matched_route
if not route then
return core.response.exit(404)
end

if route.value.service_id then
-- core.log.info("matched route: ", core.json.delay_encode(route.value))
local service = service_fetch(route.value.service_id)
if not service then
core.log.error("failed to fetch service configuration by ",
"id: ", route.value.service_id)
return core.response.exit(404)
end

local changed
route, changed = plugin.merge_service_route(service, route)
api_ctx.matched_route = route

if changed then
api_ctx.conf_type = "route&service"
api_ctx.conf_version = route.modifiedIndex .. "&"
.. service.modifiedIndex
api_ctx.conf_id = route.value.id .. "&"
.. service.value.id
else
api_ctx.conf_type = "service"
api_ctx.conf_version = service.modifiedIndex
api_ctx.conf_id = service.value.id
end

else
api_ctx.conf_type = "route"
api_ctx.conf_version = route.modifiedIndex
api_ctx.conf_id = route.value.id
end

-- todo: support upstream id

api_ctx.matched_upstream = (route.dns_value and
route.dns_value.upstream)
or route.value.upstream

local plugins = core.tablepool.fetch("plugins", 32, 0)
api_ctx.plugins = plugin.filter(route, plugins)

run_plugin("rewrite", plugins, api_ctx)
run_plugin("access", plugins, api_ctx)

set_upstream(route, api_ctx)
ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
end


Expand Down
97 changes: 97 additions & 0 deletions t/node/grpc-proxy.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

# As the test framework doesn't support sending grpc request, this
# test file is only for grpc irrelative configuration check.
# To avoid confusion, we configure a closed port so if th configuration works,
# the result will be `connect refused`.
repeat_each(1);
log_level('info');
no_root_location();
no_shuffle();

add_block_preprocessor(sub {
my ($block) = @_;

my $yaml_config = $block->yaml_config // <<_EOC_;
apisix:
node_listen: 1984
config_center: yaml
enable_admin: false
_EOC_

$block->set_value("yaml_config", $yaml_config);

if (!$block->request) {
$block->set_value("request", "POST /hello");
}
});

run_tests();

__DATA__
=== TEST 1: with upstream_id
--- apisix_yaml
upstreams:
- id: 1
type: roundrobin
nodes:
"127.0.0.1:9088": 1
routes:
- id: 1
methods:
- POST
service_protocol: grpc
uri: "/hello"
upstream_id: 1
#END
--- error_code: 502
--- error_log
proxy request to 127.0.0.1:9088
=== TEST 2: with consummer
--- apisix_yaml
consumers:
- username: jack
id: jack
plugins:
key-auth:
key: user-key
#END
routes:
- id: 1
methods:
- POST
service_protocol: grpc
uri: "/hello"
plugins:
key-auth:
consumer-restriction:
whitelist:
- jack
upstream:
type: roundrobin
nodes:
"127.0.0.1:9088": 1
#END
--- more_headers
apikey: user-key
--- error_code: 502

0 comments on commit c030783

Please sign in to comment.