1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 01:16:55 +00:00
kyverno/pkg/clients/dclient/helpers.go

124 lines
3.5 KiB
Go
Raw Normal View History

package dclient
import (
"context"
"github.com/kyverno/kyverno/ext/wildcard"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type Resource struct {
Group string
Version string
Resource string
SubResource string
Unstructured unstructured.Unstructured
}
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 - https://github.com/kubernetes/client-go/commit/46c1ad3baa101ce5886a69f7c6a47d7777db3e56 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 14:17:08 +03:00
func GetResources(ctx context.Context, c Interface, group, version, kind, subresource, namespace, name string, lselector *metav1.LabelSelector) ([]Resource, error) {
var resources []Resource
gvrss, err := c.Discovery().FindResources(group, version, kind, subresource)
if err != nil {
return nil, err
}
for gvrs := range gvrss {
dyn := c.GetDynamicInterface().Resource(gvrs.GroupVersionResource())
var sub []string
if gvrs.SubResource != "" {
sub = []string{gvrs.SubResource}
}
// we can use `GET` directly
if namespace != "" && name != "" && !wildcard.ContainsWildcard(namespace) && !wildcard.ContainsWildcard(name) {
var obj *unstructured.Unstructured
var err error
obj, err = dyn.Namespace(namespace).Get(ctx, name, metav1.GetOptions{}, sub...)
if err != nil {
return nil, err
}
resources = append(resources, Resource{
Group: gvrs.Group,
Version: gvrs.Version,
Resource: gvrs.Resource,
SubResource: gvrs.SubResource,
Unstructured: *obj,
})
} else {
// we can use `LIST`
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 - https://github.com/kubernetes/client-go/commit/46c1ad3baa101ce5886a69f7c6a47d7777db3e56 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 14:17:08 +03:00
var labelSelector string
if lselector != nil {
selector, err := metav1.LabelSelectorAsSelector(lselector)
if err != nil {
return nil, err
}
labelSelector = selector.String()
}
if gvrs.SubResource == "" {
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 - https://github.com/kubernetes/client-go/commit/46c1ad3baa101ce5886a69f7c6a47d7777db3e56 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 14:17:08 +03:00
list, err := dyn.List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, err
}
for _, obj := range list.Items {
if match(namespace, name, obj.GetNamespace(), obj.GetName()) {
resources = append(resources, Resource{
Group: gvrs.Group,
Version: gvrs.Version,
Resource: gvrs.Resource,
SubResource: gvrs.SubResource,
Unstructured: obj,
})
}
}
} else {
// we need to use `LIST` / `GET`
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 - https://github.com/kubernetes/client-go/commit/46c1ad3baa101ce5886a69f7c6a47d7777db3e56 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 14:17:08 +03:00
list, err := dyn.List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, err
}
var parentObjects []unstructured.Unstructured
for _, obj := range list.Items {
if match(namespace, name, obj.GetNamespace(), obj.GetName()) {
parentObjects = append(parentObjects, obj)
}
}
for _, parentObject := range parentObjects {
var obj *unstructured.Unstructured
var err error
if parentObject.GetNamespace() == "" {
obj, err = dyn.Get(ctx, name, metav1.GetOptions{}, sub...)
} else {
obj, err = dyn.Namespace(parentObject.GetNamespace()).Get(ctx, name, metav1.GetOptions{}, sub...)
}
if err != nil {
return nil, err
}
resources = append(resources, Resource{
Group: gvrs.Group,
Version: gvrs.Version,
Resource: gvrs.Resource,
SubResource: gvrs.SubResource,
Unstructured: *obj,
})
}
}
}
}
return resources, nil
}
func match(namespacePattern, namePattern, namespace, name string) bool {
if namespacePattern == "" && namePattern == "" {
return true
} else if namespacePattern == "" {
if wildcard.Match(namePattern, name) {
return true
}
} else if wildcard.Match(namespacePattern, namespace) {
if namePattern == "" || wildcard.Match(namePattern, name) {
return true
}
}
return false
}