From 576143780e03bacae90e9d9973d1a7b96905cc5f Mon Sep 17 00:00:00 2001 From: justinsb Date: Fri, 13 Dec 2024 07:00:30 -0800 Subject: [PATCH] export: fix name collision on BigQueryTable Table IDs are unique only within the dataset, so we should include the table id in the name. --- pkg/cli/stream/unstructuredresource_fixup.go | 24 +++++++++++++++++++ .../_generated_export_bigquerytable.golden | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/cli/stream/unstructuredresource_fixup.go b/pkg/cli/stream/unstructuredresource_fixup.go index 98ef143d91..48089607a7 100644 --- a/pkg/cli/stream/unstructuredresource_fixup.go +++ b/pkg/cli/stream/unstructuredresource_fixup.go @@ -23,6 +23,7 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/randomid" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" ) const namePath = "metadata.name" @@ -48,6 +49,9 @@ func (s *UnstructuredResourceFixupStream) Next(ctx context.Context) (*unstructur if err := defaultNameIfNotPresent(u); err != nil { return nil, err } + if err := handleSpecialCases(u); err != nil { + return nil, err + } if err := ensureNameIsK8sLegal(u); err != nil { return nil, err } @@ -99,3 +103,23 @@ func ensureNameIsK8sLegal(u *unstructured.Unstructured) error { } return nil } + +// Some resources need additional handling +func handleSpecialCases(u *unstructured.Unstructured) error { + resourceID, _, _ := unstructured.NestedString(u.Object, "spec", "resourceID") + switch u.GroupVersionKind().GroupKind() { + // Table name is only unique in a dataset, avoid collisions + case schema.GroupKind{Group: "bigquery.cnrm.cloud.google.com", Kind: "BigQueryTable"}: + datasetID, _, _ := unstructured.NestedString(u.Object, "spec", "datasetRef", "external") + tableID := resourceID + + if datasetID == "" { + return fmt.Errorf("unexpected empty value for spec.datasetRef.external in BigQueryTable resource") + } + if tableID == "" { + return fmt.Errorf("unexpected empty value for spec.resourceID in BigQueryTable resource") + } + u.SetName(datasetID + "-" + tableID) + } + return nil +} diff --git a/pkg/test/resourcefixture/testdata/basic/bigquery/v1beta1/bigquerytable/_generated_export_bigquerytable.golden b/pkg/test/resourcefixture/testdata/basic/bigquery/v1beta1/bigquerytable/_generated_export_bigquerytable.golden index 993591583f..a796bdf397 100644 --- a/pkg/test/resourcefixture/testdata/basic/bigquery/v1beta1/bigquerytable/_generated_export_bigquerytable.golden +++ b/pkg/test/resourcefixture/testdata/basic/bigquery/v1beta1/bigquerytable/_generated_export_bigquerytable.golden @@ -6,7 +6,7 @@ metadata: labels: cnrm-test: "true" managed-by-cnrm: "true" - name: bigquerytablesample${uniqueId} + name: bigquerydatasetsample${uniqueId}-bigquerytablesample${uniqueId} spec: datasetRef: external: bigquerydatasetsample${uniqueId}