Skip to content

Feat/462 configmap context#591

Open
yrsuthari wants to merge 3 commits into
kyverno:mainfrom
yrsuthari:feat/462-configmap-context
Open

Feat/462 configmap context#591
yrsuthari wants to merge 3 commits into
kyverno:mainfrom
yrsuthari:feat/462-configmap-context

Conversation

@yrsuthari

Copy link
Copy Markdown

Explanation

This PR adds support for ConfigMap context entries in kyverno-json, allowing policies to directly reference data stored in Kubernetes ConfigMaps. This enhances policy flexibility by externalizing configuration values into ConfigMaps that can be updated independently of the policies themselves, making policies more reusable and adaptable to different environments.

Related issue

Closes #462

What type of PR is this

/kind feature

Proposed Changes

  • Added ConfigMapReference type and ConfigMap field to the ContextEntry API
  • Created a ConfigMap client to fetch ConfigMap data from the Kubernetes API server
  • Updated the JSON engine compiler to handle ConfigMap references in context entries
  • Initialized ConfigMap client in the serve command
  • Added a mock ConfigMap client for playground mode for testing without a Kubernetes cluster
  • Added comprehensive unit tests for the ConfigMap functionality

Proof Manifests

ConfigMap resource

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  namespace: default
data:
  environment: "production"
  maxReplicas: "5"
  allowedEnvironments: "dev,staging,production"

Validating policy using ConfigMap

apiVersion: json.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
  name: configmap-example
spec:
  rules:
    - name: check-with-configmap
      context:
      - name: appConfig
        configMap:
          name: app-config
          namespace: default
      - name: allowedEnvs
        variable: "{{ appConfig.data.allowedEnvironments.split(',') }}"
      assert:
        all:
        - check:
            app.environment: "in {{ allowedEnvs }}"

Sample payload

{
  "app": {
    "name": "my-app",
    "environment": "production",
    "version": "1.0.0"
  }
}

Checklist

  • I have read the contributing guidelines.
  • I have read the PR documentation guide and followed the process including adding proof manifests to this PR.
  • This is a bug fix and I have added unit tests that prove my fix is effective.
  • This is a feature and I have added CLI tests that are applicable.
  • My PR needs to be cherry picked to a specific release branch which is .

Further Comments

The ConfigMap structure is accessed using the following pattern:

  • configMapName.data.<key> to access ConfigMap data fields
  • configMapName.metadata.name and configMapName.metadata.namespace to access metadata

The ConfigMap client is initialized in the serve command, and a mock version is used in playground mode to enable testing without requiring a real Kubernetes cluster.

Improves test coverage for:

- pkg/payload: from 0% to 95.2%

- pkg/engine/blocks/constant: from 0% to 100%

- pkg/engine/blocks/function: from 0% to 100%

Fixes kyverno#22

Signed-off-by: Yogi Suthari <yrsuthari@gmail.com>
Signed-off-by: Yogi Suthari <yrsuthari@gmail.com>
@yrsuthari yrsuthari force-pushed the feat/462-configmap-context branch from 3d9834f to 6ac2867 Compare March 31, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for context entries from config maps

1 participant