unify alias of api errors under pkg and staging

Kubernetes-commit: 5bc0e26c1902e7e28abfc828de19a2dbb3e492c2
This commit is contained in:
danielqsj 2019-11-12 16:26:59 +08:00 committed by Kubernetes Publisher
parent 0a26b53c37
commit 4b914450ab
4 changed files with 33 additions and 33 deletions

View File

@ -34,7 +34,7 @@ import (
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
kubeerr "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -651,7 +651,7 @@ func TestApplyRetry(t *testing.T) {
case p == pathRC && m == "PATCH": case p == pathRC && m == "PATCH":
if firstPatch { if firstPatch {
firstPatch = false firstPatch = false
statusErr := kubeerr.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first")) statusErr := apierrors.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first"))
bodyBytes, _ := json.Marshal(statusErr) bodyBytes, _ := json.Marshal(statusErr)
bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes)) bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes))
return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil
@ -1278,7 +1278,7 @@ func TestForceApply(t *testing.T) {
case strings.HasSuffix(p, pathRC) && m == "PATCH": case strings.HasSuffix(p, pathRC) && m == "PATCH":
counts["patch"]++ counts["patch"]++
if counts["patch"] <= 6 { if counts["patch"] <= 6 {
statusErr := kubeerr.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first")) statusErr := apierrors.NewConflict(schema.GroupResource{Group: "", Resource: "rc"}, "test-rc", fmt.Errorf("the object has been modified. Please apply at first"))
bodyBytes, _ := json.Marshal(statusErr) bodyBytes, _ := json.Marshal(statusErr)
bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes)) bodyErr := ioutil.NopCloser(bytes.NewReader(bodyBytes))
return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil return &http.Response{StatusCode: http.StatusConflict, Header: cmdtesting.DefaultHeader(), Body: bodyErr}, nil

View File

@ -27,7 +27,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
kapierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -483,7 +483,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
Do() Do()
if o.IgnoreNotFound { if o.IgnoreNotFound {
r.IgnoreErrors(kapierrors.IsNotFound) r.IgnoreErrors(apierrors.IsNotFound)
} }
if err := r.Err(); err != nil { if err := r.Err(); err != nil {
return err return err

View File

@ -29,7 +29,7 @@ import (
jsonpatch "github.com/evanphx/json-patch" jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
kerrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -60,10 +60,10 @@ type debugError interface {
// source is the filename or URL to the template file(*.json or *.yaml), or stdin to use to handle the resource. // source is the filename or URL to the template file(*.json or *.yaml), or stdin to use to handle the resource.
func AddSourceToErr(verb string, source string, err error) error { func AddSourceToErr(verb string, source string, err error) error {
if source != "" { if source != "" {
if statusError, ok := err.(kerrors.APIStatus); ok { if statusError, ok := err.(apierrors.APIStatus); ok {
status := statusError.Status() status := statusError.Status()
status.Message = fmt.Sprintf("error when %s %q: %v", verb, source, status.Message) status.Message = fmt.Sprintf("error when %s %q: %v", verb, source, status.Message)
return &kerrors.StatusError{ErrStatus: status} return &apierrors.StatusError{ErrStatus: status}
} }
return fmt.Errorf("error when %s %q: %v", verb, source, err) return fmt.Errorf("error when %s %q: %v", verb, source, err)
} }
@ -129,8 +129,8 @@ func checkErr(err error, handleErr func(string, int)) {
switch { switch {
case err == ErrExit: case err == ErrExit:
handleErr("", DefaultErrorExitCode) handleErr("", DefaultErrorExitCode)
case kerrors.IsInvalid(err): case apierrors.IsInvalid(err):
details := err.(*kerrors.StatusError).Status().Details details := err.(*apierrors.StatusError).Status().Details
s := "The request is invalid" s := "The request is invalid"
if details == nil { if details == nil {
handleErr(s, DefaultErrorExitCode) handleErr(s, DefaultErrorExitCode)
@ -202,7 +202,7 @@ func StandardErrorMessage(err error) (string, bool) {
if debugErr, ok := err.(debugError); ok { if debugErr, ok := err.(debugError); ok {
klog.V(4).Infof(debugErr.DebugError()) klog.V(4).Infof(debugErr.DebugError())
} }
status, isStatus := err.(kerrors.APIStatus) status, isStatus := err.(apierrors.APIStatus)
switch { switch {
case isStatus: case isStatus:
switch s := status.Status(); { switch s := status.Status(); {
@ -213,7 +213,7 @@ func StandardErrorMessage(err error) (string, bool) {
default: default:
return fmt.Sprintf("Error from server: %s", err.Error()), true return fmt.Sprintf("Error from server: %s", err.Error()), true
} }
case kerrors.IsUnexpectedObjectError(err): case apierrors.IsUnexpectedObjectError(err):
return fmt.Sprintf("Server returned an unexpected response: %s", err.Error()), true return fmt.Sprintf("Server returned an unexpected response: %s", err.Error()), true
} }
switch t := err.(type) { switch t := err.(type) {
@ -259,7 +259,7 @@ func MultilineError(prefix string, err error) string {
// Returns true if a case exists to handle the error type, or false otherwise. // Returns true if a case exists to handle the error type, or false otherwise.
func PrintErrorWithCauses(err error, errOut io.Writer) bool { func PrintErrorWithCauses(err error, errOut io.Writer) bool {
switch t := err.(type) { switch t := err.(type) {
case *kerrors.StatusError: case *apierrors.StatusError:
errorDetails := t.Status().Details errorDetails := t.Status().Details
if errorDetails != nil { if errorDetails != nil {
fmt.Fprintf(errOut, "error: %s %q is invalid\n\n", errorDetails.Kind, errorDetails.Name) fmt.Fprintf(errOut, "error: %s %q is invalid\n\n", errorDetails.Kind, errorDetails.Name)

View File

@ -23,7 +23,7 @@ import (
"time" "time"
autoscalingv1 "k8s.io/api/autoscaling/v1" autoscalingv1 "k8s.io/api/autoscaling/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api "k8s.io/apimachinery/pkg/apis/testapigroup/v1" api "k8s.io/apimachinery/pkg/apis/testapigroup/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -58,8 +58,8 @@ var (
) )
func TestReplicationControllerScaleRetry(t *testing.T) { func TestReplicationControllerScaleRetry(t *testing.T) {
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil), "patch": apierrors.NewConflict(api.Resource("Status"), "foo", nil),
} }
scaleClientExpectedAction := []string{"patch", "get"} scaleClientExpectedAction := []string{"patch", "get"}
scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, verbsOnError) scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, verbsOnError)
@ -94,8 +94,8 @@ func TestReplicationControllerScaleRetry(t *testing.T) {
} }
func TestReplicationControllerScaleInvalid(t *testing.T) { func TestReplicationControllerScaleInvalid(t *testing.T) {
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil), "patch": apierrors.NewInvalid(api.Kind("Status"), "foo", nil),
} }
scaleClientExpectedAction := []string{"patch"} scaleClientExpectedAction := []string{"patch"}
scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 1, verbsOnError) scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 1, verbsOnError)
@ -168,8 +168,8 @@ func TestReplicationControllerScaleFailsPreconditions(t *testing.T) {
} }
func TestDeploymentScaleRetry(t *testing.T) { func TestDeploymentScaleRetry(t *testing.T) {
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil), "patch": apierrors.NewConflict(api.Resource("Status"), "foo", nil),
} }
scaleClientExpectedAction := []string{"patch", "get"} scaleClientExpectedAction := []string{"patch", "get"}
scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError) scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)
@ -226,8 +226,8 @@ func TestDeploymentScale(t *testing.T) {
func TestDeploymentScaleInvalid(t *testing.T) { func TestDeploymentScaleInvalid(t *testing.T) {
scaleClientExpectedAction := []string{"patch"} scaleClientExpectedAction := []string{"patch"}
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil), "patch": apierrors.NewInvalid(api.Kind("Status"), "foo", nil),
} }
scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError) scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)
scaler := NewScaler(scaleClient) scaler := NewScaler(scaleClient)
@ -299,8 +299,8 @@ func TestStatefulSetScale(t *testing.T) {
func TestStatefulSetScaleRetry(t *testing.T) { func TestStatefulSetScaleRetry(t *testing.T) {
scaleClientExpectedAction := []string{"patch", "get"} scaleClientExpectedAction := []string{"patch", "get"}
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil), "patch": apierrors.NewConflict(api.Resource("Status"), "foo", nil),
} }
scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError) scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)
scaler := NewScaler(scaleClient) scaler := NewScaler(scaleClient)
@ -335,8 +335,8 @@ func TestStatefulSetScaleRetry(t *testing.T) {
func TestStatefulSetScaleInvalid(t *testing.T) { func TestStatefulSetScaleInvalid(t *testing.T) {
scaleClientExpectedAction := []string{"patch"} scaleClientExpectedAction := []string{"patch"}
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil), "patch": apierrors.NewInvalid(api.Kind("Status"), "foo", nil),
} }
scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError) scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)
scaler := NewScaler(scaleClient) scaler := NewScaler(scaleClient)
@ -407,8 +407,8 @@ func TestReplicaSetScale(t *testing.T) {
} }
func TestReplicaSetScaleRetry(t *testing.T) { func TestReplicaSetScaleRetry(t *testing.T) {
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewConflict(api.Resource("Status"), "foo", nil), "patch": apierrors.NewConflict(api.Resource("Status"), "foo", nil),
} }
scaleClientExpectedAction := []string{"patch", "get"} scaleClientExpectedAction := []string{"patch", "get"}
scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError) scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)
@ -443,8 +443,8 @@ func TestReplicaSetScaleRetry(t *testing.T) {
} }
func TestReplicaSetScaleInvalid(t *testing.T) { func TestReplicaSetScaleInvalid(t *testing.T) {
verbsOnError := map[string]*kerrors.StatusError{ verbsOnError := map[string]*apierrors.StatusError{
"patch": kerrors.NewInvalid(api.Kind("Status"), "foo", nil), "patch": apierrors.NewInvalid(api.Kind("Status"), "foo", nil),
} }
scaleClientExpectedAction := []string{"patch"} scaleClientExpectedAction := []string{"patch"}
scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError) scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)
@ -688,12 +688,12 @@ func TestGenericScale(t *testing.T) {
} }
} }
func createFakeScaleClient(resource string, resourceName string, replicas int, errorsOnVerb map[string]*kerrors.StatusError) *fakescale.FakeScaleClient { func createFakeScaleClient(resource string, resourceName string, replicas int, errorsOnVerb map[string]*apierrors.StatusError) *fakescale.FakeScaleClient {
shouldReturnAnError := func(verb string) (*kerrors.StatusError, bool) { shouldReturnAnError := func(verb string) (*apierrors.StatusError, bool) {
if anError, anErrorExists := errorsOnVerb[verb]; anErrorExists { if anError, anErrorExists := errorsOnVerb[verb]; anErrorExists {
return anError, true return anError, true
} }
return &kerrors.StatusError{}, false return &apierrors.StatusError{}, false
} }
newReplicas := int32(replicas) newReplicas := int32(replicas)
scaleClient := &fakescale.FakeScaleClient{} scaleClient := &fakescale.FakeScaleClient{}