-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
storage: add custom storage error #23295
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,7 +216,7 @@ func (h *etcdHelper) Create(ctx context.Context, key string, obj, out runtime.Ob | |
metrics.RecordEtcdRequestLatency("create", getTypeName(obj), startTime) | ||
trace.Step("Object created") | ||
if err != nil { | ||
return err | ||
return toStorageErr(err, key, 0) | ||
} | ||
if out != nil { | ||
if _, err := conversion.EnforcePtr(out); err != nil { | ||
|
@@ -263,7 +263,7 @@ func (h *etcdHelper) Set(ctx context.Context, key string, obj, out runtime.Objec | |
response, err = h.etcdKeysAPI.Set(ctx, key, string(data), &opts) | ||
metrics.RecordEtcdRequestLatency("compareAndSwap", getTypeName(obj), startTime) | ||
if err != nil { | ||
return err | ||
return toStorageErr(err, key, int64(version)) | ||
} | ||
} | ||
if create { | ||
|
@@ -275,7 +275,7 @@ func (h *etcdHelper) Set(ctx context.Context, key string, obj, out runtime.Objec | |
} | ||
response, err = h.etcdKeysAPI.Set(ctx, key, string(data), &opts) | ||
if err != nil { | ||
return err | ||
return toStorageErr(err, key, 0) | ||
} | ||
metrics.RecordEtcdRequestLatency("create", getTypeName(obj), startTime) | ||
} | ||
|
@@ -309,7 +309,7 @@ func (h *etcdHelper) Delete(ctx context.Context, key string, out runtime.Object) | |
_, _, err = h.extractObj(response, err, out, false, true) | ||
} | ||
} | ||
return err | ||
return toStorageErr(err, key, 0) | ||
} | ||
|
||
// Implements storage.Interface. | ||
|
@@ -366,12 +366,11 @@ func (h *etcdHelper) bodyAndExtractObj(ctx context.Context, key string, objPtr r | |
|
||
response, err := h.etcdKeysAPI.Get(ctx, key, opts) | ||
metrics.RecordEtcdRequestLatency("get", getTypeName(objPtr), startTime) | ||
|
||
if err != nil && !etcdutil.IsEtcdNotFound(err) { | ||
return "", nil, nil, err | ||
return "", nil, nil, toStorageErr(err, key, 0) | ||
} | ||
body, node, err = h.extractObj(response, err, objPtr, ignoreNotFound, false) | ||
return body, node, response, err | ||
return body, node, response, toStorageErr(err, key, 0) | ||
} | ||
|
||
func (h *etcdHelper) extractObj(response *etcd.Response, inErr error, objPtr runtime.Object, ignoreNotFound, prevNode bool) (body string, node *etcd.Node, err error) { | ||
|
@@ -433,7 +432,7 @@ func (h *etcdHelper) GetToList(ctx context.Context, key string, filter storage.F | |
if etcdutil.IsEtcdNotFound(err) { | ||
return nil | ||
} | ||
return err | ||
return toStorageErr(err, key, 0) | ||
} | ||
|
||
nodes := make([]*etcd.Node, 0) | ||
|
@@ -540,7 +539,7 @@ func (h *etcdHelper) listEtcdNode(ctx context.Context, key string) ([]*etcd.Node | |
if etcdutil.IsEtcdNotFound(err) { | ||
return nodes, index, nil | ||
} else { | ||
return nodes, index, err | ||
return nodes, index, toStorageErr(err, key, 0) | ||
} | ||
} | ||
return result.Node.Nodes, result.Index, nil | ||
|
@@ -622,7 +621,7 @@ func (h *etcdHelper) GuaranteedUpdate(ctx context.Context, key string, ptrToType | |
continue | ||
} | ||
_, _, err = h.extractObj(response, err, ptrToType, false, false) | ||
return err | ||
return toStorageErr(err, key, 0) | ||
} | ||
|
||
if string(data) == origBody { | ||
|
@@ -646,7 +645,7 @@ func (h *etcdHelper) GuaranteedUpdate(ctx context.Context, key string, ptrToType | |
continue | ||
} | ||
_, _, err = h.extractObj(response, err, ptrToType, false, false) | ||
return err | ||
return toStorageErr(err, key, int64(index)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please do the same in etcd_watcher.go file? |
||
} | ||
} | ||
|
||
|
@@ -711,3 +710,21 @@ func (h *etcdHelper) addToCache(index uint64, obj runtime.Object) { | |
metrics.ObserveNewEntry() | ||
} | ||
} | ||
|
||
func toStorageErr(err error, key string, rv int64) error { | ||
if err == nil { | ||
return nil | ||
} | ||
switch { | ||
case etcdutil.IsEtcdNotFound(err): | ||
return storage.NewKeyNotFoundError(key, rv) | ||
case etcdutil.IsEtcdNodeExist(err): | ||
return storage.NewKeyExistsError(key, rv) | ||
case etcdutil.IsEtcdTestFailed(err): | ||
return storage.NewResourceVersionConflictsError(key, rv) | ||
case etcdutil.IsEtcdUnreachable(err): | ||
return storage.NewUnreachableError(key, rv) | ||
default: | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice finding