kubewarden / helm-charts

Helm charts for the Kubewarden project
Apache License 2.0
25 stars 16 forks source link

Feature request: Delete pre `1.11` (Cluster)PolicyReports when upgrading to `1.11` #408

Closed viccuad closed 5 months ago

viccuad commented 5 months ago

Pre 1.11, (Cluster)PolicyReports where created as follows:

From 1.11 onwards, (Cluster)PolicyReports are created as follows:

The audit-scanner in 1.11 doesn't deal with the old policy reports format, leaving them hanging behind. Still, downstream consumers such as Rancher Kubewarden UI and Policy Reporter UI would read both the old format and the new, potentially showing old report results that are bound to be outdated.

Acceptance criteria

viccuad commented 5 months ago

I have the Helm template, and I'm iterating with the kubectl delete command. Note that the kubewarden/kubectl image has as entrypoint kubectl and doesn't start a shell, so we can't do fancy things.

The following command deletes all reports owned by Kubewarden, pre and post 1.11. I find this too invasive since this will happen in all helm upgrade:

kubectl delete clusterpolicyreport,policyreport \
  -l app.kubernetes.io/managed-by=kubewarden

The following command tries to delete those reports that match the pre 1.11 metadata.name schema, but this is not possible, as neither field selectors nor set-based selectors allow for wildcards or prefixes:

kubectl delete clusterpolicyreport,policyreport \
  -l app.kubernetes.io/managed-by=kubewarden \
  --field-selector metadata.name==polr-clusterwide \ # correct
  --field-selector metadata.name==polr-ns-<ns name> # not doable

Hence, I would like to add a new label to 1.11 reports that would allow to target the pre 1.11 reports, because they would be missing this new label as only 1.11 reports will have it:

metadata:
  labels:
    app.kubernetes.io/managed-by: kubewarden
    kubewarden.io/policyreport-version: v2
flavio commented 5 months ago

I think this is a good idea. In that way the delete could use the selector kubewarden.io/policyreport-version not in "v2", or something like that

viccuad commented 5 months ago

the selector should be matching for the label to not be present, or we may delete v3 policyreports in the future for example. The ideal would have been to match v1 for deletion, but, well, hindsight 50/50.

viccuad commented 5 months ago

There doesn't seem to be a way to match for missing labels, so the job will grow in time as follows:

kubectl delete clusterpolicyreport,policyreport \
  -l app.kubernetes.io/managed-by=kubewarden \
  --field-selector kubewarden.io/policyreport-version!=v2 \
  --field-selector kubewarden.io/policyreport-version!=v3 \
  etc

But by that time, we can assume v1 reports are not around, and match for ==v3 or ==v4 anyways.

Edit: This suffices,

kubectl get clusterpolicyreport,policyreport \
  -l app.kubernetes.io/managed-by=kubewarden \
  -l '!kubewarden.io/policyreport-version'