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.78k stars 178 forks source link

Segfault for `apiVersion: networking.k8s.io/v1` #340

Closed AndiDog closed 3 years ago

AndiDog commented 3 years ago

Which version of kube-score are you using?

kube-score version: 1.10.0, commit: 95faa2ad082e05d8b7637d0bd5e1ae00d1550260, built: 2020-11-07T14:17:50Z

What did you do?

Minimal example:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whatever
spec:
  rules:
    - host: abc.example.test
      http:
        paths:
          - path: /
            backend:
              serviceName: abc
              servicePort: def

What did you expect to see?

Scoring.

What did you see instead?

$ kube-score score manifest.yaml
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x19d9d9c]

goroutine 1 [running]:
github.com/zegl/kube-score/score/ingress.ingressTargetsServiceCommon(0x1cbede0, 0xc00012d040, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/gustav/src/kube-score/score/ingress/ingress.go:49 +0x19c
github.com/zegl/kube-score/score/ingress.ingressTargetsService.func1(0x1cbede0, 0xc00012d040, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/gustav/src/kube-score/score/ingress/ingress.go:17 +0x8c
github.com/zegl/kube-score/score.Score(0x1cc87e0, 0xc0001a50e0, 0xc0001755a0, 0x1, 0x1, 0x0, 0x0, 0xc0001a7f80, 0xc0001a7fb0, 0x1, ...)
    /Users/gustav/src/kube-score/score/score.go:57 +0x1b9
main.scoreFiles(0x7ffeefbff158, 0xa, 0xc0000981a0, 0x1, 0x1, 0x5, 0xc0000d9f30)
    /Users/gustav/src/kube-score/cmd/kube-score/main.go:180 +0x938
main.main.func1(0x7ffeefbff158, 0xa, 0xc0000981a0, 0x1, 0x1)
    /Users/gustav/src/kube-score/cmd/kube-score/main.go:36 +0x65
main.main()
    /Users/gustav/src/kube-score/cmd/kube-score/main.go:64 +0x3c3

Also fails with apiVersion: extensions/v1beta1. Works with apiVersion: networking.k8s.io/v1beta1.

zegl commented 3 years ago

Hey, thanks for the report and the good example.

In addition to the panic (which should not happen), the input also seems to be invalid.

To get your ingress to work, you should also change the definition to the following:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whatever
spec:
  rules:
    - host: abc.example.test
      http:
        paths:
          - path: /
            backend:
              service:
                 name: abc
                 port: def

API reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#ingressbackend-v1-networking-k8s-io