K8s/Swagger: Avoid */* media type (#101658)

* avoid */*

* use application/yaml

* fix lint
This commit is contained in:
Ryan McKinley
2025-03-06 10:13:31 +03:00
committed by GitHub
parent ac9615ec7d
commit a1d5e5dad1
6 changed files with 168 additions and 1592 deletions
+36 -1
View File
@@ -42,6 +42,7 @@ func GetOpenAPIDefinitions(builders []APIGroupBuilder) common.GetOpenAPIDefiniti
// Modify the OpenAPI spec to include the additional routes.
// Currently this requires: https://github.com/kubernetes/kube-openapi/pull/420
// In future k8s release, the hook will use Config3 rather than the same hook for both v2 and v3
// nolint:gocyclo
func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*spec3.OpenAPI) (*spec3.OpenAPI, error) {
return func(s *spec3.OpenAPI) (*spec3.OpenAPI, error) {
if s.Paths == nil {
@@ -66,12 +67,46 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
Paths: s.Paths,
}
for k := range copy.Paths.Paths {
for k, v := range copy.Paths.Paths {
if k == prefix {
continue // API discovery
}
// Remove the deprecated watch URL -- can use list with ?watch=true
if strings.HasPrefix(k, prefix+"watch/") {
delete(copy.Paths.Paths, k)
continue
}
// Remove the "for all namespaces" global routes from OpenAPI (v3)
if !strings.HasPrefix(k, prefix+"namespaces/") {
delete(copy.Paths.Paths, k)
continue
}
// Delete has all parameters in the query string already
if v.Delete != nil {
action, ok := v.Delete.VendorExtensible.Extensions.GetString("x-kubernetes-action")
if ok && (action == "deletecollection" || action == "delete") {
v.Delete.RequestBody = nil // duplicates all the parameters
}
}
// Replace any */* media types with json+yaml (protobuf?)
ops := []*spec3.Operation{v.Delete, v.Put, v.Post}
for _, op := range ops {
if op == nil || op.RequestBody == nil || len(op.RequestBody.Content) != 1 {
continue
}
content, ok := op.RequestBody.Content["*/*"]
if ok {
op.RequestBody.Content = map[string]*spec3.MediaType{
"application/json": content,
"application/yaml": content,
"application/vnd.kubernetes.protobuf": content,
}
}
}
}
sub := copy.Paths.Paths[prefix]