func()

in pkg/gcv/configs/config.go [456:502]


func (c *Configuration) finishLoad() error {
	templates := map[string]string{}
	for _, t := range c.GCPTemplates {
		templates[t.Spec.CRD.Spec.Names.Kind] = gcpConstraint
	}
	for _, t := range c.TFTemplates {
		templates[t.Spec.CRD.Spec.Names.Kind] = tfConstraint
	}
	for _, t := range c.K8STemplates {
		templates[t.Spec.CRD.Spec.Names.Kind] = k8sConstraint
	}

	byTemplate := map[string]map[string]*unstructured.Unstructured{}
	allConstraints := c.allConstraints
	c.allConstraints = nil
	for _, constraint := range allConstraints {
		gvk := constraint.GroupVersionKind()
		if gvk.Version == "v1alpha1" {
			if err := convertLegacyConstraint(constraint); err != nil {
				return fmt.Errorf("failed to convert constraint: %w", err)
			}
		}

		templateConstraints, found := byTemplate[constraint.GetKind()]
		if !found {
			templateConstraints = map[string]*unstructured.Unstructured{}
			byTemplate[constraint.GetKind()] = templateConstraints
		}
		if dup, found := templateConstraints[constraint.GetName()]; found {
			return errors.Errorf(
				"Constraint %q declared at path %q has duplicate name conflict with constraint declared at path %q",
				dup.GetName(), dup.GetAnnotations()[yamlPath], constraint.GetAnnotations()[yamlPath])
		}

		switch templates[gvk.Kind] {
		case gcpConstraint:
			c.GCPConstraints = append(c.GCPConstraints, constraint)
		case tfConstraint:
			c.TFConstraints = append(c.TFConstraints, constraint)
		case k8sConstraint:
			c.K8SConstraints = append(c.K8SConstraints, constraint)
		default:
			return errors.Errorf("constraint %s does not correspond to any templates", gvk)
		}
	}
	return nil
}