Skip to content

Add suspended annotations#4131

Merged
ranatrk merged 13 commits intomainfrom
suspend-annotations
Nov 15, 2023
Merged

Add suspended annotations#4131
ranatrk merged 13 commits intomainfrom
suspend-annotations

Conversation

@ranatrk
Copy link
Copy Markdown
Contributor

@ranatrk ranatrk commented Nov 13, 2023

Closes BE part of weaveworks/weave-gitops-enterprise#3546

What changed?
Add suspended-by and suspended-comment annotations to objects being suspended
Remove suspended-by and suspended-comment annotations from objects being resumed

Why was this change made?
To log who/what performed the suspending action on the resource

How was this change implemented?
Adding the annotations needed indicating suspended-by and suspended-comment that could possibly include extra details regarding the suspension

How did you validate the change?

  • unit tests
  • manually check annotations returned in objects suspended

Release notes

Documentation Changes

@ranatrk ranatrk added the type/enhancement New feature or request label Nov 13, 2023
@ranatrk ranatrk force-pushed the suspend-annotations branch 2 times, most recently from 6be5dea to d56f07f Compare November 14, 2023 17:19
@foot
Copy link
Copy Markdown
Contributor

foot commented Nov 15, 2023

Lets add this little snipped to the makefile and run it to fix the ci lint job

https://github.com/weaveworks/weave-gitops/pull/4005/files#diff-76ed074a9305c04054cdebb9e9aad2d818052b07091de1f20cad0bbac34ffb52R145-R148

@ranatrk ranatrk marked this pull request as ready for review November 15, 2023 11:12
@ranatrk ranatrk requested a review from a team November 15, 2023 11:12
Comment thread core/server/suspend_test.go Outdated
t.Errorf("expected annotation weave.works/suspended-by to be set to the principal %s", principalID)
}
} else {
t.Errorf("expected annotation weave.works/suspended-by not found for %s", name.String())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You don't need to call .String() because...
https://pkg.go.dev/fmt

If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v %x %X), the following two rules apply:
4. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).
5. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

See https://go.dev/play/p/G1RCTkzZDoD

Comment thread core/server/suspend_test.go Outdated
for _, tt := range tests {
name := types.NamespacedName{Name: tt.obj.GetName(), Namespace: ns.Name}
g.Expect(checkSpec(t, k, name, tt.obj)).To(BeFalse())
g.Expect(checkSpec(t, k, principalID, name, tt.obj, false)).To(BeFalse())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd probably have split this into to two expectations rather than piggy-backing on top of checkSpec ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it was to avoid having to add a switch and getting the objects for each type again, but It can be split if that makes it cleaner/clearer as a test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread core/server/suspend.go

changeSuspendAnnotations(obj, msg.Suspend, msg.Comment, principal)

if msg.Suspend {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A couple of things here...

principal := auth.Principal(ctx)
respErrors := multierror.Error{}
for _, obj := range msg.Objects {
clustersClient, err := cs.clustersManager.GetImpersonatedClient(ctx, auth.Principal(ctx))

We could reuse the principal from the first call in the second?

Also, we could include the "principalID" in the log.Info so that the user who does it is logged out?

We could also include the name and namespace, and maybe even Kind of the resource, so that the logs would be searchable.

"Who unlocked the GitRepository test/test"?

Bonus points for including the Cluster name :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the name,namespace, and kind are already included . I'll add the principal to the logs 👍

log := cs.logger.WithValues(
"user", principal.ID,
"kind", obj.GroupVersionKind().Kind,
"name", key.Name,
"namespace", key.Namespace,
)

Copy link
Copy Markdown
Contributor

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

Looks good to me, 👍🏻

@ranatrk ranatrk force-pushed the suspend-annotations branch from a9a394b to 72848f0 Compare November 15, 2023 17:03
@ranatrk ranatrk merged commit 3523a6d into main Nov 15, 2023
@ranatrk ranatrk deleted the suspend-annotations branch November 15, 2023 21:47
This was referenced Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants