Add IsWithinParent (#1765)

This commit is contained in:
Victor Agababov 2020-10-02 12:33:33 -07:00 committed by GitHub
parent 735a38c032
commit 84e91da23c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -134,6 +134,12 @@ func WithinParent(ctx context.Context, om metav1.ObjectMeta) context.Context {
return context.WithValue(ctx, parentMetaKey{}, om)
}
// IsWithinParent returns true if we're within parent context.
func IsWithinParent(ctx context.Context) bool {
_, ok := ctx.Value(parentMetaKey{}).(metav1.ObjectMeta)
return ok
}
// ParentMeta accesses the ObjectMeta of the enclosing parent resource
// from the context. See WithinParent for how to attach the parent's
// ObjectMeta to the context.

View File

@ -153,6 +153,16 @@ func TestContexts(t *testing.T) {
ctx: WithDryRun(ctx),
check: IsDryRun,
want: true,
}, {
name: "within parent",
ctx: WithDryRun(ctx),
check: IsWithinParent,
want: false,
}, {
name: "in dry run",
ctx: WithinParent(ctx, metav1.ObjectMeta{Name: "jack"}),
check: IsWithinParent,
want: true,
}}
for _, tc := range tests {