You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

queryoptions.go 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package tracker
  13. import (
  14. "fmt"
  15. "strings"
  16. "time"
  17. )
  18. type Level int
  19. const (
  20. LevelCluster = 1 << iota
  21. LevelNode
  22. LevelWorkspace
  23. LevelNamespace
  24. LevelApplication
  25. LevelController
  26. LevelPod
  27. LevelContainer
  28. LevelPVC
  29. LevelComponent
  30. LevelAdapter
  31. )
  32. var MeteringLevelMap = map[string]int{
  33. "LevelAdapter": LevelAdapter,
  34. "LevelCluster": LevelCluster,
  35. "LevelNode": LevelNode,
  36. "LevelWorkspace": LevelWorkspace,
  37. "LevelNamespace": LevelNamespace,
  38. "LevelApplication": LevelApplication,
  39. "LevelController": LevelController,
  40. "LevelPod": LevelPod,
  41. "LevelContainer": LevelContainer,
  42. "LevelPVC": LevelPVC,
  43. "LevelComponent": LevelComponent,
  44. }
  45. type QueryOption interface {
  46. Apply(*QueryOptions)
  47. }
  48. type MeterOptions struct {
  49. Start time.Time
  50. End time.Time
  51. Step time.Duration
  52. }
  53. type QueryOptions struct {
  54. Level Level
  55. ClustersName string
  56. NamespacedResourcesFilter string
  57. QueryType string
  58. ResourceFilter string
  59. ClusterName string
  60. NodeName string
  61. WorkspaceName string
  62. Namespace string
  63. WorkloadKind string
  64. WorkloadName string
  65. OwnerName string
  66. PodName string
  67. PodsName string
  68. ContainerName string
  69. AdapterId int64
  70. ServiceName string
  71. Job string
  72. Duration *time.Duration
  73. MeterOptions *MeterOptions
  74. }
  75. func NewQueryOptions() *QueryOptions {
  76. return &QueryOptions{}
  77. }
  78. type AdapterOption struct {
  79. AdapterId int64
  80. ClustersName string
  81. }
  82. func (a AdapterOption) Apply(o *QueryOptions) {
  83. o.Level = LevelAdapter
  84. o.AdapterId = a.AdapterId
  85. o.ClustersName = a.ClustersName
  86. }
  87. type ClusterOption struct {
  88. AdapterId int64
  89. ClusterName string
  90. }
  91. func (c ClusterOption) Apply(o *QueryOptions) {
  92. o.Level = LevelCluster
  93. o.AdapterId = c.AdapterId
  94. o.ClusterName = c.ClusterName
  95. }
  96. type NodeOption struct {
  97. ResourceFilter string
  98. NodeName string
  99. PVCFilter string
  100. StorageClassName string
  101. QueryType string
  102. }
  103. func (no NodeOption) Apply(o *QueryOptions) {
  104. o.Level = LevelNode
  105. o.ResourceFilter = no.ResourceFilter
  106. o.NodeName = no.NodeName
  107. o.QueryType = no.QueryType
  108. }
  109. type WorkspaceOption struct {
  110. ResourceFilter string
  111. WorkspaceName string
  112. PVCFilter string
  113. StorageClassName string
  114. }
  115. func (wo WorkspaceOption) Apply(o *QueryOptions) {
  116. o.Level = LevelWorkspace
  117. o.ResourceFilter = wo.ResourceFilter
  118. o.WorkspaceName = wo.WorkspaceName
  119. }
  120. type NamespaceOption struct {
  121. ResourceFilter string
  122. WorkspaceName string
  123. NamespaceName string
  124. PVCFilter string
  125. StorageClassName string
  126. }
  127. func (no NamespaceOption) Apply(o *QueryOptions) {
  128. o.Level = LevelNamespace
  129. o.ResourceFilter = no.ResourceFilter
  130. o.WorkspaceName = no.WorkspaceName
  131. o.Namespace = no.NamespaceName
  132. }
  133. // ApplicationsOption & OpenpitrixsOption share the same ApplicationOption struct
  134. type ApplicationOption struct {
  135. NamespaceName string
  136. Application string
  137. ApplicationComponents []string
  138. StorageClassName string
  139. }
  140. func (ao ApplicationOption) Apply(o *QueryOptions) {
  141. o.Level = LevelApplication
  142. o.Namespace = ao.NamespaceName
  143. app_components := strings.Join(ao.ApplicationComponents[:], "|")
  144. if len(app_components) > 0 {
  145. o.ResourceFilter = fmt.Sprintf(`namespace="%s", workload=~"%s"`, o.Namespace, app_components)
  146. } else {
  147. o.ResourceFilter = fmt.Sprintf(`namespace="%s", workload=~"%s"`, o.Namespace, ".*")
  148. }
  149. }
  150. type WorkloadOption struct {
  151. ResourceFilter string
  152. NamespaceName string
  153. WorkloadKind string
  154. }
  155. func (wo WorkloadOption) Apply(o *QueryOptions) {
  156. o.Level = LevelController
  157. o.ResourceFilter = wo.ResourceFilter
  158. o.Namespace = wo.NamespaceName
  159. o.WorkloadKind = wo.WorkloadKind
  160. }
  161. type ServiceOption struct {
  162. ResourceFilter string
  163. NamespaceName string
  164. ServiceName string
  165. PodNames []string
  166. }
  167. type PodOption struct {
  168. NamespacedResourcesFilter string
  169. ResourceFilter string
  170. NodeName string
  171. NamespaceName string
  172. WorkloadKind string
  173. WorkloadName string
  174. PodName string
  175. }
  176. type ControllerOption struct {
  177. Namespace string
  178. Kind string
  179. WorkloadName string
  180. PodsName string
  181. Level string
  182. }
  183. func (po PodOption) Apply(o *QueryOptions) {
  184. o.Level = LevelPod
  185. o.NamespacedResourcesFilter = po.NamespacedResourcesFilter
  186. o.ResourceFilter = po.ResourceFilter
  187. o.NodeName = po.NodeName
  188. o.Namespace = po.NamespaceName
  189. o.WorkloadKind = po.WorkloadKind
  190. o.OwnerName = po.WorkloadName
  191. o.PodName = po.PodName
  192. }
  193. func (co ControllerOption) Apply(o *QueryOptions) {
  194. o.Level = LevelController
  195. o.Namespace = co.Namespace
  196. o.WorkloadKind = co.Kind
  197. o.WorkloadName = co.WorkloadName
  198. }
  199. type ContainerOption struct {
  200. ResourceFilter string
  201. NamespaceName string
  202. PodName string
  203. ContainerName string
  204. }
  205. func (co ContainerOption) Apply(o *QueryOptions) {
  206. o.Level = LevelContainer
  207. o.ResourceFilter = co.ResourceFilter
  208. o.Namespace = co.NamespaceName
  209. o.PodName = co.PodName
  210. o.ContainerName = co.ContainerName
  211. }
  212. type PVCOption struct {
  213. ResourceFilter string
  214. NamespaceName string
  215. StorageClassName string
  216. PersistentVolumeClaimName string
  217. }
  218. func (po PVCOption) Apply(o *QueryOptions) {
  219. o.Level = LevelPVC
  220. o.ResourceFilter = po.ResourceFilter
  221. o.Namespace = po.NamespaceName
  222. }
  223. type ComponentOption struct{}
  224. func (_ ComponentOption) Apply(o *QueryOptions) {
  225. o.Level = LevelComponent
  226. }
  227. type MeterOption struct {
  228. Start time.Time
  229. End time.Time
  230. Step time.Duration
  231. }
  232. func (mo MeterOption) Apply(o *QueryOptions) {
  233. o.MeterOptions = &MeterOptions{
  234. Start: mo.Start,
  235. End: mo.End,
  236. Step: mo.Step,
  237. }
  238. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.