kubernetes: OpenAPI handler fails on duplicated path

What happened?

When installing the metrics-server to implement the metrics.k8s.io API and the prometheus-adapter to implement the custom.metrics.k8s.io API, it is possible to make the OpenAPI handler fail to initialize with this error log in apiserver: https://github.com/kubernetes/kube-openapi/blob/master/pkg/aggregator/aggregator.go#L343

handler.go:159] Error in OpenAPI handler: failed to build merge specs: unable to merge: duplicated path /apis/metrics.k8s.io/v1beta1/pods
handler.go:159] Error in OpenAPI handler: failed to build merge specs: unable to merge: duplicated path /apis/metrics.k8s.io/v1beta1/namespaces/{namespace}/pods
handler.go:159] Error in OpenAPI handler: failed to build merge specs: unable to merge: duplicated path /apis/metrics.k8s.io/v1beta1/nodes
handler.go:159] Error in OpenAPI handler: failed to build merge specs: unable to merge: duplicated path /apis/metrics.k8s.io/v1beta1/nodes/{name}
handler.go:159] Error in OpenAPI handler: failed to build merge specs: unable to merge: duplicated path /apis/metrics.k8s.io/v1beta1/nodes

This will result in kubectl discovery calls to /openapi/v2 to fail with a 503:

{
  "kind": "Event",
  "apiVersion": "audit.k8s.io/v1",
  "level": "Metadata",
  "auditID": "863d36c0-3797-4f19-bc74-df571a671a00",
  "stage": "ResponseComplete",
  "requestURI": "/openapi/v2?timeout=31s",
  "verb": "get",
...
  "userAgent": "kubectl/v1.28.2 (linux/amd64) kubernetes/cd9e211",
  "responseStatus": {
    "metadata": {},
    "code": 503
  },
  "requestReceivedTimestamp": "2023-12-19T02:50:12.882602Z",
  "stageTimestamp": "2023-12-19T02:50:12.913981Z",
  "annotations": {
    "authorization.k8s.io/decision": "allow",
    "authorization.k8s.io/reason": ""
  }
}

What did you expect to happen?

The OpenAPI handler should not fail to initialize if both metrics.k8s.io and custom.metrics.k8s.io are serving the same path for different API groups/versions.

How can we reproduce it (as minimally and precisely as possible)?

I can provide exact installation steps if needed, however, a high-level repro is:

Anything else we need to know?

No response

Kubernetes version

$ kubectl version
Server Version: version.Info{Major:"1", Minor:"28+", GitVersion:"v1.28.4-eks-8cb36c9", GitCommit:"b4ddd686b005f445825bd0afd5dba24d4414d4cf", GitTreeState:"clean", BuildDate:"2023-11-22T21:52:14Z", GoVersion:"go1.20.11", Compiler:"gc", Platform:"linux/amd64"}

Cloud provider

AWS

OS version

# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, …) and versions (if applicable)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 46 (22 by maintainers)

Most upvoted comments

Quick update: This issue was a combination of bugs both in k/k and prometheus adapter. I opened https://github.com/kubernetes/kubernetes/pull/123570 to mitigate on k/k side which should fix the issue.

The issue was difficult to reproduce because it requires a very specific ordering of OpenAPI merging to trigger, which is why I suspect many people were able to reproduce after some restarts to kube-apiserver.

Prometheus and metrics server serve differing OpenAPIs for the /metrics.k8s.io/v1beta1 endpoint. Prometheus also doesn’t seem to be registering discovery’s OpenAPI properly (missing OpenAPI for /metrics.k8s.io/ but having the OpenAPI for /metrics.k8s.io/v1beta1/{endpoints} whereas it is all present in metrics server. This inconsistency in how prometheus and metrics server handle the OpenAPI surfaced the merging issue.

Tried to reproduce the issue after cherry picking the fix https://github.com/kubernetes/kubernetes/pull/123570 and didn’t run into below error. handler.go:159] Error in OpenAPI handler: failed to build merge specs: unable to merge: duplicated path /apis/metrics.k8s.io/v1beta1/pods

Fix is working as expected.

Thanks, alright I found a way to reproduce it consistently without restarting the apiserver. Still investigating the root cause. Thanks for the detailed reproduction @natherz97 and @hakuna-matatah

@natherz97 yes, I’m running on kind.

kubernetes ❯ k get pods -A
NAMESPACE            NAME                                                 READY   STATUS             RESTARTS        AGE
...
default              my-release-prometheus-adapter-6b74748d98-xhrrw       1/1     Running            0               5m34s
kube-system          metrics-server-66f57d964b-2zcq4                      1/1     Running            0               2m3s
kube-system          metrics-server-66f57d964b-5tl5z                      1/1     Running            0               2m3s
prometheus           prometheus-alertmanager-0                            1/1     Running            0               10m
prometheus           prometheus-kube-state-metrics-745b475957-7k2f9       1/1     Running            0               10m
prometheus           prometheus-prometheus-node-exporter-8szsq            1/1     Running            0               10m
prometheus           prometheus-prometheus-node-exporter-tjhkx            1/1     Running            0               10m
prometheus           prometheus-prometheus-node-exporter-wrpcn            1/1     Running            0               10m
prometheus           prometheus-prometheus-pushgateway-6ccd698d79-5sfm6   1/1     Running            0               10m
prometheus           prometheus-server-5c99dfc547-ldwc5                   2/2     Running            0               10m
kubernetes ❯ k get apiservices | grep metrics.k8s.io   
v1beta1.custom.metrics.k8s.io     default/my-release-prometheus-adapter   True        9m7s
v1beta1.metrics.k8s.io            kube-system/metrics-server              True        9m28s

I’ll do some more digging, assuming it’s related to those PRs linked by @shyamjvs but still haven’t pinpointed anything.

@Jefftree I tried to reproduce on my local setup with kind based on steps here + apiserver restart, but I could still get a successful response when I executed kubectl get --raw /openapi/v2.

Then I reverted one of the step mentioned in here to - --prometheus-url=http://prometheus.default.svc:9090 and waited until the new my-release-prometheus-adapter pod came alive and then did a apiserver restart and I was able to reproduce the same thing on kind cluster as well i.e; I get the 503 response when I execute following command after the fact.

dev-dsk-hakuna-2c-0fa5574b % kubectl get --raw /openapi/v2     --v=9
I0228 05:21:10.106706  144733 loader.go:395] Config loaded from file:  /home/hakuna/.kube/config
I0228 05:21:10.107467  144733 round_trippers.go:466] curl -v -XGET  -H "Accept: application/json, */*" -H "User-Agent: kubectl/v1.29.2 (linux/amd64) kubernetes/4b8e819" 'https://127.0.0.1:43171/openapi/v2'
I0228 05:21:10.107789  144733 round_trippers.go:510] HTTP Trace: Dial to tcp:127.0.0.1:43171 succeed
I0228 05:21:10.135567  144733 round_trippers.go:553] GET https://127.0.0.1:43171/openapi/v2 503 Service Unavailable in 28 milliseconds
I0228 05:21:10.135593  144733 round_trippers.go:570] HTTP Statistics: DNSLookup 0 ms Dial 0 ms TLSHandshake 5 ms ServerProcessing 22 ms Duration 28 ms
I0228 05:21:10.135615  144733 round_trippers.go:577] Response Headers:
I0228 05:21:10.135632  144733 round_trippers.go:580]     X-Kubernetes-Pf-Flowschema-Uid: bcaa6da0-0e9d-4093-84eb-657d80be2b85
I0228 05:21:10.135649  144733 round_trippers.go:580]     X-Kubernetes-Pf-Prioritylevel-Uid: 97870445-4efc-44b1-8ecb-e0fbcb170a0e
I0228 05:21:10.135657  144733 round_trippers.go:580]     Content-Length: 0
I0228 05:21:10.135667  144733 round_trippers.go:580]     Date: Wed, 28 Feb 2024 05:21:10 GMT
I0228 05:21:10.135681  144733 round_trippers.go:580]     Audit-Id: 08d5275e-357e-43e9-9248-04536b3bd006
I0228 05:21:10.135689  144733 round_trippers.go:580]     Cache-Control: no-cache, private
I0228 05:21:10.135702  144733 round_trippers.go:580]     Vary: Accept-Encoding
I0228 05:21:10.135719  144733 round_trippers.go:580]     Vary: Accept
I0228 05:21:10.135756  144733 request.go:1212] Response Body: 
I0228 05:21:10.135905  144733 helpers.go:246] server response object: [{
  "metadata": {},
  "status": "Failure",
  "message": "the server is currently unable to handle the request",
  "reason": "ServiceUnavailable",
  "details": {
    "causes": [
      {
        "reason": "UnexpectedServerResponse"
      }
    ]
  },
  "code": 503
}]
Error from server (ServiceUnavailable): the server is currently unable to handle the request

After this point, even if i change it back to - --prometheus-url=http://prometheus-server.prometheus.svc:80 and restart the apiserver , the error is persistent. Let me know if this works for you to reproduce ?

PS: Also, i want to call out that there is no issue with /openapi/v3 , its only with v2.

For the record I resolved the problem (at least for now it is working) by removing the 4 reports related kyverno crds. Theoretically they shouldn’t be required as the apis are implemented by the aggregated server. I assume somehow the crds presence can clash with the aggregated apiserver although I was unable to reproduce it in a separate env. Might also be some form of a race condition.

However the problem is that the kyverno controllers including reports-controller check at startup whether those crds exist. So I acually had to readd them afterwards. But apparantly after I readded them I did not run into the same problem anymore. kubectl get --raw /openapi/v2 is working ever since I did that. I raised this here https://github.com/kyverno/kyverno/pull/11867

@Jefftree After updating the configmap, restarting the prometheus-adapter pod to pickup the changes and restarting the apiserver are both required to induce the issue. I have been able to deterministically reproduce this following those steps, please follow up here if you are still having issues.

@Jefftree hit same problem

1. install metrics-server to serve v1beta1.metrics.k8s.io
2. install prometheus-adapter to serve v1beta1.custom.metrics.k8s.io
3. restarting 1.28 kube-apiserver will result in this failure

and we cannot install any other new helm plugins. maybe we need to move forward