k8s.io/kubernetes@v1.29.3/pkg/apis/admissionregistration/util.go (about) 1 /* 2 Copyright 2023 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 admissionregistration 18 19 import ( 20 genericfeatures "k8s.io/apiserver/pkg/features" 21 utilfeature "k8s.io/apiserver/pkg/util/feature" 22 ) 23 24 // DropDisabledMutatingWebhookConfigurationFields removes disabled fields from the mutatingWebhookConfiguration metadata and spec. 25 // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a mutatingWebhookConfiguration 26 func DropDisabledMutatingWebhookConfigurationFields(mutatingWebhookConfiguration, oldMutatingWebhookConfiguration *MutatingWebhookConfiguration) { 27 if !utilfeature.DefaultFeatureGate.Enabled(genericfeatures.AdmissionWebhookMatchConditions) && !matchConditionsInUseMutatingWebhook(oldMutatingWebhookConfiguration) { 28 for i := range mutatingWebhookConfiguration.Webhooks { 29 mutatingWebhookConfiguration.Webhooks[i].MatchConditions = nil 30 } 31 } 32 } 33 34 func matchConditionsInUseMutatingWebhook(mutatingWebhookConfiguration *MutatingWebhookConfiguration) bool { 35 if mutatingWebhookConfiguration == nil { 36 return false 37 } 38 for _, webhook := range mutatingWebhookConfiguration.Webhooks { 39 if len(webhook.MatchConditions) != 0 { 40 return true 41 } 42 } 43 return false 44 } 45 46 // DropDisabledValidatingWebhookConfigurationFields removes disabled fields from the validatingWebhookConfiguration metadata and spec. 47 // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a validatingWebhookConfiguration 48 func DropDisabledValidatingWebhookConfigurationFields(validatingWebhookConfiguration, oldValidatingWebhookConfiguration *ValidatingWebhookConfiguration) { 49 if !utilfeature.DefaultFeatureGate.Enabled(genericfeatures.AdmissionWebhookMatchConditions) && !matchConditionsInUseValidatingWebhook(oldValidatingWebhookConfiguration) { 50 for i := range validatingWebhookConfiguration.Webhooks { 51 validatingWebhookConfiguration.Webhooks[i].MatchConditions = nil 52 } 53 } 54 } 55 56 func matchConditionsInUseValidatingWebhook(validatingWebhookConfiguration *ValidatingWebhookConfiguration) bool { 57 if validatingWebhookConfiguration == nil { 58 return false 59 } 60 for _, webhook := range validatingWebhookConfiguration.Webhooks { 61 if len(webhook.MatchConditions) != 0 { 62 return true 63 } 64 } 65 return false 66 }