k8s.io/apiserver@v0.31.1/pkg/endpoints/handlers/responsewriters/status.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package responsewriters
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/util/runtime"
    25  	"k8s.io/apiserver/pkg/storage"
    26  )
    27  
    28  // statusError is an object that can be converted into an metav1.Status
    29  type statusError interface {
    30  	Status() metav1.Status
    31  }
    32  
    33  // ErrorToAPIStatus converts an error to an metav1.Status object.
    34  func ErrorToAPIStatus(err error) *metav1.Status {
    35  	switch t := err.(type) {
    36  	case statusError:
    37  		status := t.Status()
    38  		if len(status.Status) == 0 {
    39  			status.Status = metav1.StatusFailure
    40  		}
    41  		switch status.Status {
    42  		case metav1.StatusSuccess:
    43  			if status.Code == 0 {
    44  				status.Code = http.StatusOK
    45  			}
    46  		case metav1.StatusFailure:
    47  			if status.Code == 0 {
    48  				status.Code = http.StatusInternalServerError
    49  			}
    50  		default:
    51  			runtime.HandleError(fmt.Errorf("apiserver received an error with wrong status field : %#+v", err))
    52  			if status.Code == 0 {
    53  				status.Code = http.StatusInternalServerError
    54  			}
    55  		}
    56  		status.Kind = "Status"
    57  		status.APIVersion = "v1"
    58  		//TODO: check for invalid responses
    59  		return &status
    60  	default:
    61  		status := http.StatusInternalServerError
    62  		switch {
    63  		//TODO: replace me with NewConflictErr
    64  		case storage.IsConflict(err):
    65  			status = http.StatusConflict
    66  		}
    67  		// Log errors that were not converted to an error status
    68  		// by REST storage - these typically indicate programmer
    69  		// error by not using pkg/api/errors, or unexpected failure
    70  		// cases.
    71  		runtime.HandleError(fmt.Errorf("apiserver received an error that is not an metav1.Status: %#+v: %v", err, err))
    72  		return &metav1.Status{
    73  			TypeMeta: metav1.TypeMeta{
    74  				Kind:       "Status",
    75  				APIVersion: "v1",
    76  			},
    77  			Status:  metav1.StatusFailure,
    78  			Code:    int32(status),
    79  			Reason:  metav1.StatusReasonUnknown,
    80  			Message: err.Error(),
    81  		}
    82  	}
    83  }