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:
- install metrics-server to serve v1beta1.metrics.k8s.io
- install prometheus-adapter to serve v1beta1.custom.metrics.k8s.io
- ensure resourceRules is populated in the prometheus-adapter config so that resource metrics are also served by v1beta1.custom.metrics.k8s.io: https://github.com/kubernetes-sigs/prometheus-adapter/blob/7a421bb91e9ea52be4e26db22392c36c8af38bc8/deploy/manifests/custom-metrics-config-map.yaml#L73-L98
- restarting apiserver will result in the OpenAPI failure and 503 responses to /openapi/v2 path
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)
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/v1beta1endpoint. 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/podsFix 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
@Jefftree I tried to reproduce on my local setup with
kindbased on steps here +apiserver restart, but I could still get a successful response when I executedkubectl get --raw /openapi/v2.Then I reverted one of the step mentioned in here to
- --prometheus-url=http://prometheus.default.svc:9090and waited until the newmy-release-prometheus-adapterpod came alive and then did a apiserver restart and I was able to reproduce the same thing onkind clusteras well i.e; I get the 503 response when I execute following command after the fact.After this point, even if i change it back to
- --prometheus-url=http://prometheus-server.prometheus.svc:80and 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, itsonly 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/v2is 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
and we cannot install any other new helm plugins. maybe we need to move forward