diff --git a/docs/README.md b/docs/README.md index 477bd8eae09..fdf7027487c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -143,6 +143,7 @@ * [`ContainerRuntime` resource](extensions/containerruntime.md) * Generic (non-essential) extensions * [`Extension` resource](extensions/extension.md) + * [Extension Admission](extensions/admission.md) * [Provider Local](extensions/provider-local.md) * [Extending project roles](extensions/project-roles.md) * [Referenced resources](extensions/referenced-resources.md) diff --git a/docs/concepts/apiserver_admission_plugins.md b/docs/concepts/apiserver_admission_plugins.md index ec0ac338804..cda3c629b41 100644 --- a/docs/concepts/apiserver_admission_plugins.md +++ b/docs/concepts/apiserver_admission_plugins.md @@ -54,6 +54,12 @@ This admission controller reacts on `CREATE` and `UPDATE` operations for `Backup For all the various extension types in the specifications of these objects, it validates whether there exists a `ControllerRegistration` in the system that is primarily responsible for the stated extension type(s). This prevents misconfigurations that would otherwise allow users to create such resources with extension types that don't exist in the cluster, effectively leading to failing reconciliation loops. +## `ExtensionLabels` + +_(enabled by default)_ + +This admission controller reacts on `CREATE` and `UPDATE` operations for `BackupBucket`s, `BackupEntry`s, `CloudProfile`s, `Seed`s and `Shoot`s. For all the various extension types in the specifications of these objects, it adds a corresponding label in the resource. This would allow extension admission webhooks to filter out the resources they are responsible for and ignore all others. This label is of the form `.extensions.gardener.cloud/ : "true"`. For example, an extension label for provider extension type `aws`, looks like `provider.extensions.gardener.cloud/aws : "true"`. + ## `PlantValidator` _(enabled by default)_ diff --git a/docs/extensions/admission.md b/docs/extensions/admission.md new file mode 100644 index 00000000000..8edcefaa0a8 --- /dev/null +++ b/docs/extensions/admission.md @@ -0,0 +1,5 @@ +# Extension Admission + +The extensions are expected to validate their respective resources for their extension specific configurations, when the resources are newly created or updated. For example, [provider extensions](../../extensions/README.md#infrastructure-provider) would validate `spec.provider.infrastructureConfig` and `spec.provider.controlPlaneConfig` in the `Shoot` resource and `spec.providerConfig` in the `CloudProfile` resource, [networking extensions](../../extensions/README.md#network-plugin) would validate `spec.networking.providerConfig` in the `Shoot` resource. As best practice, the validation should be performed only if there is a change in the `spec` of the resource. Please find an exemplary implementation [here](https://github.com/gardener/gardener-extension-provider-aws/tree/master/pkg/admission/validator). + +When a resource is newly created or updated, Gardener adds an extension label for all the extension types referenced in the `spec` of the resource. This label is of the form `.extensions.gardener.cloud/ : "true"`. For example, an extension label for provider extension type `aws`, looks like `provider.extensions.gardener.cloud/aws : "true"`. The extensions should add object selectors in their admission webhooks for these labels, to filter out the objects they are responsible for. At present, these labels are added to `BackupEntry`s, `BackupBucket`s, `CloudProfile`s, `Seed`s, and `Shoot`s. Please see [this](https://github.com/gardener/gardener/tree/master/pkg/apis/core/v1beta1/constants/types_constants.go) for the full list of extension labels. diff --git a/pkg/apis/core/v1beta1/constants/types_constants.go b/pkg/apis/core/v1beta1/constants/types_constants.go index bb28390648e..38b4579f088 100644 --- a/pkg/apis/core/v1beta1/constants/types_constants.go +++ b/pkg/apis/core/v1beta1/constants/types_constants.go @@ -293,6 +293,19 @@ const ( // LabelMonitoring is a constant for a label for monitoring stack configurations LabelMonitoring = "monitoring" + // LabelExtensionExtensionTypePrefix is used to prefix extension label for extension types. + LabelExtensionExtensionTypePrefix = "extensions.extensions.gardener.cloud/" + // LabelExtensionProviderTypePrefix is used to prefix extension label for cloud provider types. + LabelExtensionProviderTypePrefix = "provider.extensions.gardener.cloud/" + // LabelExtensionDNSRecordTypePrefix is used to prefix extension label for DNS types. + LabelExtensionDNSRecordTypePrefix = "dnsrecord.extensions.gardener.cloud/" + // LabelExtensionNetworkingTypePrefix is used to prefix extension label for networking plugin types. + LabelExtensionNetworkingTypePrefix = "networking.extensions.gardener.cloud/" + // LabelExtensionOperatingSystemConfigTypePrefix is used to prefix extension label for OperatingSystemConfig types. + LabelExtensionOperatingSystemConfigTypePrefix = "operatingsystemconfig.extensions.gardener.cloud/" + // LabelExtensionContainerRuntimeTypePrefix is used to prefix extension label for ContainerRuntime types. + LabelExtensionContainerRuntimeTypePrefix = "containerruntime.extensions.gardener.cloud/" + // LabelNetworkPolicyToBlockedCIDRs allows Egress from pods labeled with 'networking.gardener.cloud/to-blocked-cidrs=allowed'. LabelNetworkPolicyToBlockedCIDRs = "networking.gardener.cloud/to-blocked-cidrs" // LabelNetworkPolicyToDNS allows Egress from pods labeled with 'networking.gardener.cloud/to-dns=allowed' to DNS running in 'kube-system'. diff --git a/pkg/apiserver/plugins.go b/pkg/apiserver/plugins.go index 4afc7e7e12b..1c4b41dc2bd 100644 --- a/pkg/apiserver/plugins.go +++ b/pkg/apiserver/plugins.go @@ -19,6 +19,7 @@ import ( controllerregistrationresources "github.com/gardener/gardener/plugin/pkg/controllerregistration/resources" "github.com/gardener/gardener/plugin/pkg/global/customverbauthorizer" "github.com/gardener/gardener/plugin/pkg/global/deletionconfirmation" + "github.com/gardener/gardener/plugin/pkg/global/extensionlabels" "github.com/gardener/gardener/plugin/pkg/global/extensionvalidation" "github.com/gardener/gardener/plugin/pkg/global/resourcereferencemanager" managedseedshoot "github.com/gardener/gardener/plugin/pkg/managedseed/shoot" @@ -50,6 +51,7 @@ var ( lifecycle.PluginName, // NamespaceLifecycle resourcereferencemanager.PluginName, // ResourceReferenceManager extensionvalidation.PluginName, // ExtensionValidator + extensionlabels.PluginName, // ExtensionLabels shoottolerationrestriction.PluginName, // ShootTolerationRestriction shootexposureclass.PluginName, // ShootExposureClass shootdns.PluginName, // ShootDNS @@ -85,6 +87,7 @@ var ( lifecycle.PluginName, // NamespaceLifecycle resourcereferencemanager.PluginName, // ResourceReferenceManager extensionvalidation.PluginName, // ExtensionValidator + extensionlabels.PluginName, // ExtensionLabels shoottolerationrestriction.PluginName, // ShootTolerationRestriction shootexposureclass.PluginName, // ShootExposureClass shootdns.PluginName, // ShootDNS @@ -116,6 +119,7 @@ func RegisterAllAdmissionPlugins(plugins *admission.Plugins) { resourcereferencemanager.Register(plugins) deletionconfirmation.Register(plugins) extensionvalidation.Register(plugins) + extensionlabels.Register(plugins) shoottolerationrestriction.Register(plugins) shootexposureclass.Register(plugins) shootquotavalidator.Register(plugins) diff --git a/plugin/pkg/global/extensionlabels/admission.go b/plugin/pkg/global/extensionlabels/admission.go new file mode 100644 index 00000000000..621b39c842f --- /dev/null +++ b/plugin/pkg/global/extensionlabels/admission.go @@ -0,0 +1,245 @@ +// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package extensionlabels + +import ( + "context" + "errors" + "fmt" + "io" + "strings" + + "github.com/gardener/gardener/pkg/apis/core" + v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" + admissioninitializer "github.com/gardener/gardener/pkg/apiserver/admission/initializer" + internalcoreinformers "github.com/gardener/gardener/pkg/client/core/informers/internalversion" + corelisters "github.com/gardener/gardener/pkg/client/core/listers/core/internalversion" + "github.com/gardener/gardener/pkg/features" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apiserver/pkg/admission" + utilfeature "k8s.io/apiserver/pkg/util/feature" +) + +const ( + // PluginName is the name of this admission plugin. + PluginName = "ExtensionLabels" +) + +// Register registers a plugin. +func Register(plugins *admission.Plugins) { + plugins.Register(PluginName, NewFactory) +} + +// NewFactory creates a new PluginFactory. +func NewFactory(config io.Reader) (admission.Interface, error) { + return New() +} + +// ExtensionLabels contains the admission handler +type ExtensionLabels struct { + *admission.Handler + backupBucketLister corelisters.BackupBucketLister + readyFunc admission.ReadyFunc +} + +var ( + _ = admissioninitializer.WantsInternalCoreInformerFactory(&ExtensionLabels{}) + readyFuncs []admission.ReadyFunc +) + +// New creates a new ExtensionLabels admission plugin. +func New() (*ExtensionLabels, error) { + return &ExtensionLabels{ + Handler: admission.NewHandler(admission.Create, admission.Update), + }, nil +} + +// AssignReadyFunc assigns the ready function to the admission handler. +func (e *ExtensionLabels) AssignReadyFunc(f admission.ReadyFunc) { + e.readyFunc = f + e.SetReadyFunc(f) +} + +// SetInternalCoreInformerFactory sets the external garden core informer factory. +func (e *ExtensionLabels) SetInternalCoreInformerFactory(f internalcoreinformers.SharedInformerFactory) { + backupBucketInformer := f.Core().InternalVersion().BackupBuckets() + e.backupBucketLister = backupBucketInformer.Lister() + + readyFuncs = append(readyFuncs, backupBucketInformer.Informer().HasSynced) +} + +func (e *ExtensionLabels) waitUntilReady(attrs admission.Attributes) error { + // Wait until the caches have been synced + if e.readyFunc == nil { + e.AssignReadyFunc(func() bool { + for _, readyFunc := range readyFuncs { + if !readyFunc() { + return false + } + } + return true + }) + } + + if !e.WaitForReady() { + return admission.NewForbidden(attrs, errors.New("not yet ready to handle request")) + } + + return nil +} + +// ValidateInitialization checks whether the plugin was correctly initialized. +func (e *ExtensionLabels) ValidateInitialization() error { + if e.backupBucketLister == nil { + return errors.New("missing BackupBucket lister") + } + return nil +} + +var _ admission.MutationInterface = &ExtensionLabels{} + +// Admit add extension labels to resources. +func (e *ExtensionLabels) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error { + if err := e.waitUntilReady(a); err != nil { + return fmt.Errorf("err while waiting for ready %w", err) + } + + switch a.GetKind().GroupKind() { + case core.Kind("Seed"): + seed, ok := a.GetObject().(*core.Seed) + if !ok { + return apierrors.NewBadRequest("could not convert resource into Seed object") + } + + removeLabels(&seed.ObjectMeta) + addMetaDataLabelsSeed(seed) + + case core.Kind("Shoot"): + shoot, ok := a.GetObject().(*core.Shoot) + if !ok { + return apierrors.NewBadRequest("could not convert resource into Shoot object") + } + + removeLabels(&shoot.ObjectMeta) + addMetaDataLabelsShoot(shoot) + + case core.Kind("CloudProfile"): + cloudProfile, ok := a.GetObject().(*core.CloudProfile) + if !ok { + return apierrors.NewBadRequest("could not convert resource into CloudProfile object") + } + + removeLabels(&cloudProfile.ObjectMeta) + addMetaDataLabelsCloudProfile(cloudProfile) + + case core.Kind("BackupBucket"): + backupBucket, ok := a.GetObject().(*core.BackupBucket) + if !ok { + return apierrors.NewBadRequest("could not convert object into BackupBucket object") + } + + removeLabels(&backupBucket.ObjectMeta) + addMetaDataLabelsBackupBucket(backupBucket) + + case core.Kind("BackupEntry"): + backupEntry, ok := a.GetObject().(*core.BackupEntry) + if !ok { + return apierrors.NewBadRequest("could not convert object into BackupEntry object") + } + + backupBucket, err := e.backupBucketLister.Get(backupEntry.Spec.BucketName) + if err != nil { + return err + } + + removeLabels(&backupEntry.ObjectMeta) + addMetaDataLabelsBackupEntry(backupEntry, backupBucket) + } + return nil +} + +func addMetaDataLabelsSeed(seed *core.Seed) { + metav1.SetMetaDataLabel(&seed.ObjectMeta, v1beta1constants.LabelExtensionProviderTypePrefix+seed.Spec.Provider.Type, "true") + if seed.Spec.Backup != nil { + metav1.SetMetaDataLabel(&seed.ObjectMeta, v1beta1constants.LabelExtensionProviderTypePrefix+seed.Spec.Backup.Provider, "true") + } + + if seed.Spec.DNS.Provider != nil && utilfeature.DefaultFeatureGate.Enabled(features.UseDNSRecords) { + metav1.SetMetaDataLabel(&seed.ObjectMeta, v1beta1constants.LabelExtensionDNSRecordTypePrefix+seed.Spec.DNS.Provider.Type, "true") + } +} + +func addMetaDataLabelsShoot(shoot *core.Shoot) { + for _, extension := range shoot.Spec.Extensions { + if extension.Disabled != nil && *extension.Disabled { + continue + } + metav1.SetMetaDataLabel(&shoot.ObjectMeta, v1beta1constants.LabelExtensionExtensionTypePrefix+extension.Type, "true") + } + for _, pool := range shoot.Spec.Provider.Workers { + if pool.CRI != nil { + for _, cr := range pool.CRI.ContainerRuntimes { + metav1.SetMetaDataLabel(&shoot.ObjectMeta, v1beta1constants.LabelExtensionContainerRuntimeTypePrefix+cr.Type, "true") + } + } + if pool.Machine.Image != nil { + metav1.SetMetaDataLabel(&shoot.ObjectMeta, v1beta1constants.LabelExtensionOperatingSystemConfigTypePrefix+pool.Machine.Image.Name, "true") + } + } + if shoot.Spec.DNS != nil { + for _, provider := range shoot.Spec.DNS.Providers { + if provider.Type == nil || *provider.Type == core.DNSUnmanaged { + continue + } + if utilfeature.DefaultFeatureGate.Enabled(features.UseDNSRecords) { + metav1.SetMetaDataLabel(&shoot.ObjectMeta, v1beta1constants.LabelExtensionDNSRecordTypePrefix+*provider.Type, "true") + } + } + } + metav1.SetMetaDataLabel(&shoot.ObjectMeta, v1beta1constants.LabelExtensionProviderTypePrefix+shoot.Spec.Provider.Type, "true") + metav1.SetMetaDataLabel(&shoot.ObjectMeta, v1beta1constants.LabelExtensionNetworkingTypePrefix+shoot.Spec.Networking.Type, "true") +} + +func addMetaDataLabelsCloudProfile(cloudProfile *core.CloudProfile) { + metav1.SetMetaDataLabel(&cloudProfile.ObjectMeta, v1beta1constants.LabelExtensionProviderTypePrefix+cloudProfile.Spec.Type, "true") +} + +func addMetaDataLabelsBackupBucket(backupBucket *core.BackupBucket) { + metav1.SetMetaDataLabel(&backupBucket.ObjectMeta, v1beta1constants.LabelExtensionProviderTypePrefix+backupBucket.Spec.Provider.Type, "true") +} + +func addMetaDataLabelsBackupEntry(backupEntry *core.BackupEntry, backupBucket *core.BackupBucket) { + metav1.SetMetaDataLabel(&backupEntry.ObjectMeta, v1beta1constants.LabelExtensionProviderTypePrefix+backupBucket.Spec.Provider.Type, "true") +} + +func removeLabels(objectMeta *metav1.ObjectMeta) { + extensionLabels := []string{ + v1beta1constants.LabelExtensionExtensionTypePrefix, + v1beta1constants.LabelExtensionProviderTypePrefix, + v1beta1constants.LabelExtensionDNSRecordTypePrefix, + v1beta1constants.LabelExtensionNetworkingTypePrefix, + v1beta1constants.LabelExtensionOperatingSystemConfigTypePrefix, + v1beta1constants.LabelExtensionContainerRuntimeTypePrefix, + } + for k := range objectMeta.Labels { + for _, label := range extensionLabels { + if strings.HasPrefix(k, label) { + delete(objectMeta.Labels, k) + } + } + } +} diff --git a/plugin/pkg/global/extensionlabels/admission_test.go b/plugin/pkg/global/extensionlabels/admission_test.go new file mode 100644 index 00000000000..1f266fcee2a --- /dev/null +++ b/plugin/pkg/global/extensionlabels/admission_test.go @@ -0,0 +1,472 @@ +// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package extensionlabels_test + +import ( + "context" + + "github.com/gardener/gardener/pkg/apis/core" + internalcoreinformers "github.com/gardener/gardener/pkg/client/core/informers/internalversion" + . "github.com/gardener/gardener/plugin/pkg/global/extensionlabels" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apiserver/pkg/admission" + "k8s.io/utils/pointer" +) + +var _ = Describe("ExtensionLabels tests", func() { + var ( + admissionHandler *ExtensionLabels + gardenInternalCoreInformerFactory internalcoreinformers.SharedInformerFactory + ) + + BeforeEach(func() { + admissionHandler, _ = New() + admissionHandler.AssignReadyFunc(func() bool { return true }) + + gardenInternalCoreInformerFactory = internalcoreinformers.NewSharedInformerFactory(nil, 0) + admissionHandler.SetInternalCoreInformerFactory(gardenInternalCoreInformerFactory) + }) + + Context("Seed", func() { + var ( + seed *core.Seed + + providerType1 = "provider-type-1" + providerType2 = "provider-type-2" + dnsProviderType = "dns-provider" + ) + + BeforeEach(func() { + seed = &core.Seed{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-seed", + }, + Spec: core.SeedSpec{ + Provider: core.SeedProvider{ + Type: providerType1, + }, + Ingress: &core.Ingress{}, + DNS: core.SeedDNS{ + Provider: &core.SeedDNSProvider{ + Type: dnsProviderType, + }, + }, + Backup: &core.SeedBackup{ + Provider: providerType1, + }, + }, + } + }) + + It("should add all the correct labels on creation", func() { + attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), "", seed.Name, core.Resource("Seed").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := map[string]string{ + "provider.extensions.gardener.cloud/" + providerType1: "true", + "dnsrecord.extensions.gardener.cloud/" + dnsProviderType: "true", + } + + Expect(seed.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + + It("should add all the correct labels on updation", func() { + newSeed := seed.DeepCopy() + newSeed.Spec.Backup = &core.SeedBackup{ + Provider: providerType2, + } + + attrs := admission.NewAttributesRecord(newSeed, seed, core.Kind("Seed").WithVersion("version"), "", seed.Name, core.Resource("Seed").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := make(map[string]string) + expectedLabels["dnsrecord.extensions.gardener.cloud/"+dnsProviderType] = "true" + for _, providerType := range []string{providerType1, providerType2} { + expectedLabels["provider.extensions.gardener.cloud/"+providerType] = "true" + } + + Expect(newSeed.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + }) + + Context("Shoot", func() { + var ( + shoot *core.Shoot + + providerType = "provider-type" + networkingType = "networking-type" + machineImage1 = "machine-image-1" + machineImage2 = "machine-image-2" + crType1 = "containerRuntime-type-1" + crType2 = "containerRuntime-type-2" + crType3 = "containerRuntime-type-3" + crType4 = "containerRuntime-type-4" + dnsProviderType1 = "dns-external-1" + dnsProviderType2 = "dns-external-2" + extensionType1 = "extension-type-1" + extensionType2 = "extension-type-2" + extensionType3 = "extension-type-3" + ) + + BeforeEach(func() { + shoot = &core.Shoot{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-shoot", + Namespace: "test-namespace", + }, + Spec: core.ShootSpec{ + Networking: core.Networking{Type: networkingType}, + DNS: &core.DNS{ + Providers: []core.DNSProvider{ + {Type: &dnsProviderType1}, + {Type: &dnsProviderType2}, + }, + }, + Provider: core.Provider{ + Type: providerType, + Workers: []core.Worker{ + { + Machine: core.Machine{ + Type: "type-1", + Image: &core.ShootMachineImage{ + Name: machineImage1, + }, + }, + CRI: &core.CRI{ + ContainerRuntimes: []core.ContainerRuntime{ + {Type: crType1}, + {Type: crType2}, + {Type: crType2}, + }, + }, + }, + { + Machine: core.Machine{ + Type: "type-2", + Image: &core.ShootMachineImage{ + Name: machineImage1, + }, + }, + CRI: &core.CRI{ + ContainerRuntimes: []core.ContainerRuntime{ + {Type: crType1}, + {Type: crType1}, + {Type: crType2}, + }, + }, + }, + }, + }, + Extensions: []core.Extension{ + { + Type: extensionType1, + }, + { + Type: extensionType2, + Disabled: pointer.Bool(true), + }, + }, + }, + } + }) + + It("should add all the correct labels on creation", func() { + attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("Shoot").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := make(map[string]string) + + expectedLabels["networking.extensions.gardener.cloud/"+networkingType] = "true" + expectedLabels["operatingsystemconfig.extensions.gardener.cloud/"+machineImage1] = "true" + expectedLabels["provider.extensions.gardener.cloud/"+providerType] = "true" + expectedLabels["extensions.extensions.gardener.cloud/"+extensionType1] = "true" + for _, crType := range []string{crType1, crType2} { + expectedLabels["containerruntime.extensions.gardener.cloud/"+crType] = "true" + } + for _, dnsProviderType := range []string{dnsProviderType1, dnsProviderType2} { + expectedLabels["dnsrecord.extensions.gardener.cloud/"+dnsProviderType] = "true" + } + + Expect(shoot.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + + It("should add all the correct labels on updation", func() { + worker := core.Worker{ + Machine: core.Machine{ + Image: &core.ShootMachineImage{ + Name: machineImage2, + }, + }, + CRI: &core.CRI{ + ContainerRuntimes: []core.ContainerRuntime{ + { + Type: crType3, + }, + { + Type: crType4, + }, + }, + }, + } + extension := []core.Extension{ + { + Type: extensionType2, + Disabled: pointer.Bool(false), + }, + { + Type: extensionType3, + }} + + newShoot := shoot.DeepCopy() + newShoot.Spec.Provider.Workers = append(newShoot.Spec.Provider.Workers, worker) + newShoot.Spec.Extensions = extension + + attrs := admission.NewAttributesRecord(newShoot, shoot, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("Shoot").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := make(map[string]string) + + expectedLabels["networking.extensions.gardener.cloud/"+networkingType] = "true" + expectedLabels["provider.extensions.gardener.cloud/"+providerType] = "true" + for _, machineImage := range []string{machineImage1, machineImage2} { + expectedLabels["operatingsystemconfig.extensions.gardener.cloud/"+machineImage] = "true" + } + for _, crType := range []string{crType1, crType2, crType3, crType4} { + expectedLabels["containerruntime.extensions.gardener.cloud/"+crType] = "true" + } + for _, dnsProviderType := range []string{dnsProviderType1, dnsProviderType2} { + expectedLabels["dnsrecord.extensions.gardener.cloud/"+dnsProviderType] = "true" + } + for _, extensionType := range []string{extensionType2, extensionType3} { + expectedLabels["extensions.extensions.gardener.cloud/"+extensionType] = "true" + } + + Expect(newShoot.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + }) + + Context("CloudProfile", func() { + var ( + cloudProfile *core.CloudProfile + providerType1 = "provider-type-1" + providerType2 = "provider-type-2" + ) + + BeforeEach(func() { + cloudProfile = &core.CloudProfile{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cloudprofile", + }, + Spec: core.CloudProfileSpec{ + Type: providerType1, + }, + } + }) + + It("should add all the correct labels on creation", func() { + attrs := admission.NewAttributesRecord(cloudProfile, nil, core.Kind("CloudProfile").WithVersion("version"), "", cloudProfile.Name, core.Resource("CloudProfile").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := map[string]string{ + "provider.extensions.gardener.cloud/" + providerType1: "true", + } + + Expect(cloudProfile.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + + It("should add all the correct labels on updation", func() { + newCloudProfile := cloudProfile.DeepCopy() + newCloudProfile.Spec.Type = providerType2 + + attrs := admission.NewAttributesRecord(newCloudProfile, cloudProfile, core.Kind("CloudProfile").WithVersion("version"), "", cloudProfile.Name, core.Resource("CloudProfile").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := map[string]string{ + "provider.extensions.gardener.cloud/" + providerType2: "true", + } + + Expect(newCloudProfile.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + }) + + Context("Backup Bucket", func() { + var ( + backupBucket *core.BackupBucket + providerType = "provider-type" + ) + + BeforeEach(func() { + backupBucket = &core.BackupBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-backupbucket", + }, + Spec: core.BackupBucketSpec{ + Provider: core.BackupBucketProvider{ + Type: providerType, + }, + }, + } + }) + + It("should add all the correct labels on creation", func() { + attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), "", backupBucket.Name, core.Resource("BackupBucket").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := map[string]string{ + "provider.extensions.gardener.cloud/" + providerType: "true", + } + + Expect(backupBucket.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + }) + + Context("Backup Entry", func() { + var ( + backupBucket *core.BackupBucket + backupEntry *core.BackupEntry + providerType1 = "provider-type-1" + providerType2 = "provider-type-2" + ) + + BeforeEach(func() { + backupBucket = &core.BackupBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-backupbucket", + }, + Spec: core.BackupBucketSpec{ + Provider: core.BackupBucketProvider{ + Type: providerType1, + }, + }, + } + backupEntry = &core.BackupEntry{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-backupentry", + Namespace: "test-namespace", + }, + Spec: core.BackupEntrySpec{ + BucketName: backupBucket.Name, + }, + } + }) + + It("should add all the correct labels on creation", func() { + Expect(gardenInternalCoreInformerFactory.Core().InternalVersion().BackupBuckets().Informer().GetStore().Add(backupBucket)).To(Succeed()) + + attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("BackupEntry").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := map[string]string{ + "provider.extensions.gardener.cloud/" + providerType1: "true", + } + + Expect(backupEntry.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + + It("should add all the correct labels on updation", func() { + backupBucket2 := &core.BackupBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-backupbucket-2", + }, + Spec: core.BackupBucketSpec{ + Provider: core.BackupBucketProvider{ + Type: providerType2, + }, + }} + Expect(gardenInternalCoreInformerFactory.Core().InternalVersion().BackupBuckets().Informer().GetStore().Add(backupBucket2)).To(Succeed()) + + newBackupEntry := backupEntry.DeepCopy() + newBackupEntry.Spec.BucketName = backupBucket2.Name + + attrs := admission.NewAttributesRecord(newBackupEntry, backupEntry, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("BackupEntry").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) + err := admissionHandler.Admit(context.TODO(), attrs, nil) + + Expect(err).NotTo(HaveOccurred()) + + expectedLabels := map[string]string{ + "provider.extensions.gardener.cloud/" + providerType2: "true", + } + + Expect(newBackupEntry.ObjectMeta.Labels).To(Equal(expectedLabels)) + }) + + }) + + Describe("#Register", func() { + It("should register the plugin", func() { + plugins := admission.NewPlugins() + Register(plugins) + + registered := plugins.Registered() + Expect(registered).To(HaveLen(1)) + Expect(registered).To(ContainElement(PluginName)) + }) + }) + + Describe("#NewFactory", func() { + It("should create a new PluginFactory", func() { + f, err := NewFactory(nil) + + Expect(f).NotTo(BeNil()) + Expect(err).ToNot(HaveOccurred()) + }) + }) + + Describe("#New", func() { + It("should only handle CREATE + UPDATE operations", func() { + el, err := New() + + Expect(err).ToNot(HaveOccurred()) + Expect(el.Handles(admission.Create)).To(BeTrue()) + Expect(el.Handles(admission.Update)).To(BeTrue()) + Expect(el.Handles(admission.Connect)).NotTo(BeTrue()) + Expect(el.Handles(admission.Delete)).NotTo(BeTrue()) + }) + }) + + Describe("#ValidateInitialization", func() { + It("should return error if no BackupBucketLister is set", func() { + el, _ := New() + err := el.ValidateInitialization() + Expect(err).To(HaveOccurred()) + }) + + It("should not return error if BackupBucketLister and core client are set", func() { + el, _ := New() + el.SetInternalCoreInformerFactory(internalcoreinformers.NewSharedInformerFactory(nil, 0)) + err := el.ValidateInitialization() + Expect(err).ToNot(HaveOccurred()) + }) + }) +}) diff --git a/plugin/pkg/global/extensionlabels/extensionlabels_suite_test.go b/plugin/pkg/global/extensionlabels/extensionlabels_suite_test.go new file mode 100644 index 00000000000..7b25557aac8 --- /dev/null +++ b/plugin/pkg/global/extensionlabels/extensionlabels_suite_test.go @@ -0,0 +1,30 @@ +// Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package extensionlabels_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/gardener/gardener/pkg/apiserver/features" +) + +func TestExtensionlabels(t *testing.T) { + features.RegisterFeatureGates() + RegisterFailHandler(Fail) + RunSpecs(t, "Admission ExtensionLabels Suite") +} diff --git a/plugin/pkg/global/extensionvalidation/admission_test.go b/plugin/pkg/global/extensionvalidation/admission_test.go index 2bddebc672c..9191f4323c0 100644 --- a/plugin/pkg/global/extensionvalidation/admission_test.go +++ b/plugin/pkg/global/extensionvalidation/admission_test.go @@ -49,7 +49,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should do nothing because the resource is not BackupBucket, BackupEntry, Seed, or Shoot", func() { - attrs := admission.NewAttributesRecord(nil, nil, core.Kind("Foo").WithVersion("version"), "", "", core.Resource("foos").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(nil, nil, core.Kind("Foo").WithVersion("version"), "", "", core.Resource("foos").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -72,7 +72,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(extensionsv1alpha1.BackupBucketResource, backupBucket.Spec.Provider.Type, true) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Add(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -83,7 +83,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(extensionsv1alpha1.BackupBucketResource, backupBucket.Spec.Provider.Type, false) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Add(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -94,7 +94,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(extensionsv1alpha1.BackupBucketResource, "some-other-type", true) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Add(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -102,7 +102,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should prevent the object from being created because extension type is not registered", func() { - attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupBucket, nil, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -110,7 +110,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should do nothing because the spec has not changed", func() { - attrs := admission.NewAttributesRecord(backupBucket, backupBucket, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Update, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupBucket, backupBucket, core.Kind("BackupBucket").WithVersion("version"), backupBucket.Namespace, backupBucket.Name, core.Resource("backupbuckets").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) Expect(admissionHandler.Validate(context.TODO(), attrs, nil)).To(Succeed()) }) }) @@ -142,7 +142,7 @@ var _ = Describe("ExtensionValidator", func() { Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Add(controllerRegistration)).To(Succeed()) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().BackupBuckets().Informer().GetStore().Add(backupBucket)).To(Succeed()) - attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -154,7 +154,7 @@ var _ = Describe("ExtensionValidator", func() { Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Add(controllerRegistration)).To(Succeed()) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().BackupBuckets().Informer().GetStore().Add(backupBucket)).To(Succeed()) - attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -165,7 +165,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(extensionsv1alpha1.BackupEntryResource, "some-other-type", true) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Add(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -173,7 +173,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should prevent the object from being created because extension type is not registered", func() { - attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupEntry, nil, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -181,7 +181,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should do nothing because the spec has not changed", func() { - attrs := admission.NewAttributesRecord(backupEntry, backupEntry, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Update, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(backupEntry, backupEntry, core.Kind("BackupEntry").WithVersion("version"), backupEntry.Namespace, backupEntry.Name, core.Resource("backupentries").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) Expect(admissionHandler.Validate(context.TODO(), attrs, nil)).To(Succeed()) }) }) @@ -234,7 +234,7 @@ var _ = Describe("ExtensionValidator", func() { It("should allow to create the object", func() { registerAllExtensions() - attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -248,7 +248,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(registration.extensionKind, registration.extensionType, true) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Delete(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -264,7 +264,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(registration.extensionKind, registration.extensionType, false) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Update(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -274,7 +274,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should prevent the object from being created because no extension type is registered", func() { - attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(seed, nil, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -282,7 +282,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should do nothing because the spec has not changed", func() { - attrs := admission.NewAttributesRecord(seed, seed, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Update, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(seed, seed, core.Kind("Seed").WithVersion("version"), seed.Namespace, seed.Name, core.Resource("seeds").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) Expect(admissionHandler.Validate(context.TODO(), attrs, nil)).To(Succeed()) }) }) @@ -359,7 +359,7 @@ var _ = Describe("ExtensionValidator", func() { It("should allow to create the object", func() { registerAllExtensions() - attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -373,7 +373,7 @@ var _ = Describe("ExtensionValidator", func() { controllerRegistration := createControllerRegistrationForKindType(registration.extensionKind, registration.extensionType, true) Expect(gardenExternalCoreInformerFactory.Core().V1beta1().ControllerRegistrations().Informer().GetStore().Delete(controllerRegistration)).To(Succeed()) - attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -383,7 +383,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should prevent the object from being created because no extension type is registered", func() { - attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Create, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(shoot, nil, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil) err := admissionHandler.Validate(context.TODO(), attrs, nil) @@ -391,7 +391,7 @@ var _ = Describe("ExtensionValidator", func() { }) It("should do nothing because the spec has not changed", func() { - attrs := admission.NewAttributesRecord(shoot, shoot, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Update, &metav1.DeleteOptions{}, false, nil) + attrs := admission.NewAttributesRecord(shoot, shoot, core.Kind("Shoot").WithVersion("version"), shoot.Namespace, shoot.Name, core.Resource("shoots").WithVersion("version"), "", admission.Update, &metav1.UpdateOptions{}, false, nil) Expect(admissionHandler.Validate(context.TODO(), attrs, nil)).To(Succeed()) }) }) diff --git a/plugin/pkg/shoot/validator/admission.go b/plugin/pkg/shoot/validator/admission.go index d71353e3de2..95e2c23b348 100644 --- a/plugin/pkg/shoot/validator/admission.go +++ b/plugin/pkg/shoot/validator/admission.go @@ -25,7 +25,6 @@ import ( "github.com/gardener/gardener/pkg/apis/core" "github.com/gardener/gardener/pkg/apis/core/helper" - gardencorehelper "github.com/gardener/gardener/pkg/apis/core/helper" v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" admissioninitializer "github.com/gardener/gardener/pkg/apiserver/admission/initializer" coreinformers "github.com/gardener/gardener/pkg/client/core/informers/internalversion" @@ -575,7 +574,7 @@ func (c *validationContext) validateProvider(a admission.Attributes) field.Error } if a.GetOperation() == admission.Create && utilfeature.DefaultFeatureGate.Enabled(features.SecretBindingProviderValidation) { - if !gardencorehelper.SecretBindingHasType(c.secretBinding, c.shoot.Spec.Provider.Type) { + if !helper.SecretBindingHasType(c.secretBinding, c.shoot.Spec.Provider.Type) { var secretBindingProviderType string if c.secretBinding.Provider != nil { secretBindingProviderType = c.secretBinding.Provider.Type