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.6 kB

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

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.