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

Plumb context timeout to admission webhooks #81602

Merged
merged 3 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/kubeapiserver/admission/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
package admission

import (
"context"
"testing"

"k8s.io/apiserver/pkg/admission"
)

type doNothingAdmission struct{}

func (doNothingAdmission) Admit(a admission.Attributes, o admission.ObjectInterfaces) error {
func (doNothingAdmission) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {
return nil
}
func (doNothingAdmission) Handles(o admission.Operation) bool { return false }
Expand Down
10 changes: 5 additions & 5 deletions pkg/registry/apps/deployment/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (r *RollbackREST) Create(ctx context.Context, name string, obj runtime.Obje
}

if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -320,17 +320,17 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
}

func toScaleCreateValidation(f rest.ValidateObjectFunc) rest.ValidateObjectFunc {
return func(obj runtime.Object) error {
return func(ctx context.Context, obj runtime.Object) error {
scale, err := scaleFromDeployment(obj.(*apps.Deployment))
if err != nil {
return err
}
return f(scale)
return f(ctx, scale)
}
}

func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjectUpdateFunc {
return func(obj, old runtime.Object) error {
return func(ctx context.Context, obj, old runtime.Object) error {
newScale, err := scaleFromDeployment(obj.(*apps.Deployment))
if err != nil {
return err
Expand All @@ -339,7 +339,7 @@ func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjec
if err != nil {
return err
}
return f(newScale, oldScale)
return f(ctx, newScale, oldScale)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/registry/apps/deployment/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package storage

import (
"context"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -394,7 +395,7 @@ func TestCreateDeploymentRollbackValidation(t *testing.T) {
}

validationError := fmt.Errorf("admission deny")
alwaysDenyValidationFunc := func(obj runtime.Object) error { return validationError }
alwaysDenyValidationFunc := func(ctx context.Context, obj runtime.Object) error { return validationError }
_, err := rollbackStorage.Create(ctx, rollback.Name, &rollback, alwaysDenyValidationFunc, &metav1.CreateOptions{})

if err == nil || validationError != err {
Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/apps/replicaset/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,17 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
}

func toScaleCreateValidation(f rest.ValidateObjectFunc) rest.ValidateObjectFunc {
return func(obj runtime.Object) error {
return func(ctx context.Context, obj runtime.Object) error {
scale, err := scaleFromReplicaSet(obj.(*apps.ReplicaSet))
if err != nil {
return err
}
return f(scale)
return f(ctx, scale)
}
}

func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjectUpdateFunc {
return func(obj, old runtime.Object) error {
return func(ctx context.Context, obj, old runtime.Object) error {
newScale, err := scaleFromReplicaSet(obj.(*apps.ReplicaSet))
if err != nil {
return err
Expand All @@ -244,7 +244,7 @@ func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjec
if err != nil {
return err
}
return f(newScale, oldScale)
return f(ctx, newScale, oldScale)
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/apps/statefulset/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,17 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
}

func toScaleCreateValidation(f rest.ValidateObjectFunc) rest.ValidateObjectFunc {
return func(obj runtime.Object) error {
return func(ctx context.Context, obj runtime.Object) error {
scale, err := scaleFromStatefulSet(obj.(*apps.StatefulSet))
if err != nil {
return err
}
return f(scale)
return f(ctx, scale)
}
}

func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjectUpdateFunc {
return func(obj, old runtime.Object) error {
return func(ctx context.Context, obj, old runtime.Object) error {
newScale, err := scaleFromStatefulSet(obj.(*apps.StatefulSet))
if err != nil {
return err
Expand All @@ -231,7 +231,7 @@ func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjec
if err != nil {
return err
}
return f(newScale, oldScale)
return f(ctx, newScale, oldScale)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/authentication/tokenreview/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
}

if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
}

if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/authorization/selfsubjectaccessreview/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
}

if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/authorization/selfsubjectrulesreview/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
}

if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/authorization/subjectaccessreview/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
}

if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/core/namespace/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.Va
// wrong type
return nil, fmt.Errorf("expected *api.Namespace, got %v", existing)
}
if err := deleteValidation(existingNamespace); err != nil {
if err := deleteValidation(ctx, existingNamespace); err != nil {
return nil, err
}
// Set the deletion timestamp if needed
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/core/pod/storage/eviction.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (r *EvictionREST) Create(ctx context.Context, obj runtime.Object, createVal
pod := obj.(*api.Pod)

if createValidation != nil {
if err := createValidation(eviction.DeepCopyObject()); err != nil {
if err := createValidation(ctx, eviction.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/core/pod/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (r *BindingREST) Create(ctx context.Context, obj runtime.Object, createVali
}

if createValidation != nil {
if err := createValidation(binding.DeepCopyObject()); err != nil {
if err := createValidation(ctx, binding.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/registry/core/replicationcontroller/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,15 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
}

func toScaleCreateValidation(f rest.ValidateObjectFunc) rest.ValidateObjectFunc {
return func(obj runtime.Object) error {
return f(scaleFromRC(obj.(*api.ReplicationController)))
return func(ctx context.Context, obj runtime.Object) error {
return f(ctx, scaleFromRC(obj.(*api.ReplicationController)))
}
}

func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjectUpdateFunc {
return func(obj, old runtime.Object) error {
return func(ctx context.Context, obj, old runtime.Object) error {
return f(
ctx,
scaleFromRC(obj.(*api.ReplicationController)),
scaleFromRC(old.(*api.ReplicationController)),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/core/serviceaccount/storage/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var gvk = schema.GroupVersionKind{

func (r *TokenREST) Create(ctx context.Context, name string, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
return nil, err
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/registry/extensions/controller/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
}

func toScaleCreateValidation(f rest.ValidateObjectFunc) rest.ValidateObjectFunc {
return func(obj runtime.Object) error {
return f(scaleFromRC(obj.(*api.ReplicationController)))
return func(ctx context.Context, obj runtime.Object) error {
return f(ctx, scaleFromRC(obj.(*api.ReplicationController)))
}
}

func toScaleUpdateValidation(f rest.ValidateObjectUpdateFunc) rest.ValidateObjectUpdateFunc {
return func(obj, old runtime.Object) error {
return func(ctx context.Context, obj, old runtime.Object) error {
return f(
ctx,
scaleFromRC(obj.(*api.ReplicationController)),
scaleFromRC(old.(*api.ReplicationController)),
)
Expand Down
5 changes: 3 additions & 2 deletions plugin/pkg/admission/admit/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package admit

import (
"context"
"io"

"k8s.io/apiserver/pkg/admission"
Expand All @@ -40,12 +41,12 @@ var _ admission.MutationInterface = alwaysAdmit{}
var _ admission.ValidationInterface = alwaysAdmit{}

// Admit makes an admission decision based on the request attributes
func (alwaysAdmit) Admit(a admission.Attributes, o admission.ObjectInterfaces) (err error) {
func (alwaysAdmit) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
return nil
}

// Validate makes an admission decision based on the request attributes. It is NOT allowed to mutate.
func (alwaysAdmit) Validate(a admission.Attributes, o admission.ObjectInterfaces) (err error) {
func (alwaysAdmit) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions plugin/pkg/admission/admit/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package admit

import (
"context"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -27,15 +28,15 @@ import (

func TestAdmissionNonNilAttribute(t *testing.T) {
handler := admissiontesting.WithReinvocationTesting(t, NewAlwaysAdmit().(*alwaysAdmit))
err := handler.Admit(admission.NewAttributesRecord(nil, nil, api.Kind("kind").WithVersion("version"), "namespace", "name", api.Resource("resource").WithVersion("version"), "subresource", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
err := handler.Admit(context.TODO(), admission.NewAttributesRecord(nil, nil, api.Kind("kind").WithVersion("version"), "namespace", "name", api.Resource("resource").WithVersion("version"), "subresource", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
if err != nil {
t.Errorf("Unexpected error returned from admission handler")
}
}

func TestAdmissionNilAttribute(t *testing.T) {
handler := NewAlwaysAdmit()
err := handler.(*alwaysAdmit).Admit(nil, nil)
err := handler.(*alwaysAdmit).Admit(context.TODO(), nil, nil)
if err != nil {
t.Errorf("Unexpected error returned from admission handler")
}
Expand Down
5 changes: 3 additions & 2 deletions plugin/pkg/admission/alwayspullimages/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
package alwayspullimages

import (
"context"
"io"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -55,7 +56,7 @@ var _ admission.MutationInterface = &AlwaysPullImages{}
var _ admission.ValidationInterface = &AlwaysPullImages{}

// Admit makes an admission decision based on the request attributes
func (a *AlwaysPullImages) Admit(attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
func (a *AlwaysPullImages) Admit(ctx context.Context, attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
// Ignore all calls to subresources or resources other than pods.
if shouldIgnore(attributes) {
return nil
Expand All @@ -74,7 +75,7 @@ func (a *AlwaysPullImages) Admit(attributes admission.Attributes, o admission.Ob
}

// Validate makes sure that all containers are set to always pull images
func (*AlwaysPullImages) Validate(attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
func (*AlwaysPullImages) Validate(ctx context.Context, attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
if shouldIgnore(attributes) {
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions plugin/pkg/admission/alwayspullimages/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package alwayspullimages

import (
"context"
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestAdmission(t *testing.T) {
},
},
}
err := handler.Admit(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
err := handler.Admit(context.TODO(), admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
if err != nil {
t.Errorf("Unexpected error returned from admission handler")
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestValidate(t *testing.T) {
`pods "123" is forbidden: spec.containers[0].imagePullPolicy: Unsupported value: "": supported values: "Always", ` +
`pods "123" is forbidden: spec.containers[1].imagePullPolicy: Unsupported value: "Never": supported values: "Always", ` +
`pods "123" is forbidden: spec.containers[2].imagePullPolicy: Unsupported value: "IfNotPresent": supported values: "Always"]`
err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
err := handler.Validate(context.TODO(), admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
if err == nil {
t.Fatal("missing expected error")
}
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestOtherResources(t *testing.T) {
for _, tc := range tests {
handler := admissiontesting.WithReinvocationTesting(t, &AlwaysPullImages{})

err := handler.Admit(admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, &metav1.CreateOptions{}, false, nil), nil)
err := handler.Admit(context.TODO(), admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, &metav1.CreateOptions{}, false, nil), nil)

if tc.expectError {
if err == nil {
Expand Down
3 changes: 2 additions & 1 deletion plugin/pkg/admission/antiaffinity/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package antiaffinity

import (
"context"
"fmt"
"io"

Expand Down Expand Up @@ -52,7 +53,7 @@ func NewInterPodAntiAffinity() *Plugin {

// Validate will deny any pod that defines AntiAffinity topology key other than v1.LabelHostname i.e. "kubernetes.io/hostname"
// in requiredDuringSchedulingRequiredDuringExecution and requiredDuringSchedulingIgnoredDuringExecution.
func (p *Plugin) Validate(attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
func (p *Plugin) Validate(ctx context.Context, attributes admission.Attributes, o admission.ObjectInterfaces) (err error) {
// Ignore all calls to subresources or resources other than pods.
if len(attributes.GetSubresource()) != 0 || attributes.GetResource().GroupResource() != api.Resource("pods") {
return nil
Expand Down