| 165 | } |
| 166 | |
| 167 | func loadPolicySpec(policyRef string) (ecc.EnterpriseContractPolicySpec, error) { |
| 168 | content, err := os.ReadFile(policyRef) |
| 169 | if err != nil { |
| 170 | return ecc.EnterpriseContractPolicySpec{}, fmt.Errorf("failed to read policy file %q: %w", policyRef, err) |
| 171 | } |
| 172 | |
| 173 | var ecp ecc.EnterpriseContractPolicy |
| 174 | if err := yaml.Unmarshal(content, &ecp); err != nil { |
| 175 | // If parsing as EnterpriseContractPolicy fails, try as EnterpriseContractPolicySpec |
| 176 | var spec ecc.EnterpriseContractPolicySpec |
| 177 | if err := yaml.Unmarshal(content, &spec); err != nil { |
| 178 | return ecc.EnterpriseContractPolicySpec{}, fmt.Errorf("unable to parse EnterpriseContractPolicySpec: %w", err) |
| 179 | } |
| 180 | return spec, nil |
| 181 | } |
| 182 | |
| 183 | // Check if this is actually a valid CRD (has required fields) |
| 184 | if ecp.APIVersion == "" || ecp.Kind == "" { |
| 185 | // This is not a valid CRD, try parsing as EnterpriseContractPolicySpec |
| 186 | var spec ecc.EnterpriseContractPolicySpec |
| 187 | if err := yaml.Unmarshal(content, &spec); err != nil { |
| 188 | return ecc.EnterpriseContractPolicySpec{}, fmt.Errorf("unable to parse EnterpriseContractPolicySpec: %w", err) |
| 189 | } |
| 190 | return spec, nil |
| 191 | } |
| 192 | |
| 193 | return ecp.Spec, nil |
| 194 | } |