You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在kubeconfig的exec命令中调用tginfo获取token_generator路径?

问题描述

我的kubeconfig中有如下exec配置:

exec:
  apiVersion: client.authentication.k8s.io/v1
  command: PATH_DETERMINED_VIA_BINARY/token_generator
  args:
    - --ACCESS_TOKEN
  interactiveMode: Never
  provideClusterInfo: false

其中PATH_DETERMINED_VIA_BINARY需要通过执行二进制工具tginfo获取,执行命令为:

sudo tginfo path token  # 该命令会返回路径(例如/usr/lib/000012/)

此路径下包含上述command字段中用到的token_generator二进制文件。请问如何调用tginfo获取该路径并用于exec的command字段中?

解决方法

方法1:Shell脚本包装执行逻辑

创建一个名为token_wrapper.sh的脚本,内容如下:

#!/bin/bash
# 获取token_generator所在路径
TG_PATH=$(sudo tginfo path token)
# 执行token_generator并传递参数
exec "$TG_PATH/token_generator" --ACCESS_TOKEN

给脚本添加执行权限:

chmod +x token_wrapper.sh

修改kubeconfig的exec配置,将command指向这个脚本:

exec:
  apiVersion: client.authentication.k8s.io/v1
  command: /绝对路径/to/token_wrapper.sh
  interactiveMode: Never
  provideClusterInfo: false

注:若需要动态传递参数,可调整脚本接收kubeconfig传入的args。

方法2:直接用bash -c拼接命令

无需额外脚本,直接在kubeconfig中通过bash执行拼接逻辑:

exec:
  apiVersion: client.authentication.k8s.io/v1
  command: bash
  args:
    - -c
    - 'sudo tginfo path token | xargs -I {} {}token_generator --ACCESS_TOKEN'
  interactiveMode: Never
  provideClusterInfo: false

原理:xargs -I {}会把tginfo返回的路径替换到{}位置,进而执行对应路径下的token_generator。

方法3:静态替换路径(适合路径不常变更场景)

先手动执行命令获取路径:

TG_PATH=$(sudo tginfo path token)

然后直接将kubeconfig中的PATH_DETERMINED_VIA_BINARY替换为实际路径:

exec:
  apiVersion: client.authentication.k8s.io/v1
  command: /usr/lib/000012/token_generator
  args:
    - --ACCESS_TOKEN
  interactiveMode: Never
  provideClusterInfo: false

这种方式操作最简单,但路径更新后需要重新执行替换。

内容的提问来源于stack exchange,提问作者James Franco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:42:49