0

问题:有些步骤在 K8b 中创建实体,无论构建中的任何其他步骤是否成功,最终都必须将其删除。

这是一个例子cloudbyild.yaml

steps:  

  # TEST NAMESPACE
  #
  # some previous steps...
  # package jar
  # build container & etc.

  # Kubernetes RUN DB
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: deploy-db
    waitFor: ['-']
    args: ['apply',
           '--filename', './k8b/db/',
           '--location', 'somewhere',
           '--cluster', 'my-trololo-cluster']
  # Run something else in Kubernetes
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: deploy-other-things
    waitFor:
      - 'deploy-db'
    args: ['apply',
           '--filename', './k8b/other-things/',
           '--location', 'somewhere',
           '--cluster', 'my-trololo-cluster']

  # Test DB in pod
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: test-db
    waitFor:
      - 'deploy-other-things'
    entrypoint: 'bash'
    args: ['./scripts/test_db.sh']
    

  # Run REST-API
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: deploy-rest
    waitFor:
      - 'deploy-other-things'
    args: ['run',
           '--filename', './k8b/rest/',
           '--location', 'somewhere',
           '--cluster', 'my-trololo-cluster' ]
  # Test REST-API
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: test-REST-API
    waitFor:
      - 'deploy-rest'
      - 'test-db'
    entrypoint: 'bash'
    args: ['./scripts/test_rest_api.sh']

  # Cleanup steps
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: cleanup
    waitFor:
      - 'test-REST-API'
    entrypoint: 'kubectl'
    args: [ 'delete', '--filename', './k8b', '--recursive' ]
 
  # Delete PERSISTENT VOLUME
  - name: 'gcr.io/cloud-builders/gke-deploy'
    id: delete-persistent-volume
    waitFor:
      - 'test-REST-API'
    entrypoint: 'bash'
    args:
      - '-c'
      - |
          pvc_name=$(kubectl get pvc --selector=$sel -o jsonpath={.items..metadata.name})
          kubectl delete pvc ${pvc_name}

因此,如果“清理步骤”之前的任何步骤失败,GKE 中的实体删除将不会发生。并且下一次 cloudbuild 运行不会发生在干净的集群上。

我在文档中找不到这种情况的任何解决方案。

而且我认为目前我可以在每一步都使用 bash 脚本来解决这个问题,如果出现崩溃,我需要:

  • 在 bash 脚本中捕获它;
  • 发出命令以清除 bash 脚本中的集群;
  • 然后使用非零代码退出脚本。每一步都是如此。

但在我看来,这不是一个很好的解决方案。也许有更好的解决方案?

4

0 回答 0