自定义策略

    本节将介绍通过 CUE 实现自定义策略,在开始之前,你要学习 模块定义 的基本概念和 。

    通过 Policy 生成资源类似于 Trait ,策略可用于跨 component 定义事物。

    让我们使用 创建一个基本的策略脚手架:

    我们希望创建一个这样的脚手架:

    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. }

    如下是一个通过策略创建流量拆分服务网格对象的示例。

    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. }

    把这个 Trait 应用到控制平面来使其生效:

    随后我们的终端用户可以立即发现并在 Application 中使用这个 Trait。

    执行 vela up 命令后:

    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

    如果需要,你可以在策略中定义任何 Kubernetes API 对象。

    并不是所有的策略都可以生成资源, 有几个 内置策略 用于控制整个交付过程和工作流程。

    提示

    这些特殊策略通常编写在 application controller 代码中,无需通过 CUE 自定义。