func getObjectViolations()

in pkg/safeguards/helpers.go [200:229]


func getObjectViolations(ctx context.Context, c *constraintclient.Client, objects []*unstructured.Unstructured) (map[string][]string, error) {
	// Review makes sure the provided object satisfies all stored constraints.
	// On error, the responses return value will still be populated so that
	// partial results can be analyzed.

	var results = make(map[string][]string) // map of object name to slice of objectViolations

	for _, o := range objects {
		objectViolations := []string{}
		log.Debugf("Reviewing %s...", o.GetName())
		res, err := c.Review(ctx, o)
		if err != nil {
			return results, fmt.Errorf("could not review objects: %w", err)
		}

		for _, v := range res.ByTarget {
			for _, result := range v.Results {
				if result.Msg != "" {
					objectViolations = append(objectViolations, result.Msg)
				}
			}
		}

		if len(objectViolations) > 0 {
			results[o.GetName()] = objectViolations
		}
	}

	return results, nil
}