-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Make server-side apply's conflict errors more human readable #73976
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
82 changes: 82 additions & 0 deletions
82
staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/internal/conflict.go
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package internal | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/structured-merge-diff/fieldpath" | ||
"sigs.k8s.io/structured-merge-diff/merge" | ||
) | ||
|
||
// NewConflictError returns an error including details on the requests apply conflicts | ||
func NewConflictError(conflicts merge.Conflicts) *errors.StatusError { | ||
causes := []metav1.StatusCause{} | ||
for _, conflict := range conflicts { | ||
causes = append(causes, metav1.StatusCause{ | ||
Type: metav1.CauseTypeFieldManagerConflict, | ||
Message: fmt.Sprintf("conflict with %v", printManager(conflict.Manager)), | ||
Field: conflict.Path.String(), | ||
}) | ||
} | ||
return errors.NewApplyConflict(causes, getConflictMessage(conflicts)) | ||
} | ||
|
||
func getConflictMessage(conflicts merge.Conflicts) string { | ||
if len(conflicts) == 1 { | ||
return fmt.Sprintf("Apply failed with 1 conflict: conflict with %v: %v", printManager(conflicts[0].Manager), conflicts[0].Path) | ||
} | ||
|
||
m := map[string][]fieldpath.Path{} | ||
for _, conflict := range conflicts { | ||
m[conflict.Manager] = append(m[conflict.Manager], conflict.Path) | ||
} | ||
|
||
uniqueManagers := []string{} | ||
for manager := range m { | ||
uniqueManagers = append(uniqueManagers, manager) | ||
} | ||
|
||
// Print conflicts by sorted managers. | ||
sort.Strings(uniqueManagers) | ||
|
||
messages := []string{} | ||
for _, manager := range uniqueManagers { | ||
messages = append(messages, fmt.Sprintf("conflicts with %v:", printManager(manager))) | ||
for _, path := range m[manager] { | ||
messages = append(messages, fmt.Sprintf("- %v", path)) | ||
} | ||
} | ||
return fmt.Sprintf("Apply failed with %d conflicts: %s", len(conflicts), strings.Join(messages, "\n")) | ||
} | ||
|
||
func printManager(manager string) string { | ||
encodedManager := &metav1.ManagedFieldsEntry{} | ||
if err := json.Unmarshal([]byte(manager), encodedManager); err != nil { | ||
return fmt.Sprintf("%q", manager) | ||
} | ||
if encodedManager.Operation == metav1.ManagedFieldsOperationUpdate { | ||
return fmt.Sprintf("%q using %v at %v", encodedManager.Manager, encodedManager.APIVersion, encodedManager.Time.UTC().Format(time.RFC3339)) | ||
} | ||
return fmt.Sprintf("%q", encodedManager.Manager) | ||
} |
106 changes: 106 additions & 0 deletions
106
staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/internal/conflict_test.go
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package internal_test | ||
|
||
import ( | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/internal" | ||
"sigs.k8s.io/structured-merge-diff/fieldpath" | ||
"sigs.k8s.io/structured-merge-diff/merge" | ||
) | ||
|
||
// TestNewConflictError tests that NewConflictError creates the correct StatusError for a given smd Conflicts | ||
func TestNewConflictError(t *testing.T) { | ||
testCases := []struct { | ||
conflict merge.Conflicts | ||
expected *errors.StatusError | ||
}{ | ||
{ | ||
conflict: merge.Conflicts{ | ||
merge.Conflict{ | ||
Manager: `{"manager":"foo","operation":"Update","apiVersion":"v1","time":"2001-02-03T04:05:06Z"}`, | ||
Path: fieldpath.MakePathOrDie("spec", "replicas"), | ||
}, | ||
}, | ||
expected: &errors.StatusError{ | ||
ErrStatus: metav1.Status{ | ||
Status: metav1.StatusFailure, | ||
Code: http.StatusConflict, | ||
Reason: metav1.StatusReasonConflict, | ||
Details: &metav1.StatusDetails{ | ||
Causes: []metav1.StatusCause{ | ||
{ | ||
Type: metav1.CauseTypeFieldManagerConflict, | ||
Message: `conflict with "foo" using v1 at 2001-02-03T04:05:06Z`, | ||
Field: ".spec.replicas", | ||
}, | ||
}, | ||
}, | ||
Message: `Apply failed with 1 conflict: conflict with "foo" using v1 at 2001-02-03T04:05:06Z: .spec.replicas`, | ||
}, | ||
}, | ||
}, | ||
{ | ||
conflict: merge.Conflicts{ | ||
merge.Conflict{ | ||
Manager: `{"manager":"foo","operation":"Update","apiVersion":"v1","time":"2001-02-03T04:05:06Z"}`, | ||
Path: fieldpath.MakePathOrDie("spec", "replicas"), | ||
}, | ||
merge.Conflict{ | ||
Manager: `{"manager":"bar","operation":"Apply"}`, | ||
Path: fieldpath.MakePathOrDie("metadata", "labels", "app"), | ||
}, | ||
}, | ||
expected: &errors.StatusError{ | ||
ErrStatus: metav1.Status{ | ||
Status: metav1.StatusFailure, | ||
Code: http.StatusConflict, | ||
Reason: metav1.StatusReasonConflict, | ||
Details: &metav1.StatusDetails{ | ||
Causes: []metav1.StatusCause{ | ||
{ | ||
Type: metav1.CauseTypeFieldManagerConflict, | ||
Message: `conflict with "foo" using v1 at 2001-02-03T04:05:06Z`, | ||
Field: ".spec.replicas", | ||
}, | ||
{ | ||
Type: metav1.CauseTypeFieldManagerConflict, | ||
Message: `conflict with "bar"`, | ||
Field: ".metadata.labels.app", | ||
}, | ||
}, | ||
}, | ||
Message: `Apply failed with 2 conflicts: conflicts with "bar": | ||
- .metadata.labels.app | ||
conflicts with "foo" using v1 at 2001-02-03T04:05:06Z: | ||
- .spec.replicas`, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
actual := internal.NewConflictError(tc.conflict) | ||
if !reflect.DeepEqual(tc.expected, actual) { | ||
t.Errorf("Expected to get\n%+v\nbut got\n%+v", tc.expected.ErrStatus, actual.ErrStatus) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
@liggitt FYI, new API constant.