5.8.5. 使用 OLM 管理的 Operator 运行 scorecard

可使用集群服务版本(CSV)运行 scorecard,提供测试集群就绪和非 SDK Operator 的方法。

流程

  1. Scorecard 需要 Operator 部署 pod 中的代理容器来读取 Operator 日志。在使用 OLM 部署 Operator 之前,需要对 CSV 进行一些修改并创建一个额外的对象来运行代理。

    这一步可以手动执行或使用 bash 功能自动执行。选择以下任一方法。

    • 手动方法:

      1. 创建包含本地 kubeconfig 文件的代理服务器 secret。

        1. 使用 scorecard 代理的命名空间所有者引用生成用户名。

          $ echo '{"apiVersion":"","kind":"","name":"scorecard","uid":"","Namespace":"'<namespace>'"}' | base64 -w 0 1
          1
          <namespace> 替换为 Operator 将部署到的命名空间。
        2. 使用以下模板编写 Config 清单 scorecard-config.yaml,将 <username> 替换为上一步中生成的 base64 用户名:

          apiVersion: v1
          kind: Config
          clusters:
          - cluster:
              insecure-skip-tls-verify: true
              server: http://<username>@localhost:8889
            name: proxy-server
          contexts:
          - context:
              cluster: proxy-server
              user: admin/proxy-server
            name: <namespace>/proxy-server
          current-context: <namespace>/proxy-server
          preferences: {}
          users:
          - name: admin/proxy-server
            user:
              username: <username>
              password: unused
        3. Config 编码为 base64:

          $ cat scorecard-config.yaml | base64 -w 0
        4. 创建 Secret 清单 scorecard-secret.yaml

          apiVersion: v1
          kind: Secret
          metadata:
            name: scorecard-kubeconfig
            namespace: <namespace> 1
          data:
            kubeconfig: <kubeconfig_base64> 2
          1
          <namespace> 替换为 Operator 将部署到的命名空间。
          2
          <kubeconfig_base64> 替换为编码为 base64 的 Config
        5. 应用 secret:

          $ oc apply -f scorecard-secret.yaml
        6. 将一个指向 secret 的卷插入到 Operator 的部署中:

          spec:
            install:
              spec:
                deployments:
                - name: memcached-operator
                  spec:
                    ...
                    template:
                      ...
                      spec:
                        containers:
                        ...
                        volumes:
                        - name: scorecard-kubeconfig 1
                          secret:
                            secretName: scorecard-kubeconfig
                            items:
                            - key: kubeconfig
                              path: config
          1
          Scorecard kubeconfig 卷。
      2. 在 Operator 部署中将卷挂载和 KUBECONFIG 环境变量插入到每个容器中:

        spec:
          install:
            spec:
              deployments:
              - name: memcached-operator
                spec:
                  ...
                  template:
                    ...
                    spec:
                      containers:
                      - name: container1
                        ...
                        volumeMounts:
                        - name: scorecard-kubeconfig 1
                          mountPath: /scorecard-secret
                        env:
                        - name: KUBECONFIG 2
                          value: /scorecard-secret/config
                      - name: container2 3
                        ...
        1
        Scorecard kubeconfig 卷挂载。
        2
        Scorecard kubeconfig 环境变量。
        3
        对此容器和所有其他容器重复同一操作。
      3. 将 scorecard 代理容器插入到 Operator 的部署中:

        spec:
          install:
            spec:
              deployments:
              - name: memcached-operator
                spec:
                  ...
                  template:
                    ...
                    spec:
                      containers:
                      ...
                      - name: scorecard-proxy 1
                        command:
                        - scorecard-proxy
                        env:
                        - name: WATCH_NAMESPACE
                          valueFrom:
                            fieldRef:
                              apiVersion: v1
                              fieldPath: metadata.namespace
                        image: quay.io/operator-framework/scorecard-proxy:master
                        imagePullPolicy: Always
                        ports:
                        - name: proxy
                          containerPort: 8889
        1
        Scorecard 代理容器。
    • 自动方法:

      Community-operators 存储库有几个 bash 功能,可为您执行流程中的前面步骤。

      1. 运行以下 curl 命令:

        $ curl -Lo csv-manifest-modifiers.sh \
            https://raw.githubusercontent.com/operator-framework/community-operators/master/scripts/lib/file
      2. source csv-manifest-modifiers.sh 文件:

        $ . ./csv-manifest-modifiers.sh
      3. 创建 kubeconfig secret 文件:

        $ create_kubeconfig_secret_file scorecard-secret.yaml "<namespace>" 1
        1
        <namespace> 替换为 Operator 将部署到的命名空间。
      4. 应用 secret:

        $ oc apply -f scorecard-secret.yaml
      5. 插入 kubeconfig 卷:

        $ insert_kubeconfig_volume "<csv_file>" 1
        1
        <csv_file> 替换为 CSV 清单的路径。
      6. 插入 kubeconfig secret 挂载:

        $ insert_kubeconfig_secret_mount "<csv_file>"
      7. 插入代理容器:

        $ insert_proxy_container "<csv_file>" "quay.io/operator-framework/scorecard-proxy:master"
  2. 插入代理容器后,按照 Operator SDK 入门指南中的步骤捆绑 CSV 和 CRD,并在 OLM 上部署 Operator。
  3. 在 OLM 上部署了 Operator 后,请在 Operator 项目中定义 .osdk-scorecard.yaml 配置文件,并确保 csv-path: <csv_manifest_path>olm-deployed 选项都已设置。
  4. csv-path: <csv_manifest_path>olm-deployed 均已在 scorecard 配置文件中设置的情况下运行 scorecard:

    $ operator-sdk scorecard