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三个插件。
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资源完毕,清除资源。