retain_keys_visitor.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "fmt"
  16. "k8s.io/kubernetes/pkg/kubectl/apply"
  17. )
  18. func createRetainKeysStrategy(options Options, strategic *delegatingStrategy) retainKeysStrategy {
  19. return retainKeysStrategy{
  20. &mergeStrategy{strategic, options},
  21. strategic,
  22. options,
  23. }
  24. }
  25. // retainKeysStrategy merges the values in an Element into a single Result,
  26. // dropping any fields omitted from the local copy. (but merging values when
  27. // defined locally and remotely)
  28. type retainKeysStrategy struct {
  29. merge *mergeStrategy
  30. strategic *delegatingStrategy
  31. options Options
  32. }
  33. // MergeMap merges the type instances in a TypeElement into a single Result
  34. // keeping only the fields defined locally, but merging their values with
  35. // the remote values.
  36. func (v retainKeysStrategy) MergeType(e apply.TypeElement) (apply.Result, error) {
  37. // No merge logic if adding or deleting a field
  38. if result, done := v.merge.doAddOrDelete(&e); done {
  39. return result, nil
  40. }
  41. elem := map[string]apply.Element{}
  42. for key := range e.GetLocalMap() {
  43. elem[key] = e.GetValues()[key]
  44. }
  45. return v.merge.doMergeMap(elem)
  46. }
  47. // MergeMap returns an error. Only TypeElements can have retainKeys.
  48. func (v retainKeysStrategy) MergeMap(e apply.MapElement) (apply.Result, error) {
  49. return apply.Result{}, fmt.Errorf("Cannot use retainkeys with map element %v", e.Name)
  50. }
  51. // MergeList returns an error. Only TypeElements can have retainKeys.
  52. func (v retainKeysStrategy) MergeList(e apply.ListElement) (apply.Result, error) {
  53. return apply.Result{}, fmt.Errorf("Cannot use retainkeys with list element %v", e.Name)
  54. }
  55. // MergePrimitive returns an error. Only TypeElements can have retainKeys.
  56. func (v retainKeysStrategy) MergePrimitive(diff apply.PrimitiveElement) (apply.Result, error) {
  57. return apply.Result{}, fmt.Errorf("Cannot use retainkeys with primitive element %v", diff.Name)
  58. }
  59. // MergeEmpty returns an empty result
  60. func (v retainKeysStrategy) MergeEmpty(diff apply.EmptyElement) (apply.Result, error) {
  61. return v.merge.MergeEmpty(diff)
  62. }
  63. var _ apply.Strategy = &retainKeysStrategy{}