KubeSphere 对接 Github
KubeSphere 对接 Github 实践
github
github的clientID和clientSecret设置
Generate a new client secret
排错步骤
#在 cc 里配置好,等待 ks-installer 自动更新;或者手动重启 ks-apiserver
#编辑ks-installer
kubectl edit cc -n kubesphere-system ks-installer
#重启ks-installer
kubectl rollout restart -n kubesphere-system deploy ks-installer
#重启ks-apiserver
kubectl rollout restart -n kubesphere-system deploy ks-apiserver
#查看ks-apiserver日志
kubectl -n kubesphere-system logs ks-apiserver-xxxxxxxxx
本文档介绍了如何使用基于 OAuth 2.0 协议的外部身份提供者。
下图显示了 KubeSphere 与外部 OAuth 2.0 身份提供者之间的身份验证过程。
准备工作
您需要部署一个 Kubernetes 集群,并在集群中安装 KubeSphere。
开发 OAuth 2.0 插件
在本地克隆 KubeSphere ,进入本地 KubeSphere 仓库,并在 /pkg/apiserver/authentication/identityprovider/
目录下创建一个插件的包。
在插件包中,实现如下接口:
// /pkg/apiserver/authentication/identityprovider/oauth_provider.go
type OAuthProvider interface {
// Exchange identity with a remote server.
IdentityExchange(code string) (Identity, error)
}
type OAuthProviderFactory interface {
// Return the identity provider type.
Type() string
// Apply settings from kubesphere-config.
Create(options oauth.DynamicOptions) (OAuthProvider, error)
}
// /pkg/apiserver/authentication/identityprovider/identity_provider.go
type Identity interface {
// (Mandatory) Return the identifier of the user at the identity provider.
GetUserID() string
// (Optional) Return the name of the user to be referred as on KubeSphere.
GetUsername() string
// (Optional) Return the email address of the user.
GetEmail() string
}
-
在插件包的
init()
函数中注册插件。// Custom plugin package func init() { // Change <StructName> to the actual name of the struct that // implements the OAuthProviderFactory interface. identityprovider.RegisterOAuthProvider(&<StructName>{}) }
-
在
/pkg/apiserver/authentication/options/authenticate_options.go
中导入插件包。// Change <CustomPackage> to the actual name of your plugin package. import ( ... _ "kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider/<CustomPackage>" ... )
-
构建 ks-apiserver 镜像 并部署到您的集群中。
集成身份提供者
以 admin
身份登录 KubeSphere,将光标移动到右下角 点击 kubectl,然后执行以下命令来编辑 CRD ClusterConfiguration
中的 ks-installer
:
kubectl -n kubesphere-system edit cc ks-installer
在 spec:authentication
部分配置的 oauthOptions:identityProviders
以外的字段信息请参阅设置外部身份认证。
-
根据开发的身份提供者插件来配置
oauthOptions:identityProviders
中的字段。以下是使用 GitHub 作为外部身份提供者的配置示例。详情请参阅 GitHub 官方文档和 GitHubIdentityProvider 源代码 。
authentication:
jwtSecret:
oauthOptions:
accessTokenInactivityTimeout: 30m
accessTokenMaxAge: 1h
identityProviders:
- mappingMethod: auto
name: github
provider:
clientID: ******
clientSecret: ******
redirectURL: 'http://192.168.100.2:30880/oauth/redirect/github'
type: GitHubIdentityProvider
同样,您也可以使用阿里云 IDaaS 作为外部身份提供者。详情请参阅阿里云 IDaaS 文档和 AliyunIDaasProvider 源代码。
- 字段配置完成后,保存修改,然后等待 ks-installer 完成重启。
console 界面确认配置内容
#console 访问这个路径 /kapis/config.kubesphere.io/v1alpha2/configs/oauth
http://192.168.100.2:30880/kapis/config.kubesphere.io/v1alpha2/configs/oaut
查看集成效果
进入 KubeSphere 登录界面,点击 Log In with XXX (例如,Log In with GitHub)。
在外部身份提供者的登录界面,输入身份提供者配置的用户名和密码,登录 KubeSphere 。