1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/client/applyconfigurations/utils.go
Ammar Yasser 9a8e35d787
Selector with mutate target (#11208)
* feature: Add LabelSelector as a field of resource spec to allow fetching by labels

Signed-off-by: aerosouund <aerosound161@gmail.com>

* chore: Generate CRDs

Signed-off-by: aerosouund <aerosound161@gmail.com>

* feat: Add the capability to fetch with label selector

- Add the label selector as a parameter to GetResources of the engine api client and the dclient.
- Use the label selector with list options in the dclient.
- convert a metav1.LabelSelector to a labels.Selector before fetching to be able to convert it to a string to be used with ListOptions.

Signed-off-by: aerosouund <aerosound161@gmail.com>

* feat: Pass label selector to the GetResources method

Signed-off-by: aerosouund <aerosound161@gmail.com>

* feat: Return the resource selector when resolving spec

Signed-off-by: aerosouund <aerosound161@gmail.com>

* fix: Instantiate the fake client schema using the passed gvrToListKind map and by inferring schema from passed resources

All tests that use List will fail because the fake client doesn't infer the schema from the passed resources.
gvrToListKind can't be fully deprecated as some parts of kyverno use the fake client without passing resources to it (resource generation). And so both approaches have to be supported.

References:
- https://github.com/kubernetes/client-go/issues/983
- 46c1ad3baa

Signed-off-by: aerosouund <aerosound161@gmail.com>

* test: Add labelSelector unit test to mutate existing test.

- Remove the unwanted call to GetResource.
- Pass an empty map of GVR to string to the fake client constructor.

Signed-off-by: aerosouund <aerosound161@gmail.com>

* test: Add chainsaw test

Signed-off-by: aerosouund <aerosound161@gmail.com>

* chore: Run codegen

Signed-off-by: aerosouund <aerosound161@gmail.com>

* chore: Generate helm CRDs

Signed-off-by: aerosouund <aerosound161@gmail.com>

* refactor: Put the LabelSelector in a separate struct

Many types use the ResourceSpec struct and not all of them support label selectors.
This removes the field into a separate schema dedicated to target selection called TargetSelector.
It has the ResourceSpec and the selector.

Signed-off-by: aerosouund <aerosound161@gmail.com>

* chore: Run codegen after modifying selector comment

Signed-off-by: aerosouund <aerosound161@gmail.com>

* chore: Run codegen

Signed-off-by: aerosouund <aerosound161@gmail.com>

---------

Signed-off-by: aerosouund <aerosound161@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
2024-10-16 11:17:08 +00:00

277 lines
15 KiB
Go

/*
Copyright The Kubernetes Authors.
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.
*/
// Code generated by applyconfiguration-gen. DO NOT EDIT.
package applyconfigurations
import (
v1 "github.com/kyverno/kyverno/api/kyverno/v1"
v1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
v2 "github.com/kyverno/kyverno/api/kyverno/v2"
v2alpha1 "github.com/kyverno/kyverno/api/kyverno/v2alpha1"
v2beta1 "github.com/kyverno/kyverno/api/kyverno/v2beta1"
v1alpha2 "github.com/kyverno/kyverno/api/policyreport/v1alpha2"
reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
kyvernov1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v1"
kyvernov1beta1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v1beta1"
kyvernov2 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v2"
kyvernov2alpha1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v2alpha1"
kyvernov2beta1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/kyverno/v2beta1"
policyreportv1alpha2 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/policyreport/v1alpha2"
applyconfigurationsreportsv1 "github.com/kyverno/kyverno/pkg/client/applyconfigurations/reports/v1"
schema "k8s.io/apimachinery/pkg/runtime/schema"
)
// ForKind returns an apply configuration type for the given GroupVersionKind, or nil if no
// apply configuration type exists for the given GroupVersionKind.
func ForKind(kind schema.GroupVersionKind) interface{} {
switch kind {
// Group=kyverno.io, Version=v1
case v1.SchemeGroupVersion.WithKind("AnyAllConditions"):
return &kyvernov1.AnyAllConditionsApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("APICall"):
return &kyvernov1.APICallApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Attestation"):
return &kyvernov1.AttestationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Attestor"):
return &kyvernov1.AttestorApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("AttestorSet"):
return &kyvernov1.AttestorSetApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("AutogenStatus"):
return &kyvernov1.AutogenStatusApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("CEL"):
return &kyvernov1.CELApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("CertificateAttestor"):
return &kyvernov1.CertificateAttestorApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("CloneFrom"):
return &kyvernov1.CloneFromApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("CloneList"):
return &kyvernov1.CloneListApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ClusterPolicy"):
return &kyvernov1.ClusterPolicyApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Condition"):
return &kyvernov1.ConditionApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ConfigMapReference"):
return &kyvernov1.ConfigMapReferenceApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ContextAPICall"):
return &kyvernov1.ContextAPICallApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ContextEntry"):
return &kyvernov1.ContextEntryApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("CTLog"):
return &kyvernov1.CTLogApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Deny"):
return &kyvernov1.DenyApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("DryRunOption"):
return &kyvernov1.DryRunOptionApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ForEachGeneration"):
return &kyvernov1.ForEachGenerationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ForEachMutation"):
return &kyvernov1.ForEachMutationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ForEachValidation"):
return &kyvernov1.ForEachValidationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("GeneratePattern"):
return &kyvernov1.GeneratePatternApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Generation"):
return &kyvernov1.GenerationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("GlobalContextEntryReference"):
return &kyvernov1.GlobalContextEntryReferenceApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("HTTPHeader"):
return &kyvernov1.HTTPHeaderApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ImageExtractorConfig"):
return &kyvernov1.ImageExtractorConfigApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ImageRegistry"):
return &kyvernov1.ImageRegistryApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ImageRegistryCredentials"):
return &kyvernov1.ImageRegistryCredentialsApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ImageVerification"):
return &kyvernov1.ImageVerificationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("KeylessAttestor"):
return &kyvernov1.KeylessAttestorApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Manifests"):
return &kyvernov1.ManifestsApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("MatchResources"):
return &kyvernov1.MatchResourcesApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Mutation"):
return &kyvernov1.MutationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ObjectFieldBinding"):
return &kyvernov1.ObjectFieldBindingApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("PodSecurity"):
return &kyvernov1.PodSecurityApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("PodSecurityStandard"):
return &kyvernov1.PodSecurityStandardApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Policy"):
return &kyvernov1.PolicyApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("PolicyStatus"):
return &kyvernov1.PolicyStatusApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Rekor"):
return &kyvernov1.RekorApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("RequestData"):
return &kyvernov1.RequestDataApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ResourceDescription"):
return &kyvernov1.ResourceDescriptionApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ResourceFilter"):
return &kyvernov1.ResourceFilterApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ResourceSpec"):
return &kyvernov1.ResourceSpecApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Rule"):
return &kyvernov1.RuleApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("RuleCountStatus"):
return &kyvernov1.RuleCountStatusApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("SecretReference"):
return &kyvernov1.SecretReferenceApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ServiceCall"):
return &kyvernov1.ServiceCallApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Spec"):
return &kyvernov1.SpecApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("StaticKeyAttestor"):
return &kyvernov1.StaticKeyAttestorApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("TargetResourceSpec"):
return &kyvernov1.TargetResourceSpecApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("TargetSelector"):
return &kyvernov1.TargetSelectorApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("UserInfo"):
return &kyvernov1.UserInfoApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ValidateImageVerification"):
return &kyvernov1.ValidateImageVerificationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ValidatingAdmissionPolicyStatus"):
return &kyvernov1.ValidatingAdmissionPolicyStatusApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Validation"):
return &kyvernov1.ValidationApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("ValidationFailureActionOverride"):
return &kyvernov1.ValidationFailureActionOverrideApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("Variable"):
return &kyvernov1.VariableApplyConfiguration{}
case v1.SchemeGroupVersion.WithKind("WebhookConfiguration"):
return &kyvernov1.WebhookConfigurationApplyConfiguration{}
// Group=kyverno.io, Version=v1beta1
case v1beta1.SchemeGroupVersion.WithKind("AdmissionRequestInfoObject"):
return &kyvernov1beta1.AdmissionRequestInfoObjectApplyConfiguration{}
case v1beta1.SchemeGroupVersion.WithKind("RequestInfo"):
return &kyvernov1beta1.RequestInfoApplyConfiguration{}
case v1beta1.SchemeGroupVersion.WithKind("UpdateRequest"):
return &kyvernov1beta1.UpdateRequestApplyConfiguration{}
case v1beta1.SchemeGroupVersion.WithKind("UpdateRequestSpec"):
return &kyvernov1beta1.UpdateRequestSpecApplyConfiguration{}
case v1beta1.SchemeGroupVersion.WithKind("UpdateRequestSpecContext"):
return &kyvernov1beta1.UpdateRequestSpecContextApplyConfiguration{}
case v1beta1.SchemeGroupVersion.WithKind("UpdateRequestStatus"):
return &kyvernov1beta1.UpdateRequestStatusApplyConfiguration{}
// Group=kyverno.io, Version=v2
case v2.SchemeGroupVersion.WithKind("AdmissionRequestInfoObject"):
return &kyvernov2.AdmissionRequestInfoObjectApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("AnyAllConditions"):
return &kyvernov2.AnyAllConditionsApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("CleanupPolicy"):
return &kyvernov2.CleanupPolicyApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("CleanupPolicySpec"):
return &kyvernov2.CleanupPolicySpecApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("CleanupPolicyStatus"):
return &kyvernov2.CleanupPolicyStatusApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("ClusterCleanupPolicy"):
return &kyvernov2.ClusterCleanupPolicyApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("Condition"):
return &kyvernov2.ConditionApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("Exception"):
return &kyvernov2.ExceptionApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("PolicyException"):
return &kyvernov2.PolicyExceptionApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("PolicyExceptionSpec"):
return &kyvernov2.PolicyExceptionSpecApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("RequestInfo"):
return &kyvernov2.RequestInfoApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("RuleContext"):
return &kyvernov2.RuleContextApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("UpdateRequest"):
return &kyvernov2.UpdateRequestApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("UpdateRequestSpec"):
return &kyvernov2.UpdateRequestSpecApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("UpdateRequestSpecContext"):
return &kyvernov2.UpdateRequestSpecContextApplyConfiguration{}
case v2.SchemeGroupVersion.WithKind("UpdateRequestStatus"):
return &kyvernov2.UpdateRequestStatusApplyConfiguration{}
// Group=kyverno.io, Version=v2alpha1
case v2alpha1.SchemeGroupVersion.WithKind("ExternalAPICall"):
return &kyvernov2alpha1.ExternalAPICallApplyConfiguration{}
case v2alpha1.SchemeGroupVersion.WithKind("GlobalContextEntry"):
return &kyvernov2alpha1.GlobalContextEntryApplyConfiguration{}
case v2alpha1.SchemeGroupVersion.WithKind("GlobalContextEntrySpec"):
return &kyvernov2alpha1.GlobalContextEntrySpecApplyConfiguration{}
case v2alpha1.SchemeGroupVersion.WithKind("GlobalContextEntryStatus"):
return &kyvernov2alpha1.GlobalContextEntryStatusApplyConfiguration{}
case v2alpha1.SchemeGroupVersion.WithKind("KubernetesResource"):
return &kyvernov2alpha1.KubernetesResourceApplyConfiguration{}
// Group=kyverno.io, Version=v2beta1
case v2beta1.SchemeGroupVersion.WithKind("AnyAllConditions"):
return &kyvernov2beta1.AnyAllConditionsApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("CleanupPolicy"):
return &kyvernov2beta1.CleanupPolicyApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("CleanupPolicySpec"):
return &kyvernov2beta1.CleanupPolicySpecApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("CleanupPolicyStatus"):
return &kyvernov2beta1.CleanupPolicyStatusApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("ClusterCleanupPolicy"):
return &kyvernov2beta1.ClusterCleanupPolicyApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("ClusterPolicy"):
return &kyvernov2beta1.ClusterPolicyApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Condition"):
return &kyvernov2beta1.ConditionApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Deny"):
return &kyvernov2beta1.DenyApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Exception"):
return &kyvernov2beta1.ExceptionApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("ImageVerification"):
return &kyvernov2beta1.ImageVerificationApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("MatchResources"):
return &kyvernov2beta1.MatchResourcesApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Policy"):
return &kyvernov2beta1.PolicyApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("PolicyException"):
return &kyvernov2beta1.PolicyExceptionApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("PolicyExceptionSpec"):
return &kyvernov2beta1.PolicyExceptionSpecApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Rule"):
return &kyvernov2beta1.RuleApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Spec"):
return &kyvernov2beta1.SpecApplyConfiguration{}
case v2beta1.SchemeGroupVersion.WithKind("Validation"):
return &kyvernov2beta1.ValidationApplyConfiguration{}
// Group=reports.kyverno.io, Version=v1
case reportsv1.SchemeGroupVersion.WithKind("ClusterEphemeralReport"):
return &applyconfigurationsreportsv1.ClusterEphemeralReportApplyConfiguration{}
case reportsv1.SchemeGroupVersion.WithKind("EphemeralReport"):
return &applyconfigurationsreportsv1.EphemeralReportApplyConfiguration{}
case reportsv1.SchemeGroupVersion.WithKind("EphemeralReportSpec"):
return &applyconfigurationsreportsv1.EphemeralReportSpecApplyConfiguration{}
// Group=wgpolicyk8s.io, Version=v1alpha2
case v1alpha2.SchemeGroupVersion.WithKind("ClusterPolicyReport"):
return &policyreportv1alpha2.ClusterPolicyReportApplyConfiguration{}
case v1alpha2.SchemeGroupVersion.WithKind("PolicyReport"):
return &policyreportv1alpha2.PolicyReportApplyConfiguration{}
case v1alpha2.SchemeGroupVersion.WithKind("PolicyReportResult"):
return &policyreportv1alpha2.PolicyReportResultApplyConfiguration{}
case v1alpha2.SchemeGroupVersion.WithKind("PolicyReportSummary"):
return &policyreportv1alpha2.PolicyReportSummaryApplyConfiguration{}
}
return nil
}