Component Composition

This section will introduce the dependencies in components and how to pass data between components.

We use helm in the examples, make sure you enable the fluxcd addon:

  1. vela addon enable fluxcd

Dependency

We can use dependsOn to specify the dependencies between components.

For example, component A depends on component B:

  1. ...
  2. components:
  3. - name: A
  4. type: helm
  5. dependsOn:
  6. - B
  7. - name: B
  8. type: helm

In this case, KubeVela will deploy B first, and then deploy A when the component B is running.

How to use

If we want to apply a MySQL cluster, we need:

  1. Apply a secret for MySQL password.
  2. Apply MySQL controller.
  3. Apply MySQL cluster.

Apply the following file:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: mysql
  5. namespace: default
  6. spec:
  7. components:
  8. - name: mysql-secret
  9. type: raw
  10. properties:
  11. apiVersion: v1
  12. kind: Secret
  13. metadata:
  14. name: mysql-secret
  15. type: kubernetes.io/opaque
  16. stringData:
  17. ROOT_PASSWORD: test
  18. - name: mysql-controller
  19. type: helm
  20. properties:
  21. repoType: helm
  22. url: https://presslabs.github.io/charts
  23. chart: mysql-operator
  24. version: "0.4.0"
  25. - name: mysql-cluster
  26. type: raw
  27. dependsOn:
  28. - mysql-controller
  29. - mysql-secret
  30. properties:
  31. apiVersion: mysql.presslabs.org/v1alpha1
  32. kind: MysqlCluster
  33. metadata:
  34. name: mysql-cluster
  35. spec:
  36. replicas: 1
  37. secretName: mysql-secret

Expected Outcome

Check the application in the cluster:

  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. mysql mysql-secret raw runningWorkflow 2021-10-14 12:09:55 +0800 CST
  4. ├─ mysql-controller helm runningWorkflow 2021-10-14 12:09:55 +0800 CST
  5. └─ mysql-cluster raw runningWorkflow 2021-10-14 12:09:55 +0800 CST

In the beginning, the status is running workflow since the mysql-controller is not ready.

  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. mysql mysql-secret raw running healthy 2021-10-14 12:09:55 +0800 CST
  4. ├─ mysql-controller helm running healthy 2021-10-14 12:09:55 +0800 CST
  5. └─ mysql-cluster raw running 2021-10-14 12:09:55 +0800 CST

After a while, all components is running successfully. The mysql-cluster will be deployed after mysql-controller and mysql-secret is healthy.

dependsOn use healthy to check status. If the component is healthy, then KubeVela will deploy the next component. If you want to customize the healthy status of the component, please refer to Status Write Back

Inputs and Outputs

In KubeVela, we can use inputs and outputs in Components to pass data.

Outputs

Outputs is made of name and valueFrom. Input will use name to reference output.

We can write valueFrom in the following ways:

  1. Use value expression, eg. valueFrom: output.metadata.name. Note that output is a built-in field referring to the resource in the component that is rendered and deployed to the cluster.
  2. Use CUE expressions, eg. use + to combine value and string: valueFrom: output.metadata.name + "testString" or you can import built-in packages in CUE like:
  1. valueFrom: |
  2. import "strings"
  3. strings.Join(["1","2"], ",")

Inputs

Inputs is made of from and parameterKey. Input uses from to reference output, parameterKey is a expression that assigns the value of the input to the corresponding field.

eg.

  1. Specify inputs:
  1. ...
  2. - name: wordpress
  3. type: helm
  4. inputs:
  5. - from: mysql-svc
  6. parameterKey: properties.values.externalDatabase.host
  1. The field parameterKey specifies the field path of the parameter key in component to be assigned after rendering:

Which means the input value will be passed into the below properties:

  1. ...
  2. - name: wordpress
  3. type: helm
  4. properties:
  5. values:
  6. externalDatabase:
  7. host: <input value>

How to use

In the following we will apply a WordPress server with the MySQL address passed from a MySQL component:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: wordpress-with-mysql
  5. namespace: default
  6. spec:
  7. components:
  8. - name: mysql
  9. type: helm
  10. outputs:
  11. # the output is the mysql service address
  12. - name: mysql-svc
  13. valueFrom: output.metadata.name + ".default.svc.cluster.local"
  14. properties:
  15. repoType: helm
  16. url: https://charts.bitnami.com/bitnami
  17. chart: mysql
  18. version: "8.8.2"
  19. values:
  20. auth:
  21. rootPassword: mypassword
  22. - name: wordpress
  23. type: helm
  24. inputs:
  25. # set the host to mysql service address
  26. - from: mysql-svc
  27. parameterKey: properties.values.externalDatabase.host
  28. properties:
  29. repoType: helm
  30. url: https://charts.bitnami.com/bitnami
  31. chart: wordpress
  32. version: "12.0.3"
  33. values:
  34. mariadb:
  35. enabled: false
  36. externalDatabase:
  37. user: root
  38. password: mypassword
  39. database: mysql
  40. port: 3306

Expected Outcome

Check the application in the cluster:

  1. $ vela ls
  2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
  3. wordpress-with-mysql mysql helm running healthy 2021-10-12 18:04:10 +0800 CST
  4. └─ wordpress helm running healthy 2021-10-12 18:04:10 +0800 CST

The WordPress with MySQL has been successfully applied.