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:

    1. // $ cat myroute.cue
    2. "my-plc": {
    3. annotations: {}
    4. attributes: {}
    5. description: "My ingress route policy."
    6. labels: {}
    7. type: "policy"
    8. }
    9. template: {
    10. }

    The rule is aligned with component definition, you must specify output, while you can use outputs for more objects, the format as below:

    1. "my-plc": {
    2. description: "My service mesh policy."
    3. type: "policy"
    4. }
    5. template: {
    6. #ServerWeight: {
    7. }
    8. parameter: {
    9. weights: [...#ServerWeight]
    10. }
    11. output: {
    12. apiVersion: "split.smi-spec.io/v1alpha3"
    13. kind: "TrafficSplit"
    14. metadata: name: context.name
    15. spec: {
    16. service: context.name
    17. backends: parameter.weights
    18. }
    19. }
    20. }

    Apply to our control plane to make this Policy work:

    Then our end users can discover it immediately and use it in Application.

    After using vela up by the following command:

    1. cat <<EOF | vela up -f -
    2. apiVersion: core.oam.dev/v1beta1
    3. metadata:
    4. spec:
    5. components:
    6. - name: server-v1
    7. type: webservice
    8. properties:
    9. image: oamdev/hello-world:v1
    10. - name: server-v2
    11. type: webservice
    12. properties:
    13. image: oamdev/hello-world:v2
    14. policies:
    15. - type: my-plc
    16. name: unified
    17. properties:
    18. weights:
    19. - service: server-v1
    20. weight: 80
    21. - service: server-v2

    The policy will generate Kubernetes resources by KubeVela like below:

    Not all policies generate resources, there’re several built-in policies which are used to control the whole delivery precess and workflows.

    tip

    These special polices are usually coded in the application controller.