stage('Deploy to k3s') {
    steps {
        dir('more_dots') {
            script {
                def cluster = params.TARGET_CLUSTER
                def fullImageWithTag = "${FULL_IMAGE_NAME}:${env.IMAGE_TAG}"

                if (cluster == 'cluster1' || cluster == 'both') {
                    withCredentials([
                        file(credentialsId: 'k3s-cluster1-config', variable: 'KUBECONFIG_CLUSTER1'),
                        usernamePassword(credentialsId: REGISTRY_CREDENTIALS_ID, usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASS')
                    ]) {
                        sh """
                            set -eux

                            # 查找kubectl路径
                            KUBECTL_PATH=\$(command -v kubectl 2>/dev/null || true)
                            if [ -z "\$KUBECTL_PATH" ]; then
                                for p in /usr/local/bin/kubectl /usr/bin/kubectl /bin/kubectl; do
                                    if [ -x "\$p" ]; then
                                        KUBECTL_PATH="\$p"
                                        break
                                    fi
                                done
                            fi

                            echo "使用kubectl路径: \$KUBECTL_PATH"

                            # 定义kubectl函数
                            k() {
                                sudo \$KUBECTL_PATH --kubeconfig=${KUBECONFIG_CLUSTER1} "\$@"
                            }

                            # 检查命名空间
                            k get namespace ${params.DEPLOY_ENV} || k create namespace ${params.DEPLOY_ENV}

                            # 创建imagePullSecret
                            k create secret docker-registry regcred-130 \\
                                --docker-server=${REGISTRY_URL} \\
                                --docker-username=${REGISTRY_USER} \\
                                --docker-password=${REGISTRY_PASS} \\
                                --namespace=${params.DEPLOY_ENV} \\
                                --dry-run=client -o yaml | k apply -f -

                            # 替换镜像并部署
                            sed "s|image:.*more_dots.*|image: ${fullImageWithTag}|g" k8s/deployment.yaml > /tmp/deployment-${params.DEPLOY_ENV}.yaml
                            k apply -f /tmp/deployment-${params.DEPLOY_ENV}.yaml -n ${params.DEPLOY_ENV}

                            # 确保使用imagePullSecret（注意 deployment 名称是 more-dots）
                            k patch deployment more-dots -n ${params.DEPLOY_ENV} \\
                                -p '{"spec":{"template":{"spec":{"imagePullSecrets":[{"name":"regcred-130"}]}}}}' || true

                            # 重启并等待
                            k rollout restart deployment/more-dots -n ${params.DEPLOY_ENV} 2>/dev/null || true
                            k rollout status deployment/more-dots -n ${params.DEPLOY_ENV} --timeout=300s
                        """
                    }
                }

                // cluster2 部分同样修改...
            }
        }
    }
}