Skip to content

Commit

Permalink
export: fix name collision on BigQueryTable
Browse files Browse the repository at this point in the history
Table IDs are unique only within the dataset, so we should include the table id in the name.
  • Loading branch information
justinsb committed Dec 13, 2024
1 parent f0c7607 commit 5761437
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pkg/cli/stream/unstructuredresource_fixup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 5761437

Please sign in to comment.