2.3.5. カスタムポリシーコントローラーの作成

カスタムポリシーコントローラーの作成、適用、表示、および更新について説明します。ポリシーコントローラーがクラスターにデプロイする YAML ファイルを作成できます。以下のセクションを参照して、ポリシーコントローラーを作成します。

2.3.5.1. ポリシーコントローラーの作成

multicloud-operators-policy-controller リポジトリーにあるポリシーコントローラーフレームワークを使用します。ポリシーコントローラーを作成するには、以下の手順を完了します。

  1. 以下のコマンドを実行して multicloud-operators-policy-controller リポジトリーのクローンを作成します。

    git clone git@github.com:open-cluster-management/multicloud-operators-policy-controller.git
  2. ポリシースキーマ定義を更新してコントローラーポリシーをカスタマイズします。ポリシーは次のような内容になります。

    metadata:
      name: samplepolicies.policies.open-cluster-management.io
    spec:
      group: policy.open-cluster-management.io
      names:
        kind: SamplePolicy
        listKind: SamplePolicyList
        plural: samplepolicies
        singular: samplepolicy
  3. SamplePolicy の種類を監視するようにポリシーコントローラーを更新します。以下のコマンドを実行します。

    for file in $(find . -name "*.go" -type f); do  sed -i "" "s/SamplePolicy/g" $file; done
    for file in $(find . -name "*.go" -type f); do  sed -i "" "s/samplepolicy-controller/samplepolicy-controller/g" $file; done
  4. 以下の手順を実行してポリシーコントローラーを再コンパイルし、実行します。

    1. クラスターにログインします。
    2. ユーザーアイコンを選択し、クライアントの設定 をクリックします。
    3. 設定情報をコマンドラインにコピーアンドペーストし、Enter を押します。
    4. 以下のコマンドを実行してポリシー CRD を適用し、コントローラーを起動します。

      export GO111MODULE=on
      
      kubectl apply -f deploy/crds/policy.open-cluster-management.io_samplepolicies_crd.yaml
      
      operator-sdk run --local --verbose

      コントローラーが実行していることを示す以下の出力が表示される場合があります。

      {“level”:”info”,”ts”:1578503280.511274,”logger”:”controller-runtime.manager”,”msg”:”starting metrics server”,”path”:”/metrics”}
      {“level”:”info”,”ts”:1578503281.215883,”logger”:”controller-runtime.controller”,”msg”:”Starting Controller”,”controller”:”samplepolicy-controller”}
      {“level”:”info”,”ts”:1578503281.3203468,”logger”:”controller-runtime.controller”,”msg”:”Starting workers”,”controller”:”samplepolicy-controller”,”worker count”:1}
      Waiting for policies to be available for processing…
    5. ポリシーを作成し、コントローラーがポリシーを取得し、そのポリシーをクラスターに適用していることを確認します。以下のコマンドを実行します。

      kubectl apply -f deploy/crds/policy.open-cluster-management.io_samplepolicies_crd.yaml

      ポリシーが適用されると、カスタムコントローラーによってポリシーが監視され、検出されることを示すメッセージが表示されます。メッセージは次のような内容になります。

    {"level":"info","ts":1578503685.643426,"logger":"controller_samplepolicy","msg":"Reconciling SamplePolicy","Request.Namespace":"default","Request.Name":"example-samplepolicy"}
    {"level":"info","ts":1578503685.855259,"logger":"controller_samplepolicy","msg":"Reconciling SamplePolicy","Request.Namespace":"default","Request.Name":"example-samplepolicy"}
    Available policies in namespaces:
    namespace = kube-public; policy = example-samplepolicy
    namespace = default; policy = example-samplepolicy
    namespace = kube-node-lease; policy = example-samplepolicy
  5. 以下のコマンドを実行して、status フィールドでコンプライアンスの詳細を確認します。

    kubectl describe SamplePolicy example-samplepolicy -n default

    出力は次のような内容になります。

    status:
      compliancyDetails:
        example-samplepolicy:
          cluster-wide:
          - 5 violations detected in namespace `cluster-wide`, there are 0 users violations
            and 5 groups violations
          default:
          - 0 violations detected in namespace `default`, there are 0 users violations
            and 0 groups violations
          kube-node-lease:
          - 0 violations detected in namespace `kube-node-lease`, there are 0 users violations
            and 0 groups violations
          kube-public:
          - 1 violations detected in namespace `kube-public`, there are 0 users violations
            and 1 groups violations
      compliant: NonCompliant
  6. ポリシールールおよびポリシーロジックを変更して、ポリシーコントローラーの新規ルールを作成します。以下の手順を実行します。

    1. SamplePolicySpec を更新して、YAML ファイルに新規フィールドを追加します。仕様は次のような内容になります。

      spec:
        description: SamplePolicySpec defines the desired state of SamplePolicy
        properties:
          labelSelector:
            additionalProperties:
              type: string
            type: object
          maxClusterRoleBindingGroups:
            type: integer
          maxClusterRoleBindingUsers:
            type: integer
          maxRoleBindingGroupsPerNamespace:
            type: integer
          maxRoleBindingUsersPerNamespace:
            type: integer
    2. 新しいフィールドを持つ samplepolicy_controller.go で、SamplePolicySpec 構造を更新します。
    3. samplepolicy_controller.go ファイルの PeriodicallyExecSamplePolicies 関数を、ポリシーコントローラーを実行する新しいロジックで更新します。PeriodicallyExecSamplePolicies フィールドの例については、open-cluster-management/multicloud-operators-policy-controller を参照してください。
    4. ポリシーコントローラーを再コンパイルし、実行します。「ポリシーコントローラーの作成」を参照してください。

ポリシーコントローラーが機能します。