-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: auto read kubernetes service discovery token #12057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2180a35
29cf505
8e306e8
1f09891
cf153b1
325588b
6c8625b
679365d
178731f
f6f87f3
ce18076
ffca30d
7d9466e
0184ff4
5a6a786
11218bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ local core = require("apisix.core") | |
| local util = require("apisix.cli.util") | ||
| local local_conf = require("apisix.core.config_local").local_conf() | ||
| local informer_factory = require("apisix.discovery.kubernetes.informer_factory") | ||
| local lfs = require("lfs") | ||
|
|
||
|
|
||
| local ctx | ||
|
|
@@ -283,6 +284,45 @@ local function read_env(key) | |
| return key | ||
| end | ||
|
|
||
| local function read_token(token_file) | ||
| local token, err = util.read_file(token_file) | ||
| if err then | ||
| return nil, err | ||
| end | ||
|
|
||
| -- remove possible extra whitespace | ||
| local trimmed_token = token:gsub("%s+", "") | ||
| return trimmed_token | ||
| end | ||
|
|
||
|
|
||
| local function update_token(handle) | ||
| if not handle.apiserver.token_file or handle.apiserver.token_file == "" then | ||
| return | ||
|
||
| end | ||
|
|
||
| local token_file_path = handle.apiserver.token_file | ||
| local attributes, err = lfs.attributes(token_file_path) | ||
| if not attributes then | ||
| core.log.error("failed to fetch ", token_file_path, " attributes: ", err) | ||
| return | ||
| end | ||
|
|
||
| local last_modification_time = attributes.modification | ||
| if handle.token_file_mtime == last_modification_time then | ||
| return | ||
| end | ||
|
|
||
| local token, err = read_token(token_file_path) | ||
| if err then | ||
| return nil, err | ||
|
||
| end | ||
|
|
||
| handle.apiserver.token = token | ||
| handle.token_file_mtime = last_modification_time | ||
| core.log.warn("kubernetes service account token has been updated") | ||
|
||
| end | ||
|
|
||
|
|
||
| local function get_apiserver(conf) | ||
| local apiserver = { | ||
|
|
@@ -324,23 +364,21 @@ local function get_apiserver(conf) | |
| return nil, err | ||
| end | ||
| elseif conf.client.token_file and conf.client.token_file ~= "" then | ||
| local file | ||
| file, err = read_env(conf.client.token_file) | ||
| local token_file, err = read_env(conf.client.token_file) | ||
| if err then | ||
| return nil, err | ||
| end | ||
|
|
||
| apiserver.token, err = util.read_file(file) | ||
| apiserver.token, err = read_token(token_file) | ||
| if err then | ||
| return nil, err | ||
| end | ||
|
|
||
| apiserver.token_file = token_file | ||
| else | ||
| return nil, "one of [client.token,client.token_file] should be set but none" | ||
| end | ||
|
|
||
| -- remove possible extra whitespace | ||
nic-6443 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| apiserver.token = apiserver.token:gsub("%s+", "") | ||
|
|
||
| if apiserver.schema == "https" and apiserver.token == "" then | ||
| return nil, "apiserver.token should set to non-empty string when service.schema is https" | ||
| end | ||
|
|
@@ -379,6 +417,8 @@ local function start_fetch(handle) | |
| return | ||
| end | ||
|
|
||
| update_token(handle) | ||
|
|
||
| local ok, status = pcall(handle.list_watch, handle, handle.apiserver) | ||
|
|
||
| local retry_interval = 0 | ||
|
|
@@ -459,7 +499,8 @@ local function single_mode_init(conf) | |
| ctx = setmetatable({ | ||
| endpoint_dict = endpoint_dict, | ||
| apiserver = apiserver, | ||
| default_weight = default_weight | ||
| default_weight = default_weight, | ||
| token_file_mtime = nil | ||
| }, { __index = endpoints_informer }) | ||
|
|
||
| start_fetch(ctx) | ||
|
|
@@ -565,7 +606,8 @@ local function multiple_mode_init(confs) | |
| ctx[id] = setmetatable({ | ||
| endpoint_dict = endpoint_dict, | ||
| apiserver = apiserver, | ||
| default_weight = default_weight | ||
| default_weight = default_weight, | ||
| token_file_mtime = nil | ||
| }, { __index = endpoints_informer }) | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
waiting