Garbage Collect

By default, KubeVela Application will recycle outdated resources when new version is deployed and confirmed to be healthy. In some cases, you may want to have more customized control to the recycle of outdated resources, where you can leverage the garbage-collect policy.

In garbage-collect policy, there are two major capabilities you can use.

Keep legacy resources

Suppose you want to keep the resources created by the old version of the application. Use the garbage-collect policy and enable the option keepLegacyResource.

  1. # app.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: first-vela-app
  6. spec:
  7. components:
  8. - name: express-server
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. port: 8000
  13. traits:
  14. - type: ingress-1-20
  15. properties:
  16. domain: testsvc.example.com
  17. http:
  18. "/": 8000
  19. policies:
  20. - name: keep-legacy-resource
  21. type: garbage-collect
  22. properties:
  23. keepLegacyResource: true
  1. create app
  1. vela up -f app.yaml
  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. first-vela-app express-server webservice ingress-1-20 running healthy Ready:1/1 2022-04-06 16:20:25 +0800 CST
  1. update the app
  1. # app1.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: first-vela-app
  6. spec:
  7. components:
  8. - name: express-server-1
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. port: 8000
  13. traits:
  14. - type: ingress-1-20
  15. properties:
  16. domain: testsvc.example.com
  17. http:
  18. "/": 8000
  19. policies:
  20. - name: keep-legacy-resource
  21. type: garbage-collect
  22. properties:
  23. keepLegacyResource: true
  1. vela up -f app1.yaml
  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. first-vela-app express-server-1 webservice ingress-1-20 running healthy Ready:1/1 2022-04-06 16:20:25 +0800 CST

check whether legacy resources are reserved.

In the following steps, we’ll use kubectl command to do some verification. You can also use vela status first-vela-app to check the aggregated application status and see if components are healthy.

  1. $ kubectl get deploy
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. express-server 1/1 1 1 10m
  4. express-server-1 1/1 1 1 40s
  1. $ kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. express-server ClusterIP 10.96.102.249 <none> 8000/TCP 10m
  4. express-server-1 ClusterIP 10.96.146.10 <none> 8000/TCP 46s
  1. $ kubectl get ingress
  2. NAME CLASS HOSTS ADDRESS PORTS AGE
  3. express-server <none> testsvc.example.com 80 10m
  4. express-server-1 <none> testsvc.example.com 80 50s
  1. $ kubectl get resourcetracker
  2. NAME AGE
  3. first-vela-app-default 12m
  4. first-vela-app-v1-default 12m
  5. first-vela-app-v2-default 2m56s
  1. delete the app
  1. $ vela delete first-vela-app

If you hope to delete resources in one specified version, you can run kubectl delete resourcetracker first-vela-app-v1-default.

Persist resources

You can also persist some resources, which skips the normal garbage-collect process when the application is updated.

Take the following app as an example, in the garbage-collect policy, a rule is added which marks all the resources created by the expose trait to use the onAppDelete strategy. This will keep those services until application is deleted.

  1. $ cat <<EOF | vela up -f -
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: garbage-collect-app
  6. spec:
  7. components:
  8. - name: hello-world
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. traits:
  13. - type: expose
  14. properties:
  15. port: [8000]
  16. policies:
  17. - name: garbage-collect
  18. type: garbage-collect
  19. properties:
  20. rules:
  21. - selector:
  22. traitTypes:
  23. - expose
  24. strategy: onAppDelete
  25. EOF

You can find deployment and service created.

  1. $ kubectl get deployment
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. hello-world 1/1 1 1 74s
  4. $ kubectl get service
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. hello-world ClusterIP 10.96.160.208 <none> 8000/TCP 78s

If you upgrade the application and use a different component, you will find the old versioned deployment is deleted but the service is kept.

  1. $ cat <<EOF | vela up -f -
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: garbage-collect-app
  6. spec:
  7. components:
  8. - name: hello-world-new
  9. type: webservice
  10. properties:
  11. image: oamdev/hello-world
  12. traits:
  13. - type: expose
  14. properties:
  15. port: [8000]
  16. policies:
  17. - name: garbage-collect
  18. type: garbage-collect
  19. properties:
  20. rules:
  21. - selector:
  22. traitTypes:
  23. - expose
  24. strategy: onAppDelete
  25. EOF
  26. $ kubectl get deployment
  27. NAME READY UP-TO-DATE AVAILABLE AGE
  28. hello-world-new 1/1 1 1 10s
  29. $ kubectl get service
  30. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  31. hello-world ClusterIP 10.96.160.208 <none> 8000/TCP 5m56s
  32. hello-world-new ClusterIP 10.96.20.4 <none> 8000/TCP 13s

If you want to deploy job-like components, in which cases the resources in the component are not expected to be recycled even after the application is deleted, you can use the component type selector and set strategy to never as follows.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: garbage-collect-app
  5. spec:
  6. components:
  7. - name: hello-world-new
  8. type: job-like-component
  9. policies:
  10. - name: garbage-collect
  11. type: garbage-collect
  12. properties:
  13. rules:
  14. - selector:
  15. componentTypes:
  16. - webservice
  17. strategy: never

An alternative selector for the component resources is the component name selector.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: create-ns-app
  5. spec:
  6. components:
  7. - name: example-addon-namespace
  8. type: k8s-objects
  9. properties:
  10. objects:
  11. - apiVersion: v1
  12. kind: Namespace
  13. policies:
  14. - name: garbage-collect
  15. type: garbage-collect
  16. properties:
  17. rules:
  18. - selector:
  19. componentNames:
  20. - example-addon-namespace
  21. strategy: never