core服务的DNS名称是动态的,无法在adapter服务的配置文件中提前确定,因此在部署helm chart时动态生成adapter的配置文件

pull/1198/head
zhangxinhui02 2025-08-20 02:32:03 +08:00
parent 32439dd235
commit 91692a8e6c
No known key found for this signature in database
GPG Key ID: 22C23383864A313F
9 changed files with 151 additions and 23 deletions

View File

@ -0,0 +1,41 @@
#!/bin/python3
# 这个脚本的作用是在部署helm chart时动态生成adapter的配置文件保存在configmap中
# 需要动态生成的原因是core服务的DNS名称是动态的无法在adapter服务的配置文件中提前确定
# 一些与k8s现有资源冲突的配置也会在这里重置
import os
import toml
import base64
from kubernetes import client, config
config.load_incluster_config()
v1 = client.CoreV1Api()
# 读取部署的关键信息
namespace = os.getenv("NAMESPACE")
release_name = os.getenv("RELEASE_NAME")
data_b64 = os.getenv("DATA_B64")
# 解析并覆盖关键配置
# 这里被覆盖的配置应当在helm chart中针对对应的k8s资源来灵活修改
data = toml.loads(base64.b64decode(data_b64).decode("utf-8"))
data['napcat_server']['host'] = '0.0.0.0'
data['napcat_server']['port'] = 8095
data['maibot_server']['host'] = f'{release_name}-maibot-core' # 根据release名称动态拼接core服务的DNS名称
data['maibot_server']['port'] = 8000
# 创建/修改configmap
cm_name = f'{release_name}-maibot-adapter'
cm = client.V1ConfigMap(
metadata=client.V1ObjectMeta(name=cm_name),
data={'config.toml': toml.dumps(data)}
)
try:
v1.create_namespaced_config_map(namespace, cm)
print(f"ConfigMap `{cm_name}` created successfully")
except client.exceptions.ApiException as e:
if e.status == 409: # 已存在,更新
v1.replace_namespaced_config_map(cm_name, namespace, cm)
print(f"ConfigMap `{cm_name}` replaced successfully")
else:
raise

View File

@ -0,0 +1,5 @@
#!/bin/sh
# 这个脚本的作用是安装必要的python包为adapter-cm-generator.py脚本做准备
pip3 install -i https://mirrors.ustc.edu.cn/pypi/simple kubernetes toml
python3 adapter-cm-generator.py

View File

@ -1,8 +0,0 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-maibot-adapter
namespace: {{ .Release.Namespace }}
data:
config.toml: |
{{ .Values.config.adapter_config | nindent 4 }}

View File

@ -1,8 +0,0 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-maibot-core-scripts
namespace: {{ .Release.Namespace }}
data:
volume-linker.sh: |
{{ .Files.Get "files/volume-linker.sh" | nindent 4 }}

View File

@ -70,7 +70,7 @@ spec:
items:
- key: volume-linker.sh
path: volume-linker.sh
name: {{ .Release.Name }}-maibot-core-scripts
name: {{ .Release.Name }}-maibot-scripts
name: scripts
- configMap:
items:

View File

@ -0,0 +1,50 @@
# 动态生成adapter配置文件的configmap的job仅会在部署时运行一次
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Release.Name }}-adapter-cm-generator
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": pre-install
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
backoffLimit: 0
template:
spec:
serviceAccountName: {{ .Release.Name }}-adapter-cm-generator
restartPolicy: Never
containers:
- name: python
image: python:slim
workingDir: /app
command:
- sh
args:
- adapter-pip-installer.sh
env:
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: RELEASE_NAME
value: {{ .Release.Name }}
- name: DATA_B64
value: {{ .Values.config.adapter_config | b64enc }} # 将配置文件编码为base64从环境变量注入
volumeMounts:
- mountPath: /app/adapter-pip-installer.sh
name: scripts
readOnly: true
subPath: adapter-pip-installer.sh
- mountPath: /app/adapter-cm-generator.py
name: scripts
readOnly: true
subPath: adapter-cm-generator.py
volumes:
- name: scripts
configMap:
name: {{ .Release.Name }}-maibot-scripts
items:
- key: adapter-pip-installer.sh
path: adapter-pip-installer.sh
- key: adapter-cm-generator.py
path: adapter-cm-generator.py

View File

@ -0,0 +1,39 @@
# 动态生成adapter配置文件的configmap所需要的rbac授权
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ .Release.Name }}-adapter-cm-generator
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": pre-install
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ .Release.Name }}-adapter-cm-gen-role
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": pre-install
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ .Release.Name }}-adapter-cm-gen-role-binding
namespace: {{ .Release.Namespace }}
annotations:
"helm.sh/hook": pre-install
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
subjects:
- kind: ServiceAccount
name: {{ .Release.Name }}-adapter-cm-generator
namespace: {{ .Release.Namespace }}
roleRef:
kind: Role
name: {{ .Release.Name }}-adapter-cm-gen-role
apiGroup: rbac.authorization.k8s.io

View File

@ -0,0 +1,15 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-maibot-scripts
namespace: {{ .Release.Namespace }}
data:
# pre-install hook
adapter-pip-installer.sh: |
{{ .Files.Get "files/adapter-pip-installer.sh" | nindent 4 }}
adapter-cm-generator.py: |
{{ .Files.Get "files/adapter-cm-generator.py" | nindent 4 }}
# core
volume-linker.sh: |
{{ .Files.Get "files/volume-linker.sh" | nindent 4 }}

View File

@ -221,14 +221,8 @@ config:
nickname = ""
[napcat_server] # Napcat连接的ws服务设置
host = "0.0.0.0" # Napcat设定的主机地址
port = 8095 # Napcat设定的端口
heartbeat_interval = 30 # 与Napcat设置的心跳相同按秒计
[maibot_server] # 连接麦麦的ws服务设置
host = "localhost" # 麦麦在.env文件中设置的主机地址即HOST字段
port = 8000 # 麦麦在.env文件中设置的端口即PORT字段
[chat] # 黑白名单功能
group_list_type = "whitelist" # 群组名单类型可选为whitelist, blacklist
group_list = [] # 群组名单