hello云胜

技术与生活

0%

编译kubesphere apiserver

遇到的问题:

  1. 找不到依赖问题

kubesphere使用vendor管理依赖。

需要执行

1
2
3
go mod tidy
go mod download
go mod vendor

将依赖拷贝到vendor目录下,否则会找不到

  1. 无执行权限问题

    1
    chomd +x hack/*.sh
  2. Dockerfile配置goproxy

    1
    ENV GOPROXY https://goproxy.cn
  3. build镜像时需要传参

    以确定下载的helm的安装包

    TARGETARCH : amd64

    TARGETOS : linux

  4. 加速Docker build

    有一些去远程下载的文件,换成本地

    helm是从远程下载的,提前下好,放在本地

    1
    COPY helm-${HELM_VERSION}-${TARGETOS}-${TARGETARCH}.tar.gz /tmp
  5. 提供的获取token的接口,必须是post

    并且Content-Type是application/x-www-form-urlencoded

    ![image-20230802134138690](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802134138690.png)

编译打包apiserver

1
make ks-apiserver

编译成功,会在bin/cmd目录下生成可执行文件

![image-20230731172458071](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230731172458071.png)

获取kubesphere.yaml

在之前已部署好的ks环境执行

1
kubectl -n kubesphere-system get cm kubesphere-config -o yaml > kubesphere.yaml

然后改一改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
kubernetes:
kubeconfig: "/root/.kube/config"
master: https://10.x.x.x:6443
qps: 1e+06
burst: 1000000

authentication:
authenticateRateLimiterMaxTries: 10
authenticateRateLimiterDuration: 10m0s
loginHistoryRetentionPeriod: 168h
maximumClockSkew: 10s
multipleLogin: True
kubectlImage: kubesphere/kubectl:v1.18.0
jwtSecret: "mzmyz0FufNThfklUF3vAJVPaSJZDJyaL"

redis:
host: redis.kubesphere-system.svc
port: 6379
password: ""
db: 0
network:
enableNetworkPolicy: true
ippoolType: none
monitoring:
endpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
logging:
host: http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200
indexPrefix: ks-logstash-log
events:
host: http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200
indexPrefix: ks-logstash-events
auditing:
enable: true
host: http://elasticsearch-logging-data.kubesphere-logging-system.svc:9200
indexPrefix: ks-logstash-auditing

alerting:
prometheusEndpoint: http://prometheus-operated.kubesphere-monitoring-system.svc:9090
thanosRulerEndpoint: http://thanos-ruler-operated.kubesphere-monitoring-system.svc:10902
thanosRuleResourceLabels: thanosruler=thanos-ruler,role=thanos-alerting-rules

kubesphere会读取这个配置文件,先从启动的当前目录下找,找不到去/etc/kubesphere/下找

所以,我们现在把kubesphere.yaml放在代码目录的/bin/cmd下

启动

1
./ks-apiserver

![image-20230801093045431](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230801093045431.png)

启动成功,在9090端口启动监听。

测试

我们之前写的测试接口

![image-20230801093135620](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230801093135620.png)

构建镜像

在代码的根目录执行

1
docker build -f build/ks-apiserver/Dockerfile -t harbor-test.xxx.net/kubesphere/ks-apiserver:1.3.0 --build-arg TARGETOS=linux --build-arg TARGETARCH=amd64 .

然后再push到我们自己的harbor上

部署到集群中

1
kubectl -n kubesphere-system edit deploy ks-apiserver

原来的镜像是

1
image: kubesphere/ks-apiserver:v3.1.1

替换为我们自己的

验证

![image-20230801100844578](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230801100844578.png)

从ks-console的端口访问我们自定义的接口,成功

修改ks-installer

1
kubectl -n kubesphere-system edit cc ks-installer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spec:
authentication:
jwtSecret: ''
authenticateRateLimiterMaxTries: 10
authenticateRateLimiterDuration: 10m0s
oauthOptions:
accessTokenMaxAge: 1h
accessTokenInactivityTimeout: 30m
identityProviders:
- name: xxx
type: xxxIDaaSProvider
mappingMethod: auto
provider:
clientID: 'f7d8ba1339b079883cb8e8496690b6c6'
clientSecret: '0ae8e4f66358cd8894b1f6c697726968'
redirectURL: http://1x.xxx.151.208:30880/oauth/redirect/xxx
endpoint:
tokenURL: http://1x.xxx.7.21:30484/auth/getToken
authURL: https://iama.xxx.net/
userInfoURL: http://1x.xx.132.10:8080/auth/getUserInfo

要说的是这个redirectURL。就写这个地址,是ks-console的接口地址。最后的xxx是provider的name

查看日志

1
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

![image-20230801171026395](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230801171026395.png)

  1. 调用authURL 获取一个code
  2. 调用tokenURL 获取token。 这里需要上一步返回的code作为入参
  3. 拿着这个token 再去调userInfoURL 获取用户信息
  4. redirectURL 是完成后跳转的地址,写 http://1x.xxx.151.208:30880/oauth/redirect/xxx

![image-20230802153715226](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802153715226.png)

“users.iam.kubesphere.io is forbidden: User "system:pre-registration" cannot create resource "users" in API group "iam.kubesphere.io" at the cluster scope

![image-20230802170859639](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802170859639.png)

globalroles.iam.kubesphere.io pre-registration 是有create user的权限的

![image-20230802171020181](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802171020181.png)

看globalrolebindings.iam.kubesphere.io 是把pre-registration 这个role和名为pre-registration的group绑定了

![image-20230802171404975](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802171404975.png)

查看过clusterroles.rbac.authorization.k8s.io 并没有pre-registration的相关信息

既然报错说system:pre-registration这个用户没有权限,那就给他加上

1
kubectl edit globalrolebindings.iam.kubesphere.io  pre-registration
1
2
3
- apiGroup: iam.kubesphere.io/v1alpha2
kind: User
name: system:pre-registration

![image-20230802173405927](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802173405927.png)

成功

![image-20230802173527464](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230802173527464.png)

第二天,不知道为啥又坏了,报错

![image-20230803103644475](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230803103644475.png)

这个报错只能看出是代码出错了,没有其他有用的信息

只能看源码

pkg/kapis/oauth/register.go 是rest请求的路由注册代码

通过反向追踪代码,找到这个源码入口

pkg/kapis/oauth/handler.go:342

![image-20230803103749558](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230803103749558.png)

通过log

![image-20230803151653144](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230803151653144.png)

发现

1
requestInfo, _ := request.RequestInfoFrom(req.Request.Context())

这个requestInfo很奇怪,怎么去调用github了

怀疑是今天网不通了,昨天通。所以昨天好的,今天忽然不好了。

![image-20230803173144211](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230803173144211.png)

1
2
3
4
message: 'workspaces.tenant.kubesphere.io is forbidden: User "system:pre-registration" cannot list resource "workspaces" in API group "tenant.kubesphere.io" at the cluster scope',
reason: 'Forbidden',
details: { group: 'tenant.kubesphere.io', kind: 'workspaces' },

3.1版本的console解析的result

![image-20230804101053395](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230804101053395.png)

和3.3版本的apiserver返回的结构对的上

![image-20230804101208792](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230804101208792.png)

![image-20230804101236192](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230804101236192-16911151567331.png)

apiserver返回的是 access_token bearer refresh_token

1
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTExMTc5NTIsImlhdCI6MTY5MTExNDM1MiwiaXNzIjoia3ViZXNwaGVyZSIsInN1YiI6InN5c3RlbTpwcmUtcmVnaXN0cmF0aW9uIiwidG9rZW5fdHlwZSI6ImFjY2Vzc190b2tlbiIsInVzZXJuYW1lIjoic3lzdGVtOnByZS1yZWdpc3RyYXRpb24iLCJleHRyYSI6eyJlbWFpbCI6WyJ5YW5neXVuc2hlbmcuaXRAaGFpZXIuY29tIl0sImlkcCI6WyJoYWllciJdLCJ1aWQiOlsiMDE0NzEwNzYiXSwidXNlcm5hbWUiOlsi5p2o5LqR6IOcIl19fQ.Q64FGW2986-jmbLvy-IX8hF6p12LEZnX1dFzUtxeTlY Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTExMTk3NTIsImlhdCI6MTY5MTExNDM1MiwiaXNzIjoia3ViZXNwaGVyZSIsInN1YiI6InN5c3RlbTpwcmUtcmVnaXN0cmF0aW9uIiwidG9rZW5fdHlwZSI6InJlZnJlc2hfdG9rZW4iLCJ1c2VybmFtZSI6InN5c3RlbTpwcmUtcmVnaXN0cmF0aW9uIiwiZXh0cmEiOnsiZW1haWwiOlsieWFuZ3l1bnNoZW5nLml0QGhhaWVyLmNvbSJdLCJpZHAiOlsiaGFpZXIiXSwidWlkIjpbIjAxNDcxMDc2Il0sInVzZXJuYW1lIjpbIuadqOS6keiDnCJdfX0.YqlZpYeL0wGvZDg32tpW5RmJq44LXWmwckMxSEoVONg  3600

看起来也没有问题

进到ks-console的容器里

![image-20230804112024461](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230804112024461.png)

直接访问了一下api-server的callback接口

返回没有问题

我们使用的是3.1版本的ks-console

![image-20230804134808804](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230804134808804.png)

最后定位出来原因了

username不能是中文

3.3版本的console做了优化

![image-20230804134959102](D:\github\docs\云原生\kubesphere\编译kubesphere apiserver.assets\image-20230804134959102.png)

所以3.3版本可能能使用中文名来登录,不确定。没试。