github.com/oam-dev/kubevela@v1.9.11/docs/examples/application/README.md (about)

     1  # Definition Docs
     2  
     3  ## Reserved word
     4  ### Patch
     5  Perform the CUE AND operation with the content declared by 'patch' and workload cr,
     6  
     7  you can define the strategy of list merge through comments, example as follows
     8  
     9  base model
    10   ```
    11  containers: [{
    12          name: "x1"
    13  }, {
    14          name: "x2"
    15          image:  string
    16          envs: [{
    17                  name: "OPS"
    18                  value: string
    19          }, ...]
    20  }, ...]
    21  ```
    22  define patch model
    23  ```
    24   // +patchKey=name
    25  containers: [{
    26          name: "x2"
    27          image:  "test-image"
    28          envs: [{
    29                  name: "OPS1"
    30                  value: "V-OPS1"
    31          },{
    32                  name: "OPS"
    33                  value: "V-OPS"
    34          }, ...]
    35  }, ...]
    36  ```
    37  and the result model after patch is follow
    38  ```
    39  containers: [{
    40          name: "x1" 
    41   },{
    42          name: "x2"
    43          image:  "test-image"
    44          envs: [{
    45                  name: "OPS1"
    46                  value: "V-OPS1"
    47          },{
    48                  name: "OPS"
    49                  value: "V-OPS"
    50          }, ...]
    51  }, ...]
    52   ```
    53  
    54  
    55  ### output
    56  Generate a new cr, which is generally associated with workload cr
    57  
    58  ## ComponentDefinition
    59  The following ComponentDefinition is to generate a deployment
    60  ```
    61  apiVersion: core.oam.dev/v1beta1
    62  kind: ComponentDefinition
    63  metadata:
    64    name: worker
    65    annotations:
    66      definition.oam.dev/description: "Long-running scalable backend worker without network endpoint"
    67  spec:
    68    workload:
    69      definition:
    70        apiVersion: apps/v1
    71        kind: Deployment
    72    schematic:
    73      cue:
    74        template: |
    75          output: {
    76            apiVersion: "apps/v1"
    77            kind:       "Deployment"
    78            spec: {
    79              selector: matchLabels: {
    80                "app.oam.dev/component": context.name
    81              }
    82  
    83              template: {
    84                metadata: labels: {
    85                  "app.oam.dev/component": context.name
    86                }
    87  
    88                spec: {
    89                  containers: [{
    90                    name:  context.name
    91                    image: parameter.image
    92  
    93                    if parameter["cmd"] != _|_ {
    94                      command: parameter.cmd
    95                    }
    96                  }]
    97                }
    98              }
    99            }
   100          }
   101  
   102          parameter: {
   103            // +usage=Which image would you like to use for your service
   104            // +short=i
   105            image: string
   106  
   107            cmd?: [...string]
   108          }
   109  ```
   110  
   111  If defined an application as follows
   112  ```
   113  apiVersion: core.oam.dev/v1beta1
   114  kind: Application
   115  metadata:
   116    name: application-sample
   117  spec:
   118    components:
   119      - name: myweb
   120        type: worker
   121        properties:
   122          image: "busybox"
   123          cmd:
   124          - sleep
   125          - "1000"
   126  ```
   127  we will get a deployment
   128  ```
   129  apiVersion: apps/v1
   130  kind: Deployment
   131  spec:
   132    selector:
   133      matchLabels:
   134        app.oam.dev/component: myweb
   135    template:
   136      metadata:
   137        labels:
   138          app.oam.dev/component: myweb
   139      spec:
   140        containers:
   141        - command:
   142          - sleep
   143          - "1000"
   144          image: busybox
   145          name: myweb
   146  ```
   147  ##  Service Trait Definition
   148  
   149  Define a trait Definition that appends service to workload(worker) , as shown below
   150  ```
   151  apiVersion: core.oam.dev/v1beta1
   152  kind: TraitDefinition
   153  metadata:
   154    annotations:
   155      definition.oam.dev/description: "service the app"
   156    name: kservice
   157  spec:
   158    appliesToWorkloads:
   159      - deployments.apps
   160    schematic:
   161      cue:
   162        template: |-
   163          patch: {spec: template: metadata: labels: app: context.name}
   164          outputs: service: {
   165            apiVersion: "v1"
   166            kind: "Service"
   167            metadata: name: context.name
   168            spec: {
   169              selector:  app: context.name
   170              ports: [
   171                for k, v in parameter.http {
   172                  port: v
   173                  targetPort: v
   174                }
   175              ]
   176            }
   177          }
   178          parameter: {
   179            http: [string]: int
   180          }
   181  ```
   182  
   183  If add service capability to the application, as follows
   184  ```
   185  apiVersion: core.oam.dev/v1beta1
   186  kind: Application
   187  metadata:
   188    name: application-sample
   189  spec:
   190    components:
   191      - name: myweb
   192        type: worker
   193        properties:
   194          image: "busybox"
   195          cmd:
   196          - sleep
   197          - "1000"
   198        traits:
   199          - type: kservice
   200            properties:
   201              http:
   202                server: 80
   203  ```
   204  
   205  we will get a new deployment and service
   206  ```
   207  // origin deployment  template add labels
   208  apiVersion: apps/v1
   209  kind: Deployment
   210  spec:
   211    selector:
   212      matchLabels:
   213        app.oam.dev/component: myweb
   214    template:
   215      metadata:
   216        labels:
   217          // add label app
   218          app: myweb
   219          app.oam.dev/component: myweb
   220      spec:
   221        containers:
   222        - command:
   223          - sleep
   224          - "1000"
   225          image: busybox
   226          name: myweb
   227  ---
   228  apiVersion: v1
   229  kind: Service
   230  metadata:
   231    name: myweb
   232  spec:
   233    ports:
   234    - port: 80
   235      targetPort: 80
   236    selector:
   237      app: myweb         
   238  ```
   239  
   240  ## Scaler Trait Definition
   241  
   242  Define a trait Definition that scale workload(worker) replicas
   243  ```
   244  apiVersion: core.oam.dev/v1beta1
   245  kind: TraitDefinition
   246  metadata:
   247    annotations:
   248      definition.oam.dev/description: "Manually scale the app"
   249    name: scaler
   250  spec:
   251    appliesToWorkloads:
   252      - deployments.apps
   253    schematic:
   254      cue:
   255        template: |-
   256          patch: {
   257            spec: replicas: parameter.replicas
   258          }
   259          parameter: {
   260            //+short=r
   261            replicas: *1 | int
   262          }
   263  ```
   264  If add scaler capability to the application, as follows
   265  ```
   266    components:
   267      - name: myweb
   268        type: worker
   269        properties:
   270          image: "busybox"
   271          cmd:
   272          - sleep
   273          - "1000"
   274        traits:
   275          - type: kservice
   276            properties:
   277              http:
   278                server: 80           
   279          - type: scaler
   280            properties:
   281              replicas: 10
   282  ```
   283  
   284  The deployment replicas will be scale to 10
   285  ```
   286  apiVersion: apps/v1
   287  kind: Deployment
   288  spec:
   289    selector:
   290      matchLabels:
   291        app.oam.dev/component: myweb
   292    // scale to 10    
   293    replicas: 10    
   294    template:
   295      metadata:
   296        labels:
   297          // add label app
   298          app: myweb
   299          app.oam.dev/component: myweb
   300      spec:
   301        containers:
   302        - command:
   303          - sleep
   304          - "1000"
   305          image: busybox
   306          name: myweb
   307  ```
   308  
   309  ## Sidecar Trait Definition
   310  
   311  Define a trait Definition that append containers to workload(worker)
   312  ```
   313  apiVersion: core.oam.dev/v1beta1
   314  kind: TraitDefinition
   315  metadata:
   316    annotations:
   317      definition.oam.dev/description: "add sidecar to the app"
   318    name: sidecar
   319  spec:
   320    appliesToWorkloads:
   321      - deployments.apps
   322    schematic:
   323      cue:
   324        template: |-
   325          patch: {
   326            // +patchKey=name
   327            spec: template: spec: containers: [parameter]
   328          }
   329          parameter: {
   330            name: string
   331            image: string
   332            command?: [...string]
   333          }
   334  ```
   335  
   336  If add sidercar capability to the application, as follows
   337  ```
   338  apiVersion: core.oam.dev/v1beta1
   339  kind: Application
   340  metadata:
   341    name: application-sample
   342  spec:
   343    components:
   344      - name: myweb
   345        type: worker
   346        properties:
   347          image: "busybox"
   348          cmd:
   349          - sleep
   350          - "1000"
   351        traits:
   352          - type: scaler
   353            properties:
   354              replicas: 10
   355          - type: sidecar
   356            properties:
   357              name: "sidecar-test"
   358              image: "nginx"
   359          - type: kservice
   360            properties:
   361              http:
   362                server: 80
   363  ```
   364  The deployment updated as follows
   365  ```
   366  apiVersion: apps/v1
   367  kind: Deployment
   368  spec:
   369    selector:
   370      matchLabels:
   371        app.oam.dev/component: myweb
   372    // scale to 10    
   373    replicas: 10    
   374    template:
   375      metadata:
   376        labels:
   377          // add label app
   378          app: myweb
   379          app.oam.dev/component: myweb
   380      spec:
   381        containers:
   382        - command:
   383          - sleep
   384          - "1000"
   385          image: busybox
   386          name: myweb
   387        - name: sidecar-test
   388          image: nginx  
   389  ```