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 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. LevelOpenpitrix
  26. LevelWorkload
  27. LevelService
  28. LevelPod
  29. LevelContainer
  30. LevelPVC
  31. LevelComponent
  32. LevelIngress
  33. )
  34. var MeteringLevelMap = map[string]int{
  35. "LevelCluster": LevelCluster,
  36. "LevelNode": LevelNode,
  37. "LevelWorkspace": LevelWorkspace,
  38. "LevelNamespace": LevelNamespace,
  39. "LevelApplication": LevelApplication,
  40. "LevelWorkload": LevelWorkload,
  41. "LevelService": LevelService,
  42. "LevelPod": LevelPod,
  43. "LevelContainer": LevelContainer,
  44. "LevelPVC": LevelPVC,
  45. "LevelComponent": LevelComponent,
  46. }
  47. type QueryOption interface {
  48. Apply(*QueryOptions)
  49. }
  50. type Meteroptions struct {
  51. Start time.Time
  52. End time.Time
  53. Step time.Duration
  54. }
  55. type QueryOptions struct {
  56. Level Level
  57. NamespacedResourcesFilter string
  58. QueryType string
  59. ResourceFilter string
  60. NodeName string
  61. WorkspaceName string
  62. NamespaceName string
  63. WorkloadKind string
  64. WorkloadName string
  65. PodName string
  66. ContainerName string
  67. StorageClassName string
  68. PersistentVolumeClaimName string
  69. PVCFilter string
  70. ApplicationName string
  71. ServiceName string
  72. Ingress string
  73. Job string
  74. Duration *time.Duration
  75. MeterOptions *Meteroptions
  76. }
  77. func NewQueryOptions() *QueryOptions {
  78. return &QueryOptions{}
  79. }
  80. type ClusterOption struct{}
  81. func (_ ClusterOption) Apply(o *QueryOptions) {
  82. o.Level = LevelCluster
  83. }
  84. type NodeOption struct {
  85. ResourceFilter string
  86. NodeName string
  87. PVCFilter string
  88. StorageClassName string
  89. QueryType string
  90. }
  91. func (no NodeOption) Apply(o *QueryOptions) {
  92. o.Level = LevelNode
  93. o.ResourceFilter = no.ResourceFilter
  94. o.NodeName = no.NodeName
  95. o.PVCFilter = no.PVCFilter
  96. o.StorageClassName = no.StorageClassName
  97. o.QueryType = no.QueryType
  98. }
  99. type WorkspaceOption struct {
  100. ResourceFilter string
  101. WorkspaceName string
  102. PVCFilter string
  103. StorageClassName string
  104. }
  105. func (wo WorkspaceOption) Apply(o *QueryOptions) {
  106. o.Level = LevelWorkspace
  107. o.ResourceFilter = wo.ResourceFilter
  108. o.WorkspaceName = wo.WorkspaceName
  109. o.PVCFilter = wo.PVCFilter
  110. o.StorageClassName = wo.StorageClassName
  111. }
  112. type NamespaceOption struct {
  113. ResourceFilter string
  114. WorkspaceName string
  115. NamespaceName string
  116. PVCFilter string
  117. StorageClassName string
  118. }
  119. func (no NamespaceOption) Apply(o *QueryOptions) {
  120. o.Level = LevelNamespace
  121. o.ResourceFilter = no.ResourceFilter
  122. o.WorkspaceName = no.WorkspaceName
  123. o.NamespaceName = no.NamespaceName
  124. o.PVCFilter = no.PVCFilter
  125. o.StorageClassName = no.StorageClassName
  126. }
  127. type ApplicationsOption struct {
  128. NamespaceName string
  129. Applications []string
  130. StorageClassName string
  131. }
  132. func (aso ApplicationsOption) Apply(o *QueryOptions) {
  133. // nothing should be done
  134. //nolint:gosimple
  135. return
  136. }
  137. type OpenpitrixsOption struct {
  138. Cluster string
  139. NamespaceName string
  140. Openpitrixs []string
  141. StorageClassName string
  142. }
  143. func (oso OpenpitrixsOption) Apply(o *QueryOptions) {
  144. // nothing should be done
  145. //nolint:gosimple
  146. return
  147. }
  148. // ApplicationsOption & OpenpitrixsOption share the same ApplicationOption struct
  149. type ApplicationOption struct {
  150. NamespaceName string
  151. Application string
  152. ApplicationComponents []string
  153. StorageClassName string
  154. }
  155. func (ao ApplicationOption) Apply(o *QueryOptions) {
  156. o.Level = LevelApplication
  157. o.NamespaceName = ao.NamespaceName
  158. o.ApplicationName = ao.Application
  159. o.StorageClassName = ao.StorageClassName
  160. app_components := strings.Join(ao.ApplicationComponents[:], "|")
  161. if len(app_components) > 0 {
  162. o.ResourceFilter = fmt.Sprintf(`namespace="%s", workload=~"%s"`, o.NamespaceName, app_components)
  163. } else {
  164. o.ResourceFilter = fmt.Sprintf(`namespace="%s", workload=~"%s"`, o.NamespaceName, ".*")
  165. }
  166. }
  167. type WorkloadOption struct {
  168. ResourceFilter string
  169. NamespaceName string
  170. WorkloadKind string
  171. }
  172. func (wo WorkloadOption) Apply(o *QueryOptions) {
  173. o.Level = LevelWorkload
  174. o.ResourceFilter = wo.ResourceFilter
  175. o.NamespaceName = wo.NamespaceName
  176. o.WorkloadKind = wo.WorkloadKind
  177. }
  178. type ServicesOption struct {
  179. NamespaceName string
  180. Services []string
  181. }
  182. func (sso ServicesOption) Apply(o *QueryOptions) {
  183. // nothing should be done
  184. //nolint:gosimple
  185. return
  186. }
  187. type ServiceOption struct {
  188. ResourceFilter string
  189. NamespaceName string
  190. ServiceName string
  191. PodNames []string
  192. }
  193. func (so ServiceOption) Apply(o *QueryOptions) {
  194. o.Level = LevelService
  195. o.NamespaceName = so.NamespaceName
  196. o.ServiceName = so.ServiceName
  197. pod_names := strings.Join(so.PodNames, "|")
  198. if len(pod_names) > 0 {
  199. o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, pod_names, o.NamespaceName)
  200. } else {
  201. o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, ".*", o.NamespaceName)
  202. }
  203. }
  204. type PodOption struct {
  205. NamespacedResourcesFilter string
  206. ResourceFilter string
  207. NodeName string
  208. NamespaceName string
  209. WorkloadKind string
  210. WorkloadName string
  211. PodName string
  212. }
  213. func (po PodOption) Apply(o *QueryOptions) {
  214. o.Level = LevelPod
  215. o.NamespacedResourcesFilter = po.NamespacedResourcesFilter
  216. o.ResourceFilter = po.ResourceFilter
  217. o.NodeName = po.NodeName
  218. o.NamespaceName = po.NamespaceName
  219. o.WorkloadKind = po.WorkloadKind
  220. o.WorkloadName = po.WorkloadName
  221. o.PodName = po.PodName
  222. }
  223. type ContainerOption struct {
  224. ResourceFilter string
  225. NamespaceName string
  226. PodName string
  227. ContainerName string
  228. }
  229. func (co ContainerOption) Apply(o *QueryOptions) {
  230. o.Level = LevelContainer
  231. o.ResourceFilter = co.ResourceFilter
  232. o.NamespaceName = co.NamespaceName
  233. o.PodName = co.PodName
  234. o.ContainerName = co.ContainerName
  235. }
  236. type PVCOption struct {
  237. ResourceFilter string
  238. NamespaceName string
  239. StorageClassName string
  240. PersistentVolumeClaimName string
  241. }
  242. func (po PVCOption) Apply(o *QueryOptions) {
  243. o.Level = LevelPVC
  244. o.ResourceFilter = po.ResourceFilter
  245. o.NamespaceName = po.NamespaceName
  246. o.StorageClassName = po.StorageClassName
  247. o.PersistentVolumeClaimName = po.PersistentVolumeClaimName
  248. // for meter
  249. o.PVCFilter = po.PersistentVolumeClaimName
  250. }
  251. type IngressOption struct {
  252. ResourceFilter string
  253. NamespaceName string
  254. Ingress string
  255. Job string
  256. Pod string
  257. Duration *time.Duration
  258. }
  259. func (no IngressOption) Apply(o *QueryOptions) {
  260. o.Level = LevelIngress
  261. o.ResourceFilter = no.ResourceFilter
  262. o.NamespaceName = no.NamespaceName
  263. o.Ingress = no.Ingress
  264. o.Job = no.Job
  265. o.PodName = no.Pod
  266. o.Duration = no.Duration
  267. }
  268. type ComponentOption struct{}
  269. func (_ ComponentOption) Apply(o *QueryOptions) {
  270. o.Level = LevelComponent
  271. }
  272. type MeterOption struct {
  273. Start time.Time
  274. End time.Time
  275. Step time.Duration
  276. }
  277. func (mo MeterOption) Apply(o *QueryOptions) {
  278. o.MeterOptions = &Meteroptions{
  279. Start: mo.Start,
  280. End: mo.End,
  281. Step: mo.Step,
  282. }
  283. }

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.