Skip to content

Commit

Permalink
Change BigqueryDataset Location to Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweim committed Nov 7, 2024
1 parent 885f4af commit de154da
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 52 deletions.
22 changes: 10 additions & 12 deletions apis/bigquery/v1beta1/dataset_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func NewBigQueryDatasetRef(ctx context.Context, reader client.Reader, obj *BigQu
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
location := obj.Spec.Location
id.parent = &BigQueryDatasetParent{ProjectID: projectID, Location: valueOf(location)}
id.parent = &BigQueryDatasetParent{ProjectID: projectID}

// Get desired ID
resourceID := valueOf(obj.Spec.ResourceID)
Expand All @@ -125,15 +124,15 @@ func NewBigQueryDatasetRef(ctx context.Context, reader client.Reader, obj *BigQu
if actualParent.ProjectID != projectID {
return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualParent.ProjectID, projectID)
}
if actualParent.Location != valueOf(location) {
return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, valueOf(location))
}
// if actualParent.Location != valueOf(location) {
// return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, valueOf(location))
// }
if actualResourceID != resourceID {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
resourceID, actualResourceID)
}
id.External = externalRef
id.parent = &BigQueryDatasetParent{ProjectID: projectID, Location: valueOf(location)}
id.parent = &BigQueryDatasetParent{ProjectID: projectID}
return id, nil
}

Expand All @@ -153,28 +152,27 @@ func (r *BigQueryDatasetRef) Parent() (*BigQueryDatasetParent, error) {

type BigQueryDatasetParent struct {
ProjectID string
Location string
}

func (p *BigQueryDatasetParent) String() string {
return "projects/" + p.ProjectID + "/locations/" + p.Location
return "projects/" + p.ProjectID
}

func asBigQueryDatasetExternal(parent *BigQueryDatasetParent, resourceID string) (external string) {
// Link Reference https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/get
return parent.String() + "/datasets/" + resourceID
}

func ParseBigQueryDatasetExternal(external string) (parent *BigQueryDatasetParent, resourceID string, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "datasets" {
return nil, "", fmt.Errorf("format of BigQueryDataset external=%q was not known (use projects/<projectId>/locations/<location>/datasets/<datasetID>)", external)
if len(tokens) != 4 || tokens[0] != "projects" || tokens[2] != "datasets" {
return nil, "", fmt.Errorf("format of BigQueryDataset external=%q was not known (use projects/<projectId>/datasets/<datasetID>)", external)
}
parent = &BigQueryDatasetParent{
ProjectID: tokens[1],
Location: tokens[3],
}
resourceID = tokens[5]
resourceID = tokens[3]
return parent, resourceID, nil
}

Expand Down
13 changes: 9 additions & 4 deletions apis/bigquery/v1beta1/dataset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ type BigQueryDatasetSpec struct {
// references.
IsCaseInsensitive *bool `json:"isCaseInsensitive,omitempty"`

// The geographic location where the dataset should reside. See
// Optional. The geographic location where the dataset should reside. See
// https://cloud.google.com/bigquery/docs/locations for supported
// locations.
// +required
Location *string `json:"location"`
Location *string `json:"location,omitempty"`

// Optional. Defines the time travel window in hours. The value can be from 48
// to 168 hours (2 to 7 days). The default value is 168 hours if this is not
Expand Down Expand Up @@ -136,11 +135,17 @@ type BigQueryDatasetStatus struct {
// Output only. A URL that can be used to access the resource again. You can
// use this URL in Get or Update requests to the resource.
SelfLink *string `json:"selfLink,omitempty"`

// ObservedState is the state of the resource as most recently observed in GCP.
ObservedState *BigQueryDatasetObservedState `json:"observedState,omitempty"`
}

// BigQueryDatasetSpec defines the desired state of BigQueryDataset
// BigQueryDatasetObservedState defines the desired state of BigQueryDataset
// +kcc:proto=google.cloud.bigquery.v2.dataset
type BigQueryDatasetObservedState struct {

// Optional. If the location is not specified in the spec, the GCP server defaults to a location and will be captured here.
Location *string `json:"location,omitempty"`
}

// +genclient
Expand Down
10 changes: 10 additions & 0 deletions apis/bigquery/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ spec:
does not affect routine references.
type: boolean
location:
description: The geographic location where the dataset should reside.
See https://cloud.google.com/bigquery/docs/locations for supported
locations.
description: Optional. The geographic location where the dataset should
reside. See https://cloud.google.com/bigquery/docs/locations for
supported locations.
type: string
maxTimeTravelHours:
description: Optional. Defines the time travel window in hours. The
Expand Down Expand Up @@ -342,8 +342,6 @@ spec:
storageBillingModel:
description: Optional. Updates storage_billing_model for the dataset.
type: string
required:
- location
type: object
status:
description: BigQueryDatasetStatus defines the config connector machine
Expand Down Expand Up @@ -400,6 +398,16 @@ spec:
the resource.
format: int64
type: integer
observedState:
description: ObservedState is the state of the resource as most recently
observed in GCP.
properties:
location:
description: Optional. If the location is not specified in the
spec, the GCP server defaults to a location and will be captured
here.
type: string
type: object
selfLink:
description: Output only. A URL that can be used to access the resource
again. You can use this URL in Get or Update requests to the resource.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ func BigQueryDatasetStatus_FromAPI(mapCtx *direct.MapContext, in *api.Dataset) *
out.CreationTime = direct.LazyPtr(in.CreationTime)
out.LastModifiedTime = direct.LazyPtr(in.LastModifiedTime)
out.SelfLink = direct.LazyPtr(in.SelfLink)
out.ObservedState = &krm.BigQueryDatasetObservedState{Location: direct.LazyPtr(in.Location)}
return out
}
func BigQueryDatasetStatusObservedState_ToAPI(mapCtx *direct.MapContext, in *krm.BigQueryDatasetStatus) *api.Dataset {
func BigQueryDatasetStatus_ToAPI(mapCtx *direct.MapContext, in *krm.BigQueryDatasetStatus) *api.Dataset {
if in == nil {
return nil
}
Expand All @@ -99,6 +100,7 @@ func BigQueryDatasetStatusObservedState_ToAPI(mapCtx *direct.MapContext, in *krm
out.CreationTime = direct.ValueOf(in.CreationTime)
out.LastModifiedTime = direct.ValueOf(in.LastModifiedTime)
out.SelfLink = direct.ValueOf(in.SelfLink)
out.Location = direct.ValueOf(in.ObservedState.Location)
return out
}
func Access_ToAPI(mapCtx *direct.MapContext, in *krm.Access) *api.DatasetAccess {
Expand Down
15 changes: 14 additions & 1 deletion pkg/controller/direct/bigquerydataset/dataset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ func (a *Adapter) Update(ctx context.Context, updateOp *directbase.UpdateOperati

resource := clone.Clone(a.actual).(*api.Dataset)

// If the location is not specified upon creation, merge the desired with the previously applied location defaulting.
presviouslyApplied, _, _ := unstructured.NestedMap(u.Object, "status")
if presviouslyApplied != nil {
if presviouslyApplied["observedState"] != nil {
switch observedState := presviouslyApplied["observedState"].(type) {
case map[string]interface{}:
var observed krm.BigQueryDatasetObservedState
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(observedState, &observed); err == nil {
desired.Location = direct.ValueOf(observed.Location)
}
}
}
}

// Check for immutable fields
if !reflect.DeepEqual(desired.Location, resource.Location) {
return fmt.Errorf("BigQueryDataset %s/%s location cannot be changed, actual: %s, desired: %s", u.GetNamespace(), u.GetName(), resource.Location, desired.Location)
Expand Down Expand Up @@ -286,7 +300,6 @@ func (a *Adapter) Export(ctx context.Context) (*unstructured.Unstructured, error
}

obj.Spec.ProjectRef = &refs.ProjectRef{Name: parent.ProjectID}
obj.Spec.Location = &parent.Location
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
- role: WRITER
specialGroup: projectWriters
friendlyName: bigquerydataset-sample-updated
location: us-central1
location: US
maxTimeTravelHours: "168"
projectRef:
external: ${projectId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ metadata:
namespace: ${uniqueId}
spec:
friendlyName: bigquerydataset-sample-updated
location: us-central1
projectRef:
external: ${projectId}
status:
Expand All @@ -26,7 +25,9 @@ status:
type: Ready
creationTime: "1970-01-01T00:00:00Z"
etag: abcdef123456
externalRef: projects/${projectId}/locations/us-central1/datasets/bigquerydatasetsample${uniqueId}
externalRef: projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}
lastModifiedTime: "1970-01-01T00:00:00Z"
observedGeneration: 2
observedState:
location: US
selfLink: https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ User-Agent: kcc/controller-manager
"labels": {
"cnrm-test": "true",
"managed-by-cnrm": "true"
},
"location": "us-central1"
}
}

200 OK
Expand Down Expand Up @@ -89,7 +88,7 @@ X-Xss-Protection: 0
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "us-central1",
"location": "US",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}",
"type": "DEFAULT"
}
Expand Down Expand Up @@ -143,7 +142,7 @@ X-Xss-Protection: 0
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "us-central1",
"location": "US",
"maxTimeTravelHours": "168",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}",
"type": "DEFAULT"
Expand Down Expand Up @@ -188,7 +187,7 @@ User-Agent: kcc/controller-manager
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "us-central1",
"location": "US",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}",
"type": "DEFAULT"
}
Expand Down Expand Up @@ -237,7 +236,7 @@ X-Xss-Protection: 0
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "us-central1",
"location": "US",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}",
"type": "DEFAULT"
}
Expand Down Expand Up @@ -292,7 +291,7 @@ X-Xss-Protection: 0
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "us-central1",
"location": "US",
"maxTimeTravelHours": "168",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}",
"type": "DEFAULT"
Expand Down Expand Up @@ -347,7 +346,7 @@ X-Xss-Protection: 0
"managed-by-cnrm": "true"
},
"lastModifiedTime": "123456789",
"location": "us-central1",
"location": "US",
"maxTimeTravelHours": "168",
"selfLink": "https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/datasets/bigquerydatasetsample${uniqueId}",
"type": "DEFAULT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ metadata:
alpha.cnrm.cloud.google.com/reconciler: "direct"
spec:
friendlyName: bigquerydataset-sample
location: us-central1
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ metadata:
alpha.cnrm.cloud.google.com/reconciler: "direct"
spec:
friendlyName: bigquerydataset-sample-updated
location: us-central1
Loading

0 comments on commit de154da

Please sign in to comment.