Version: v1.0

How-to

In this section, it will introduce how to use simple template to declare Kubernetes API resource into a component.

Before reading this part, please make sure you’ve learned the definition and template concepts.

Declare ComponentDefinition

Here is a simple template based ComponentDefinition example which provides a abstraction for worker workload type:

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: ComponentDefinition
  3. metadata:
  4. name: kube-worker
  5. namespace: default
  6. spec:
  7. workload:
  8. definition:
  9. apiVersion: apps/v1
  10. kind: Deployment
  11. schematic:
  12. kube:
  13. template:
  14. apiVersion: apps/v1
  15. kind: Deployment
  16. spec:
  17. selector:
  18. matchLabels:
  19. app: nginx
  20. template:
  21. metadata:
  22. labels:
  23. app: nginx
  24. spec:
  25. containers:
  26. - name: nginx
  27. ports:
  28. - containerPort: 80
  29. parameters:
  30. - name: image
  31. required: true
  32. type: string
  33. fieldPaths:
  34. - "spec.template.spec.containers[0].image"

In detail, the .spec.schematic.kube contains template of a workload resource and configurable parameters.

  • .spec.schematic.kube.template is the simple template in YAML format.
  • .spec.schematic.kube.parameters contains a set of configurable parameters. The name, type, and fieldPaths are required fields, description and required are optional fields.
    • The parameter name must be unique in a ComponentDefinition.
    • type indicates the data type of value set to the field. This is a required field which will help KubeVela to generate a OpenAPI JSON schema for the parameters automatically. In simple template, only basic data types are allowed, including string, number, and boolean, while array and object are not.
    • fieldPaths in the parameter specifies an array of fields within the template that will be overwritten by the value of this parameter. Fields are specified as JSON field paths without a leading dot, for example spec.replicas, spec.containers[0].image.

Declare an Application

Here is an example Application.

  1. apiVersion: core.oam.dev/v1beta1
  2. kind: Application
  3. metadata:
  4. name: myapp
  5. namespace: default
  6. spec:
  7. components:
  8. - name: mycomp
  9. type: kube-worker
  10. properties:
  11. image: nginx:1.14.0

Since parameters only support basic data type, values in properties should be simple key-value, <parameterName>: <parameterValue>.

Deploy the Application and verify the running workload instance.

  1. $ kubectl get deploy
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. mycomp 1/1 1 1 66m

And check the parameter works.

  1. $ kubectl get deployment mycomp -o json | jq '.spec.template.spec.containers[0].image'
  2. "nginx:1.14.0"