Merge pull request #61949 from liggitt/tolerate-openapi

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Tolerate 406 mime-type errors attempting to load new openapi schema

Fixes #61805
Fixes #61943

```release-note
kubectl: improves compatibility with older servers when creating/updating API objects
```

Kubernetes-commit: 66100690bc157fa6bdc67f93daf8d72aedb84e0b
This commit is contained in:
Kubernetes Publisher 2018-04-02 03:44:29 -07:00
commit bf48af0146
3 changed files with 93 additions and 59 deletions

106
Godeps/Godeps.json generated
View File

@ -364,215 +364,215 @@
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/equality", "ImportPath": "k8s.io/apimachinery/pkg/api/equality",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/errors", "ImportPath": "k8s.io/apimachinery/pkg/api/errors",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/meta", "ImportPath": "k8s.io/apimachinery/pkg/api/meta",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/resource", "ImportPath": "k8s.io/apimachinery/pkg/api/resource",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/testing", "ImportPath": "k8s.io/apimachinery/pkg/api/testing",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/testing/fuzzer", "ImportPath": "k8s.io/apimachinery/pkg/api/testing/fuzzer",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/api/testing/roundtrip", "ImportPath": "k8s.io/apimachinery/pkg/api/testing/roundtrip",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apimachinery", "ImportPath": "k8s.io/apimachinery/pkg/apimachinery",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apimachinery/announced", "ImportPath": "k8s.io/apimachinery/pkg/apimachinery/announced",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apimachinery/registered", "ImportPath": "k8s.io/apimachinery/pkg/apimachinery/registered",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/fuzzer", "ImportPath": "k8s.io/apimachinery/pkg/apis/meta/fuzzer",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/internalversion", "ImportPath": "k8s.io/apimachinery/pkg/apis/meta/internalversion",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1", "ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured", "ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1beta1", "ImportPath": "k8s.io/apimachinery/pkg/apis/meta/v1beta1",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/conversion", "ImportPath": "k8s.io/apimachinery/pkg/conversion",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/conversion/queryparams", "ImportPath": "k8s.io/apimachinery/pkg/conversion/queryparams",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/fields", "ImportPath": "k8s.io/apimachinery/pkg/fields",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/labels", "ImportPath": "k8s.io/apimachinery/pkg/labels",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime", "ImportPath": "k8s.io/apimachinery/pkg/runtime",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/schema", "ImportPath": "k8s.io/apimachinery/pkg/runtime/schema",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer", "ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/json", "ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/json",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/protobuf", "ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/protobuf",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/recognizer", "ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/recognizer",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/streaming", "ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/streaming",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/versioning", "ImportPath": "k8s.io/apimachinery/pkg/runtime/serializer/versioning",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/selection", "ImportPath": "k8s.io/apimachinery/pkg/selection",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/types", "ImportPath": "k8s.io/apimachinery/pkg/types",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/cache", "ImportPath": "k8s.io/apimachinery/pkg/util/cache",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/clock", "ImportPath": "k8s.io/apimachinery/pkg/util/clock",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/diff", "ImportPath": "k8s.io/apimachinery/pkg/util/diff",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/errors", "ImportPath": "k8s.io/apimachinery/pkg/util/errors",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/framer", "ImportPath": "k8s.io/apimachinery/pkg/util/framer",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream", "ImportPath": "k8s.io/apimachinery/pkg/util/httpstream",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/httpstream/spdy", "ImportPath": "k8s.io/apimachinery/pkg/util/httpstream/spdy",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/intstr", "ImportPath": "k8s.io/apimachinery/pkg/util/intstr",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/json", "ImportPath": "k8s.io/apimachinery/pkg/util/json",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/mergepatch", "ImportPath": "k8s.io/apimachinery/pkg/util/mergepatch",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/net", "ImportPath": "k8s.io/apimachinery/pkg/util/net",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/remotecommand", "ImportPath": "k8s.io/apimachinery/pkg/util/remotecommand",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/runtime", "ImportPath": "k8s.io/apimachinery/pkg/util/runtime",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/sets", "ImportPath": "k8s.io/apimachinery/pkg/util/sets",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/strategicpatch", "ImportPath": "k8s.io/apimachinery/pkg/util/strategicpatch",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/validation", "ImportPath": "k8s.io/apimachinery/pkg/util/validation",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/validation/field", "ImportPath": "k8s.io/apimachinery/pkg/util/validation/field",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/wait", "ImportPath": "k8s.io/apimachinery/pkg/util/wait",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/util/yaml", "ImportPath": "k8s.io/apimachinery/pkg/util/yaml",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/version", "ImportPath": "k8s.io/apimachinery/pkg/version",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/pkg/watch", "ImportPath": "k8s.io/apimachinery/pkg/watch",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json", "ImportPath": "k8s.io/apimachinery/third_party/forked/golang/json",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/netutil", "ImportPath": "k8s.io/apimachinery/third_party/forked/golang/netutil",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect", "ImportPath": "k8s.io/apimachinery/third_party/forked/golang/reflect",
"Rev": "b1fd01e100ba294ab4a44353d193c0d199f4d3b9" "Rev": "7256b11ddc89ab4b3be85518884e17d1534eb93e"
}, },
{ {
"ImportPath": "k8s.io/kube-openapi/pkg/util/proto", "ImportPath": "k8s.io/kube-openapi/pkg/util/proto",

View File

@ -335,7 +335,7 @@ func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) { func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", mimePb).Do().Raw() data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", mimePb).Do().Raw()
if err != nil { if err != nil {
if errors.IsForbidden(err) || errors.IsNotFound(err) { if errors.IsForbidden(err) || errors.IsNotFound(err) || errors.IsNotAcceptable(err) {
// single endpoint not found/registered in old server, try to fetch old endpoint // single endpoint not found/registered in old server, try to fetch old endpoint
// TODO(roycaihw): remove this in 1.11 // TODO(roycaihw): remove this in 1.11
data, err = d.restClient.Get().AbsPath("/swagger-2.0.0.pb-v1").Do().Raw() data, err = d.restClient.Get().AbsPath("/swagger-2.0.0.pb-v1").Do().Raw()

View File

@ -326,12 +326,12 @@ var returnedOpenAPI = openapi_v2.Document{
}, },
} }
func openapiSchemaDeprecatedFakeServer() (*httptest.Server, error) { func openapiSchemaDeprecatedFakeServer(status int) (*httptest.Server, error) {
var sErr error var sErr error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// old server returns 403 on new endpoint request
if req.URL.Path == "/openapi/v2" { if req.URL.Path == "/openapi/v2" {
w.WriteHeader(http.StatusForbidden) // write the error status for the new endpoint request
w.WriteHeader(status)
return return
} }
if req.URL.Path != "/swagger-2.0.0.pb-v1" { if req.URL.Path != "/swagger-2.0.0.pb-v1" {
@ -398,8 +398,42 @@ func TestGetOpenAPISchema(t *testing.T) {
} }
} }
func TestGetOpenAPISchemaFallback(t *testing.T) { func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
server, err := openapiSchemaDeprecatedFakeServer() server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden)
if err != nil {
t.Errorf("unexpected error starting fake server: %v", err)
}
defer server.Close()
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
got, err := client.OpenAPISchema()
if err != nil {
t.Fatalf("unexpected error getting openapi: %v", err)
}
if e, a := returnedOpenAPI, *got; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}
func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound)
if err != nil {
t.Errorf("unexpected error starting fake server: %v", err)
}
defer server.Close()
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
got, err := client.OpenAPISchema()
if err != nil {
t.Fatalf("unexpected error getting openapi: %v", err)
}
if e, a := returnedOpenAPI, *got; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, got %v", e, a)
}
}
func TestGetOpenAPISchemaNotAcceptableFallback(t *testing.T) {
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable)
if err != nil { if err != nil {
t.Errorf("unexpected error starting fake server: %v", err) t.Errorf("unexpected error starting fake server: %v", err)
} }