@@ -4,12 +4,21 @@ package internal
44import (
55 "errors"
66 "fmt"
7+ "os"
78 "path"
9+ "path/filepath"
810
911 yaml "gopkg.in/yaml.v3"
1012 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1113 "sigs.k8s.io/kustomize/api/krusty"
1214 "sigs.k8s.io/kustomize/kyaml/filesys"
15+
16+ "open-cluster-management.io/policy-generator-plugin/internal/types"
17+ )
18+
19+ const (
20+ localSchemaFileName = "schema.json"
21+ kustomizeDir = "kustomize"
1322)
1423
1524type manifestPatcher struct {
@@ -18,6 +27,7 @@ type manifestPatcher struct {
1827 // The Kustomize patches to apply on the manifests. Note that modifications are made
1928 // to the input maps. If this is an issue, provide a deep copy of the patches.
2029 patches []map [string ]interface {}
30+ openAPI types.OpenAPI
2131}
2232
2333// validateManifestInfo verifies that the apiVersion, kind, metadata.name fields from a manifest
@@ -169,14 +179,14 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
169179 // Create the file system in memory with the Kustomize YAML files
170180 fSys := filesys .MakeFsInMemory ()
171181
172- err := fSys . Mkdir ( kustomizeDir )
182+ err := InitializeInMemoryKustomizeDir ( fSys , m . openAPI . Path )
173183 if err != nil {
174- return nil , fmt .Errorf ("an unexpected error occurred when configuring Kustomize : %w" , err )
184+ return nil , fmt .Errorf ("failed to initialize Kustomize dir, err : %w" , err )
175185 }
176186
177- kustomizationYAMLFile := map [ string ][] interface {}{
178- "resources" : {},
179- "patches" : {},
187+ kustomizationYAMLFile := types. KustomizeJSON {}
188+ if m . openAPI . Path != "" {
189+ kustomizationYAMLFile . OpenAPI . Path = localSchemaFileName
180190 }
181191
182192 options := []struct {
@@ -206,13 +216,10 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
206216 }
207217
208218 if option .kustomizeKey != "patches" {
209- kustomizationYAMLFile [option .kustomizeKey ] = append (
210- kustomizationYAMLFile [option .kustomizeKey ], manifestFileName ,
211- )
219+ kustomizationYAMLFile .Resources = append (kustomizationYAMLFile .Resources , manifestFileName )
212220 } else {
213- kustomizationYAMLFile [option .kustomizeKey ] = append (
214- kustomizationYAMLFile [option .kustomizeKey ], map [string ]interface {}{"path" : manifestFileName },
215- )
221+ kustomizationYAMLFile .Patches = append (kustomizationYAMLFile .Patches ,
222+ types.Patch {Path : manifestFileName })
216223 }
217224 }
218225 }
@@ -253,3 +260,27 @@ func (m *manifestPatcher) ApplyPatches() ([]map[string]interface{}, error) {
253260
254261 return manifests , nil
255262}
263+
264+ // Initializes the in-memory file system with base directory and open API schema
265+ func InitializeInMemoryKustomizeDir (fSys filesys.FileSystem , schema string ) (err error ) {
266+ err = fSys .Mkdir (kustomizeDir )
267+ if err != nil {
268+ return fmt .Errorf ("an unexpected error occurred when configuring Kustomize: %w" , err )
269+ }
270+
271+ if schema != "" {
272+ schema = filepath .Clean (schema )
273+
274+ schemaJSON , err := os .ReadFile (schema )
275+ if err != nil {
276+ return fmt .Errorf ("unable to open file: %s, err: %w " , schema , err )
277+ }
278+
279+ err = fSys .WriteFile (path .Join (kustomizeDir , localSchemaFileName ), schemaJSON )
280+ if err != nil {
281+ return fmt .Errorf ("error writing schema, err: %w" , err )
282+ }
283+ }
284+
285+ return nil
286+ }
0 commit comments