@@ -15,10 +15,11 @@ import (
1515 "gopkg.in/yaml.v3"
1616)
1717
18- // getManifests will get all of the manifest files associated with the input policy configuration.
19- // An error is returned if a manifest path cannot be read.
20- func getManifests (policyConf * types.PolicyConfig ) ([]map [string ]interface {}, error ) {
21- manifests := []map [string ]interface {}{}
18+ // getManifests will get all of the manifest files associated with the input policy configuration
19+ // separated by policyConf.Manifests entries. An error is returned if a manifest path cannot
20+ // be read.
21+ func getManifests (policyConf * types.PolicyConfig ) ([][]map [string ]interface {}, error ) {
22+ manifests := [][]map [string ]interface {}{}
2223 for _ , manifest := range policyConf .Manifests {
2324 manifestPaths := []string {}
2425 manifestFiles := []map [string ]interface {}{}
@@ -107,7 +108,7 @@ func getManifests(policyConf *types.PolicyConfig) ([]map[string]interface{}, err
107108 manifestFiles = * patchedFiles
108109 }
109110
110- manifests = append (manifests , manifestFiles ... )
111+ manifests = append (manifests , manifestFiles )
111112 }
112113
113114 return manifests , nil
@@ -120,42 +121,45 @@ func getManifests(policyConf *types.PolicyConfig) ([]map[string]interface{}, err
120121// that each template includes a single manifest specified in policyConf.
121122// An error is returned if one or more manifests cannot be read or are invalid.
122123func getPolicyTemplates (policyConf * types.PolicyConfig ) ([]map [string ]map [string ]interface {}, error ) {
123- manifests , err := getManifests (policyConf )
124+ manifestGroups , err := getManifests (policyConf )
124125 if err != nil {
125126 return nil , err
126127 }
127128
128- if len (manifests ) == 0 {
129- return nil , fmt .Errorf (
130- "the policy %s must specify at least one non-empty manifest file" , policyConf .Name ,
131- )
132- }
133-
134- objectTemplatesLength := len (manifests )
129+ objectTemplatesLength := len (manifestGroups )
135130 policyTemplatesLength := 1
136131 if ! policyConf .ConsolidateManifests {
137- policyTemplatesLength = len (manifests )
132+ policyTemplatesLength = len (manifestGroups )
138133 objectTemplatesLength = 0
139134 }
140135 objectTemplates := make ([]map [string ]interface {}, 0 , objectTemplatesLength )
141136 policyTemplates := make ([]map [string ]map [string ]interface {}, 0 , policyTemplatesLength )
142- for _ , manifest := range manifests {
143- objTemplate := map [string ]interface {}{
144- "complianceType" : policyConf .ComplianceType ,
145- "objectDefinition" : manifest ,
146- }
147- if policyConf .ConsolidateManifests {
148- // put all objTemplate with manifest into single consolidated objectTemplates object
149- objectTemplates = append (objectTemplates , objTemplate )
150- } else {
151- // casting each objTemplate with manifest to objectTemplates type
152- // build policyTemplate for each objectTemplates
153- policyTemplate := buildPolicyTemplate (policyConf , & []map [string ]interface {}{objTemplate })
154- setNamespaceSelector (policyConf , policyTemplate )
155- policyTemplates = append (policyTemplates , * policyTemplate )
137+ for i , manifestGroup := range manifestGroups {
138+ complianceType := policyConf .Manifests [i ].ComplianceType
139+ for _ , manifest := range manifestGroup {
140+ objTemplate := map [string ]interface {}{
141+ "complianceType" : complianceType ,
142+ "objectDefinition" : manifest ,
143+ }
144+ if policyConf .ConsolidateManifests {
145+ // put all objTemplate with manifest into single consolidated objectTemplates object
146+ objectTemplates = append (objectTemplates , objTemplate )
147+ } else {
148+ // casting each objTemplate with manifest to objectTemplates type
149+ // build policyTemplate for each objectTemplates
150+ policyTemplate := buildPolicyTemplate (policyConf , & []map [string ]interface {}{objTemplate })
151+ setNamespaceSelector (policyConf , policyTemplate )
152+ policyTemplates = append (policyTemplates , * policyTemplate )
153+ }
156154 }
157155 }
158156
157+ if len (policyTemplates ) == 0 && len (objectTemplates ) == 0 {
158+ return nil , fmt .Errorf (
159+ "the policy %s must specify at least one non-empty manifest file" , policyConf .Name ,
160+ )
161+ }
162+
159163 // just build one policyTemplate by using the above consolidated objectTemplates
160164 if policyConf .ConsolidateManifests {
161165 policyTemplate := buildPolicyTemplate (policyConf , & objectTemplates )
@@ -164,8 +168,10 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]map[string
164168 }
165169
166170 // check the enabled expanders and add additional policy templates
167- expandedPolicyTemplates := handleExpanders (manifests , policyConf )
168- policyTemplates = append (policyTemplates , expandedPolicyTemplates ... )
171+ for _ , manifestGroup := range manifestGroups {
172+ expandedPolicyTemplates := handleExpanders (manifestGroup , policyConf )
173+ policyTemplates = append (policyTemplates , expandedPolicyTemplates ... )
174+ }
169175
170176 return policyTemplates , nil
171177}
0 commit comments