Files
kubevela/config/samples/vela-server
Jian.Li 1f2c38d3c7 supports strategy patch for definition template (#798)
* supports strategy patch for definition template

* strategy unify

* lint code

* test cases

* add docs

* add test cases

* lint code

* add test cases for walk

* fix close fun

* lint code

* const variable for patchKey

* lint code

* interceptor for ast.Node

* add test cases

* check diff

* strategyUnify

* listMergeByKey

* prompts
2020-12-24 13:35:34 +08:00
..

Definition Docs

Reserved word

Patch

Perform the CUE AND operation with the content declared by 'patch' and workload cr,

you can define the strategy of list merge through comments, example as follows

base model

containers: [{
       name: "x1"
}, {
       name: "x2"
       image:  string
       envs: [{
               name: "OPS"
               value: string
       }, ...]
}, ...]

define patch model

 // +patchKey=name
containers: [{
        name: "x2"
        image:  "test-image"
        envs: [{
                name: "OPS1"
                value: "V-OPS1"
        },{
                name: "OPS"
                value: "V-OPS"
        }, ...]
}, ...]

and the result model after patch is follow

containers: [{
        name: "x1" 
 },{
        name: "x2"
        image:  "test-image"
        envs: [{
                name: "OPS1"
                value: "V-OPS1"
        },{
                name: "OPS"
                value: "V-OPS"
        }, ...]
}, ...]

output

Generate a new cr, which is generally associated with workload cr

Workload Definition

The following workload definition is to generate a deployment

apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
metadata:
  name: worker
  annotations:
    definition.oam.dev/description: "Long-running scalable backend worker without network endpoint"
spec:
  definitionRef:
    name: deployments.apps
  extension:
    template: |
      output: {
      	apiVersion: "apps/v1"
      	kind:       "Deployment"
      	spec: {
      		selector: matchLabels: {
      			"app.oam.dev/component": context.name
      		}

      		template: {
      			metadata: labels: {
      				"app.oam.dev/component": context.name
      			}

      			spec: {
      				containers: [{
      					name:  context.name
      					image: parameter.image

      					if parameter["cmd"] != _|_ {
      						command: parameter.cmd
      					}
      				}]
      			}
      		}
      	}
      }

      parameter: {
      	// +usage=Which image would you like to use for your service
      	// +short=i
      	image: string

      	cmd?: [...string]
      }

If defined an application as follows

apiVersion: core.oam.dev/v1alpha2
kind: Application
metadata:
  name: application-sample
spec:
  services:
    myweb:
      type: worker
      image: "busybox"
      cmd:
      - sleep
      - "1000"

we will get a deployment

apiVersion: apps/v1
kind: Deployment
spec:
  selector:
    matchLabels:
      app.oam.dev/component: myweb
  template:
    metadata:
      labels:
        app.oam.dev/component: myweb
    spec:
      containers:
      - command:
        - sleep
        - "1000"
        image: busybox
        name: myweb

Service Trait Definition

Define a trait Definition that appends service to workload(worker) , as shown below

apiVersion: core.oam.dev/v1alpha2
kind: TraitDefinition
metadata:
  annotations:
    definition.oam.dev/description: "service the app"
  name: service
spec:
  appliesToWorkloads:
    - webservice
    - worker
  definitionRef:
    name: service.v1
  extension:
    template: |-
      patch: {spec: template: metadata: labels: app: context.name}
      output: {
        apiVersion: "v1"
        kind: "Service"
        metadata: name: context.name
        spec: {
          selector:  app: context.name
          ports: [
            for k, v in parameter.http {
              port: v
              targetPort: v
            }
          ]
        }
      }
      parameter: {
        http: [string]: int
      }

If add service capability to the application, as follows

apiVersion: core.oam.dev/v1alpha2
kind: Application
metadata:
  name: application-sample
spec:
  services:
    myweb:
      type: worker
      image: "busybox"
      cmd:
      - sleep
      - "1000"
      service:
        http:
          server: 80

we will get a new deployment and service

// origin deployment  template add labels
apiVersion: apps/v1
kind: Deployment
spec:
  selector:
    matchLabels:
      app.oam.dev/component: myweb
  template:
    metadata:
      labels:
        // add label app
        app: myweb
        app.oam.dev/component: myweb
    spec:
      containers:
      - command:
        - sleep
        - "1000"
        image: busybox
        name: myweb
---
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: myweb         

Scaler Trait Definition

Define a trait Definition that scale workload(worker) replicas

apiVersion: core.oam.dev/v1alpha2
kind: TraitDefinition
metadata:
  annotations:
    definition.oam.dev/description: "Manually scale the app"
  name: scaler
spec:
  appliesToWorkloads:
    - webservice
    - worke
  extension:
    template: |-
      patch: {
         spec: replicas: parameter.replicas
      }
      parameter: {
      	//+short=r
      	replicas: *1 | int
      }

If add scaler capability to the application, as follows

apiVersion: core.oam.dev/v1alpha2
kind: Application
metadata:
  name: application-sample
spec:
  services:
    myweb:
      type: worker
      image: "busybox"
      cmd:
      - sleep
      - "1000"
      service:
        http:
          server: 80
      scaler:
        replicas: 10     

The deployment replicas will be scale to 10

apiVersion: apps/v1
kind: Deployment
spec:
  selector:
    matchLabels:
      app.oam.dev/component: myweb
  // scale to 10    
  replicas: 10    
  template:
    metadata:
      labels:
        // add label app
        app: myweb
        app.oam.dev/component: myweb
    spec:
      containers:
      - command:
        - sleep
        - "1000"
        image: busybox
        name: myweb

Sidecar Trait Definition

Define a trait Definition that append containers to workload(worker)

apiVersion: core.oam.dev/v1alpha2
kind: TraitDefinition
metadata:
  annotations:
    definition.oam.dev/description: "add sidecar to the app"
  name: sidecar
spec:
  appliesToWorkloads:
    - webservice
    - worke
  extension:
    template: |-
      patch: {
         // +patchKey=name
         spec: template: spec: containers: [parameter]
      }
      parameter: {
         name: string
         image: string
         command?: [...string]
      }

If add sidercar capability to the application, as follows

apiVersion: core.oam.dev/v1alpha2
kind: Application
metadata:
  name: application-sample
spec:
  services:
    myweb:
      type: worker
      image: "busybox"
      cmd:
      - sleep
      - "1000"
      service:
        http:
          server: 80
      scaler:
        replicas: 10    
      sidercar:
        name: "sidecar-test"
        image: "nginx"   

The deployment updated as follows

apiVersion: apps/v1
kind: Deployment
spec:
  selector:
    matchLabels:
      app.oam.dev/component: myweb
  // scale to 10    
  replicas: 10    
  template:
    metadata:
      labels:
        // add label app
        app: myweb
        app.oam.dev/component: myweb
    spec:
      containers:
      - command:
        - sleep
        - "1000"
        image: busybox
        name: myweb
      - name: sidecar-test
        image: nginx