|
| 1 | +apiVersion: policy.open-cluster-management.io/v1 |
| 2 | +kind: Policy |
| 3 | +metadata: |
| 4 | + name: argocd-policy-healthchecks |
| 5 | + annotations: |
| 6 | + policy.open-cluster-management.io/standards: NIST SP 800-53 |
| 7 | + policy.open-cluster-management.io/categories: CM Configuration Management |
| 8 | + policy.open-cluster-management.io/controls: CM-2 Baseline Configuration |
| 9 | + policy.open-cluster-management.io/description: >- |
| 10 | + This policy configures healthchecks for open-cluster-management-io Policy kinds on any ArgoCD |
| 11 | + instances found on the cluster. |
| 12 | +spec: |
| 13 | + remediationAction: inform |
| 14 | + disabled: false |
| 15 | + policy-templates: |
| 16 | + - objectDefinition: |
| 17 | + apiVersion: policy.open-cluster-management.io/v1 |
| 18 | + kind: ConfigurationPolicy |
| 19 | + metadata: |
| 20 | + name: config-argocd-policy-healthchecks |
| 21 | + spec: |
| 22 | + severity: medium |
| 23 | + # Apply the healthcheck configuration to all ArgoCD instances that are |
| 24 | + # found - this helps it to work on different environments, and ensures |
| 25 | + # that the configuration is not applied before the GitOps operator |
| 26 | + # creates the initial instance. |
| 27 | + object-templates-raw: | |
| 28 | + {{- range (lookup "argoproj.io/v1beta1" "ArgoCD" "" "").items }} |
| 29 | + - complianceType: musthave |
| 30 | + objectDefinition: |
| 31 | + apiVersion: argoproj.io/v1beta1 |
| 32 | + kind: ArgoCD |
| 33 | + metadata: |
| 34 | + name: {{ .metadata.name }} |
| 35 | + namespace: {{ .metadata.namespace }} |
| 36 | + spec: |
| 37 | + resourceHealthChecks: |
| 38 | + - group: policy.open-cluster-management.io |
| 39 | + kind: CertificatePolicy |
| 40 | + check: | |
| 41 | + hs = {} |
| 42 | + if obj.status == nil or obj.status.compliant == nil then |
| 43 | + hs.status = "Progressing" |
| 44 | + hs.message = "Waiting for the status to be reported" |
| 45 | + return hs |
| 46 | + end |
| 47 | + if obj.status.compliant == "Compliant" then |
| 48 | + hs.status = "Healthy" |
| 49 | + hs.message = "All certificates found comply with the policy" |
| 50 | + return hs |
| 51 | + else |
| 52 | + hs.status = "Degraded" |
| 53 | + hs.message = "At least once certificate does not comply with the policy" |
| 54 | + return hs |
| 55 | + end |
| 56 | + - group: policy.open-cluster-management.io |
| 57 | + kind: ConfigurationPolicy |
| 58 | + check: | |
| 59 | + hs = {} |
| 60 | + if obj.status == nil or obj.status.compliant == nil then |
| 61 | + hs.status = "Progressing" |
| 62 | + hs.message = "Waiting for the status to be reported" |
| 63 | + return hs |
| 64 | + end |
| 65 | + if obj.status.lastEvaluatedGeneration ~= obj.metadata.generation then |
| 66 | + hs.status = "Progressing" |
| 67 | + hs.message = "Waiting for the status to be updated" |
| 68 | + return hs |
| 69 | + end |
| 70 | + if obj.status.compliant == "Compliant" then |
| 71 | + hs.status = "Healthy" |
| 72 | + else |
| 73 | + hs.status = "Degraded" |
| 74 | + end |
| 75 | + if obj.status.compliancyDetails ~= nil then |
| 76 | + messages = {} |
| 77 | + for i, compliancy in ipairs(obj.status.compliancyDetails) do |
| 78 | + if compliancy.conditions ~= nil then |
| 79 | + for i, condition in ipairs(compliancy.conditions) do |
| 80 | + if condition.message ~= nil and condition.type ~= nil then |
| 81 | + table.insert(messages, condition.type .. " - " .. condition.message) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + hs.message = table.concat(messages, "; ") |
| 87 | + return hs |
| 88 | + end |
| 89 | + hs.status = "Progressing" |
| 90 | + hs.message = "Waiting for compliance" |
| 91 | + return hs |
| 92 | + - group: policy.open-cluster-management.io |
| 93 | + kind: OperatorPolicy |
| 94 | + check: | |
| 95 | + hs = {} |
| 96 | + if obj.status == nil or obj.status.conditions == nil then |
| 97 | + hs.status = "Progressing" |
| 98 | + hs.message = "Waiting for the status to be reported" |
| 99 | + return hs |
| 100 | + end |
| 101 | + if obj.status.observedGeneration ~= nil and obj.status.observedGeneration ~= obj.metadata.generation then |
| 102 | + hs.status = "Progressing" |
| 103 | + hs.message = "Waiting for the status to be updated" |
| 104 | + return hs |
| 105 | + end |
| 106 | + for i, condition in ipairs(obj.status.conditions) do |
| 107 | + if condition.type == "Compliant" then |
| 108 | + hs.message = condition.message |
| 109 | + if condition.status == "True" then |
| 110 | + hs.status = "Healthy" |
| 111 | + return hs |
| 112 | + else |
| 113 | + hs.status = "Degraded" |
| 114 | + return hs |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + hs.status = "Progressing" |
| 119 | + hs.message = "Waiting for the compliance condition" |
| 120 | + return hs |
| 121 | + - group: policy.open-cluster-management.io |
| 122 | + kind: Policy |
| 123 | + check: | |
| 124 | + hs = {} |
| 125 | + if obj.status == nil or obj.status.compliant == nil then |
| 126 | + hs.status = "Progressing" |
| 127 | + hs.message = "Waiting for the status to be reported" |
| 128 | + return hs |
| 129 | + end |
| 130 | + if obj.status.compliant == "Compliant" then |
| 131 | + hs.status = "Healthy" |
| 132 | + else |
| 133 | + hs.status = "Degraded" |
| 134 | + end |
| 135 | + noncompliants = {} |
| 136 | + if obj.status.status ~= nil then |
| 137 | + -- "root" policy |
| 138 | + for i, entry in ipairs(obj.status.status) do |
| 139 | + if entry.compliant ~= "Compliant" then |
| 140 | + noncompliants[i] = entry.clustername |
| 141 | + end |
| 142 | + end |
| 143 | + if table.getn(noncompliants) == 0 then |
| 144 | + hs.message = "All clusters are compliant" |
| 145 | + else |
| 146 | + hs.message = "NonCompliant clusters: " .. table.concat(noncompliants, ", ") |
| 147 | + end |
| 148 | + elseif obj.status.details ~= nil then |
| 149 | + -- "replicated" policy |
| 150 | + for i, entry in ipairs(obj.status.details) do |
| 151 | + if entry.compliant ~= "Compliant" then |
| 152 | + noncompliants[i] = entry.templateMeta.name |
| 153 | + end |
| 154 | + end |
| 155 | + if table.getn(noncompliants) == 0 then |
| 156 | + hs.message = "All templates are compliant" |
| 157 | + else |
| 158 | + hs.message = "NonCompliant templates: " .. table.concat(noncompliants, ", ") |
| 159 | + end |
| 160 | + end |
| 161 | + return hs |
| 162 | + {{- end }} |
0 commit comments