Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always allow decoding of status when returned from the API #35810

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions pkg/client/restclient/client.go
Expand Up @@ -173,9 +173,16 @@ func createSerializers(config ContentConfig) (*Serializers, error) {
info = mediaTypes[0]
}

internalGV := unversioned.GroupVersion{
Group: config.GroupVersion.Group,
Version: runtime.APIVersionInternal,
internalGV := unversioned.GroupVersions{
{
Group: config.GroupVersion.Group,
Version: runtime.APIVersionInternal,
},
// always include the legacy group as a decoding target to handle non-error `Status` return types
{
Group: "",
Version: runtime.APIVersionInternal,
},
}

s := &Serializers{
Expand Down
29 changes: 25 additions & 4 deletions pkg/client/restclient/client_test.go
Expand Up @@ -47,6 +47,26 @@ type TestParam struct {
testBodyErrorIsNotNil bool
}

// TestSerializer makes sure that you're always able to decode an unversioned API object
func TestSerializer(t *testing.T) {
contentConfig := ContentConfig{
ContentType: "application/json",
GroupVersion: &unversioned.GroupVersion{Group: "other", Version: runtime.APIVersionInternal},
NegotiatedSerializer: api.Codecs,
}

serializer, err := createSerializers(contentConfig)
if err != nil {
t.Fatal(err)
}
// bytes based on actual return from API server when encoding an "unversioned" object
obj, err := runtime.Decode(serializer.Decoder, []byte(`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}`))
t.Log(obj)
if err != nil {
t.Fatal(err)
}
}

func TestDoRequestSuccess(t *testing.T) {
testServer, fakeHandler, status := testServerEnv(t, 200)
defer testServer.Close()
Expand Down Expand Up @@ -162,10 +182,11 @@ func TestBadRequest(t *testing.T) {
}

func validate(testParam TestParam, t *testing.T, body []byte, fakeHandler *utiltesting.FakeHandler) {
if testParam.expectingError {
if testParam.actualError == nil {
t.Errorf("Expected error")
}
switch {
case testParam.expectingError && testParam.actualError == nil:
t.Errorf("Expected error")
case !testParam.expectingError && testParam.actualError != nil:
t.Error(testParam.actualError)
}
if !testParam.expCreated {
if testParam.actualCreated {
Expand Down
10 changes: 10 additions & 0 deletions pkg/kubectl/resource_printer.go
Expand Up @@ -489,6 +489,7 @@ var (
clusterRoleColumns = []string{"NAME", "AGE"}
clusterRoleBindingColumns = []string{"NAME", "AGE"}
storageClassColumns = []string{"NAME", "TYPE"}
statusColumns = []string{"STATUS", "REASON", "MESSAGE"}

// TODO: consider having 'KIND' for third party resource data
thirdPartyResourceDataColumns = []string{"NAME", "LABELS", "DATA"}
Expand Down Expand Up @@ -591,6 +592,7 @@ func (h *HumanReadablePrinter) addDefaultHandlers() {
h.Handler(certificateSigningRequestColumns, printCertificateSigningRequestList)
h.Handler(storageClassColumns, printStorageClass)
h.Handler(storageClassColumns, printStorageClassList)
h.Handler(statusColumns, printStatus)
}

func (h *HumanReadablePrinter) unknown(data []byte, w io.Writer) error {
Expand Down Expand Up @@ -2114,6 +2116,14 @@ func printStorageClassList(scList *storage.StorageClassList, w io.Writer, option
return nil
}

func printStatus(status *unversioned.Status, w io.Writer, options PrintOptions) error {
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n", status.Status, status.Reason, status.Message); err != nil {
return err
}

return nil
}

func AppendLabels(itemLabels map[string]string, columnLabels []string) string {
var buffer bytes.Buffer

Expand Down