in scripts/resource-autogen/main.go [441:542]
func krmifySpec(tfSpec map[string]interface{}, tfType string, dependencyGraph *sampleconversion.DependencyGraph, rc v1alpha1.ResourceConfig, tfToGVK map[string]schema.GroupVersionKind) (krmSpec map[string]interface{}, containerAnnotation map[string]string, err error) {
krmSpec = make(map[string]interface{})
containerAnnotation = make(map[string]string)
refConfigMap := getReferenceConfigMap(rc)
containerMap := getContainerMap(rc)
for tfFieldName, value := range tfSpec {
krmFieldName := text.SnakeCaseToLowerCamelCase(tfFieldName)
tfRefVal, valueTemplate, containsTFRef, err := sampleconversion.GetTFReferenceValue(value)
if err != nil {
return nil, nil, fmt.Errorf("error getting TF reference value for field %v: %w", tfFieldName, err)
}
if containsTFRef {
dependencyGraph.AddDependencyWithTFRefVal(tfRefVal, tfType)
// The use case that the field has a reference in the Tf sample, but
// is not a reference field in the KRM resource can't be handled.
refConfig, ok := refConfigMap[tfFieldName]
if !ok {
krmRefVal, err := sampleconversion.ConstructKRMExternalRefValFromTFRefVal(tfRefVal, valueTemplate, tfToGVK)
if err != nil {
return nil, nil, fmt.Errorf("cannot construct KRM value for a TF reference field %v: %w", krmFieldName, err)
}
krmSpec[krmFieldName] = krmRefVal
continue
}
krmFieldName = refConfig.Key
// For organizational resources that shouldn't be created, but used
// as a reference, use the default one instead.
defaultVal, ok := defaultOrganizationalResourcesMap[refConfig.GVK.Kind]
if ok {
krmRefVal := sampleconversion.ConstructKRMExternalReferenceObject(defaultVal)
krmSpec[krmFieldName] = krmRefVal
continue
}
krmRefVal, err := sampleconversion.ConstructKRMNameReferenceObject(tfRefVal, tfToGVK)
if err != nil {
return nil, nil, fmt.Errorf("error constructing KRM reference value for field %v: %w", krmFieldName, err)
}
krmSpec[krmFieldName] = krmRefVal
continue
}
if isProjectNameWithNumber(value) {
testProjectNameWithNumber := "projects/${projectNumber}"
testProjectID := "${projectId}"
// It's possible that the field with a value of the relative
// resource name of a GCP project is a reference field in KRM.
refConfig, ok := refConfigMap[tfFieldName]
if !ok {
container, ok := containerMap[tfFieldName]
if !ok {
krmSpec[krmFieldName] = testProjectNameWithNumber
continue
}
if !isProjectContainer(container) {
return nil, nil, fmt.Errorf("expected container type for field %v to be project but is %+v", tfFieldName, container.Type)
}
if len(containerAnnotation) > 0 {
return nil, nil, fmt.Errorf("more than one container annotation found: '%+v' and '%v: %v'", containerAnnotation, tfFieldName, testProjectNameWithNumber)
}
containerAnnotation[k8s.GetAnnotationForContainerType(container.Type)] = testProjectID
continue
}
krmFieldName = refConfig.Key
krmRefVal := sampleconversion.ConstructKRMExternalReferenceObject(testProjectNameWithNumber)
krmSpec[krmFieldName] = krmRefVal
continue
}
if isOrganizationName(value) {
testOrgID := "${TEST_ORG_ID}"
testOrgName := fmt.Sprintf("organizations/%v", testOrgID)
// It's possible that the field with a value of the relative
// resource name of a GCP organization is a reference field in KRM.
refConfig, ok := refConfigMap[tfFieldName]
if !ok {
container, ok := containerMap[tfFieldName]
if !ok {
krmSpec[krmFieldName] = testOrgName
continue
}
if !isOrganizationContainer(container) {
return nil, nil, fmt.Errorf("expected container type for field %v to be organization but is %+v", tfFieldName, container.Type)
}
if len(containerAnnotation) > 0 {
return nil, nil, fmt.Errorf("more than one container annotation found: '%+v' and '%v: %v'", containerAnnotation, tfFieldName, testOrgName)
}
containerAnnotation[k8s.GetAnnotationForContainerType(container.Type)] = testOrgID
continue
}
krmFieldName = refConfig.Key
krmRefVal := sampleconversion.ConstructKRMExternalReferenceObject(testOrgName)
krmSpec[krmFieldName] = krmRefVal
continue
}
result, err := krmifyNestedField(value)
if err != nil {
return nil, nil, fmt.Errorf("error krmifying the nested field %s: %w", krmFieldName, err)
}
krmSpec[krmFieldName] = result
}
return krmSpec, containerAnnotation, nil
}