DEV Community

Gianluca
Gianluca

Posted on

Declutter Your Kubernetes Cluster: Automate Resource Removal and Updates with Cleaner

In the realm of cloud-native applications, Kubernetes has emerged as the de facto platform for orchestrating containers and managing distributed systems. However, as Kubernetes environments grow in size and complexity, it becomes increasingly challenging to maintain a clean and efficient cluster. This is where the Kubernetes controller Cleaner comes into play.

Introducing Cleaner: A Powerful Tool for Resource Management

Cleaner is a Kubernetes controller that proactively identifies, removes, or updates stale resources to optimize resource utilization and maintain a clean and organized cluster. It offers a wide range of features that empower developers to effectively manage their Kubernetes environments:

  • Flexible Scheduling: Cleaner allows you to specify the frequency at which it scans the cluster and identifies stale resources using the Cron syntax, a widely adopted scheduling language. This ensures that resource cleanup tasks are performed at regular intervals, keeping your cluster clean and efficient.

  • Label-based Selection: Cleaner enables you to select resources based on their labels, allowing for precise resource management. You can filter resources based on label keys, operations (equal, different, etc.), and values, ensuring that only the intended resources are targeted for removal or update.

  • Lua-based Selection Criteria: Cleaner goes beyond label filtering with the introduction of Lua scripts. Lua functions, named evaluate, receive the resource object as obj and allow for complex and dynamic filtering rules. This flexibility empowers you to define custom selection criteria tailored to specific resource management needs.

  • Resource Removal and Updates: Cleaner's core functionality lies in its ability to remove or update stale resources. You can choose to delete outdated resources that are no longer required or update existing resources to align with the latest configurations. This ensures that your cluster remains consistent and adheres to current deployment standards.

  • DryRun Mode: Cleaner provides a DryRun flag that allows you to preview which resources match its filtering criteria without actually deleting or updating them. This is a valuable feature for safely testing the Cleaner's logic before committing to resource changes.

apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
  name: cleaner-sample1
spec:
  schedule: "* 0 * * *" # Executes every day at midnight
  resourcePolicySet:
    resourceSelectors:
    - namespace: test
      kind: Deployment
      group: "apps"
      version: v1
      labelFilters:
      - key: serving
        operation: Equal
        value: api # Identifies Deployments with "serving" label set to "api"
      - key: environment
        operation: Different
        value: prouction # Identifies Deployments with "environment" label different from "production"
    action: Delete # Deletes matching Deployments
Enter fullscreen mode Exit fullscreen mode
apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
  name: cleaner-failed-pods-lua
spec:
  schedule: "* 0 * * *"
  resourcePolicySet:
    resourceSelectors:
    - namespace: all
      kind: Pod
      group: ""
      version: v1
    evaluate: |
      function evaluate()
        hs = {}
        hs.matching = false
        if obj.status.phase == "Failed" then
          hs.matching = true
        end
        return hs
      end
  action: Delete
Enter fullscreen mode Exit fullscreen mode

For more complex cases, refer to README

Conclusion

The Kubernetes controller Cleaner represents a valuable tool for developers seeking to maintain a clean, optimized, and efficient Kubernetes environment. Its flexible scheduling, powerful filtering capabilities, and the ability to automate resource removal and updates make it an indispensable tool for managing resource utilization and ensuring consistent application deployment. By adopting Cleaner, developers can focus on building and deploying applications while Cleaner takes care of maintaining a healthy and optimized Kubernetes cluster.

Top comments (5)

Collapse
 
bcouetil profile image
Benoit COUETIL 💫

Oh great, thank you for sharing 🙏

I suppose I could use this for completed pods also, I often have lots of past jobs cluttering my K9S experience 😅

Collapse
 
gianlucam76 profile image
Gianluca • Edited

This will do

apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
  name: completed-pods
spec:
  schedule: "8 * * * *"
  dryRun: false
  resourcePolicySet:
    resourceSelectors:
    - kind: Pod
      group: ""
      version: v1
      evaluate: |
        function evaluate()
          hs = {}
          hs.matching = false
          if obj.status.conditions ~= nil then
            for _, condition in ipairs(obj.status.conditions) do
              if condition.reason == "PodCompleted" and condition.status == "True" then
                hs.matching = true
              end
            end
          end
          return hs
        end
    action: Delete
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bcouetil profile image
Benoit COUETIL 💫 • Edited

Thanks man, you rock 🤗

Collapse
 
gianlucam76 profile image
Gianluca

Yes. It will cover that case as well.

Collapse
 
gianlucam76 profile image
Gianluca

Since this seems to be useful to many, I added an example section in the repo

I encourage everyone to contribute to the example directory by adding your own Cleaner configurations. This will help the community benefit from your expertise and build a stronger knowledge base of Cleaner use cases.