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

add admissionRequest in ctx #2718

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions apis/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -245,6 +246,7 @@ func IsDryRun(ctx context.Context) bool {
// This is attached to contexts passed to webhook interfaces with
// additional context from the HTTP request.
type httpReq struct{}
type AdmissionReq struct{}
rahulii marked this conversation as resolved.
Show resolved Hide resolved

// WithHTTPRequest associated the HTTP request object the webhook
// received with the context.
Expand All @@ -260,3 +262,18 @@ func GetHTTPRequest(ctx context.Context) *http.Request {
}
return v.(*http.Request)
}

// WithAdmissionRequest associated the admissionv1.AdmissionRequest object the webhook
// received with the context.
func WithAdmissionRequest(ctx context.Context, r *admissionv1.AdmissionRequest) context.Context {
return context.WithValue(ctx, AdmissionReq{}, r)
}

// GetAdmissionRequest fetches the admissionv1.AdmissionRequest received by the webhook.
func GetAdmissionRequest(ctx context.Context) *admissionv1.AdmissionRequest {
v := ctx.Value(AdmissionReq{})
if v == nil {
return nil
}
return v.(*admissionv1.AdmissionRequest)
}
23 changes: 23 additions & 0 deletions apis/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
admissionv1 "k8s.io/api/admission/v1"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -241,3 +242,25 @@ func TestGetHTTPRequest(t *testing.T) {
t.Errorf("GetHTTPRequest() = %v, wanted %v", got, want)
}
}

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

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

admReq := admissionv1.AdmissionRequest{
Operation: admissionv1.Create,
Kind: metav1.GroupVersionKind{
Group: "pkg.knative.dev",
Version: "v1alpha1",
Kind: "Resource",
},
}
ctx = WithAdmissionRequest(ctx, &admReq)

if want, got := &admReq, GetAdmissionRequest(ctx); got != want {
t.Errorf("GetAdmissionRequest() = %v, wanted %v", got, want)
}
}