| 1 |
/* |
| 2 |
Copyright 2016 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 etcd3 |
| 18 |
|
| 19 |
import ( |
| 20 |
goerrors "errors" |
| 21 |
"net/http" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/api/errors" |
| 24 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 |
"k8s.io/apiserver/pkg/storage" |
| 26 |
|
| 27 |
etcdrpc "go.etcd.io/etcd/api/v3/v3rpc/rpctypes" |
| 28 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 29 |
) |
| 30 |
|
| 31 |
func interpretWatchError(err error) error { |
| 32 |
switch { |
| 33 |
case err == etcdrpc.ErrCompacted: |
| 34 |
return errors.NewResourceExpired("The resourceVersion for the provided watch is too old.") |
| 35 |
} |
| 36 |
|
| 37 |
var corruptobjDeletedErr *corruptObjectDeletedError |
| 38 |
if goerrors.As(err, &corruptobjDeletedErr) { |
| 39 |
return &errors.StatusError{ |
| 40 |
ErrStatus: metav1.Status{ |
| 41 |
Status: metav1.StatusFailure, |
| 42 |
Code: http.StatusInternalServerError, |
| 43 |
Reason: metav1.StatusReasonStoreReadError, |
| 44 |
Message: corruptobjDeletedErr.Error(), |
| 45 |
}, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return err |
| 50 |
} |
| 51 |
|
| 52 |
const ( |
| 53 |
expired string = "The resourceVersion for the provided list is too old." |
| 54 |
continueExpired string = "The provided continue parameter is too old " + |
| 55 |
"to display a consistent list result. You can start a new list without " + |
| 56 |
"the continue parameter." |
| 57 |
inconsistentContinue string = "The provided continue parameter is too old " + |
| 58 |
"to display a consistent list result. You can start a new list without " + |
| 59 |
"the continue parameter, or use the continue token in this response to " + |
| 60 |
"retrieve the remainder of the results. Continuing with the provided " + |
| 61 |
"token results in an inconsistent list - objects that were created, " + |
| 62 |
"modified, or deleted between the time the first chunk was returned " + |
| 63 |
"and now may show up in the list." |
| 64 |
) |
| 65 |
|
| 66 |
func interpretListError(err error, paging bool, continueKey, keyPrefix string) error { |
| 67 |
switch { |
| 68 |
case err == etcdrpc.ErrCompacted: |
| 69 |
if paging { |
| 70 |
return handleCompactedErrorForPaging(continueKey, keyPrefix) |
| 71 |
} |
| 72 |
return errors.NewResourceExpired(expired) |
| 73 |
} |
| 74 |
return err |
| 75 |
} |
| 76 |
|
| 77 |
func handleCompactedErrorForPaging(continueKey, keyPrefix string) error { |
| 78 |
// continueToken.ResoureVersion=-1 means that the apiserver can |
| 79 |
// continue the list at the latest resource version. We don't use rv=0 |
| 80 |
// for this purpose to distinguish from a bad token that has empty rv. |
| 81 |
newToken, err := storage.EncodeContinue(continueKey, keyPrefix, -1) |
| 82 |
if err != nil { |
| 83 |
utilruntime.HandleError(err) |
| 84 |
return errors.NewResourceExpired(continueExpired) |
| 85 |
} |
| 86 |
statusError := errors.NewResourceExpired(inconsistentContinue) |
| 87 |
statusError.ErrStatus.ListMeta.Continue = newToken |
| 88 |
return statusError |
| 89 |
} |