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

adds WithNamespace ability to contexts #2390

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apis/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ func IsWithinParent(ctx context.Context) bool {
return ok
}

type namespaceKey struct{}
// WithNamespace attaches the request's namespace. Intended to be used
// where defaulting depends on the namespace (eventing channels, brokers)
// and the client may not provide it in the resource ObjectMeta
func WithNamespace(ctx context.Context, ns string) context.Context {
return context.WithValue(ctx, namespaceKey{}, ns)
}

func GetNamespace(ctx context.Context) string {
if ns, ok := ctx.Value(namespaceKey{}).(string); ok {
return ns
}

return ""
}

// 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.
Expand Down
15 changes: 15 additions & 0 deletions apis/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,18 @@ func TestGetHTTPRequest(t *testing.T) {
t.Errorf("GetHTTPRequest() = %v, wanted %v", got, want)
}
}

func TestGetNamespace(t *testing.T) {
ctx := context.Background()

if got := GetNamespace(ctx); got != "" {
t.Errorf("GetNamespace() = \"%v\", wanted \"\"", got)
}

ns := "some-namespace"
ctx = WithNamespace(ctx, ns)

if want, got := ns, GetNamespace(ctx); got != want {
t.Errorf("GetNamespace() = \"%v\", wanted \"%v\"", got, want)
}
}
2 changes: 1 addition & 1 deletion webhook/resourcesemantics/defaulting/defaulting.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (ac *reconciler) mutate(ctx context.Context, req *admissionv1.AdmissionRequ
ctx = apis.WithinCreate(ctx)
}
ctx = apis.WithUserInfo(ctx, &req.UserInfo)

ctx = apis.WithNamespace(ctx, req.Namespace)
// Default the new object.
if patches, err = setDefaults(ctx, patches, newObj); err != nil {
logger.Errorw("Failed the resource specific defaulter", zap.Error(err))
Expand Down