kubesphere source code:
https://github.com/kubesphere/kubesphere
kubesphere解析
部分源於kubesphere 官方文檔。文檔不對具體代碼解析,只對模塊進行說明。
API 架構
下圖是 KubeSphere API 的架構,所有請求都將通過 API Gateway 進行認證授權代理后發送到各個服務組件。
API路由
kubesphere使用caddy實現路由分發。下面是caddy配置文件。
####3個插件 authenticate { token-idle-timeout 40m redis-url redis://redis.kubesphere-system.svc:6379 secret {$JWT_SECRET} path / except GET /kapis/openpitrix.io/v1/apps /kapis/openpitrix.io/v1/attachments /kapis/openpitrix.io/v1/categories except * /apis/account.kubesphere.io/v1alpha1/authenticate /kapis/iam.kubesphere.io/v1alpha2/login /kapis/iam.kubesphere.io/v1alpha2/authenticate /images /kapis/devops.kubesphere.io/v1alpha2/webhook/github /kapis/devops.kubesphere.io/v1alpha2/webhook/git /swagger /kapis/v1alpha1/configz } authentication { path / except * /kapis/tenant.kubesphere.io/v1alpha2 /kapis/alerting.kubesphere.io/v1/comment /kapis/alerting.kubesphere.io/v1/resource_type /kapis/alerting.kubesphere.io/v1/metric /kapis/notification.kubesphere.io /kapis/resources.kubesphere.io/v1alpha2/registry /kapis/iam.kubesphere.io/v1alpha2/rulesmapping /kapis/jenkins.kubesphere.io /kapis/devops.kubesphere.io /apis/devops.kubesphere.io /kapis/resources.kubesphere.io/v1alpha2/git/verify } swagger ###路由策略 # k8s api proxy /api https://kubernetes.default { header_upstream Authorization "Bearer {$KUBESPHERE_TOKEN}" insecure_skip_verify transparent websocket } # fix jenkins auth plugin proxy /apis/account.kubesphere.io/v1alpha1/authenticate http://ks-account.kubesphere-system.svc/kapis/iam.kubesphere.io/v1alpha2/authenticate { without /apis/account.kubesphere.io/v1alpha1/authenticate transparent } # jenkins proxy /kapis/jenkins.kubesphere.io http://ks-jenkins.kubesphere-devops-system.svc { without /kapis/jenkins.kubesphere.io transparent } proxy /job http://ks-jenkins.kubesphere-devops-system.svc { transparent } # old devops api proxy /kapis/devops.kubesphere.io/v1alpha/ http://ks-devops.kubesphere-devops-system.svc/api { without /kapis/devops.kubesphere.io transparent } # new devops api proxy /kapis/devops.kubesphere.io/v1alpha2/ http://ks-apiserver.kubesphere-system.svc { transparent } # iam proxy /kapis/iam.kubesphere.io http://ks-account.kubesphere-system.svc { transparent } # tenant proxy /kapis/tenant.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } # operations proxy /kapis/operations.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } # openpitrix proxy /kapis/openpitrix.io http://ks-apiserver.kubesphere-system.svc { transparent } # logging proxy /kapis/logging.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } # alerting proxy /kapis/alerting.kubesphere.io http://alerting-client-server.kubesphere-alerting-system.svc:9200/api { without /kapis/alerting.kubesphere.io transparent } # servicemesh proxy /kapis/servicemesh.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } proxy /kapis/resources.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } proxy /kapis/metrics.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } # terminal proxy /kapis/terminal.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent websocket } # monitoring proxy /kapis/monitoring.kubesphere.io http://ks-apiserver.kubesphere-system.svc { transparent } # notification proxy /kapis/notification.kubesphere.io http://notification.kubesphere-alerting-system.svc:9200 { without /kapis/notification.kubesphere.io transparent } # everything else goes to ks-apiserver proxy /kapis http://ks-apiserver.kubesphere-system.svc { transparent } log / stdout "{remote} {when} {method} {uri} {proto} {status} {size} {latency_ms}ms" }
kubesphere模塊
代碼目錄說明
源於docs/en/guides/Kubesphere-code-layout.md。一些目錄已經使用中文說明
├── api // 自動生成的api 文檔 │ ├── api-rules │ ├── ks-openapi-spec │ └── openapi-spec ├── build // Dockerfile,用於生成各個模塊docker鏡像 │ ├── hypersphere │ ├── ks-apigateway │ ├── ks-apiserver │ ├── ks-controller-manager │ ├── ks-iam │ └── ks-network ├── cmd // kubesphere各個模塊的程序入口. │ ├── controller-manager │ │ └── app │ ├── hypersphere │ ├── ks-apigateway // ks-apigateway模塊 │ │ └── app │ ├── ks-apiserver //ks-apiserver模塊 │ │ └── app │ ├── ks-iam // ks-iam模塊 │ │ └── app │ └── ks-network ├── config // CRD 配置文件 │ ├── crds // CRD yaml files │ ├── default // kustomization yaml files │ ├── manager // controller manager yaml files │ ├── rbac // rbac yaml files │ ├── samples // CRD sample │ └── webhook // webhook yaml files ├── docs │ ├── en │ │ ├── concepts-and-designs │ │ └── guides │ └── images ├── hack // Script files to help people develop │ └── lib ├── pkg // 主要代碼. │ ├── api // REST API的消息體 │ │ ├── devops │ │ ├── logging │ │ └── monitoring │ ├── apigateway // ks-apigateway模塊的重要依賴。 │ │ └── caddy-plugin //caddy的3個插件 │ ├── apis // CRD資源數據的結構體, │ │ ├── devops │ │ ├── network │ │ ├── servicemesh │ │ └── tenant │ ├── apiserver // ks-apiserver模塊的重要依賴。REST API的執行函數 │ │ ├── components │ │ ├── devops │ │ ├── git │ │ ├── iam │ │ ├── logging │ │ ├── monitoring │ │ ├── openpitrix │ │ ├── operations │ │ ├── quotas │ │ ├── registries │ │ ├── resources │ │ ├── revisions │ │ ├── routers │ │ ├── runtime │ │ ├── servicemesh │ │ ├── tenant │ │ ├── terminal │ │ ├── workloadstatuses │ │ └── workspaces │ ├── client // k8s CRD客戶端。代碼自動生成 │ │ ├── clientset │ │ ├── informers │ │ └── listers │ ├── constants // common constants │ ├── controller // controller manger's reconciliation logic │ │ ├── application │ │ ├── clusterrolebinding │ │ ├── destinationrule │ │ ├── job │ │ ├── namespace │ │ ├── network │ │ ├── s2ibinary │ │ ├── s2irun │ │ ├── storage │ │ ├── virtualservice │ │ └── workspace │ ├── db // 數據庫表 │ │ ├── ddl │ │ ├── schema │ │ └── scripts │ ├── gojenkins // Jenkins客戶端 │ │ ├── _tests │ │ └── utils │ ├── informers │ ├── kapis // ks-apiserver模塊的重要依賴。REST API注冊 │ │ ├── devops │ │ ├── iam │ │ ├── logging │ │ ├── monitoring │ │ ├── openpitrix │ │ ├── operations │ │ ├── resources │ │ ├── servicemesh │ │ ├── tenant │ │ └── terminal │ ├── models // ks-apiserver模塊的重要依賴。REST API的數據層 │ │ ├── components │ │ ├── devops │ │ ├── git │ │ ├── iam │ │ ├── kubeconfig │ │ ├── kubectl │ │ ├── log │ │ ├── metrics │ │ ├── nodes │ │ ├── openpitrix │ │ ├── quotas │ │ ├── registries │ │ ├── resources │ │ ├── revisions │ │ ├── routers │ │ ├── servicemesh │ │ ├── status │ │ ├── storage │ │ ├── tenant │ │ ├── terminal │ │ ├── workloads │ │ └── workspaces │ ├── server // Data processing part of REST API │ │ ├── config │ │ ├── errors │ │ ├── filter │ │ ├── options │ │ └── params │ ├── simple // common clients │ │ └── client │ ├── test │ ├── utils // common utils │ │ ├── hashutil │ │ ├── idutils │ │ ├── iputil │ │ ├── jsonutil │ │ ├── jwtutil │ │ ├── k8sutil │ │ ├── net │ │ ├── readerutils │ │ ├── reflectutils │ │ ├── signals │ │ ├── sliceutil │ │ ├── stringutils │ │ └── term │ ├── version │ └── webhook ├── test // e2e test code │ ├── e2e ├── tools // tools to genereate API docs │ ├── cmd │ │ ├── crd-doc-gen // gen CRD API docs │ │ └── doc-gen // gen REST API docs │ └── lib
kubesphere apigateway
- 使用caddy作為網關。
- 添加authenticate,authentication,swagger三個插件。
- authenticate:驗證是否有token。所有的請求必須有token。否則重新登錄
- authentication:驗證該用戶和命名空間是否符合。關於k8s的認證可以查看(https://blog.csdn.net/u013289746/article/details/81061015)
- swagger:用於展示rest api
kubesphere apiserver
- 直接使用go-restful,實現rest api接口
- 接口的注冊位置/pkg/kapis
- 接口函數實現/pkg/apiserver
- 數據層/pkg/models
kubesphere iam
- 實現基本等同於kubesphere apiserver。
kubesphere control manager
- 功能類似於k8s control-manager。監控自定義CRD資源。並根據資源狀態執行相關操作。
k8s apiserver
- 自定義crd資源,原生資源都是直接走k8s-apiserver
- 端口6443。可以使用
curl -k -H "Authorization: Bearer {token}" https://ip:6443/
直接訪問。 - crd資源的客戶端是自動生成的。kubesphere apiserver也會訪問k8s apiserver的內容
S2i builder
流程如下
1.s2ioperator定義CRD資源 2.用戶向api server申請執行S2ibuilder資源 3.ks control-manager監控到S2ibuilder資源。 a.ks control-manager向api server申請執行deployment,configmap等資源。 b.api server將根據deployment,configmap。運行起s2irun容器,並將目錄掛載該容器上 c.s2irun容器執行git clone、docker build和docker pull等操作。 4.ks control-manager監控到S2ibuilder資源完畢,清除資源。