diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index feb09e67d..01a1c70f8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -474,6 +474,27 @@ jobs: with: path: velero-mpol + velero-vpol: + needs: kubernetes-matrix + strategy: + fail-fast: false + matrix: + k8s-version: ${{ fromJSON(needs.kubernetes-matrix.outputs.versions) }} + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - name: Setup Environment + uses: ./.github/actions/setup-env + with: + k8s-version: ${{ matrix.k8s-version }} + node-image: ${{ fromJSON(needs.kubernetes-matrix.outputs.images)[matrix.k8s-version] }} + kind-version: ${{ needs.kubernetes-matrix.outputs.kind-version }} + - name: Run Tests + uses: ./.github/actions/run-tests + with: + path: velero-vpol + flux-vpol: needs: kubernetes-matrix strategy: @@ -520,6 +541,7 @@ jobs: - psa-mpol - psp-migration-mpol - velero-mpol + - velero-vpol - flux-vpol runs-on: ubuntu-latest if: ${{ success() }} @@ -551,6 +573,7 @@ jobs: - psa-mpol - psp-migration-mpol - velero-mpol + - velero-vpol - flux-vpol runs-on: ubuntu-latest if: ${{ failure() || cancelled() }} diff --git a/velero-vpol/validate-cron-schedule/.chainsaw-test/bad-schedule.yaml b/velero-vpol/validate-cron-schedule/.chainsaw-test/bad-schedule.yaml new file mode 100644 index 000000000..1f510435f --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.chainsaw-test/bad-schedule.yaml @@ -0,0 +1,29 @@ +apiVersion: velero.io/v1 +kind: Schedule +metadata: + name: badschedule01 +spec: + schedule: 0 7 * * * * + template: + includedNamespaces: + - 'default' +--- +apiVersion: velero.io/v1 +kind: Schedule +metadata: + name: badschedule02 +spec: + schedule: 0 7 * */ * + template: + includedNamespaces: + - 'default' +--- +apiVersion: velero.io/v1 +kind: Schedule +metadata: + name: badschedule03 +spec: + schedule: 0 24 * * * + template: + includedNamespaces: + - 'default' diff --git a/velero-vpol/validate-cron-schedule/.chainsaw-test/chainsaw-test.yaml b/velero-vpol/validate-cron-schedule/.chainsaw-test/chainsaw-test.yaml new file mode 100644 index 000000000..a1416be4d --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.chainsaw-test/chainsaw-test.yaml @@ -0,0 +1,38 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json +apiVersion: chainsaw.kyverno.io/v1alpha1 +kind: Test +metadata: + creationTimestamp: null + name: validate-cron-schedule +spec: + template: false + steps: + - name: step-01 + try: + - assert: + file: crd-assert.yaml + - apply: + file: rbac.yaml + - name: step-02 + try: + - apply: + file: ../validate-cron-schedule.yaml + - patch: + resource: + apiVersion: policies.kyverno.io/v1 + kind: ValidatingPolicy + metadata: + name: validate-cron-schedule + spec: + validationActions: [Deny] + - assert: + file: policy-ready.yaml + - name: step-03 + try: + - apply: + file: good-schedule.yaml + - apply: + expect: + - check: + ($error != null): true + file: bad-schedule.yaml diff --git a/velero-vpol/validate-cron-schedule/.chainsaw-test/crd-assert.yaml b/velero-vpol/validate-cron-schedule/.chainsaw-test/crd-assert.yaml new file mode 100644 index 000000000..9b0937ad9 --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.chainsaw-test/crd-assert.yaml @@ -0,0 +1,13 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: schedules.velero.io +spec: {} +status: + acceptedNames: + kind: Schedule + listKind: ScheduleList + plural: schedules + singular: schedule + storedVersions: + - v1 diff --git a/velero-vpol/validate-cron-schedule/.chainsaw-test/good-schedule.yaml b/velero-vpol/validate-cron-schedule/.chainsaw-test/good-schedule.yaml new file mode 100644 index 000000000..aa101f657 --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.chainsaw-test/good-schedule.yaml @@ -0,0 +1,21 @@ +apiVersion: velero.io/v1 +kind: Schedule +metadata: + name: goodschedule01 +spec: + schedule: 0 7 * * * + template: + includedNamespaces: + - '*' + excludedNamespaces: + - some-namespace +--- +apiVersion: velero.io/v1 +kind: Schedule +metadata: + name: goodschedule02 +spec: + schedule: '*/15 0-6 1,15 * 1-5' + template: + includedNamespaces: + - 'default' diff --git a/velero-vpol/validate-cron-schedule/.chainsaw-test/policy-ready.yaml b/velero-vpol/validate-cron-schedule/.chainsaw-test/policy-ready.yaml new file mode 100644 index 000000000..a59db904c --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.chainsaw-test/policy-ready.yaml @@ -0,0 +1,15 @@ +apiVersion: policies.kyverno.io/v1 +kind: ValidatingPolicy +metadata: + name: validate-cron-schedule +status: + conditionStatus: + (conditions[?type == 'RBACPermissionsGranted']): + - message: Policy is ready for reporting. + reason: Succeeded + status: "True" + (conditions[?type == 'WebhookConfigured']): + - message: Webhook configured. + reason: Succeeded + status: "True" + type: WebhookConfigured diff --git a/velero-vpol/validate-cron-schedule/.chainsaw-test/rbac.yaml b/velero-vpol/validate-cron-schedule/.chainsaw-test/rbac.yaml new file mode 100644 index 000000000..a8fdbba17 --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.chainsaw-test/rbac.yaml @@ -0,0 +1,21 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: kyverno:aggregate-to-admission-controller:velero-validate-cron-schedule + labels: + rbac.kyverno.io/aggregate-to-admission-controller: "true" +rules: + - apiGroups: [velero.io] + resources: [schedules] + verbs: [get, list, watch] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: kyverno:aggregate-to-reports-controller:velero-validate-cron-schedule + labels: + rbac.kyverno.io/aggregate-to-reports-controller: "true" +rules: + - apiGroups: [velero.io] + resources: [schedules] + verbs: [get, list, watch] diff --git a/velero-vpol/validate-cron-schedule/.kyverno-test/kyverno-test.yaml b/velero-vpol/validate-cron-schedule/.kyverno-test/kyverno-test.yaml new file mode 100644 index 000000000..766fbca6b --- /dev/null +++ b/velero-vpol/validate-cron-schedule/.kyverno-test/kyverno-test.yaml @@ -0,0 +1,23 @@ +apiVersion: cli.kyverno.io/v1alpha1 +kind: Test +metadata: + name: validate-cron-schedule +policies: +- ../validate-cron-schedule.yaml +resources: +- ../.chainsaw-test/bad-schedule.yaml +- ../.chainsaw-test/good-schedule.yaml +results: +- policy: validate-cron-schedule + kind: Schedule + resources: + - badschedule01 + - badschedule02 + - badschedule03 + result: fail +- policy: validate-cron-schedule + kind: Schedule + resources: + - goodschedule01 + - goodschedule02 + result: pass diff --git a/velero-vpol/validate-cron-schedule/artifacthub-pkg.yml b/velero-vpol/validate-cron-schedule/artifacthub-pkg.yml new file mode 100644 index 000000000..4850f80c8 --- /dev/null +++ b/velero-vpol/validate-cron-schedule/artifacthub-pkg.yml @@ -0,0 +1,23 @@ +name: validate-cron-schedule-vpol +version: 1.0.0 +displayName: Validate Cron Schedule in CEL expressions +description: >- + A Velero Schedule is given in Cron format and must be accurate to ensure operation. This policy validates that the schedule is a valid Cron format. +install: |- + ```shell + kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/velero-vpol/validate-cron-schedule/validate-cron-schedule.yaml + ``` +keywords: + - velero + - kyverno + - CEL Expressions +readme: | + A Velero Schedule is given in Cron format and must be accurate to ensure operation. This policy validates that the schedule is a valid Cron format. + + Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/ +annotations: + kyverno/category: "Velero in CEL" + kyverno/kubernetesVersion: "1.34-1.35" + kyverno/subject: "Schedule" +digest: caa5f1e5bb9a77e57d2ef4279e2b174e5cb54ce75f6f7d1e9a9621ee77b6aabf +createdAt: "2026-11-21T09:24:20Z" diff --git a/velero-vpol/validate-cron-schedule/validate-cron-schedule.yaml b/velero-vpol/validate-cron-schedule/validate-cron-schedule.yaml new file mode 100644 index 000000000..be1a91d17 --- /dev/null +++ b/velero-vpol/validate-cron-schedule/validate-cron-schedule.yaml @@ -0,0 +1,31 @@ +apiVersion: policies.kyverno.io/v1 +kind: ValidatingPolicy +metadata: + name: validate-cron-schedule + annotations: + policies.kyverno.io/title: Validate Schedule in CEL expressions + policies.kyverno.io/category: Velero in CEL + policies.kyverno.io/severity: medium + policies.kyverno.io/subject: Schedule + kyverno.io/kyverno-version: 1.17.0 + policies.kyverno.io/minversion: 1.17.0 + kyverno.io/kubernetes-version: "1.34-1.35" + policies.kyverno.io/description: >- + A Velero Schedule is given in Cron format and must be accurate to ensure + operation. This policy validates that the schedule is a valid Cron format. +spec: + validationActions: + - Audit + evaluation: + background: + enabled: true + matchConstraints: + resourceRules: + - apiGroups: ["velero.io"] + apiVersions: [v1] + operations: [CREATE, UPDATE] + resources: [schedules] + validations: + - message: The backup schedule must be in a valid cron format. + expression: >- + object.spec.schedule.matches('^((?:\\*|[0-5]?[0-9](?:(?:-[0-5]?[0-9])|(?:,[0-5]?[0-9])+)?)(?:\\/[0-9]+)?)\\s+((?:\\*|(?:1?[0-9]|2[0-3])(?:(?:-(?:1?[0-9]|2[0-3]))|(?:,(?:1?[0-9]|2[0-3]))+)?)(?:\\/[0-9]+)?)\\s+((?:\\*|(?:[1-9]|[1-2][0-9]|3[0-1])(?:(?:-(?:[1-9]|[1-2][0-9]|3[0-1]))|(?:,(?:[1-9]|[1-2][0-9]|3[0-1]))+)?)(?:\\/[0-9]+)?)\\s+((?:\\*|(?:[1-9]|1[0-2])(?:(?:-(?:[1-9]|1[0-2]))|(?:,(?:[1-9]|1[0-2]))+)?)(?:\\/[0-9]+)?)\\s+((?:\\*|[0-7](?:-[0-7]|(?:,[0-7])+)?)(?:\\/[0-9]+)?)$')