参数化模板

StoryBoard 链接: https://storyboard.openstack.org/#!/story/2004056

本文档提出了一种基于包含参数的模板快速创建 Vitrage 模板的方法。

问题描述

Vitrage 模板语言功能强大,但略显冗长,并且在从头开始创建新模板时很容易出现语法错误。此外,在实际生产环境中,我们发现许多模板具有非常相似的结构,例如“主机上的警报会传播到其实例”。我们希望更容易地生成最常见的模板。

此功能的另一个动机是 Heat 的使用。Heat 用户应该能够轻松定义 Vitrage 模板,以基于特定警报自动修复或自动扩展其堆栈。有关更多信息,请参阅 Heat_specHeat_template

提议的变更

模板的某些部分,例如警报名称、资源状态等,可以编写为 parameters(参数)。在创建新模板时,用户必须为没有默认值的每个参数分配实际值。该概念类似于参数在 Heat 中的使用方式。

可以将 Parameters 部分添加到标准模板中。每个 parameter 将包含两个可选字段

  • description

  • default

所有值都将是字符串。

可以使用 get_param(param) 语法在模板中使用参数。

Vitrage template add API 将包含一个新选项,用于基于包含参数的模板创建模板。只有在设置了所有参数后,模板才会添加到数据库中。

带有参数的模板示例

metadata:
  version: 2
  type: standard
  name: host_affects_instance_prototype
  description: Template prototype for scenarios where a Zabbix alarm on a host affects its instances
parameters:
  host_alarm_rawtext:
    description: Zabbix rawtext of the alarm on the host
  instance_alarm_name:
    description: Name of the alarm to be created on the instance
  instance_alarm_severity:
    description: Severity of the alarm to be created on the instance
    default: WARNING
  host_state:
    description: New state to be set for the host
    default: SUBOPTIMAL
  instance_state:
    description: New state to be set for the instance
    default: SUBOPTIMAL
definitions:
  entities:
   - entity:
      category: ALARM
      type: zabbix
      rawtext: get_param(host_alarm_rawtext)
      template_id: host_alarm
   - entity:
      category: ALARM
      type: vitrage
      name: get_param(instance_alarm_name)
      template_id: instance_alarm
   - entity:
      category: RESOURCE
      type: nova.instance
      template_id: instance
   - entity:
      category: RESOURCE
      type: nova.host
      template_id: host
  relationships:
   - relationship:
      source: host_alarm
      relationship_type: on
      target: host
      template_id : alarm_on_host
   - relationship:
      source: host
      relationship_type: contains
      target: instance
      template_id : host_contains_instance
   - relationship:
      source: instance_alarm
      relationship_type: on
      target: instance
      template_id : alarm_on_instance
scenarios:
  - scenario:
     condition: alarm_on_host and host_contains_instance
     actions:
      - action:
          action_type: raise_alarm
          action_target:
            target: instance
          properties:
            alarm_name: get_param(instance_alarm_name)
            severity: get_param(instance_alarm_severity)
      - action:
          action_type: set_state
          action_target:
            target: instance
          properties:
            state: get_param(instance_state)
  - scenario:
     condition: alarm_on_host and host_contains_instance and alarm_on_instance
     actions:
      - action:
          action_type: add_causal_relationship
          action_target:
            source: host_alarm
            target: instance_alarm
  - scenario:
     condition: alarm_on_host
     actions:
      - action:
          action_type: set_state
          action_target:
            target: host
          properties:
            state: SUBOPTIMAL

CLI 示例

vitrage template add --path ./host_affects_instance_prototype.yaml --param 'host_alarm_rawtext'='High CPU load on host' --param 'instance_alarm_name'='High CPU load on instance'

或者

vitrage template add --path ./host_affects_instance_prototype.yaml --params 'host_alarm_rawtext'='High CPU load on host', 'instance_alarm_name'='High CPU load on instance'

备选方案

保留模板原型目录

定义类型为 prototype(原型)的模板,这些模板将像其他模板一样添加到 Vitrage 数据库,但会被评估器忽略。

将提供 API 以

  • 添加模板原型

  • 查看所有模板原型

  • 基于现有原型名称添加模板

优点

  • 在运行时,Vitrage 将保留一个模板原型目录,供最终用户使用。

缺点

  • 由于用户不会经常添加新模板,因此优势并不重要。

  • 在 Heat 中使用原型功能会更困难,因为 HOT 模板必须依赖 Vitrage 的运行时信息。

  • 我们需要决定如何处理原型更新。如果 Vitrage 中的原型发生更改,则相同的 HOT 模板的行为将不同。

在 Heat 中实现此功能

与其在 Vitrage 中添加模板原型,不如在 Heat 中相当容易地实现原型功能。当然,主要的缺点是该功能将不可用于 Vitrage。

整个 Vitrage 模板可以以两种方式写入 Heat 模板中

  1. 作为 Heat 中的 Vitrage Template 资源的一部分。缺点是 Heat 资源将与 Vitrage 紧密耦合,Vitrage 中的任何语法更改都应反映在 Heat 中。

  2. 作为 Heat 中 Vitrage Template 资源内的注释。缺点是编写一个长 yaml 文件作为注释不太清晰,并且可能导致语法和对齐错误。

数据模型影响

无。模板参数将获得实际值,并且标准模板将被添加到数据库中。

REST API 影响

如果 path 指向包含参数的模板,则应将参数添加到 vitrage template add 函数调用中。模板中需要的所有参数都必须设置。

有两种方法可以指定参数

  • param:后跟键=值对。可以有几个 param 对。

  • params:后跟一个键=值参数列表,用“,”分隔。

版本影响

这些更改将是 Vitrage 模板版本 3 的一部分。

其他最终用户影响

部署者影响

开发者影响

Horizon 影响

无。将来此功能可用于编写模板编辑器 UI。

实现

负责人

主要负责人

ifat_afek

工作项

  • 引入 Vitrage 模板版本 3

  • 支持模板加载和参数注入,用于模板参数

  • 支持带有参数的模板的模板验证

  • template add API 的新参数

  • 文档和测试

依赖项

测试

单元测试、功能测试和 Tempest 测试

文档影响

将记录新的模板格式

参考资料