---
# 1. 创建 base 命名空间 (如果不存在)
apiVersion: v1
kind: Namespace
metadata:
  name: base
---
# 2. 服务账号 (ServiceAccount)
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner
  namespace: base
---
# 3. 集群角色 (ClusterRole) - 赋予操作 PV 和 PVC 的权限
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "update", "patch"]
---
# 4. 角色绑定 (ClusterRoleBinding)
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-provisioner
    namespace: base
roleRef:
  kind: ClusterRole
  name: nfs-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
# 5. 部署 NFS 供给器 (Deployment)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-provisioner
  namespace: base
  labels:
    app: nfs-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-provisioner
  template:
    metadata:
      labels:
        app: nfs-provisioner
    spec:
      serviceAccountName: nfs-provisioner
      containers:
        - name: nfs-provisioner
          # 使用阿里云镜像加速拉取
          image: registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/nfs-subdir-external-provisioner:v4.0.2
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: 192.168.1.9
            - name: NFS_PATH
              value: /data/k3s_storage
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.1.9
            path: /data/k3s_storage
---
# 6. 定义存储类 (StorageClass)
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client
  namespace: base
  annotations:
    # 将其设置为默认存储类，这样 Harbor 部署时会自动使用它
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
  archiveOnDelete: "false"
reclaimPolicy: Delete
volumeBindingMode: Immediate
