yonahd / kor

A Golang Tool to discover unused Kubernetes Resources
MIT License
1.04k stars 96 forks source link

Feature: Add include-labels filter flag #187

Closed philippe-granet closed 9 months ago

philippe-granet commented 10 months ago

Is your feature request related to a problem? Please describe. I want to only clean secrets that have certains labels

Describe the solution you'd like Selector to filter label, Example: --include-labels key1=value1,key2=value2.

yonahd commented 10 months ago

This should be fairly simple to add as we already support the exclude labels. @philippe-granet would you like to attempt this?

philippe-granet commented 10 months ago

Sorry, I've never coded in Go and I don't really have time available at the moment

Forget-C commented 10 months ago

@yonahd I want to add a filtering interface to implement unified resource filtering logic. currently, if you want to add a filtering method, it needs to be processed in each resource implementation. I want to change to this mode:

func ProcessNamespaceDeployments(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, error) {
    deploymentsList, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        return nil, err
    }

    var deploymentsWithoutReplicas []string

    for _, deployment := range deploymentsList.Items {

        filterFunc := func(obj interface) bool {
            deployment := obj.(*appsv1.Deployment)
            if *deployment.Spec.Replicas == 0 {
                return true
            }
            return false
        }
        // Run will run the registered filter method
        if pass := filter.AddFilter(filterFunc).Run();pass{
            deploymentsWithoutReplicas = append(deploymentsWithoutReplicas, deployment.Name)
        }
    }

    return deploymentsWithoutReplicas, nil
}
func someInit() {
    filter := NewFilter()
    filter = filter.AddFilter(func(obj interface{}) bool {
        return !HasExcludedLabel(deployment.Labels, filterOpts.ExcludeLabels)
    })
    filter = filter.AddFilter(func(obj interface{}) bool {
        return HasIncludedAge(deployment.CreationTimestamp, filterOpts)
    })
    filter = filter.AddFilter(func(obj interface{}) bool {
        metadata, _, _ := objectmeta.GetObjectMeta(obj)
        if metadata.Labels["kor/used"] == "true" {
            return true
        }
    })
}

The filter object implements unified filtering logic, and the different filtering methods call filter.AddFilter(filterFunc).Run().

yonahd commented 10 months ago

@yonahd I want to add a filtering interface to implement unified resource filtering logic. currently, if you want to add a filtering method, it needs to be processed in each resource implementation. I want to change to this mode:

func ProcessNamespaceDeployments(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, error) {
  deploymentsList, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
  if err != nil {
      return nil, err
  }

  var deploymentsWithoutReplicas []string

  for _, deployment := range deploymentsList.Items {

        filterFunc := func(obj interface) bool {
            deployment := obj.(*appsv1.Deployment)
            if *deployment.Spec.Replicas == 0 {
              return true
          }
            return false
        }
        // Run will run the registered filter method
      if pass := filter.AddFilter(filterFunc).Run();pass{
            deploymentsWithoutReplicas = append(deploymentsWithoutReplicas, deployment.Name)
        }
  }

  return deploymentsWithoutReplicas, nil
}
func someInit() {
    filter := NewFilter()
    filter = filter.AddFilter(func(obj interface{}) bool {
        return !HasExcludedLabel(deployment.Labels, filterOpts.ExcludeLabels)
    })
    filter = filter.AddFilter(func(obj interface{}) bool {
        return HasIncludedAge(deployment.CreationTimestamp, filterOpts)
    })
    filter = filter.AddFilter(func(obj interface{}) bool {
        metadata, _, _ := objectmeta.GetObjectMeta(obj)
        if metadata.Labels["kor/used"] == "true" {
          return true
      }
    })
}

The filter object implements unified filtering logic, and the different filtering methods call filter.AddFilter(filterFunc).Run().

Hey @Forget-C You are welcome to make an attempt at this. It would be a great enhancement

Forget-C commented 10 months ago

@yonahd I want to add a filtering interface to implement unified resource filtering logic. currently, if you want to add a filtering method, it needs to be processed in each resource implementation. I want to change to this mode:

func ProcessNamespaceDeployments(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, error) {
    deploymentsList, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
    if err != nil {
        return nil, err
    }

    var deploymentsWithoutReplicas []string

    for _, deployment := range deploymentsList.Items {

        filterFunc := func(obj interface) bool {
            deployment := obj.(*appsv1.Deployment)
            if *deployment.Spec.Replicas == 0 {
                return true
            }
            return false
        }
        // Run will run the registered filter method
        if pass := filter.AddFilter(filterFunc).Run();pass{
            deploymentsWithoutReplicas = append(deploymentsWithoutReplicas, deployment.Name)
        }
    }

    return deploymentsWithoutReplicas, nil
}
func someInit() {
    filter := NewFilter()
    filter = filter.AddFilter(func(obj interface{}) bool {
        return !HasExcludedLabel(deployment.Labels, filterOpts.ExcludeLabels)
    })
    filter = filter.AddFilter(func(obj interface{}) bool {
        return HasIncludedAge(deployment.CreationTimestamp, filterOpts)
    })
    filter = filter.AddFilter(func(obj interface{}) bool {
        metadata, _, _ := objectmeta.GetObjectMeta(obj)
        if metadata.Labels["kor/used"] == "true" {
            return true
        }
    })
}

The filter object implements unified filtering logic, and the different filtering methods call filter.AddFilter(filterFunc).Run().

Hey @Forget-C You are welcome to make an attempt at this. It would be a great enhancement

please assigne to me @yonahd