@@ -169,6 +169,8 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
169169 objectTemplates := make ([]map [string ]interface {}, 0 , objectTemplatesLength )
170170 policyTemplates := make ([]map [string ]interface {}, 0 , policyTemplatesLength )
171171
172+ policyNameCounter := 0
173+
172174 for i , manifestGroup := range manifestGroups {
173175 complianceType := policyConf .Manifests [i ].ComplianceType
174176 metadataComplianceType := policyConf .Manifests [i ].MetadataComplianceType
@@ -177,7 +179,9 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
177179 ignorePending := policyConf .Manifests [i ].IgnorePending
178180 extraDeps := policyConf .Manifests [i ].ExtraDependencies
179181
180- for j , manifest := range manifestGroup {
182+ manifestNameCounter := 0
183+
184+ for _ , manifest := range manifestGroup {
181185 err := setGatekeeperEnforcementAction (manifest ,
182186 policyConf .Manifests [i ].GatekeeperEnforcementAction )
183187 if err != nil {
@@ -204,8 +208,7 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
204208 policyConf ,
205209 manifest ["object-templates-raw" ],
206210 & policyConf .Manifests [i ].ConfigurationPolicyOptions ,
207- getConfigurationPolicyName (policyConf , i , len (policyTemplates ),
208- j , ! policyConf .ConsolidateManifests ),
211+ getConfigurationPolicyName (policyConf , i , & policyNameCounter , & manifestNameCounter ),
209212 )
210213 } else {
211214 policyTemplate = map [string ]interface {}{"objectDefinition" : manifest }
@@ -262,8 +265,8 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
262265 policyConf ,
263266 []map [string ]interface {}{objTemplate },
264267 & policyConf .Manifests [i ].ConfigurationPolicyOptions ,
265- getConfigurationPolicyName (policyConf , i , len ( policyTemplates ),
266- j , ! policyConf . ConsolidateManifests ),
268+ getConfigurationPolicyName (policyConf , i ,
269+ & policyNameCounter , & manifestNameCounter ),
267270 )
268271
269272 setTemplateOptions (policyTemplate , ignorePending , extraDeps )
@@ -282,11 +285,13 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
282285 // just build one policyTemplate by using the above non-empty consolidated objectTemplates
283286 // ConsolidateManifests = true or there is non-policy-type manifest
284287 if policyConf .ConsolidateManifests && len (objectTemplates ) > 0 {
288+ // If ConsolidateManifests is true and multiple manifest[].names are provided, the configuration policy
289+ // name will be the first name of manifest[].names
285290 policyTemplate := buildPolicyTemplate (
286291 policyConf ,
287292 objectTemplates ,
288293 & policyConf .ConfigurationPolicyOptions ,
289- getConfigurationPolicyName (policyConf , 0 , 0 , 0 , false ),
294+ getConfigurationPolicyName (policyConf , 0 , & policyNameCounter , nil ),
290295 )
291296 setTemplateOptions (policyTemplate , policyConf .IgnorePending , policyConf .ExtraDependencies )
292297 policyTemplates = append (policyTemplates , policyTemplate )
@@ -322,27 +327,35 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
322327 return policyTemplates , nil
323328}
324329
325- // This function returns configurationPolicyName with Index+1 based on the provided PolicyConfig.
326- // When index is 0 , it won't attach any number. When useManifestName is true,
327- // it returns the policyConf.Manifests[manifestGroupIndex].Name otherwise ,
328- // It returns policyConf.Name + index
329- func getConfigurationPolicyName (policyConf * types.PolicyConfig , manifestGroupIndex , policyNum ,
330- manifestIndex int , useManifestName bool ,
330+ // getConfigurationPolicyName returns the `ConfigurationPolicy` name based on the provided PolicyConfig.
331+ // When a name is assigned , it increments the associated counter, using it as a suffix if it's greater
332+ // than one to create unique names. If both `manifest[].name` and `policies.name` are provided ,
333+ // `manifest[].name` takes priority as the configuration name.
334+ func getConfigurationPolicyName (policyConf * types.PolicyConfig , manifestGroupIndex int ,
335+ policyNameCounter * int , manifestNameCounter * int ,
331336) string {
332337 // Do not append a number to configName when it is used for the first time.
333338 configName := policyConf .Manifests [manifestGroupIndex ].Name
334339
335- if useManifestName && configName != "" {
336- if manifestIndex > 0 {
337- return fmt .Sprintf ("%s%d" , configName , manifestIndex + 1 )
340+ // For the case where ConsolidateManifests is true and the manifest's name is provided,
341+ if manifestNameCounter == nil && configName != "" {
342+ return configName
343+ }
344+
345+ if manifestNameCounter != nil && configName != "" {
346+ * manifestNameCounter ++
347+ if * manifestNameCounter > 1 {
348+ return fmt .Sprintf ("%s%d" , configName , * manifestNameCounter )
338349 }
339350
340351 return configName
341352 }
342353
343354 configName = policyConf .Name
344- if policyNum > 0 {
345- return fmt .Sprintf ("%s%d" , configName , policyNum + 1 )
355+
356+ * policyNameCounter ++
357+ if * policyNameCounter > 1 {
358+ return fmt .Sprintf ("%s%d" , configName , * policyNameCounter )
346359 }
347360
348361 return configName
0 commit comments