strategic_visitor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2017 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 strategy
  14. import (
  15. "k8s.io/kubernetes/pkg/kubectl/apply"
  16. )
  17. // delegatingStrategy delegates merging fields to other visitor implementations
  18. // based on the merge strategy preferred by the field.
  19. type delegatingStrategy struct {
  20. options Options
  21. merge mergeStrategy
  22. replace replaceStrategy
  23. retainKeys retainKeysStrategy
  24. }
  25. // createDelegatingStrategy returns a new delegatingStrategy
  26. func createDelegatingStrategy(options Options) *delegatingStrategy {
  27. v := &delegatingStrategy{
  28. options: options,
  29. }
  30. v.replace = createReplaceStrategy(options, v)
  31. v.merge = createMergeStrategy(options, v)
  32. v.retainKeys = createRetainKeysStrategy(options, v)
  33. return v
  34. }
  35. // MergeList delegates visiting a list based on the field patch strategy.
  36. // Defaults to "replace"
  37. func (v delegatingStrategy) MergeList(diff apply.ListElement) (apply.Result, error) {
  38. switch diff.GetFieldMergeType() {
  39. case apply.MergeStrategy:
  40. return v.merge.MergeList(diff)
  41. case apply.ReplaceStrategy:
  42. return v.replace.MergeList(diff)
  43. case apply.RetainKeysStrategy:
  44. return v.retainKeys.MergeList(diff)
  45. default:
  46. return v.replace.MergeList(diff)
  47. }
  48. }
  49. // MergeMap delegates visiting a map based on the field patch strategy.
  50. // Defaults to "merge"
  51. func (v delegatingStrategy) MergeMap(diff apply.MapElement) (apply.Result, error) {
  52. switch diff.GetFieldMergeType() {
  53. case apply.MergeStrategy:
  54. return v.merge.MergeMap(diff)
  55. case apply.ReplaceStrategy:
  56. return v.replace.MergeMap(diff)
  57. case apply.RetainKeysStrategy:
  58. return v.retainKeys.MergeMap(diff)
  59. default:
  60. return v.merge.MergeMap(diff)
  61. }
  62. }
  63. // MergeType delegates visiting a map based on the field patch strategy.
  64. // Defaults to "merge"
  65. func (v delegatingStrategy) MergeType(diff apply.TypeElement) (apply.Result, error) {
  66. switch diff.GetFieldMergeType() {
  67. case apply.MergeStrategy:
  68. return v.merge.MergeType(diff)
  69. case apply.ReplaceStrategy:
  70. return v.replace.MergeType(diff)
  71. case apply.RetainKeysStrategy:
  72. return v.retainKeys.MergeType(diff)
  73. default:
  74. return v.merge.MergeType(diff)
  75. }
  76. }
  77. // MergePrimitive delegates visiting a primitive to the ReplaceVisitorSingleton.
  78. func (v delegatingStrategy) MergePrimitive(diff apply.PrimitiveElement) (apply.Result, error) {
  79. // Always replace primitives
  80. return v.replace.MergePrimitive(diff)
  81. }
  82. // MergeEmpty
  83. func (v delegatingStrategy) MergeEmpty(diff apply.EmptyElement) (apply.Result, error) {
  84. return v.merge.MergeEmpty(diff)
  85. }
  86. // doConflictDetect detects conflicts in element when option enabled, return error if conflict happened.
  87. func (v delegatingStrategy) doConflictDetect(e apply.Element) error {
  88. if v.options.FailOnConflict {
  89. if e, ok := e.(apply.ConflictDetector); ok {
  90. return e.HasConflict()
  91. }
  92. }
  93. return nil
  94. }
  95. var _ apply.Strategy = &delegatingStrategy{}