19.14. 应用 RAN 策略来监控集群活动

ZTP 使用 Red Hat Advanced Cluster Management (RHACM) 来使用基于策略的监管方法自动监控集群活动,以应用无线访问网络 (RAN) 策略。

策略生成器 (PolicyGen) 是一个 Kustomize 插件,可帮助从预定义的自定义资源创建 ACM 策略。有三个主要项目:策略类别、源 CR 策略和 PolicyGenTemplate。PolicyGen 依赖于这些策略生成策略及其放置绑定和规则。

下图显示了 RAN 策略生成器如何与 GitOps 和 ACM 交互。

RAN 策略生成器

RAN 策略分为三个主要组:

Common
Common category 中存在的策略应用于由站点计划表示的所有集群。
Groups 类别中存在的策略应用到一组集群。每个集群组都可以具有自己在组类别下的策略。例如,Groups/group1 可以具有自己的策略,应用到属于 group1 的 群集。
Sites
Sites 类别中存在的策略应用到特定的集群。任何集群可能有自己的策略,它们存在于 Sites 类别中。例如,Sites/cluster1 会将自己的策略应用到 cluster1

下图显示了策略的生成方式。

生成策略

19.14.1. 应用源自定义资源策略

源自定义资源策略包括以下几项:

  • SR-IOV 策略
  • PTP 策略
  • Performance Add-on Operator 策略
  • MachineConfigPool 策略
  • SCTP 策略

您需要定义用于生成 ACM 策略的源自定义资源,并考虑可能的元数据或 spec/data 覆盖。例如,common-namespace-policy 包含所有受管集群中存在的 Namespace 定义。此 命名空间 放置在 Common 类别下,其 spec 或所有集群中的数据没有更改。

命名空间策略示例

以下示例显示了此命名空间的源自定义资源:

apiVersion: v1
kind: Namespace
metadata:
 name: openshift-sriov-network-operator
 labels:
   openshift.io/run-level: "1"

输出示例

应用此 命名空间 生成的策略包括上面定义的命名空间,而不做任何更改,如下例所示:

apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:
   name: common-sriov-sub-ns-policy
   namespace: common-sub
   annotations:
       policy.open-cluster-management.io/categories: CM Configuration Management
       policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
       policy.open-cluster-management.io/standards: NIST SP 800-53
spec:
   remediationAction: enforce
   disabled: false
   policy-templates:
       - objectDefinition:
           apiVersion: policy.open-cluster-management.io/v1
           kind: ConfigurationPolicy
           metadata:
               name: common-sriov-sub-ns-policy-config
           spec:
               remediationAction: enforce
               severity: low
               namespaceselector:
                   exclude:
                       - kube-*
                   include:
                       - '*'
               object-templates:
                   - complianceType: musthave
                     objectDefinition:
                       apiVersion: v1
                       kind: Namespace
                       metadata:
                           labels:
                               openshift.io/run-level: "1"
                           name: openshift-sriov-network-operator

SRIOV 策略示例

以下示例显示了存在于不同集群中的一个 SriovNetworkNodePolicy 定义,每个集群都有不同的规格。这个示例还显示了 SriovNetworkNodePolicy 的源自定义资源:

apiVersion: sriovnetwork.openshift.io/v1
kind: SriovNetworkNodePolicy
metadata:
  name: sriov-nnp
  namespace: openshift-sriov-network-operator
spec:
  # The $ tells the policy generator to overlay/remove the spec.item in the generated policy.
  deviceType: $deviceType
  isRdma: false
  nicSelector:
    pfNames: [$pfNames]
  nodeSelector:
    node-role.kubernetes.io/worker: ""
  numVfs: $numVfs
  priority: $priority
  resourceName: $resourceName

输出示例

对于所有集群,SriovNetworkNodePolicy 名称和 namespace 相同,因此在 source SriovNetworkNodePolicy 中定义。但是,生成的策略需要 $deviceType$numVfs 作为输入参数,以便调整每个集群的策略。本例中显示了生成的策略:

apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:
    name: site-du-sno-1-sriov-nnp-mh-policy
    namespace: sites-sub
    annotations:
        policy.open-cluster-management.io/categories: CM Configuration Management
        policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
        policy.open-cluster-management.io/standards: NIST SP 800-53
spec:
    remediationAction: enforce
    disabled: false
    policy-templates:
        - objectDefinition:
            apiVersion: policy.open-cluster-management.io/v1
            kind: ConfigurationPolicy
            metadata:
                name: site-du-sno-1-sriov-nnp-mh-policy-config
            spec:
                remediationAction: enforce
                severity: low
                namespaceselector:
                    exclude:
                        - kube-*
                    include:
                        - '*'
                object-templates:
                    - complianceType: musthave
                      objectDefinition:
                        apiVersion: sriovnetwork.openshift.io/v1
                        kind: SriovNetworkNodePolicy
                        metadata:
                            name: sriov-nnp-du-mh
                            namespace: openshift-sriov-network-operator
                        spec:
                            deviceType: vfio-pci
                            isRdma: false
                            nicSelector:
                                pfNames:
                                    - ens7f0
                            nodeSelector:
                                node-role.kubernetes.io/worker: ""
                            numVfs: 8
                            resourceName: du_mh
注意

不强制将所需的输入参数定义为 $value,如 $deviceType$ 告知策略生成器从生成的策略中覆盖或删除项目。否则,值不会改变。

19.14.2. PolicyGenTemplate

PolicyGenTemplate.yaml 文件是一个自定义资源定义(CRD),用于告知 PolicyGen 在何处对生成的策略和需要过量的策略进行分类。

以下示例显示了 PolicyGenTemplate.yaml 文件:

apiVersion: ran.openshift.io/v1
kind: PolicyGenTemplate
metadata:
  name: "group-du-sno"
  namespace: "group-du-sno"
spec:
  bindingRules:
    group-du-sno: ""
  mcp: "master"
  sourceFiles:
    - fileName: ConsoleOperatorDisable.yaml
      policyName: "console-policy"
    - fileName: ClusterLogging.yaml
      policyName: "cluster-log-policy"
      spec:
        curation:
          curator:
            schedule: "30 3 * * *"
          collection:
            logs:
              type: "fluentd"
              fluentd: {}

group-du-ranGen.yaml 文件在名为 group-du 的组中定义了一组策略。此文件定义 MachineConfigPool worker-du,用作 sourceFiles 中定义的任何其他策略的节点选择器。为 sourceFiles 中的所有源文件生成 ACM 策略。此外,生成单个放置绑定和放置规则,为 group-du 策略应用集群选择规则。

使用源文件 PtpConfigSlave.yaml 作为示例,PtpConfigSlave 具有 PtpConfig 自定义资源 (CR) 的定义。为 PtpConfigSlave 示例生成的策略名为 group-du-ptp-config-policy。生成的 group-du-ptp-config-policy 中定义的 PtpConfig CR 被命名为 du-ptp-slavePtpConfigSlave.yaml 中定义的 spec 放置在 du-ptp-slave 下,以及与源文件中定义的其他 spec 项目一起放置。

以下示例显示了 group-du-ptp-config-policy:

apiVersion: policy.open-cluster-management.io/v1
kind: Policy
metadata:
  name: group-du-ptp-config-policy
  namespace: groups-sub
  annotations:
    policy.open-cluster-management.io/categories: CM Configuration Management
    policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
    policy.open-cluster-management.io/standards: NIST SP 800-53
spec:
    remediationAction: enforce
    disabled: false
    policy-templates:
        - objectDefinition:
            apiVersion: policy.open-cluster-management.io/v1
            kind: ConfigurationPolicy
            metadata:
                name: group-du-ptp-config-policy-config
            spec:
                remediationAction: enforce
                severity: low
                namespaceselector:
                    exclude:
                        - kube-*
                    include:
                        - '*'
                object-templates:
                    - complianceType: musthave
                      objectDefinition:
                        apiVersion: ptp.openshift.io/v1
                        kind: PtpConfig
                        metadata:
                            name: slave
                            namespace: openshift-ptp
                        spec:
                            recommend:
                                - match:
                                - nodeLabel: node-role.kubernetes.io/worker-du
                                  priority: 4
                                  profile: slave
                            profile:
                                - interface: ens5f0
                                  name: slave
                                  phc2sysOpts: -a -r -n 24
                                  ptp4lConf: |
                                    [global]
                                    #
                                    # Default Data Set
                                    #
                                    twoStepFlag 1
                                    slaveOnly 0
                                    priority1 128
                                    priority2 128
                                    domainNumber 24
                                    .....

19.14.3. 创建自定义资源策略时的注意事项

  • 定义用于创建 ACM 策略的自定义资源时,应考虑可能的元数据和 spec/data 覆盖。例如,如果自定义资源 metadata.name 在集群间没有改变,则应在自定义资源文件中设置 metadata.name 值。如果自定义资源在同一集群中有多个实例,则必须在策略模板文件中定义自定义资源 metadata.name
  • 要为特定机器配置池应用节点选择器,您必须将节点选择器值设置为 $mcp,以便策略生成器使用策略模板中定义的 mcp 覆盖 $mcp 值。
  • 订阅源文件不会改变。

19.14.4. 生成 RAN 策略

先决条件

流程

  1. 配置 kustomization.yaml 文件来引用 policyGenerator.yaml 文件。以下示例显示了 PolicyGenerator 定义:

    apiVersion: policyGenerator/v1
    kind: PolicyGenerator
    metadata:
      name: acm-policy
      namespace: acm-policy-generator
    # The arguments should be given and defined as below with same order --policyGenTempPath= --sourcePath= --outPath= --stdout --customResources
    argsOneLiner: ./ranPolicyGenTempExamples ./sourcePolicies ./out true false

    其中:

    • policyGenTempPathpolicyGenTemp 文件的路径。
    • sourcePath:是源策略的路径。
    • outPath:是保存生成的 ACM 策略的路径。
    • stdout:如果为 true,请将生成的策略输出到控制台。
    • CustomResources: 如果为 true,从 sourcePolicies 文件生成没有 ACM 策略的 CR。
  2. 运行以下命令来测试 PolicyGen:

    $ cd cnf-features-deploy/ztp/ztp-policy-generator/
    $ XDG_CONFIG_HOME=./ kustomize build --enable-alpha-plugins

    out 目录会创建并带有预期的策略,如下例所示:

    out
    ├── common
    │   ├── common-log-sub-ns-policy.yaml
    │   ├── common-log-sub-oper-policy.yaml
    │   ├── common-log-sub-policy.yaml
    │   ├── common-pao-sub-catalog-policy.yaml
    │   ├── common-pao-sub-ns-policy.yaml
    │   ├── common-pao-sub-oper-policy.yaml
    │   ├── common-pao-sub-policy.yaml
    │   ├── common-policies-placementbinding.yaml
    │   ├── common-policies-placementrule.yaml
    │   ├── common-ptp-sub-ns-policy.yaml
    │   ├── common-ptp-sub-oper-policy.yaml
    │   ├── common-ptp-sub-policy.yaml
    │   ├── common-sriov-sub-ns-policy.yaml
    │   ├── common-sriov-sub-oper-policy.yaml
    │   └── common-sriov-sub-policy.yaml
    ├── groups
    │   ├── group-du
    │   │   ├── group-du-mc-chronyd-policy.yaml
    │   │   ├── group-du-mc-mount-ns-policy.yaml
    │   │   ├── group-du-mcp-du-policy.yaml
    │   │   ├── group-du-mc-sctp-policy.yaml
    │   │   ├── group-du-policies-placementbinding.yaml
    │   │   ├── group-du-policies-placementrule.yaml
    │   │   ├── group-du-ptp-config-policy.yaml
    │   │   └── group-du-sriov-operconfig-policy.yaml
    │   └── group-sno-du
    │       ├── group-du-sno-policies-placementbinding.yaml
    │       ├── group-du-sno-policies-placementrule.yaml
    │       ├── group-sno-du-console-policy.yaml
    │       ├── group-sno-du-log-forwarder-policy.yaml
    │       └── group-sno-du-log-policy.yaml
    └── sites
        └── site-du-sno-1
            ├── site-du-sno-1-policies-placementbinding.yaml
            ├── site-du-sno-1-policies-placementrule.yaml
            ├── site-du-sno-1-sriov-nn-fh-policy.yaml
            ├── site-du-sno-1-sriov-nnp-mh-policy.yaml
            ├── site-du-sno-1-sriov-nw-fh-policy.yaml
            ├── site-du-sno-1-sriov-nw-mh-policy.yaml
            └── site-du-sno-1-.yaml

    通用策略是扁平的,因为它们将应用到所有群集。但是,组和站点具有每个组和站点的子目录,因为它们将应用到不同的群集。