Giter Club home page Giter Club logo

kube-stale-resources's Introduction

kube-stale-resources

Build Status

This is a utility to detect stale resources in Kubernetes clusters between resources from YAML manifests supplied via local file or stdin (target state) and a Kubernetes cluster (live state). All resources that exist in the live state but not in the target state are considered stale as they deviate from the intended state of the Kubernetes cluster (closed world assumption). It is intended as a complement to kubectl diff.

Using a blacklist you can ignore resources from the live state from the comparison so they are not considered stale even though they do not exist in the given target state. This is useful when those resources are created by Kubernetes itself (e.g. the kubernetes service in the default namespace), managed by the Kubernetes cluster provider or another tool outside the scope.

A use case for kube-stale-resources is using it alongside kubectl diff on locally present YAML manifests so kubectl diff can detect newly created Kubernetes resources and changes to those resources and kube-stale-resources can alert the user on stale resources that should be deleted from the cluster.

Limitations:

  • currently only works on namespaced resources
  • currently requires explicit metadata.namespace field even for resources in default namespace
  • requires unauthenticated HTTP(S) access to Kubernetes apiserver, e.g. via kubectl proxy
  • only accounts for apiVersion deprecations up until Kubernetes 1.16

Usage

You need Python 3.8 or higher.

Assuming you have a properly setup .kube/config (e.g. using minikube after a successful minikube start) running:

kubectl proxy &

# ignore minikube resources
cat <<EOF > blacklist.txt
^kubernetes-dashboard:.*$
^default:events.k8s.io/v1beta1:Event:minikube..*$
EOF

# orderly created resource using YAML manifest in e.g. git
cat <<EOF > version-controlled-resources.yml
---
apiVersion: v1
kind: Service
metadata:
  name: foo
  namespace: default
spec:
  type: ClusterIP
  ports:
    - port: 80
      name: http
      targetPort: 80
  selector:
    app: foo
EOF
kubectl apply -f version-controlled-resources.yml

# on-the-fly created resource using imperative command
kubectl create service clusterip bar --tcp='8080:8080'

cat version-controlled-resources.yml | python kube-stale-resources.py -f - --blacklist blacklist.txt

This should yield a similar output to as we ignored what minikube sets up by default (e.g. the whole kubernetes-dashboard namespace):

Reading blacklist file blacklist.txt...
Retrieving target state...
Retrieving live state from http://localhost:8001...
Live dynamic configmaps that are not in target (stale):
.. 0 entries

Live resources w/o dynamic configmaps that are not in target (stale):
  default:v1:Service:bar
.. 1 entries

Run python kube-stale-resources.py -h for full list of options.

Blacklisting

Example blacklist file for a cluster on GKE that also uses cert-manager:

^.*:v1:ResourceQuota:gke-resource-quotas$
^default:v1:LimitRange:limits$

^.*:certmanager.k8s.io/v1alpha1:Order:.*$

In general a blacklist file contains one regular expression per line that are matched against a string of format <namespace-name>:<apiVersion>:<kind>:<resource-name> for each resource.

License

kube-stale-resources is licensed under the Apache License, Version 2.0. See LICENSE for more information

kube-stale-resources's People

Contributors

cmur2 avatar dependabot-preview[bot] avatar dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar

Watchers

 avatar  avatar

kube-stale-resources's Issues

Dependency Dashboard

View repository job log here.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-python v5
  • actions/checkout v4
  • actions/setup-python v5
  • engineerd/setup-kind v0.5.0
  • actions/checkout v4
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • ubuntu 22.04
  • ubuntu 22.04
  • ubuntu 22.04
  • ubuntu 22.04
pep621
pyproject.toml
  • poetry >=1.0
poetry
pyproject.toml
  • python ^3.8
  • pyyaml 6.0.1
  • requests ~2.29.0
  • mypy 1.11.0
  • pylint ~3.2.0
  • rope ~1.13.0
  • types-PyYAML ~6.0
  • types-requests ~2.28
  • toml ~0.10
  • yapf ~0.40.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.