Skip to content

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

Merged
merged 16 commits into from
Mar 25, 2025

Conversation

Baoyuantop
Copy link
Contributor

@Baoyuantop Baoyuantop commented Mar 17, 2025

Description

Automatically reads the latest content from the token file before the program reads the token value, ensuring that the latest token is used for every request.

Fixes #11779

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request labels Mar 17, 2025
Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. it is not a new feature, it should be a bug, pls update your title
  2. we should use cache, eg: 1hour or 1day, and then we can refresh it, this is more acceptable

local function start_fetch(handle)
local timer_runner
timer_runner = function(premature)
if premature then
return
end

if handle.token_file then
refresh_token(handle, handle.token_file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad name

cache -> refresh: this is a good name
no cache -> refresh: it is weird

it should be name to read token or fetch token

@membphis membphis changed the title feat: add kubernetes service discovery token refresh bugfix: add kubernetes service discovery token refresh Mar 19, 2025
@Baoyuantop Baoyuantop changed the title bugfix: add kubernetes service discovery token refresh fix: timed read kubernetes service discovery token Mar 19, 2025
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Mar 19, 2025
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Mar 20, 2025
end

-- remove possible extra whitespace
local trimmed_token = token:gsub("%s+", "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting


local token, err = read_token(token_file_path)
if err then
return nil, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only can choose one of them:

if not attributes then
        core.log.error("failed to fetch ", token_file_path, " attributes: ", err)
        return
    end
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use log.infolog.notice is suitable


local function update_token(handle)
if not handle.apiserver.token_file or handle.apiserver.token_file == "" then
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for succ or fail, we should return different value

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Mar 24, 2025
@dosubot dosubot bot removed the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 24, 2025
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 24, 2025
@Baoyuantop Baoyuantop changed the title fix: timed read kubernetes service discovery token fix: auto read kubernetes service discovery token Mar 24, 2025
@Baoyuantop Baoyuantop requested review from membphis and nic-6443 March 24, 2025 12:01
nic-6443
nic-6443 previously approved these changes Mar 25, 2025
@Baoyuantop Baoyuantop merged commit 861a6ff into apache:master Mar 25, 2025
30 checks passed
@Baoyuantop Baoyuantop deleted the fix-token-fetch branch March 25, 2025 09:51
Crazy-xyr pushed a commit to Crazy-xyr/apisix that referenced this pull request Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: apisix with kubernetes discovery will fail after token file expires
5 participants