Skip to content

Commit

Permalink
tokenFile 可配置
Browse files Browse the repository at this point in the history
  • Loading branch information
sevennt committed May 10, 2024
1 parent de6da20 commit c2f3c3c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
37 changes: 28 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,45 @@
## 3 K8S配置
```go
type Config struct {
Addr string // 地址
Debug bool // 调试信息
Token string // token信息
Namespaces []string // 命名空间列表
DeploymentPrefix string // 前缀
TLSClientConfigInsecure bool // 是否开启tls
// Addr k8s API Server 地址
Addr string
// Debug 是否开启debug模式
Debug bool
// Token k8s API Server 请求token
Token string
// Token k8s API Server 请求token file
// 本地运行时需要将 tokenFile 参数显式的设置为 ""
TokenFile string
// Namespaces 需要进行查询和监听的 Namespace 列表
Namespaces []string
// DeploymentPrefix 命名前缀
DeploymentPrefix string
// TLSClientConfigInsecure 是否启用 TLS
TLSClientConfigInsecure bool
}
```

## 4 默认配置
* host: KUBERNETES_SERVICE_HOST 环境变量
* port: KUBERNETES_SERVICE_PORT 环境变量
* token: /var/run/secrets/kubernetes.io/serviceaccount/token 文件路径
* token: /var/run/secrets/kubernetes.io/serviceaccount/token 文件中值
* tokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token 文件路径

## 5 根据K8S信息,调用gRPC
### 5.1 K8S配置
```toml
[k8s]
addr=""
token=""
# k8s API Server 地址
addr="https://$API_SERVER_ADDR"
# Debug 是否开启debug模式
debug = true
# Token k8s API Server 请求token
token="$API_SERVER_TOKEN"
# Token k8s API Server 请求token file
# 本地运行时:需要显式设置 tokenFile = ""
# 集群模式下运行时:需要 tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" 或释掉 tokenFile 这个 key
tokenFile = ""
# Namespaces 需要进行查询和监听的 Namespace 列表
namespaces=["default"]

[grpc.test]
Expand Down
29 changes: 20 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ import (
"k8s.io/client-go/rest"
)

// Config ...
// Config 定义了ek8s组件配置结构
type Config struct {
Addr string
Debug bool
Token string
Namespaces []string
DeploymentPrefix string // 命名前缀
// Addr k8s API Server 地址
Addr string
// Debug 是否开启debug模式
Debug bool
// Token k8s API Server 请求token
Token string
// Token k8s API Server 请求token file
// 本地运行时:一定需要显式设置 tokenFile = ""
// 集群模式下运行时:需要 tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" 或释掉 tokenFile 这个 key
TokenFile string
// Namespaces 需要进行查询和监听的 Namespace 列表
Namespaces []string
// DeploymentPrefix 命名前缀
DeploymentPrefix string
// TLSClientConfigInsecure 是否启用 TLS
TLSClientConfigInsecure bool
tokenFile string
}

// DefaultConfig 返回默认配置,默认采用集群内模式
Expand All @@ -27,7 +36,9 @@ func DefaultConfig() *Config {
Token: inClusterToken(),
Namespaces: []string{inClusterNamespace()},
TLSClientConfigInsecure: true,
tokenFile: tokenFile,
// NOTICE, 如配置文件中"tokenFile"这个key不存在,则TokenFile值为tokenFile常量
// 如key存在,无论这个值是否为空,则TokenFile值都为该key的值
TokenFile: tokenFile,
}
}

Expand All @@ -37,7 +48,7 @@ func (c *Config) toRestConfig() *rest.Config {
cfg := &rest.Config{
Host: c.Addr,
BearerToken: c.Token,
BearerTokenFile: c.tokenFile,
BearerTokenFile: c.TokenFile,
TLSClientConfig: rest.TLSClientConfig{
Insecure: c.TLSClientConfigInsecure,
},
Expand Down
11 changes: 11 additions & 0 deletions examples/kubegrpc/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
[k8s]
# k8s API Server 地址
addr="https://$API_SERVER_ADDR"
# Debug 是否开启debug模式
debug = true
# Token k8s API Server 请求token
token="$API_SERVER_TOKEN"
# Token k8s API Server 请求token file
# 本地运行时:需要 tokenFile = ""
# 集群模式下运行时:需要 tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" 或释掉 tokenFile 这个 key
tokenFile = ""
# Namespaces 需要进行查询和监听的 Namespace 列表
namespaces=["default"]

[grpc.test]
Expand Down
4 changes: 1 addition & 3 deletions examples/kubegrpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func main() {

func invokerGrpc() error {
// 构建k8s registry,并注册为grpc resolver
registry.Load("registry").Build(
registry.WithClient(ek8s.Load("k8s").Build()),
)
registry.DefaultContainer().Build(registry.WithClient(ek8s.Load("k8s").Build()))
// 构建gRPC.ClientConn组件
grpcConn := egrpc.Load("grpc.test").Build()
// 构建gRPC Client组件
Expand Down
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func WithToken(token string) Option {
}
}

func WithTokenFile(tokenFile string) Option {
return func(c *Container) {
c.config.TokenFile = tokenFile
}
}

func WithNamespaces(namespaces []string) Option {
return func(c *Container) {
c.config.Namespaces = namespaces
Expand Down

0 comments on commit c2f3c3c

Please sign in to comment.