Version: v1.0

Attach Traits

Traits in KubeVela can be attached to Helm based component seamlessly.

In this sample application below, we add two traits, scaler and virtualgroup to a Helm based component.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: myapp
  5. namespace: default
  6. spec:
  7. components:
  8. - name: demo-podinfo
  9. type: webapp-chart
  10. properties:
  11. image:
  12. tag: "5.1.2"
  13. traits:
  14. - type: scaler
  15. properties:
  16. replicas: 4
  17. - type: virtualgroup
  18. properties:
  19. group: "my-group1"
  20. type: "cluster"

Note: when use traits with Helm based component, please make sure the target workload in your Helm chart strictly follows the qualified-full-name convention in Helm. For example in this chart, the workload name is composed of release name and chart name.

This is because KubeVela relies on the name to discovery the workload, otherwise it cannot apply traits to the workload. KubeVela will generate a release name based on your Application name and component name automatically, so you need to make sure never override the full name template in your Helm chart.

Verify traits work correctly

You may need to wait a few seconds to check the trait attached because of reconciliation interval.

Check the scaler trait takes effect.

  1. $ kubectl get manualscalertrait
  2. NAME AGE
  3. demo-podinfo-scaler-d8f78c6fc 13m
  1. $ kubectl get deployment myapp-demo-podinfo -o json | jq .spec.replicas
  2. 4

Check the virtualgroup trait.

  1. $ kubectl get deployment myapp-demo-podinfo -o json | jq .spec.template.metadata.labels
  2. {
  3. "app.cluster.virtual.group": "my-group1",
  4. "app.kubernetes.io/name": "myapp-demo-podinfo"
  5. }

Update Application

After the application is deployed and workloads/traits are created successfully, you can update the application, and corresponding changes will be applied to the workload instances.

Let’s make several changes on the configuration of the sample application.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: myapp
  5. namespace: default
  6. spec:
  7. components:
  8. - name: demo-podinfo
  9. type: webapp-chart
  10. properties:
  11. image:
  12. tag: "5.1.3" # 5.1.2 => 5.1.3
  13. traits:
  14. - type: scaler
  15. properties:
  16. replicas: 2 # 4 => 2
  17. - type: virtualgroup
  18. properties:
  19. group: "my-group2" # my-group1 => my-group2
  20. type: "cluster"

Apply the new configuration and check the results after several minutes.

Check the new values (image.tag = 5.1.3) from application’s properties are assigned to the chart.

  1. $ kubectl get deployment myapp-demo-podinfo -o json | jq '.spec.template.spec.containers[0].image'
  2. "ghcr.io/stefanprodan/podinfo:5.1.3"

Under the hood, Helm makes an upgrade to the release (revision 1 => 2).

  1. $ helm ls -A
  2. NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
  3. myapp-demo-podinfo default 2 2021-03-15 08:52:00.037690148 +0000 UTC deployed podinfo-5.1.4 5.1.4

Check the scaler trait.

  1. $ kubectl get deployment myapp-demo-podinfo -o json | jq .spec.replicas
  2. 2

Check the virtualgroup trait.

  1. $ kubectl get deployment myapp-demo-podinfo -o json | jq .spec.template.metadata.labels
  2. {
  3. "app.cluster.virtual.group": "my-group2",
  4. "app.kubernetes.io/name": "myapp-demo-podinfo"
  5. }

Detach Trait

Let’s have a try detach a trait from the application.

  1. apiVersion: core.oam.dev/v1alpha2
  2. kind: Application
  3. metadata:
  4. name: myapp
  5. namespace: default
  6. spec:
  7. components:
  8. - name: demo-podinfo
  9. type: webapp-chart
  10. settings:
  11. image:
  12. tag: "5.1.3"
  13. traits:
  14. # - name: scaler
  15. # properties:
  16. # replicas: 2
  17. - name: virtualgroup
  18. properties:
  19. group: "my-group2"
  20. type: "cluster"

Apply the application and check manualscalertrait has been deleted.

  1. $ kubectl get manualscalertrait
  2. No resources found