merge existing materials with group ones, taking the contract's one in case of conflict
(group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachment, fromContract []*v1.CraftingSchema_Material, logger *zerolog.Logger)
| 426 | |
| 427 | // merge existing materials with group ones, taking the contract's one in case of conflict |
| 428 | func getGroupMaterialsToAdd(group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachment, fromContract []*v1.CraftingSchema_Material, logger *zerolog.Logger) ([]*v1.CraftingSchema_Material, error) { |
| 429 | toAdd := make([]*v1.CraftingSchema_Material, 0) |
| 430 | for _, groupMaterial := range group.GetSpec().GetPolicies().GetMaterials() { |
| 431 | // if material has no name, it's not enforced |
| 432 | if groupMaterial.GetName() == "" { |
| 433 | continue |
| 434 | } |
| 435 | |
| 436 | // apply bindings if needed |
| 437 | csm, err := groupMaterialToCraftingSchemaMaterial(groupMaterial, group, pgAtt, logger) |
| 438 | if err != nil { |
| 439 | return nil, err |
| 440 | } |
| 441 | // skip if interpolated material name is still empty |
| 442 | if csm.GetName() == "" { |
| 443 | continue |
| 444 | } |
| 445 | |
| 446 | // If the material is already declared in the contract, keep the contract |
| 447 | // definition and don't add a duplicate. Declaring the same material in |
| 448 | // both the contract and a policy group is a legitimate, expected case, so |
| 449 | // it's merged silently when the definitions are compatible; we only warn |
| 450 | // when the types genuinely conflict. |
| 451 | ignore := false |
| 452 | for _, mat := range fromContract { |
| 453 | if mat.GetName() != csm.GetName() { |
| 454 | continue |
| 455 | } |
| 456 | ignore = true |
| 457 | if mat.GetType() != csm.GetType() { |
| 458 | logger.Warn().Msgf("material '%s' is declared in both the contract (%s) and policy group '%s' (%s); using the contract definition", |
| 459 | mat.GetName(), mat.GetType(), group.GetMetadata().GetName(), csm.GetType()) |
| 460 | } |
| 461 | break |
| 462 | } |
| 463 | if !ignore { |
| 464 | toAdd = append(toAdd, csm) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return toAdd, nil |
| 469 | } |
| 470 | |
| 471 | // translates materials and interpolates material names |
| 472 | func groupMaterialToCraftingSchemaMaterial(gm *v1.PolicyGroup_Material, group *v1.PolicyGroup, pgAtt *v1.PolicyGroupAttachment, logger *zerolog.Logger) (*v1.CraftingSchema_Material, error) { |
no test coverage detected