Skip to content

Commit

Permalink
populate terraform attributes from kubernetes resource metadata.name
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Hoehl <[email protected]>
  • Loading branch information
hown3d committed Nov 20, 2024
1 parent ba35c31 commit 3e2cc34
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ type Resource struct {
// requiredFields are the fields that will be marked as required in the
// generated CRD schema, although they are not required in the TF schema.
requiredFields []string

// AttributesToPopulateWithMetadataName defines Terraform attributes that
// should be populated with the resources metadata.name
AttributesToPopulateWithMetadataName []string
}

// RequiredFields returns slice of the marked as required fieldpaths.
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/external_tfpluginsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func getExtendedParameters(ctx context.Context, tr resource.Terraformed, externa
if err = resource.GetSensitiveParameters(ctx, &APISecretClient{kube: kube}, tr, params, tr.GetConnectionDetailsMapping()); err != nil {
return nil, errors.Wrap(err, "cannot store sensitive parameters into params")
}
for _, a := range cfg.AttributesToPopulateWithMetadataName {
params[a] = tr.GetName()
}
cfg.ExternalName.SetIdentifierArgumentFn(params, externalName)
if cfg.TerraformConfigurationInjector != nil {
m, err := getJSONMap(tr)
Expand Down
2 changes: 1 addition & 1 deletion pkg/examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (eg *Generator) StoreExamples() error { // nolint:gocyclo
func paveCRManifest(exampleParams map[string]any, r *config.Resource, eName, group, version, eGroup string) *reference.PavedWithManifest {
delete(exampleParams, "depends_on")
delete(exampleParams, "lifecycle")
transformFields(r, exampleParams, r.ExternalName.OmittedFields, "")
transformFields(r, exampleParams, append(r.ExternalName.OmittedFields, r.AttributesToPopulateWithMetadataName...), "")
metadata := map[string]any{
"labels": map[string]string{
labelExampleName: eName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/pipeline/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (cg *CRDGenerator) Generate(cfg *config.Resource) (string, error) {
wrapper.WithHeaderPath(cg.LicenseHeaderPath),
)

deleteOmittedFields(cfg.TerraformResource.Schema, cfg.ExternalName.OmittedFields)
deleteOmittedFields(cfg.TerraformResource.Schema, append(cfg.ExternalName.OmittedFields, cfg.AttributesToPopulateWithMetadataName...))
cfg.TerraformResource.Schema["id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand Down
3 changes: 3 additions & 0 deletions pkg/terraform/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func NewFileProducer(ctx context.Context, client resource.SecretClient, dir stri
return nil, errors.Wrap(err, "cannot get sensitive parameters")
}
fp.Config.ExternalName.SetIdentifierArgumentFn(params, meta.GetExternalName(tr))
for _, a := range cfg.AttributesToPopulateWithMetadataName {
params[a] = tr.GetName()
}
fp.parameters = params

obs, err := tr.GetObservation()
Expand Down

0 comments on commit 3e2cc34

Please sign in to comment.