Skip to content

Removes unnecessary Kubernetes resources based on a configurable pod restart threshold.

License

Notifications You must be signed in to change notification settings

att-cloudnative-labs/kubesweeper

Repository files navigation

Kubesweeper

Automatically iterates through resources in a lab Kubernetes cluster and acts according to certain conditions outlined here. As of now, Kubesweeper will delete Deployments and their associated resources if the waiting reason and/or pod restart count and/or deployment age dictates. Additionally, you can use configurable Boolean environment variables to choose to delete associated Services, Ingresses, and HorizontalPodAutoscalers.

If your lab Kubernetes clusters are filling up with non-Running pods, then Kubesweeper's automatic deletion can assist. Future iterations of this project can involve other actions based on crawling through Kubernetes cluster resources, such as generating reports per namespace without actually deleting.

Please note that Kubesweeper is intended for use in lab—not production, customer-facing—clusters. Any automated cleanup in such an environment is unadvisable, at least for Kubesweeper.

Deployment as a Kubernetes CronJob

If the desired cluster does not have Knative installed, then Kubesweeper can be installed as a Kubernetes CronJob.

  1. Build Docker image
$ docker build -t kubesweeper .
  1. Create Kubernetes resources from install directory
$ kubectl apply -f install/

Note that step 2 must be run in the context of the Kubernetes cluster. After that command is run, the appropriate Kubernetes resources will be created from the .yaml files in install.

Deployment as a Knative CronJobSource

If you wish to deploy Kubesweeper on Knative as a CronJobSource, you can use Helm. For information on installing Helm, please refer to the Helm quickstart guide. After installing Helm, the following steps can be manually run:

  1. Build Docker image
$ docker build -t kubesweeper .
  1. Run Helm template to install Kubesweeper
$ helm template kubesweeper --set image=<KUBESWEEPER_IMAGE> | kubectl create -f -

In lieu of step 2, a Makefile can be used to pull values from ~/install/helm/kubesweeper/values.yaml:

$ make

Configuration Defaults

Under the configs folder, the config.yaml has the following default configurations:

  • Pod waiting reasons
    • CrashLoopBackOff
    • ImagePullBackOff
    • ErrImagePull
    • Completed
    • Failed
  • Pod restart threshold (in other words, if pod restarts exceed this number, then delete)
    • 100
      • If the pod restart threshold is at least this number and has a pod waiting reason of CrashLoopBackOff, then Kubesweeper will delete the associated resources
  • Deployment age threshold (in other words, if the deployment creation date is before this number of days, then delete)
    • 90

You are able to configure these values to your choosing.

Helm function configurations can be found in ~/install/helm/kubesweeper/values.yaml.

  • name
    • Name to use for deployment
  • image
    • Image used in deployment
  • cron
    • Cron expression used to schedule Kubesweeper
      • Any valid cron expression can be used
  • namespace
    • Namespace job will be deployed in

Contributing

  1. Fork Kubesweeper
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Additional info

Maintained and in-use by the Platform Team @ AT&T Entertainment Cloud Native Labs.

Distributed under the AT&T MIT license. See LICENSE for more information.