From 9679bfaea0781c047e7b65ac0fb61e060facf325 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:00:02 +0000 Subject: [PATCH 1/2] enh: support direct updates Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- mockgcp/mockdataform/repository.go | 6 ++ .../direct/dataform/repository_controller.go | 14 ++++- ...ated_object_dataformrepository.golden.yaml | 8 ++- .../v1alpha1/dataformrepository/_http.log | 59 +++++++++++++++++++ .../v1alpha1/dataformrepository/update.yaml | 6 +- 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/mockgcp/mockdataform/repository.go b/mockgcp/mockdataform/repository.go index b3f9ddf443..daabd275a4 100644 --- a/mockgcp/mockdataform/repository.go +++ b/mockgcp/mockdataform/repository.go @@ -16,6 +16,7 @@ package mockdataform import ( "context" + "fmt" "net/http" "strings" @@ -87,6 +88,10 @@ func (r *RepositoryV1Beta1) UpdateRepository(ctx context.Context, request *pb.Up updateMask := request.GetUpdateMask() for _, path := range updateMask.Paths { switch path { + case "gitRemoteSettings": + obj.GitRemoteSettings = request.GetRepository().GetGitRemoteSettings() + case "workspaceCompilationOverrides": + obj.WorkspaceCompilationOverrides = request.GetRepository().GetWorkspaceCompilationOverrides() default: return nil, status.Errorf(codes.InvalidArgument, "field %q is not yet handled in mock", path) } @@ -96,6 +101,7 @@ func (r *RepositoryV1Beta1) UpdateRepository(ctx context.Context, request *pb.Up return nil, err } + fmt.Println("todo acpana did it") return obj, nil } func (r *RepositoryV1Beta1) DeleteRepository(ctx context.Context, request *pb.DeleteRepositoryRequest) (*empty.Empty, error) { diff --git a/pkg/controller/direct/dataform/repository_controller.go b/pkg/controller/direct/dataform/repository_controller.go index 5c23a7ab7a..4a59581371 100644 --- a/pkg/controller/direct/dataform/repository_controller.go +++ b/pkg/controller/direct/dataform/repository_controller.go @@ -17,6 +17,7 @@ package dataform import ( "context" "fmt" + "reflect" krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/dataform/v1alpha1" refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" @@ -188,7 +189,17 @@ func (a *Adapter) Create(ctx context.Context, u *unstructured.Unstructured) erro func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) error { updateMask := &fieldmaskpb.FieldMask{} - // TODO(acpana): handle updates + + if a.desired.Spec.GitRemoteSettings != nil { + if !reflect.DeepEqual(a.desired.Spec.GitRemoteSettings, a.actual.GitRemoteSettings) { + updateMask.Paths = append(updateMask.Paths, "git_remote_settings") + } + } + if a.desired.Spec.WorkspaceCompilationOverrides != nil { + if !reflect.DeepEqual(a.desired.Spec.WorkspaceCompilationOverrides, a.actual.WorkspaceCompilationOverrides) { + updateMask.Paths = append(updateMask.Paths, "workspace_compilation_overrides") + } + } desired := a.desired.DeepCopy() mapCtx := &direct.MapContext{} @@ -197,6 +208,7 @@ func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) erro return fmt.Errorf("converting DataformRepository spec to api: %w", mapCtx.Err()) } + resource.Name = a.fullyQualifiedName() req := &dataformpb.UpdateRepositoryRequest{UpdateMask: updateMask, Repository: resource} _, err := a.gcpClient.UpdateRepository(ctx, req) if err != nil { diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_generated_object_dataformrepository.golden.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_generated_object_dataformrepository.golden.yaml index 319093d39b..e1b39f7b46 100644 --- a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_generated_object_dataformrepository.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_generated_object_dataformrepository.golden.yaml @@ -6,7 +6,7 @@ metadata: finalizers: - cnrm.cloud.google.com/finalizer - cnrm.cloud.google.com/deletion-defender - generation: 1 + generation: 2 labels: cnrm-test: "true" name: dataformrepository-${uniqueId} @@ -15,6 +15,10 @@ spec: projectRef: external: ${projectId} region: us-west2 + workspaceCompilationOverrides: + defaultDatabase: database + schemaSuffix: _suffix + tablePrefix: prefix_ status: conditions: - lastTransitionTime: "1970-01-01T00:00:00Z" @@ -22,4 +26,4 @@ status: reason: UpToDate status: "True" type: Ready - observedGeneration: 1 + observedGeneration: 2 diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_http.log b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_http.log index 2252a86aee..261607e327 100644 --- a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_http.log @@ -57,6 +57,65 @@ Grpc-Metadata-Content-Type: application/grpc --- +PATCH https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint&updateMask=workspaceCompilationOverrides +Content-Type: application/json +x-goog-request-params: repository.name=projects%2F${projectId}%2Flocations%2Fus-west2%2Frepositories%2Fdataformrepository-${uniqueId} + +{ + "name": "projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}", + "workspaceCompilationOverrides": { + "defaultDatabase": "database", + "schemaSuffix": "_suffix", + "tablePrefix": "prefix_" + } +} + +200 OK +Content-Type: application/json +Grpc-Metadata-Content-Type: application/grpc + +{ + "displayName": "", + "gitRemoteSettings": null, + "labels": {}, + "name": "projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}", + "npmrcEnvironmentVariablesSecretVersion": "", + "serviceAccount": "", + "setAuthenticatedUserAdmin": false, + "workspaceCompilationOverrides": { + "defaultDatabase": "database", + "schemaSuffix": "_suffix", + "tablePrefix": "prefix_" + } +} + +--- + +GET https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-west2%2Frepositories%2Fdataformrepository-${uniqueId} + +200 OK +Content-Type: application/json +Grpc-Metadata-Content-Type: application/grpc + +{ + "displayName": "", + "gitRemoteSettings": null, + "labels": {}, + "name": "projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}", + "npmrcEnvironmentVariablesSecretVersion": "", + "serviceAccount": "", + "setAuthenticatedUserAdmin": false, + "workspaceCompilationOverrides": { + "defaultDatabase": "database", + "schemaSuffix": "_suffix", + "tablePrefix": "prefix_" + } +} + +--- + DELETE https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-west2%2Frepositories%2Fdataformrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/update.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/update.yaml index fc3682c572..8e25f33a85 100644 --- a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/update.yaml +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/update.yaml @@ -19,4 +19,8 @@ metadata: spec: projectRef: external: ${projectId} - region: us-west2 \ No newline at end of file + region: us-west2 + workspaceCompilationOverrides: + defaultDatabase: "database" + schemaSuffix: "_suffix" + tablePrefix: "prefix_" \ No newline at end of file From 3abef3dd9d5392b2df01f052cb8ad00af860a467 Mon Sep 17 00:00:00 2001 From: Alex Pana <8968914+acpana@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:12:05 +0000 Subject: [PATCH 2/2] refactor:tests: base and full test suite Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- ...object_dataformrepository-base.golden.yaml | 25 +++++++ .../dataformrepository-base/_http.log | 69 +++++++++++++++++++ .../create.yaml | 0 .../dataformrepository-base/update.yaml | 22 ++++++ ...bject_dataformrepository-full.golden.yaml} | 0 .../_http.log | 0 .../dataformrepository-full/create.yaml | 22 ++++++ .../update.yaml | 0 8 files changed, 138 insertions(+) create mode 100644 pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_generated_object_dataformrepository-base.golden.yaml create mode 100644 pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_http.log rename pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/{dataformrepository => dataformrepository-base}/create.yaml (100%) create mode 100644 pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/update.yaml rename pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/{dataformrepository/_generated_object_dataformrepository.golden.yaml => dataformrepository-full/_generated_object_dataformrepository-full.golden.yaml} (100%) rename pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/{dataformrepository => dataformrepository-full}/_http.log (100%) create mode 100644 pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/create.yaml rename pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/{dataformrepository => dataformrepository-full}/update.yaml (100%) diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_generated_object_dataformrepository-base.golden.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_generated_object_dataformrepository-base.golden.yaml new file mode 100644 index 0000000000..09a43d5af3 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_generated_object_dataformrepository-base.golden.yaml @@ -0,0 +1,25 @@ +apiVersion: dataform.cnrm.cloud.google.com/v1alpha1 +kind: DataformRepository +metadata: + annotations: + cnrm.cloud.google.com/management-conflict-prevention-policy: none + finalizers: + - cnrm.cloud.google.com/finalizer + - cnrm.cloud.google.com/deletion-defender + generation: 1 + labels: + cnrm-test: "true" + name: dataformrepository-${uniqueId} + namespace: ${uniqueId} +spec: + projectRef: + external: ${projectId} + region: us-west2 +status: + conditions: + - lastTransitionTime: "1970-01-01T00:00:00Z" + message: The resource is up to date + reason: UpToDate + status: "True" + type: Ready + observedGeneration: 1 \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_http.log b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_http.log new file mode 100644 index 0000000000..2252a86aee --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/_http.log @@ -0,0 +1,69 @@ +GET https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-west2%2Frepositories%2Fdataformrepository-${uniqueId} + +404 Not Found +Content-Type: application/json + +{ + "code": 5, + "details": [], + "message": "repository \"projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}\" not found" +} + +--- + +POST https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=dataformrepository-${uniqueId} +Content-Type: application/json +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-west2 + +{} + +200 OK +Content-Type: application/json +Grpc-Metadata-Content-Type: application/grpc + +{ + "displayName": "", + "gitRemoteSettings": null, + "labels": {}, + "name": "projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}", + "npmrcEnvironmentVariablesSecretVersion": "", + "serviceAccount": "", + "setAuthenticatedUserAdmin": false, + "workspaceCompilationOverrides": null +} + +--- + +GET https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-west2%2Frepositories%2Fdataformrepository-${uniqueId} + +200 OK +Content-Type: application/json +Grpc-Metadata-Content-Type: application/grpc + +{ + "displayName": "", + "gitRemoteSettings": null, + "labels": {}, + "name": "projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}", + "npmrcEnvironmentVariablesSecretVersion": "", + "serviceAccount": "", + "setAuthenticatedUserAdmin": false, + "workspaceCompilationOverrides": null +} + +--- + +DELETE https://dataform.googleapis.com/v1beta1/projects/${projectId}/locations/us-west2/repositories/dataformrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-west2%2Frepositories%2Fdataformrepository-${uniqueId} + +200 OK +Content-Type: application/json +Grpc-Metadata-Content-Type: application/grpc +Grpc-Metadata-X-Http-Code: 204 + +{} \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/create.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/create.yaml similarity index 100% rename from pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/create.yaml rename to pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/create.yaml diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/update.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/update.yaml new file mode 100644 index 0000000000..fc3682c572 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-base/update.yaml @@ -0,0 +1,22 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: dataform.cnrm.cloud.google.com/v1alpha1 +kind: DataformRepository +metadata: + name: dataformrepository-${uniqueId} +spec: + projectRef: + external: ${projectId} + region: us-west2 \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_generated_object_dataformrepository.golden.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/_generated_object_dataformrepository-full.golden.yaml similarity index 100% rename from pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_generated_object_dataformrepository.golden.yaml rename to pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/_generated_object_dataformrepository-full.golden.yaml diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_http.log b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/_http.log similarity index 100% rename from pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/_http.log rename to pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/_http.log diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/create.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/create.yaml new file mode 100644 index 0000000000..fc3682c572 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/create.yaml @@ -0,0 +1,22 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: dataform.cnrm.cloud.google.com/v1alpha1 +kind: DataformRepository +metadata: + name: dataformrepository-${uniqueId} +spec: + projectRef: + external: ${projectId} + region: us-west2 \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/update.yaml b/pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/update.yaml similarity index 100% rename from pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository/update.yaml rename to pkg/test/resourcefixture/testdata/basic/dataform/v1alpha1/dataformrepository-full/update.yaml