Version: v1.0
How-to
In this section, it will introduce how to use raw K8s Object to declare app components via ComponentDefinition.
Before reading this part, please make sure you’ve learned the definition and template concepts.
Declare ComponentDefinition
Here is a raw template based ComponentDefinition example which provides a abstraction for worker workload type:
apiVersion: core.oam.dev/v1beta1kind: ComponentDefinitionmetadata:name: kube-workernamespace: defaultspec:workload:definition:apiVersion: apps/v1kind: Deploymentschematic:kube:template:apiVersion: apps/v1kind: Deploymentspec:selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginxports:- containerPort: 80parameters:- name: imagerequired: truetype: stringfieldPaths:- "spec.template.spec.containers[0].image"
In detail, the .spec.schematic.kube contains template of a workload resource and configurable parameters.
.spec.schematic.kube.templateis the raw template in YAML format..spec.schematic.kube.parameterscontains a set of configurable parameters. Thename,type, andfieldPathsare required fields,descriptionandrequiredare optional fields.- The parameter
namemust be unique in aComponentDefinition. typeindicates 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 raw template, only basic data types are allowed, includingstring,number, andboolean, whilearrayandobjectare not.fieldPathsin 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 examplespec.replicas,spec.containers[0].image.
- The parameter
Declare an Application
Here is an example Application.
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: myappnamespace: defaultspec:components:- name: mycomptype: kube-workerproperties: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.
$ kubectl get deployNAME READY UP-TO-DATE AVAILABLE AGEmycomp 1/1 1 1 66m
And check the parameter works.
$ kubectl get deployment mycomp -o json | jq '.spec.template.spec.containers[0].image'"nginx:1.14.0"