zegl / kube-score

Kubernetes object analysis with recommendations for improved reliability and security. kube-score actively prevents downtime and bugs in your Kubernetes YAML and Charts. Static code analysis for Kubernetes.
https://kube-score.com
MIT License
2.74k stars 176 forks source link

False-positive Service error when defining a Deployment #377

Closed thannaske closed 3 years ago

thannaske commented 3 years ago

Which version of kube-score are you using? 1.11.0

What did you do? When defining a Service alongside a Deployment kube-score throws the following error:

[CRITICAL] logo-cache v1/Service: The services selector does not match any pods

I'm using the following definitions:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: logo-cache
  name: logo-cache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: logo-cache
  template:
    metadata:
      labels:
        app: logo-cache
    spec:
      containers:
      - image: nginx:stable-alpine
        name: nginx
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: logo-cache
  name: logo-cache
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: logo-cache
  type: ClusterIP

kube-score is executed as following:

find . | grep ".yaml" | xargs -i kube-score score --kubernetes-version v1.19 --output-format ci {}

The YAML files are stored in sub-directories (to be used with ArgoCD).

What did you expect to see? As you can see the selector is a simple app=logo-cache which the Deployment properly applies to all replicas created by the subsequent ReplicaSet. So the Service does indeed match the corresponding Pods.

What did you see instead?

[CRITICAL] logo-cache v1/Service: The services selector does not match any pods
zegl commented 3 years ago

When using "xargs -i" kube-score is executed once per file, instead of once with all files as inputs. Drop "-i" and "{}" and it should work out of the box.

The full command would be:

find . | grep ".yaml" | xargs kube-score score --kubernetes-version v1.19 --output-format ci