Policy Definition
Generate resources in policy is similar to trait, policy can be used to define things across components.
Let’s use to create a basic policy scaffold:
The content of the scaffold expected to be:
// $ cat myroute.cue
"my-plc": {
annotations: {}
attributes: {}
description: "My ingress route policy."
labels: {}
type: "policy"
}
template: {
}
Below is an example that we create a traffic split service mesh object in policy.
"my-plc": {
description: "My service mesh policy."
type: "policy"
}
template: {
#ServerWeight: {
weight: int
}
parameter: {
weights: [...#ServerWeight]
}
output: {
apiVersion: "split.smi-spec.io/v1alpha3"
kind: "TrafficSplit"
metadata: name: context.name
spec: {
service: context.name
backends: parameter.weights
}
}
}
Apply to our control plane to make this trait work:
Then our end users can discover it immediately and use it in Application
.
cat <<EOF | vela up -f -
apiVersion: core.oam.dev/v1beta1
metadata:
spec:
components:
- name: server-v1
type: webservice
properties:
image: oamdev/hello-world:v1
- name: server-v2
type: webservice
properties:
image: oamdev/hello-world:v2
policies:
- type: my-plc
name: unified
properties:
weights:
- service: server-v1
weight: 80
- service: server-v2
EOF
The policy will generate Kubernetes resources by KubeVela like below:
You can define any Kubernetes API objects in policies if you want.
Special Policy
Not all policies generate resources, there’re several built-in policies which are used to control the whole delivery precess and workflows.
These special polices are usually coded in the application controller.