Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add spanner database mockgcp #1829

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

jasonvigil
Copy link
Collaborator

@jasonvigil jasonvigil commented May 17, 2024

Change description

  • Add protos for Spanner Database
  • Add mockgcp implementation for Spanner Database

Tasks

  • Run make ready-pr to ensure this PR is ready for review.
  • Perform necessary E2E testing for changed resources.

mockgcp/fixup-third-party.sh Outdated Show resolved Hide resolved
mockgcp/mockspanner/database.go Outdated Show resolved Hide resolved
mockgcp/mockspanner/names.go Outdated Show resolved Hide resolved
@justinsb
Copy link
Collaborator

Code LGTM. I'll look at that test failure :-)

@justinsb
Copy link
Collaborator

I think the test failure is that we need to implement DropDatabase in mockgcp...

    samples.go:297: error while polling for resource cleanup on SpannerDatabase with name 'spannerdatabase-test': context deadline exceeded; last seen status: map[conditions:[map[lastTransitionTime:2024-05-20T13:28:12-04:00 message:Delete call failed: error deleting resource: [{0 Error when reading or editing Database: googleapi: got HTTP response code 501 with body: {"code":12, "message":"method DropDatabase not implemented", "details":[]}  []}] reason:DeleteFailed status:False type:Ready]] observedGeneration:3 state:STATE_UNSPECIFIED]

I got this by running hack/compare-mock fixture/externalwithsubresource locally

(I determined it was externalwithsubresource by downloading the raw logs and searching for FAIL:)

@jasonvigil
Copy link
Collaborator Author

Thanks @justinsb, fixed those issues. Ready for another review.

@justinsb
Copy link
Collaborator

I think there are some merge conflicts now that we merged SpannerInstance - mind rebasing?

@jasonvigil
Copy link
Collaborator Author

@justinsb / @yuwenma ok, rebased. PTAL when you have a chance :)

@jasonvigil jasonvigil changed the title Add mockgcp implementation for Spanner Database Add direct controller for Spanner Database May 31, 2024
@jasonvigil jasonvigil changed the title Add direct controller for Spanner Database [WIP] Add direct controller for Spanner Database May 31, 2024
@jasonvigil jasonvigil force-pushed the spanner-db branch 2 times, most recently from 55823e4 to 45efbf0 Compare June 5, 2024 17:14
@@ -8,7 +8,6 @@ metadata:
cnrm.cloud.google.com/managed-by-kcc: "true"
cnrm.cloud.google.com/stability-level: stable
cnrm.cloud.google.com/system: "true"
cnrm.cloud.google.com/tf2crd: "true"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This infers the spanner database is using the TF controller. I think that is good, because we are not ready to turn on the direct controller on this resource yet.
Then to test the mockGCP with the direct controller, you would need to use the KCC_USE_DIRECT_RECONCILERS flag. Right now you can put the test coverage for "direct controller" here https://github.com/GoogleCloudPlatform/k8s-config-connector/blob/master/scripts/github-actions/tests-e2e-direct.sh with KCC_USE_DIRECT_RECONCILERS flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just my hack to enable testing the direct controller manually in a real gcp environment with make deploy-controller && kubectl delete pods --namespace cnrm-system --all. Not planning to merge it, will remove later.

Copy link
Collaborator

@yuwenma yuwenma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good. You can consider spliting the mockGCP test (for TF controller) so it can goes in first.

@@ -5,6 +5,8 @@ set -x

export KUBEBUILDER_ASSETS=$(go run sigs.k8s.io/controller-runtime/tools/setup-envtest@latest use -p path)

export KCC_USE_DIRECT_RECONCILERS=SpannerDatabase
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: cleanup. (I know this PR is WIP)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused on this actually. What is our final / production method for switching a resource to use the "direct" controller instead of the terraform-based controller?

Copy link
Collaborator

@yuwenma yuwenma Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To switch a resource to the direct controller, you only need to remove the CRD's tf2crd label (for DCL based it is dcl2crd). If a resource is not yet ready for direct controller but you want to run mock test against the direct controller, you can use the KCC_USE_DIRECT_RECONCILERS flag.

In other words, direct controller is the default.

@@ -59,7 +59,7 @@ import (
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocksecretmanager"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockservicenetworking"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockserviceusage"
mockspannerinstance "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockspanner/admin"
mockspanner "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockspanner/admin"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I had a question on this -- Why did you create the admin/ subdirectory for the instance mock? For other resources it looks like the code is placed directly in mockgcp/mock<resourcename>/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to align that with its cloud client API https://pkg.go.dev/cloud.google.com/go/spanner#section-directories where the admin/ has instance and database and executor has executor. I assume that's how spanner categorize its resources (and maybe will add more resources in the executor dir). But I don't have strong preference on this.

return nil, err
}

return s.operations.NewLRO(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would want to use StartLRO if possible which better reflects the actual service behavior. (Feel free to make the improvement in follow up PRs.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will look into this. Thanks 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a copy of your code from #1761. And, actually, I would prefer to reorganize and put the "util" code elsewhere, there is some guidance I've seen to avoid "util" files / packages, since they tend to become dumping grounds. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I'm not a fan of utils or helpers. I think those functions are generic and could be reused by other mock functions. Feel free to refactor the code!

@jasonvigil jasonvigil changed the title [WIP] Add direct controller for Spanner Database Feat: Add spanner database mockgcp Jun 13, 2024
@jasonvigil jasonvigil force-pushed the spanner-db branch 2 times, most recently from a991444 to 31cb74c Compare June 17, 2024 23:00
Copy link
Collaborator

@yuwenma yuwenma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

Looks great! Some minor questions

Statements: req.Statements,
}
return s.operations.StartLRO(ctx, fqn, metadata, func() (proto.Message, error) {
// Many fields are not populated in the LRO result
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: set the createTime, updateTime

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no field for updateTime for this resource, but just double-checked that createTime is being set.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it looks like the Database uses Reconciling (bool) to tell if the update succeeds or not.

// Given a list of DDL extra statement strings, find one with the format
// "ALTER DATABASE `%s` SET OPTIONS (version_retention_period = '%s')"
// and return the version_retention_period, or "1h" if no match.
func versionRetentionPeriodFromDDL(ddl []string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah cool!

generation: 2
labels:
cnrm-test: "true"
name: spannerdatabase-test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: There's some naming convention requirement. I think this should be spannerdatabase-${uniqueId}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks :) I think I had accidentally changed this for manual testing.


---

PATCH https://spanner.googleapis.com/v1/projects/${projectId}/instances/spannerinstance-${uniqueId}/databases/spannerdatabase-test/ddl?alt=json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an implementation detail of the terraform controller. I think it works around the issue that it's impossible to specify extra DDL statements for Postgres-flavor databases in the create call by just always taking two steps (even if it's a GOOGLESQL-flavor database).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I see this is a really good example on relying the mockgcp to test, validate and verify controller operations.

@google-oss-prow google-oss-prow bot removed the lgtm label Jun 18, 2024
@jasonvigil jasonvigil requested a review from yuwenma June 18, 2024 16:37
@justinsb
Copy link
Collaborator

LGTM!

(Failure on tests-e2e-fixtures looks unrelated and looks like envtest crashed, hopefully if we can get to mockkubeapiserver that should address these sorts of flakes!)

@yuwenma
Copy link
Collaborator

yuwenma commented Jun 18, 2024

/lgtm
/approve

@google-oss-prow google-oss-prow bot added the lgtm label Jun 18, 2024
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: yuwenma

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@yuwenma
Copy link
Collaborator

yuwenma commented Jun 20, 2024

/lgtm

@google-oss-prow google-oss-prow bot added the lgtm label Jun 20, 2024
@google-oss-prow google-oss-prow bot merged commit be7c60d into GoogleCloudPlatform:master Jun 20, 2024
13 checks passed
@jasonvigil jasonvigil deleted the spanner-db branch June 20, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants