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

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.