@@ -16,13 +16,16 @@ import (
1616 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1717 "open-cluster-management.io/ocm-kustomize-generator-plugins/internal/expanders"
1818 "open-cluster-management.io/ocm-kustomize-generator-plugins/internal/types"
19+ "sigs.k8s.io/kustomize/api/krusty"
20+ "sigs.k8s.io/kustomize/kyaml/filesys"
1921)
2022
2123// getManifests will get all of the manifest files associated with the input policy configuration
2224// separated by policyConf.Manifests entries. An error is returned if a manifest path cannot
2325// be read.
2426func getManifests (policyConf * types.PolicyConfig ) ([][]map [string ]interface {}, error ) {
2527 manifests := [][]map [string ]interface {}{}
28+ hasKustomize := map [string ]bool {}
2629
2730 for _ , manifest := range policyConf .Manifests {
2831 manifestPaths := []string {}
@@ -34,6 +37,8 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
3437 return nil , readErr
3538 }
3639
40+ resolvedFiles := []string {}
41+
3742 if manifestPathInfo .IsDir () {
3843 files , err := ioutil .ReadDir (manifest .Path )
3944 if err != nil {
@@ -45,14 +50,26 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
4550 continue
4651 }
4752
48- ext := path .Ext (f .Name ())
53+ filepath := f .Name ()
54+ ext := path .Ext (filepath )
55+
4956 if ext != ".yaml" && ext != ".yml" {
5057 continue
5158 }
59+ // Handle when a Kustomization directory is specified
60+ _ , filename := path .Split (filepath )
61+ if filename == "kustomization.yml" || filename == "kustomization.yaml" {
62+ hasKustomize [manifest .Path ] = true
63+ resolvedFiles = []string {manifest .Path }
64+
65+ break
66+ }
5267
5368 yamlPath := path .Join (manifest .Path , f .Name ())
54- manifestPaths = append (manifestPaths , yamlPath )
69+ resolvedFiles = append (resolvedFiles , yamlPath )
5570 }
71+
72+ manifestPaths = append (manifestPaths , resolvedFiles ... )
5673 } else {
5774 // Unmarshal the manifest in order to check for metadata patch replacement
5875 manifestFile , err := unmarshalManifestFile (manifest .Path )
@@ -85,7 +102,15 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
85102 }
86103
87104 for _ , manifestPath := range manifestPaths {
88- manifestFile , err := unmarshalManifestFile (manifestPath )
105+ var manifestFile * []map [string ]interface {}
106+ var err error
107+
108+ if hasKustomize [manifestPath ] {
109+ manifestFile , err = processKustomizeDir (manifestPath )
110+ } else {
111+ manifestFile , err = unmarshalManifestFile (manifestPath )
112+ }
113+
89114 if err != nil {
90115 return nil , err
91116 }
@@ -245,6 +270,28 @@ func setNamespaceSelector(policyConf *types.PolicyConfig, policyTemplate *map[st
245270 }
246271}
247272
273+ // processKustomizeDir runs a provided directory through Kustomize in order to generate the manifests within it.
274+ func processKustomizeDir (path string ) (* []map [string ]interface {}, error ) {
275+ k := krusty .MakeKustomizer (krusty .MakeDefaultOptions ())
276+
277+ resourceMap , err := k .Run (filesys .MakeFsOnDisk (), path )
278+ if err != nil {
279+ return nil , fmt .Errorf ("failed to process provided kustomize directory '%s': %w" , path , err )
280+ }
281+
282+ manifestsYAML , err := resourceMap .AsYaml ()
283+ if err != nil {
284+ return nil , fmt .Errorf ("failed to convert the kustomize manifest(s) to YAML from directory '%s': %w" , path , err )
285+ }
286+
287+ manifests , err := unmarshalManifestBytes (manifestsYAML )
288+ if err != nil {
289+ return nil , fmt .Errorf ("failed to read the kustomize manifest(s) from directory '%s': %w" , path , err )
290+ }
291+
292+ return manifests , nil
293+ }
294+
248295// buildPolicyTemplate generates single policy template by using objectTemplates with manifests.
249296// policyNum defines which number the configuration policy is in the policy. If it is greater than
250297// one then the configuration policy name will have policyNum appended to it.
0 commit comments