feat: 新增 18 个 K8s 资源配置模板

工作负载: Pod, StatefulSet, DaemonSet, Job, CronJob
服务发现: Ingress
存储管理: PersistentVolume, PersistentVolumeClaim, StorageClass
配置与密钥: ConfigMap, Secret
安全与权限: ServiceAccount, Role, ClusterRole, RoleBinding, ClusterRoleBinding, NetworkPolicy
集群管理: Namespace

总计支持 26 个组件,覆盖 K8s 核心资源全谱
This commit is contained in:
cnbugs
2026-07-18 19:39:45 +08:00
parent 4c25ae183e
commit 4f702b833a
20 changed files with 3455 additions and 15 deletions
+140
View File
@@ -0,0 +1,140 @@
// K8s ClusterRole Schema - 由 ConfTemplate 生成
// 生成时间: 自动生成
export const k8sClusterRoleSchema = {
id: 'k8s-clusterrole',
name: 'K8s ClusterRole',
icon: 'ShieldCheck',
category: '云原生',
description: 'Kubernetes 集群级 RBAC 角色',
format: 'yaml',
fileName: 'clusterrole.yaml',
groups: [
{
title: '基础信息',
fields: [
{
key: 'name',
label: 'ClusterRole 名称',
type: 'text',
placeholder: 'my-cluster-role',
default: 'my-cluster-role',
required: true,
tip: '将用作 ClusterRole 的 metadata.name',
},
],
},
{
title: '权限规则',
fields: [
{
key: 'resources',
label: '资源类型',
type: 'select',
multiple: true,
options: [
{ label: 'Pods', value: 'pods' },
{ label: 'Services', value: 'services' },
{ label: 'ConfigMaps', value: 'configmaps' },
{ label: 'Deployments', value: 'deployments' },
{ label: 'Secrets', value: 'secrets' },
{ label: 'Nodes', value: 'nodes' },
{ label: 'Namespaces', value: 'namespaces' },
{ label: 'All (*)', value: '*' },
],
default: ['pods'],
tip: '可多选,选择要授权的资源类型',
},
{
key: 'verbs',
label: '操作动词',
type: 'select',
multiple: true,
options: [
{ label: 'Get (读取)', value: 'get' },
{ label: 'List (列表)', value: 'list' },
{ label: 'Watch (监听)', value: 'watch' },
{ label: 'Create (创建)', value: 'create' },
{ label: 'Update (更新)', value: 'update' },
{ label: 'Delete (删除)', value: 'delete' },
{ label: 'Patch (补丁)', value: 'patch' },
{ label: 'All (*)', value: '*' },
],
default: ['get', 'list', 'watch'],
tip: '可多选,选择允许的操作',
},
{
key: 'apiGroups',
label: 'API 组',
type: 'text',
placeholder: '"" (核心组留空)',
default: '',
tip: 'API 组,核心资源(如 pods, services)留空',
},
],
},
],
}
function toYaml(obj, indent = 0) {
const lines = []
const prefix = ' '.repeat(indent)
for (const [key, value] of Object.entries(obj)) {
if (value === null || value === undefined || value === '') continue
if (Array.isArray(value)) {
lines.push(`${prefix}${key}:`)
for (const item of value) {
if (typeof item === 'object' && item !== null) {
const entries = Object.entries(item).filter(([, v]) => v !== null && v !== undefined && v !== '')
if (entries.length === 0) continue
const first = entries[0]
lines.push(`${prefix} - ${first[0]}: ${first[1]}`)
for (let i = 1; i < entries.length; i++) {
lines.push(`${prefix} ${entries[i][0]}: ${entries[i][1]}`)
}
} else {
lines.push(`${prefix} - ${item}`)
}
}
} else if (typeof value === 'object') {
lines.push(`${prefix}${key}:`)
lines.push(toYaml(value, indent + 1))
} else {
lines.push(`${prefix}${key}: ${value}`)
}
}
return lines.join('\n')
}
export function generateK8sClusterRoleYaml(config) {
const resources = Array.isArray(config.resources) ? config.resources : [config.resources || 'pods']
const verbs = Array.isArray(config.verbs) ? config.verbs : [config.verbs || 'get']
const apiGroups = config.apiGroups !== undefined && config.apiGroups !== ''
? config.apiGroups.split(',').map(g => g.trim())
: ['']
const rule = {
apiGroups,
resources,
verbs,
}
const clusterRole = {
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'ClusterRole',
metadata: {
name: config.name,
},
rules: [rule],
}
const header = `# K8s ClusterRole - 由 ConfTemplate 生成
# 生成时间: ${new Date().toLocaleString('zh-CN')}
# 部署命令: kubectl apply -f ${config.fileName || 'clusterrole.yaml'}
`
return header + '\n' + toYaml(clusterRole)
}