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

chore: improve secretmanager on edge case handling #3274

Merged
merged 5 commits into from
Dec 3, 2024

Conversation

yuwenma
Copy link
Collaborator

@yuwenma yuwenma commented Dec 2, 2024

Change description

Fixes #

Tests you have done

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

@@ -19,14 +20,13 @@ spec:
userManaged:
replicas:
- location: us-central1
resourceID: secretmanagersecret-${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.

TF always add the spec.resourceID even if state-into-spec: absent

"label-one": "value-one",
"cnrm-test": "true",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(regression?) TF-based uses metadata.labels + managed-by-cnrm - system labels , the direct uses spec.labels + managed-by-cnrm.

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 that's a behavioural change. It agree with the direction, but I think it needs a design as it's a big topic.

@@ -3,17 +3,20 @@ kind: SecretManagerSecretVersion
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/observed-secret-versions: (removed)
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 a legacy annotation. The TF-based approach builds a mechanism around this annotation to determine if a sensitive data is changed.

labels:
cnrm-test: "true"
name: secretmanagersecretversion-${uniqueId}
namespace: ${uniqueId}
spec:
enabled: false
resourceID: "1"
Copy link
Collaborator Author

@yuwenma yuwenma Dec 3, 2024

Choose a reason for hiding this comment

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

secret version is a service-genereated-ID resource, that TF_based always write back to spec.resourceID. In the direct approach, we use status.externalRef instead.

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 that's technically a regression, but I think it's a minor one. So I think if we add this to the release notes (which you can do in this PR if we have a release note file on the go) then I think that's reasonable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Besides the basic test suite, I also added full test suite, where the TF-based controller cannot reconcile correctly because it assume a mutable field is immutable.
Here's the error, and here's the tracking issue with more details.

actualExternalRef, _, err2 = unstructured.NestedString(u.Object, "status", "name")
if err2 != nil {
err2 = fmt.Errorf("SecretManagerSecret `status.name` not configured: %w", err2)
return "", fmt.Errorf("%w\n%w", err1, err2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I think errors.Join is good here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call! Thanks!

//
// Label keys must be between 1 and 63 characters long, have a UTF-8 encoding
// of maximum 128 bytes, and must conform to the following PCRE regular
// expression: `[\p{Ll}\p{Lo}][\p{Ll}\p{Lo}\p{N}_-]{0,62}`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this regex from our proto? Can we express it in human?

"label-one": "value-one",
"cnrm-test": "true",
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 that's a behavioural change. It agree with the direction, but I think it needs a design as it's a big topic.

labels:
cnrm-test: "true"
name: secretmanagersecretversion-${uniqueId}
namespace: ${uniqueId}
spec:
enabled: false
resourceID: "1"
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 that's technically a regression, but I think it's a minor one. So I think if we add this to the release notes (which you can do in this PR if we have a release note file on the go) then I think that's reasonable.

@@ -84,6 +84,22 @@ spec:
is scheduled to expire. This is always provided on output, regardless
of what was sent on input.
type: string
labels:
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 this is what needs a separate design, I'm afraid.

resource := SecretManagerSecretSpec_ToProto(mapCtx, &desired.Spec)
if mapCtx.Err() != nil {
return mapCtx.Err()
}

resource.Annotations = ComputeAnnotations(desired)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we also dropping support for annotations? I think that should be mentioned in the labels design, but I don't think it is as tricky because it's not as widely used.

I'm not even sure if terraform maps metadata.annotations -> GCP annotations - does it?

@@ -262,6 +239,10 @@ func (a *Adapter) Update(ctx context.Context, op *directbase.UpdateOperation) er
if mapCtx.Err() != nil {
return mapCtx.Err()
}
// Set externalRef. This field is empty after migration from the TF-based controller to the direct.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we still go back to the terraform controller after creating resources with the direction controller? i.e. does it matter if spec.resourceID is not set? (Is it always == metadata.name?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Secret is not service generated ID resource (SecretVersion does). So it can switch back to use the TF-based controller. The real problem on using the TF-based controller is that the Secret spec.rotation.rotationPeriod is broken. If user configures that fields in Direct and switched back to TF-based, it no longer works.

@@ -164,6 +164,19 @@ func (a *SecretVersionAdapter) Create(ctx context.Context, createOp *directbase.
}
log.V(2).Info("successfully created SecretVersion", "name", a.id)

// The default status for newly created resource is "enabled".
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bummer, but I verified that this is indeed the behaviour!

Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb

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

@google-oss-prow google-oss-prow bot merged commit caaedb4 into GoogleCloudPlatform:master Dec 3, 2024
18 checks passed
@yuwenma yuwenma added this to the 1.126 milestone Dec 11, 2024
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.

2 participants