See the PolicyGenerator reference YAML to see the full structure of the PolicyGenerator manifest.
The policy generator plugin is a binary written in Go. The binary accepts an input manifest of the
PolicyGenerator kind in the policy.open-cluster-management.io/v1 API. When Kustomize finds a
manifest of that kind under the generators: array in kustomization.yaml, it looks in its
directories for a binary of the same name ("PolicyGenerator") in the
policy.open-cluster-management.io/vi/policygenerator directory and calls the binary as
<path>/PolicyGenerator <cached-manifest> to run the generator as its plugin. The flag
--enable-alpha-plugins must be specified in the Kustomize command in order for the external
generator plugin to be enabled.
By default, a Placement and PlacementBinding are created for each policy with the policy name as the
suffix. To signal that you'd like to consolidate policies that use the same Placement under a single
PlacementBinding, specify placement.placementPath to an existing Placement manifest or set
placement.name along with placement.labelSelector. When the PlacementBinding is consolidated in
this way, placementBindingDefaults.name must be specified so that the generator can create unique
names for the bindings.
Policy expanders provide logic to create additional policies based on a given kind to give a
complete picture of violations or status using Open Cluster Management policies. Generally, the
expanders point to kinds provided by policy engines such as Kyverno and
Gatekeeper. These expanders are enabled by
default but can be disabled individually by setting the flag associated with the expander in the
PolicyGenerator manifest either in policyDefaults or for a particular manifest in the policies
array.
To contribute a policy expander, you'll need to:
- Add your expander to the
getExpanders()method in expanders.go and familiarize yourself with the Interface there that you'll be implementing. - Add your expander file to the internal/expanders/ directory. You can follow the other files there as an example.
- Choose a name for your boolean expander setting. (Existing names have followed the pattern
Inform<engine-name>Policies.) - Add your expander setting to both the
PolicyDefaultsand thePolicyConfigstructs in types.go - Add your expander setting to the
applyDefaults()method in plugin.go to set defaults for bothPolicyDefaultsandPolicies. - Update the policygenerator-reference.yaml with your expander setting.
- Add tests for your expander to the internal/expanders/ directory.
DIRECTORY TREE PACKAGE DESCRIPTION
================================================================================================
.
├── cmd
│ └── main.go main Parent binary (imports the internal package)
└── internal
├── expanders
│ ├── expanders.go expanders Policy expander interface
├── types
│ └── types.go types Generator structs
├── patches.go internal Code to patch input manifests
├── plugin.go internal Primary generator methods
├── typohelper.go internal Helpers for identifying manifest typos
├── utils.go internal Helper/utility functions
The Policy Generator supports OpenAPI schemas through Kustomize (see the
openapi Kustomize
documentation). The goal of this feature is to support patching non-Kubernetes custom resource
objects that contain list of objects. The OpenAPI object in this project has the same format of the
OpenAPI object in the Kustomize project. The path indicates the relative path of the schema JSON
file relative to the kustomization.yaml file:
openapi:
path: schema.jsonThe OpenAPI object is included with the manifest object in the plugin file:
apiVersion: policy.open-cluster-management.io/v1
kind: PolicyGenerator
. . .
policies:
- name: myapp
manifests:
- path: input-kustomize/
patches: []
openapi:
path: schema.jsonIdeally the OpenAPI schema is provided by the developper of the Custom Resource (CR). To retrieve a schema from a running Kubernetes cluster manually, do the following:
kustomize openapi fetchThen cut and paste the subset containing the resources that need to be patched. Next, identify the list objects in the schema and select a key from the fields of the object that would be use to index the list, for instance a name. After the definition of the list, add the following text:
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge"x-kubernetes-patch-merge-key indicates the field in the object that is used to uniquely identify
it in the list in this case the name field. x-kubernetes-patch-strategy indicates the patch
strategy. Merge would merge fields, replace would replace the object identified by the key with
patch content.
Note: The "key" selected in this step is used in patches to uniquely identify a list object. An example of schema is shown at openapi-schema.json