labels.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. const (
  18. // SkipReconcileLabelKey is a label indicating that the pod should not be reconciled
  19. SkipReconcileLabelKey = "ceph.rook.io/do-not-reconcile"
  20. )
  21. // LabelsSpec is the main spec label for all daemons
  22. type LabelsSpec map[KeyType]Labels
  23. // KeyType type safety
  24. type KeyType string
  25. // Labels are label for a given daemons
  26. type Labels map[string]string
  27. func (a LabelsSpec) All() Labels {
  28. return a[KeyAll]
  29. }
  30. // GetMgrLabels returns the Labels for the MGR service
  31. func GetMgrLabels(a LabelsSpec) Labels {
  32. return mergeAllLabelsWithKey(a, KeyMgr)
  33. }
  34. // GetMonLabels returns the Labels for the MON service
  35. func GetMonLabels(a LabelsSpec) Labels {
  36. return mergeAllLabelsWithKey(a, KeyMon)
  37. }
  38. // GetKeyRotationLabels returns labels for the key Rotation job
  39. func GetKeyRotationLabels(a LabelsSpec) Labels {
  40. return mergeAllLabelsWithKey(a, KeyRotation)
  41. }
  42. // GetOSDPrepareLabels returns the Labels for the OSD prepare job
  43. func GetOSDPrepareLabels(a LabelsSpec) Labels {
  44. return mergeAllLabelsWithKey(a, KeyOSDPrepare)
  45. }
  46. // GetOSDLabels returns the Labels for the OSD service
  47. func GetOSDLabels(a LabelsSpec) Labels {
  48. return mergeAllLabelsWithKey(a, KeyOSD)
  49. }
  50. // GetCleanupLabels returns the Labels for the cleanup job
  51. func GetCleanupLabels(a LabelsSpec) Labels {
  52. return mergeAllLabelsWithKey(a, KeyCleanup)
  53. }
  54. // GetMonitoringLabels returns the Labels for monitoring resources
  55. func GetMonitoringLabels(a LabelsSpec) Labels {
  56. return mergeAllLabelsWithKey(a, KeyMonitoring)
  57. }
  58. // GetCrashCollectorLabels returns the Labels for the crash collector resources
  59. func GetCrashCollectorLabels(a LabelsSpec) Labels {
  60. return mergeAllLabelsWithKey(a, KeyCrashCollector)
  61. }
  62. func GetCephExporterLabels(a LabelsSpec) Labels {
  63. return mergeAllLabelsWithKey(a, KeyCephExporter)
  64. }
  65. func mergeAllLabelsWithKey(a LabelsSpec, name KeyType) Labels {
  66. all := a.All()
  67. if all != nil {
  68. return all.Merge(a[name])
  69. }
  70. return a[name]
  71. }
  72. // ApplyToObjectMeta adds labels to object meta unless the keys are already defined.
  73. func (a Labels) ApplyToObjectMeta(t *metav1.ObjectMeta) {
  74. if t.Labels == nil {
  75. t.Labels = map[string]string{}
  76. }
  77. for k, v := range a {
  78. if _, ok := t.Labels[k]; !ok {
  79. t.Labels[k] = v
  80. }
  81. }
  82. }
  83. // OverwriteApplyToObjectMeta adds labels to object meta, overwriting keys that are already defined.
  84. func (a Labels) OverwriteApplyToObjectMeta(t *metav1.ObjectMeta) {
  85. if t.Labels == nil {
  86. t.Labels = map[string]string{}
  87. }
  88. for k, v := range a {
  89. t.Labels[k] = v
  90. }
  91. }
  92. // Merge returns a Labels which results from merging the attributes of the
  93. // original Labels with the attributes of the supplied one. The supplied
  94. // Labels attributes will override the original ones if defined.
  95. func (a Labels) Merge(with Labels) Labels {
  96. ret := Labels{}
  97. for k, v := range a {
  98. if _, ok := ret[k]; !ok {
  99. ret[k] = v
  100. }
  101. }
  102. for k, v := range with {
  103. if _, ok := ret[k]; !ok {
  104. ret[k] = v
  105. }
  106. }
  107. return ret
  108. }