From 3e2cc34ef338e13f78d02dc463ecb19858b48119 Mon Sep 17 00:00:00 2001 From: Lukas Hoehl Date: Wed, 20 Nov 2024 21:01:39 +0100 Subject: [PATCH] populate terraform attributes from kubernetes resource metadata.name Signed-off-by: Lukas Hoehl --- pkg/config/resource.go | 4 ++++ pkg/controller/external_tfpluginsdk.go | 3 +++ pkg/examples/example.go | 2 +- pkg/pipeline/crd.go | 2 +- pkg/terraform/files.go | 3 +++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/config/resource.go b/pkg/config/resource.go index 81304bfa..7ef65547 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -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. diff --git a/pkg/controller/external_tfpluginsdk.go b/pkg/controller/external_tfpluginsdk.go index 8fed8acd..553bd694 100644 --- a/pkg/controller/external_tfpluginsdk.go +++ b/pkg/controller/external_tfpluginsdk.go @@ -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) diff --git a/pkg/examples/example.go b/pkg/examples/example.go index 363a3f46..a92c1e7c 100644 --- a/pkg/examples/example.go +++ b/pkg/examples/example.go @@ -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, diff --git a/pkg/pipeline/crd.go b/pkg/pipeline/crd.go index 4cfc9ec9..0f7ca308 100644 --- a/pkg/pipeline/crd.go +++ b/pkg/pipeline/crd.go @@ -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, diff --git a/pkg/terraform/files.go b/pkg/terraform/files.go index 26a8fe1c..3c5b0dae 100644 --- a/pkg/terraform/files.go +++ b/pkg/terraform/files.go @@ -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()