k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/mutating/reinvocationcontext.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mutating
    18  
    19  import (
    20  	apiequality "k8s.io/apimachinery/pkg/api/equality"
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  )
    24  
    25  type webhookReinvokeContext struct {
    26  	// lastWebhookOutput holds the result of the last webhook admission plugin call
    27  	lastWebhookOutput runtime.Object
    28  	// previouslyInvokedReinvocableWebhooks holds the set of webhooks that have been invoked and
    29  	// should be reinvoked if a later mutation occurs
    30  	previouslyInvokedReinvocableWebhooks sets.String
    31  	// reinvokeWebhooks holds the set of webhooks that should be reinvoked
    32  	reinvokeWebhooks sets.String
    33  }
    34  
    35  func (rc *webhookReinvokeContext) ShouldReinvokeWebhook(webhook string) bool {
    36  	return rc.reinvokeWebhooks.Has(webhook)
    37  }
    38  
    39  func (rc *webhookReinvokeContext) IsOutputChangedSinceLastWebhookInvocation(object runtime.Object) bool {
    40  	return !apiequality.Semantic.DeepEqual(rc.lastWebhookOutput, object)
    41  }
    42  
    43  func (rc *webhookReinvokeContext) SetLastWebhookInvocationOutput(object runtime.Object) {
    44  	if object == nil {
    45  		rc.lastWebhookOutput = nil
    46  		return
    47  	}
    48  	rc.lastWebhookOutput = object.DeepCopyObject()
    49  }
    50  
    51  func (rc *webhookReinvokeContext) AddReinvocableWebhookToPreviouslyInvoked(webhook string) {
    52  	if rc.previouslyInvokedReinvocableWebhooks == nil {
    53  		rc.previouslyInvokedReinvocableWebhooks = sets.NewString()
    54  	}
    55  	rc.previouslyInvokedReinvocableWebhooks.Insert(webhook)
    56  }
    57  
    58  func (rc *webhookReinvokeContext) RequireReinvokingPreviouslyInvokedPlugins() {
    59  	if len(rc.previouslyInvokedReinvocableWebhooks) > 0 {
    60  		if rc.reinvokeWebhooks == nil {
    61  			rc.reinvokeWebhooks = sets.NewString()
    62  		}
    63  		for s := range rc.previouslyInvokedReinvocableWebhooks {
    64  			rc.reinvokeWebhooks.Insert(s)
    65  		}
    66  		rc.previouslyInvokedReinvocableWebhooks = sets.NewString()
    67  	}
    68  }