mirror of https://github.com/knative/pkg.git
Add IsWithinParent (#1765)
This commit is contained in:
parent
735a38c032
commit
84e91da23c
|
@ -134,6 +134,12 @@ func WithinParent(ctx context.Context, om metav1.ObjectMeta) context.Context {
|
||||||
return context.WithValue(ctx, parentMetaKey{}, om)
|
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
|
// ParentMeta accesses the ObjectMeta of the enclosing parent resource
|
||||||
// from the context. See WithinParent for how to attach the parent's
|
// from the context. See WithinParent for how to attach the parent's
|
||||||
// ObjectMeta to the context.
|
// ObjectMeta to the context.
|
||||||
|
|
|
@ -153,6 +153,16 @@ func TestContexts(t *testing.T) {
|
||||||
ctx: WithDryRun(ctx),
|
ctx: WithDryRun(ctx),
|
||||||
check: IsDryRun,
|
check: IsDryRun,
|
||||||
want: true,
|
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 {
|
for _, tc := range tests {
|
||||||
|
|
Loading…
Reference in New Issue