| 1 |
/* |
| 2 |
Copyright 2024 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/types" |
| 23 |
"k8s.io/apimachinery/pkg/util/sets" |
| 24 |
) |
| 25 |
|
| 26 |
type key struct { |
| 27 |
PolicyUID types.NamespacedName |
| 28 |
BindingUID types.NamespacedName |
| 29 |
ParamUID types.NamespacedName |
| 30 |
MutationIndex int |
| 31 |
} |
| 32 |
|
| 33 |
type policyReinvokeContext struct { |
| 34 |
// lastPolicyOutput holds the result of the last Policy admission plugin call |
| 35 |
lastPolicyOutput runtime.Object |
| 36 |
// previouslyInvokedReinvocablePolicys holds the set of policies that have been invoked and |
| 37 |
// should be reinvoked if a later mutation occurs |
| 38 |
previouslyInvokedReinvocablePolicies sets.Set[key] |
| 39 |
// reinvokePolicies holds the set of Policies that should be reinvoked |
| 40 |
reinvokePolicies sets.Set[key] |
| 41 |
} |
| 42 |
|
| 43 |
func (rc *policyReinvokeContext) ShouldReinvoke(policy key) bool { |
| 44 |
return rc.reinvokePolicies.Has(policy) |
| 45 |
} |
| 46 |
|
| 47 |
func (rc *policyReinvokeContext) IsOutputChangedSinceLastPolicyInvocation(object runtime.Object) bool { |
| 48 |
return !apiequality.Semantic.DeepEqual(rc.lastPolicyOutput, object) |
| 49 |
} |
| 50 |
|
| 51 |
func (rc *policyReinvokeContext) SetLastPolicyInvocationOutput(object runtime.Object) { |
| 52 |
if object == nil { |
| 53 |
rc.lastPolicyOutput = nil |
| 54 |
return |
| 55 |
} |
| 56 |
rc.lastPolicyOutput = object.DeepCopyObject() |
| 57 |
} |
| 58 |
|
| 59 |
func (rc *policyReinvokeContext) AddReinvocablePolicyToPreviouslyInvoked(policy key) { |
| 60 |
if rc.previouslyInvokedReinvocablePolicies == nil { |
| 61 |
rc.previouslyInvokedReinvocablePolicies = sets.New[key]() |
| 62 |
} |
| 63 |
rc.previouslyInvokedReinvocablePolicies.Insert(policy) |
| 64 |
} |
| 65 |
|
| 66 |
func (rc *policyReinvokeContext) RequireReinvokingPreviouslyInvokedPlugins() { |
| 67 |
if len(rc.previouslyInvokedReinvocablePolicies) > 0 { |
| 68 |
if rc.reinvokePolicies == nil { |
| 69 |
rc.reinvokePolicies = sets.New[key]() |
| 70 |
} |
| 71 |
for s := range rc.previouslyInvokedReinvocablePolicies { |
| 72 |
rc.reinvokePolicies.Insert(s) |
| 73 |
} |
| 74 |
rc.previouslyInvokedReinvocablePolicies = sets.New[key]() |
| 75 |
} |
| 76 |
} |