Skip to content

Commit cecc1d3

Browse files
edcdavidopenshift-merge-bot[bot]
authored andcommitted
Addressing comments from Dale
Signed-off-by: David Elie-Dit-Cosaque <[email protected]>
1 parent a7f8eaf commit cecc1d3

16 files changed

+182
-950
lines changed

docs/policygenerator-reference.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ policies:
279279
# An example modification to the manifest
280280
annotations:
281281
friends-character: Chandler Bing
282-
# The OpenAPI schema used to merge patches (useful for non Kubernetes CRs that containt list of items)
282+
# The OpenAPI schema used to merge patches (useful for non-Kubernetes CRs that contain lists of items)
283283
openapi:
284-
# The path to the OpenAPI schema to use when applying patches defined in patches
284+
# The path to the OpenAPI schema to use when applying patches defined from the `patches` array.
285285
path: ""
286286
# Optional. (See policyDefaults.categories for description.)
287287
categories:

docs/policygenerator.md

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,50 +75,46 @@ DIRECTORY TREE PACKAGE DESCRIPTION
7575
├── typohelper.go internal Helpers for identifying manifest typos
7676
├── utils.go internal Helper/utility functions
7777
```
78-
## Openapi schema support
79-
policy-genenerator-plugin supports openapi schemas as defined in https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/openapi by Kustomize. The goal of this feature is to support patching non kubernetest CR objects that contain list of objects.
80-
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
81-
``` yaml
78+
79+
## OpenAPI schema support
80+
The Policy Generator supports OpenAPI schemas as defined in
81+
https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/openapi by Kustomize. The goal of this feature is
82+
to support patching non-Kubernetes custom resource objects that contain list of objects. The OpenAPI object in this
83+
project has the same format of the OpenAPI object in the Kustomize project. The path indicates the relative path of the
84+
schema JSON file relative to the `kustomization.yaml` file:
85+
```yaml
8286
openapi:
83-
path: schema.json
87+
path: schema.json
8488
```
85-
The openapi object is part of the manifest object in the plugin file:
86-
``` yaml
89+
The OpenAPI object is included with the manifest object in the plugin file:
90+
```yaml
8791
apiVersion: policy.open-cluster-management.io/v1
8892
kind: PolicyGenerator
89-
...
93+
. . .
9094
policies:
91-
- name: myapp
92-
manifests:
93-
- path: input-kustomize/
94-
patches:
95-
openapi:
96-
path: schema.json
95+
- name: myapp
96+
manifests:
97+
- path: input-kustomize/
98+
patches: []
99+
openapi:
100+
path: schema.json
97101
```
98102
### How to create a Kustomize schema manually
99-
Ideally the openapi schema should be provided by the developper of the Custom Resource (CR).
100-
To retrieve a schema from a running kubernetes cluster manually, do the following:
101-
102-
``` default
103+
Ideally the OpenAPI schema is provided by the developper of the Custom Resource (CR). To retrieve a schema from a
104+
running Kubernetes cluster manually, do the following:
105+
```shell
103106
kustomize openapi fetch
104107
```
105-
106108
Then cut and paste the subset containing the resources that need to be patched.
107-
108-
Next, identify the list objects in the schema and select a key from the fields
109-
of the object that would be use to index the list, for instance a name. After
110-
the definition of the list, add the following text:
111-
112-
``` default
113-
"x-kubernetes-patch-merge-key": "name",
114-
"x-kubernetes-patch-strategy": "merge"
109+
Next, identify the list objects in the schema and select a key from the fields of the object that would be use to index
110+
the list, for instance a name. After the definition of the list, add the following text:
111+
```yaml
112+
"x-kubernetes-patch-merge-key": "name",
113+
"x-kubernetes-patch-strategy": "merge"
115114
```
116-
117-
`x-kubernetes-patch-merge-key` indicates the field in the object that is used to
118-
uniquely identify it in the list in this case the `name` field
119-
`x-kubernetes-patch-strategy` indicates the patch strategy. Merge would merge
120-
fields, replace would replace the object identified by the key with patch
121-
content.
115+
`x-kubernetes-patch-merge-key` indicates the field in the object that is used to uniquely identify it in the list in
116+
this case the `name` field. `x-kubernetes-patch-strategy` indicates the patch strategy. Merge would merge fields,
117+
replace would replace the object identified by the key with patch content.
122118
`Note:` The "key" selected in this step is used in patches to uniquely identify a list object.
123-
124-
An example of schema for the ptp-operator PtpConfig CR is shown at [link](internal/testdata/OpenAPI/newptpconfig-schema.json)
119+
An example of schema is shown at
120+
[link](internal/testdata/OpenAPI/openapi-schema.json)openapi-schema.json)

examples/policyGenerator-kustomize.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ policies:
1717
- metadata:
1818
name: "myname"
1919
openapi:
20-
path: schema.json
20+
path: schema.json

internal/patches.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@ const (
2121
kustomizeDir = "kustomize"
2222
)
2323

24+
type KustomizeJSON struct {
25+
types.Filepath `json:"openapi,omitempty" yaml:"openapi,omitempty"`
26+
Patches []Patch `json:"patches" yaml:"patches"`
27+
Resources []string `json:"resources" yaml:"resources"`
28+
}
29+
30+
type Patch struct {
31+
Path string `yaml:"path,omitempty" json:"path,omitempty"`
32+
}
2433
type manifestPatcher struct {
2534
// The manifests to patch.
2635
manifests []map[string]interface{}
2736
// The Kustomize patches to apply on the manifests. Note that modifications are made
2837
// to the input maps. If this is an issue, provide a deep copy of the patches.
2938
patches []map[string]interface{}
30-
openAPI types.OpenAPI
39+
openAPI types.Filepath
3140
}
3241

3342
// validateManifestInfo verifies that the apiVersion, kind, metadata.name fields from a manifest
@@ -181,12 +190,12 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
181190

182191
err := InitializeInMemoryKustomizeDir(fSys, m.openAPI.Path)
183192
if err != nil {
184-
return nil, fmt.Errorf("failed to initialize Kustomize dir, err: %w", err)
193+
return nil, fmt.Errorf("failed to initialize Kustomize dir: %w", err)
185194
}
186195

187-
kustomizationYAMLFile := types.KustomizeJSON{}
196+
kustomizationYAMLFile := KustomizeJSON{}
188197
if m.openAPI.Path != "" {
189-
kustomizationYAMLFile.OpenAPI.Path = localSchemaFileName
198+
kustomizationYAMLFile.Filepath.Path = localSchemaFileName
190199
}
191200

192201
options := []struct {
@@ -219,7 +228,7 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
219228
kustomizationYAMLFile.Resources = append(kustomizationYAMLFile.Resources, manifestFileName)
220229
} else {
221230
kustomizationYAMLFile.Patches = append(kustomizationYAMLFile.Patches,
222-
types.Patch{Path: manifestFileName})
231+
Patch{Path: manifestFileName})
223232
}
224233
}
225234
}

internal/patches_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestValidate(t *testing.T) {
5050
},
5151
}
5252

53-
openAPIConfig := types.OpenAPI{Path: ""}
53+
openAPIConfig := types.Filepath{Path: ""}
5454

5555
patcher := manifestPatcher{manifests: manifests, patches: patches, openAPI: openAPIConfig}
5656
err := patcher.Validate()

0 commit comments

Comments
 (0)