1

我使用舵图部署了drone.io。构建工作正常。对于我的秘密,我遵循了这个文档:https ://readme.drone.io/extend/secrets/kubernetes/install/

所以我创建了一个秘密来保存插件和无人机服务器之间的共享密钥(对不起,ansible 标记):

apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: drone-kubernetes
data:
  server: {{ server.stdout | b64encode }}
  cert: {{ cert.stdout | b64encode }}
  token: {{ token.stdout | b64encode }}
  secret: {{ secret.stdout | b64encode }}

Kubernetes 秘密插件的部署:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: drone
    component: secrets
    release: drone
  name: drone-drone-secrets
spec:
  selector:
    matchLabels:
      app: drone
      component: secrets
      release: drone
  template:
    metadata:
      labels:
        app: drone
        component: secrets
        release: drone
    spec:
      containers:
      - env:
        - name: SECRET_KEY
          valueFrom:
            secretKeyRef:
              key: secret
              name: drone-kubernetes
        image: docker.io/drone/kubernetes-secrets:linux-arm64
        imagePullPolicy: IfNotPresent
        name: secrets
        ports:
        - containerPort: 3000
          name: secretapi
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/kubernetes/config
          name: kube
      volumes:
      - name: kube
        hostPath:
          path: /etc/kubernetes/admin.conf
          type: File

以及该部署的服务:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: drone
    component: secrets
    release: drone
  name: drone-secrets
spec:
  ports:
  - name: secretapi
    port: 3000
    protocol: TCP
  selector:
    app: drone
    component: secrets
    release: drone
  type: ClusterIP

我修补了无人机服务器部署以设置 DRONE_SECRET_SECRET 和 DRONE_SECRET_ENDPOINT 变量。

kubernetes-secrets 插件的 pod 确实可以按预期看到文件“/etc/kubernetes/config”,并且将 SECRET_KEY 作为环境。并从无人机服务器吊舱:

kubectl exec -i drone-drone-server-some-hash-here -- sh -c 'curl -s $DRONE_SECRET_ENDPOINT'
Invalid or Missing Signature

到目前为止,一切都很好。一切似乎都设置正确。

这是我的测试项目的 .drone.yml 文件:

kind: pipeline
name: default
steps:

- name: kubectl
  image: private-repo.local:5000/drone-kubectl
  settings:
    kubectl: "get pods"
    kubernetes_server:
      from_secret: kubernetes_server
    kubernetes_cert:
      from_secret: kubernetes_cert

image_pull_secrets:
 - kubernetes_server
 - kubernetes_cert

---
kind: secret
name: kubernetes_server
get:
  path: drone-kubernetes
  name: server
---
kind: secret
name: kubernetes_cert
get:
  path: drone-kubernetes
  name: cert
---
kind: secret
name: kubernetes_token
get:
  path: drone-kubernetes
  name: token

目前,自定义插件drone-kubectl 只运行 env 命令来查看我是否得到了我的秘密,而我没有......我错过了什么?

4

1 回答 1

2

好的,我在drone-drone-secrets 部署中使用环境变量 DEBUG 发现了我的问题。错误是:

time="2019-06-10T06:29:22Z" level=debug msg="secrets: cannot find secret cert: kubernetes api: Failure 403 secrets \"drone-kubernetes\" is forbidden: User \"system:serviceaccount:toolchain:default\" cannot get resource \"secrets\" in API group \"\" in the namespace \"toolchain\""

所以我创建了这个 serviceaccount 和相关角色:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: drone-drone-secrets
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: drone-drone-secrets
rules:
- apiGroups: [""] 
  resources: ["secrets"]
  verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: drone-drone-secrets
subjects:
- kind: ServiceAccount
  name: drone-drone-secrets
roleRef:
  kind: Role
  name: drone-drone-secrets
  apiGroup: rbac.authorization.k8s.io

并修补了部署以使用该服务帐户。现在一切正常。

于 2019-06-10T06:57:25.793 回答