annotations.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2019 The Rook Authors. All rights reserved.
  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 v1
  14. import (
  15. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  16. )
  17. // AnnotationsSpec is the main spec annotation for all daemons
  18. // +kubebuilder:pruning:PreserveUnknownFields
  19. // +nullable
  20. type AnnotationsSpec map[KeyType]Annotations
  21. // Annotations are annotations
  22. type Annotations map[string]string
  23. func (a AnnotationsSpec) All() Annotations {
  24. return a[KeyAll]
  25. }
  26. // GetMgrAnnotations returns the Annotations for the MGR service
  27. func GetMgrAnnotations(a AnnotationsSpec) Annotations {
  28. return mergeAllAnnotationsWithKey(a, KeyMgr)
  29. }
  30. // GetMonAnnotations returns the Annotations for the MON service
  31. func GetMonAnnotations(a AnnotationsSpec) Annotations {
  32. return mergeAllAnnotationsWithKey(a, KeyMon)
  33. }
  34. // GetKeyRotationAnnotations returns the annotations for the key rotation job
  35. func GetKeyRotationAnnotations(a AnnotationsSpec) Annotations {
  36. return mergeAllAnnotationsWithKey(a, KeyRotation)
  37. }
  38. // GetOSDPrepareAnnotations returns the annotations for the OSD service
  39. func GetOSDPrepareAnnotations(a AnnotationsSpec) Annotations {
  40. return mergeAllAnnotationsWithKey(a, KeyOSDPrepare)
  41. }
  42. // GetOSDAnnotations returns the annotations for the OSD service
  43. func GetOSDAnnotations(a AnnotationsSpec) Annotations {
  44. return mergeAllAnnotationsWithKey(a, KeyOSD)
  45. }
  46. // GetCleanupAnnotations returns the Annotations for the cleanup job
  47. func GetCleanupAnnotations(a AnnotationsSpec) Annotations {
  48. return mergeAllAnnotationsWithKey(a, KeyCleanup)
  49. }
  50. // GetCephExporterAnnotations returns the Annotations for the MGR service
  51. func GetCephExporterAnnotations(a AnnotationsSpec) Annotations {
  52. return mergeAllAnnotationsWithKey(a, KeyCephExporter)
  53. }
  54. func GetClusterMetadataAnnotations(a AnnotationsSpec) Annotations {
  55. return a[KeyClusterMetadata]
  56. }
  57. func mergeAllAnnotationsWithKey(a AnnotationsSpec, name KeyType) Annotations {
  58. all := a.All()
  59. if all != nil {
  60. return all.Merge(a[name])
  61. }
  62. return a[name]
  63. }
  64. // ApplyToObjectMeta adds annotations to object meta unless the keys are already defined.
  65. func (a Annotations) ApplyToObjectMeta(t *metav1.ObjectMeta) {
  66. if t.Annotations == nil {
  67. t.Annotations = map[string]string{}
  68. }
  69. for k, v := range a {
  70. if _, ok := t.Annotations[k]; !ok {
  71. t.Annotations[k] = v
  72. }
  73. }
  74. }
  75. // Merge returns an Annotations which results from merging the attributes of the
  76. // original Annotations with the attributes of the supplied one. The supplied
  77. // Annotation attributes will override the original ones if defined.
  78. func (a Annotations) Merge(with map[string]string) Annotations {
  79. ret := a
  80. if ret == nil {
  81. ret = map[string]string{}
  82. }
  83. for k, v := range with {
  84. if _, ok := ret[k]; !ok {
  85. ret[k] = v
  86. }
  87. }
  88. return ret
  89. }