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

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

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.