conflict.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package internal
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "sort"
  18. "strings"
  19. "time"
  20. "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
  23. "sigs.k8s.io/structured-merge-diff/v4/merge"
  24. )
  25. // NewConflictError returns an error including details on the requests apply conflicts
  26. func NewConflictError(conflicts merge.Conflicts) *errors.StatusError {
  27. causes := []metav1.StatusCause{}
  28. for _, conflict := range conflicts {
  29. causes = append(causes, metav1.StatusCause{
  30. Type: metav1.CauseTypeFieldManagerConflict,
  31. Message: fmt.Sprintf("conflict with %v", printManager(conflict.Manager)),
  32. Field: conflict.Path.String(),
  33. })
  34. }
  35. return errors.NewApplyConflict(causes, getConflictMessage(conflicts))
  36. }
  37. func getConflictMessage(conflicts merge.Conflicts) string {
  38. if len(conflicts) == 1 {
  39. return fmt.Sprintf("Apply failed with 1 conflict: conflict with %v: %v", printManager(conflicts[0].Manager), conflicts[0].Path)
  40. }
  41. m := map[string][]fieldpath.Path{}
  42. for _, conflict := range conflicts {
  43. m[conflict.Manager] = append(m[conflict.Manager], conflict.Path)
  44. }
  45. uniqueManagers := []string{}
  46. for manager := range m {
  47. uniqueManagers = append(uniqueManagers, manager)
  48. }
  49. // Print conflicts by sorted managers.
  50. sort.Strings(uniqueManagers)
  51. messages := []string{}
  52. for _, manager := range uniqueManagers {
  53. messages = append(messages, fmt.Sprintf("conflicts with %v:", printManager(manager)))
  54. for _, path := range m[manager] {
  55. messages = append(messages, fmt.Sprintf("- %v", path))
  56. }
  57. }
  58. return fmt.Sprintf("Apply failed with %d conflicts: %s", len(conflicts), strings.Join(messages, "\n"))
  59. }
  60. func printManager(manager string) string {
  61. encodedManager := &metav1.ManagedFieldsEntry{}
  62. if err := json.Unmarshal([]byte(manager), encodedManager); err != nil {
  63. return fmt.Sprintf("%q", manager)
  64. }
  65. managerStr := fmt.Sprintf("%q", encodedManager.Manager)
  66. if encodedManager.Subresource != "" {
  67. managerStr = fmt.Sprintf("%s with subresource %q", managerStr, encodedManager.Subresource)
  68. }
  69. if encodedManager.Operation == metav1.ManagedFieldsOperationUpdate {
  70. if encodedManager.Time == nil {
  71. return fmt.Sprintf("%s using %v", managerStr, encodedManager.APIVersion)
  72. }
  73. return fmt.Sprintf("%s using %v at %v", managerStr, encodedManager.APIVersion, encodedManager.Time.UTC().Format(time.RFC3339))
  74. }
  75. return managerStr
  76. }