tools/auth包源码分析
tools/auth包
k8s认证的工具类(目前只有关于exec方式的工具类)
- 函数
// LoadFromFile 从文件路径解析一个 Info 对象. func LoadFromFile(path string) (*Info, error) { var info Info if _, err := os.Stat(path); os.IsNotExist(err) { return nil, err } data, err := ioutil.ReadFile(path) if err != nil { return nil, err } err = json.Unmarshal(data, &info) if err != nil { return nil, err } return &info, err }
- 结构体
// Info 包含 Kubernetes API 授权配置. 它旨在作为 JSON 对象从文件中读取/写入. type Info struct { // 用户名称 User string // 用户密码 Password string `datapolicy:"password"` // 认证证书授权路径 CAFile string // 认证证书路径 CertFile string // 认证证书公钥路径 KeyFile string // 认证token BearerToken string `datapolicy:"token"` Insecure *bool } // MergeWithConfig 返回带有来自 Info形成的 client.Config 的副本(只是把部分字段覆盖) func (info Info) MergeWithConfig(c restclient.Config) (restclient.Config, error) { var config = c config.Username = info.User config.Password = info.Password config.CAFile = info.CAFile config.CertFile = info.CertFile config.KeyFile = info.KeyFile config.BearerToken = info.BearerToken if info.Insecure != nil { config.Insecure = *info.Insecure } return config, nil } // 如果 Kubernetes API 授权信息完整,则 Complete 返回 true。 func (info Info) Complete() bool { return len(info.User) > 0 || len(info.CertFile) > 0 || len(info.BearerToken) > 0 }
- exec包
- exec.go
- 变量
// 设置的exec插件的执行环境变量(其中设置的value作为ExceCluster) const execInfoEnv = "KUBERNETES_EXEC_INFO" // 全局scheme var scheme = runtime.NewScheme() // 全局编解码器 var codecs = serializer.NewCodecFactory(scheme) func init() { install.Install(scheme) }
- 函数
// LoadExecCredential 加载 exec 插件与 集群通信所需的配置。 // // LoadExecCredential 期望提供的数据是序列化的 client.authentication.k8s.io ExecCredential 对象(任何版本)。如果提供的数据无效(即,它无法解组到任何已知的 client.authentication.k8s.io ExecCredential 版本), // 将返回错误。成功解组的 ExecCredential 将作为第一个返回值返回。 func LoadExecCredential(data []byte) (runtime.Object, *rest.Config, error) { // 解码byte[]数据为scheme中包含的gvk obj, gvk, err := codecs.UniversalDeserializer().Decode(data, nil, nil) if err != nil { return nil, nil, fmt.Errorf("decode: %w", err) } // 构建期望的gk expectedGK := schema.GroupKind{ Group: clientauthentication.SchemeGroupVersion.Group, Kind: "ExecCredential", } // 对比期望的gk和解码的gk是否相等 if gvk.GroupKind() != expectedGK { return nil, nil, fmt.Errorf( "invalid group/kind: wanted %s, got %s", expectedGK.String(), gvk.GroupKind().String(), ) } // 这里转化为内部的gvk对应的ExecCredential var execCredential clientauthentication.ExecCredential // 使用scheme中的convert做类型转化 if err := scheme.Convert(obj, &execCredential, nil); err != nil { return nil, nil, fmt.Errorf("cannot convert to ExecCredential: %w", err) } // 判断execCredential的spec中的Cluster是否为nil if execCredential.Spec.Cluster == nil { return nil, nil, errors.New("ExecCredential does not contain cluster information") } // 调用之前文章讲解rest包时的ExecClusterToConfig函数,转化ExecCluster为rest config restConfig, err := rest.ExecClusterToConfig(execCredential.Spec.Cluster) if err != nil { return nil, nil, fmt.Errorf("cannot create rest.Config: %w", err) } return obj, restConfig, nil } // LoadExecCredentialFromEnv 是 LoadExecCredential 的辅助包装器,它从 KUBERNETES_EXEC_INFO 环境变量加载。 func LoadExecCredentialFromEnv() (runtime.Object, *rest.Config, error) { // 获取环境变量KUBERNETES_EXEC_INFO env := os.Getenv(execInfoEnv) // 判断是否存在 if env == "" { return nil, nil, errors.New("KUBERNETES_EXEC_INFO env var is unset or empty") } // 加载环境变量对应的ExecCredential转化为rest config return LoadExecCredential([]byte(env)) }
- 变量
- exec.go