|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- /*
-
- Copyright (c) [2023] [pcm]
- [pcm-coordinator] is licensed under Mulan PSL v2.
- You can use this software according to the terms and conditions of the Mulan PSL v2.
- You may obtain a copy of Mulan PSL v2 at:
- http://license.coscl.org.cn/MulanPSL2
- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- See the Mulan PSL v2 for more details.
-
- */
-
- package tracker
-
- import (
- "fmt"
- "strings"
- "time"
- )
-
- type Level int
-
- const (
- LevelCluster = 1 << iota
- LevelNode
- LevelWorkspace
- LevelNamespace
- LevelApplication
- LevelOpenpitrix
- LevelController
- LevelService
- LevelPod
- LevelContainer
- LevelPVC
- LevelComponent
- LevelIngress
- )
-
- var MeteringLevelMap = map[string]int{
- "LevelCluster": LevelCluster,
- "LevelNode": LevelNode,
- "LevelWorkspace": LevelWorkspace,
- "LevelNamespace": LevelNamespace,
- "LevelApplication": LevelApplication,
- "LevelController": LevelController,
- "LevelService": LevelService,
- "LevelPod": LevelPod,
- "LevelContainer": LevelContainer,
- "LevelPVC": LevelPVC,
- "LevelComponent": LevelComponent,
- }
-
- type QueryOption interface {
- Apply(*QueryOptions)
- }
-
- type Meteroptions struct {
- Start time.Time
- End time.Time
- Step time.Duration
- }
-
- type QueryOptions struct {
- Level Level
-
- NamespacedResourcesFilter string
- QueryType string
- ResourceFilter string
- NodeName string
- WorkspaceName string
- Namespace string
- WorkloadKind string
- WorkloadName string
- OwnerName string
- PodName string
- PodsName string
- ContainerName string
- StorageClassName string
- PersistentVolumeClaimName string
- PVCFilter string
- ApplicationName string
- ServiceName string
- Ingress string
- Job string
- Duration *time.Duration
- MeterOptions *Meteroptions
- }
-
- func NewQueryOptions() *QueryOptions {
- return &QueryOptions{}
- }
-
- type ClusterOption struct{}
-
- func (_ ClusterOption) Apply(o *QueryOptions) {
- o.Level = LevelCluster
- }
-
- type NodeOption struct {
- ResourceFilter string
- NodeName string
- PVCFilter string
- StorageClassName string
- QueryType string
- }
-
- func (no NodeOption) Apply(o *QueryOptions) {
- o.Level = LevelNode
- o.ResourceFilter = no.ResourceFilter
- o.NodeName = no.NodeName
- o.PVCFilter = no.PVCFilter
- o.StorageClassName = no.StorageClassName
- o.QueryType = no.QueryType
- }
-
- type WorkspaceOption struct {
- ResourceFilter string
- WorkspaceName string
- PVCFilter string
- StorageClassName string
- }
-
- func (wo WorkspaceOption) Apply(o *QueryOptions) {
- o.Level = LevelWorkspace
- o.ResourceFilter = wo.ResourceFilter
- o.WorkspaceName = wo.WorkspaceName
- o.PVCFilter = wo.PVCFilter
- o.StorageClassName = wo.StorageClassName
- }
-
- type NamespaceOption struct {
- ResourceFilter string
- WorkspaceName string
- NamespaceName string
- PVCFilter string
- StorageClassName string
- }
-
- func (no NamespaceOption) Apply(o *QueryOptions) {
- o.Level = LevelNamespace
- o.ResourceFilter = no.ResourceFilter
- o.WorkspaceName = no.WorkspaceName
- o.Namespace = no.NamespaceName
- o.PVCFilter = no.PVCFilter
- o.StorageClassName = no.StorageClassName
- }
-
- type ApplicationsOption struct {
- NamespaceName string
- Applications []string
- StorageClassName string
- }
-
- func (aso ApplicationsOption) Apply(o *QueryOptions) {
- // nothing should be done
- //nolint:gosimple
- return
- }
-
- type OpenpitrixsOption struct {
- Cluster string
- NamespaceName string
- Openpitrixs []string
- StorageClassName string
- }
-
- func (oso OpenpitrixsOption) Apply(o *QueryOptions) {
- // nothing should be done
- //nolint:gosimple
- return
- }
-
- // ApplicationsOption & OpenpitrixsOption share the same ApplicationOption struct
- type ApplicationOption struct {
- NamespaceName string
- Application string
- ApplicationComponents []string
- StorageClassName string
- }
-
- func (ao ApplicationOption) Apply(o *QueryOptions) {
- o.Level = LevelApplication
- o.Namespace = ao.NamespaceName
- o.ApplicationName = ao.Application
- o.StorageClassName = ao.StorageClassName
-
- app_components := strings.Join(ao.ApplicationComponents[:], "|")
-
- if len(app_components) > 0 {
- o.ResourceFilter = fmt.Sprintf(`namespace="%s", workload=~"%s"`, o.Namespace, app_components)
- } else {
- o.ResourceFilter = fmt.Sprintf(`namespace="%s", workload=~"%s"`, o.Namespace, ".*")
- }
- }
-
- type WorkloadOption struct {
- ResourceFilter string
- NamespaceName string
- WorkloadKind string
- }
-
- func (wo WorkloadOption) Apply(o *QueryOptions) {
- o.Level = LevelController
- o.ResourceFilter = wo.ResourceFilter
- o.Namespace = wo.NamespaceName
- o.WorkloadKind = wo.WorkloadKind
- }
-
- type ServicesOption struct {
- NamespaceName string
- Services []string
- }
-
- func (sso ServicesOption) Apply(o *QueryOptions) {
- // nothing should be done
- //nolint:gosimple
- return
- }
-
- type ServiceOption struct {
- ResourceFilter string
- NamespaceName string
- ServiceName string
- PodNames []string
- }
-
- func (so ServiceOption) Apply(o *QueryOptions) {
- o.Level = LevelService
- o.Namespace = so.NamespaceName
- o.ServiceName = so.ServiceName
-
- pod_names := strings.Join(so.PodNames, "|")
-
- if len(pod_names) > 0 {
- o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, pod_names, o.Namespace)
- } else {
- o.ResourceFilter = fmt.Sprintf(`pod=~"%s", namespace="%s"`, ".*", o.Namespace)
- }
- }
-
- type PodOption struct {
- NamespacedResourcesFilter string
- ResourceFilter string
- NodeName string
- NamespaceName string
- WorkloadKind string
- WorkloadName string
- PodName string
- }
-
- type ControllerOption struct {
- Namespace string
- Kind string
- WorkloadName string
- PodsName string
- Level string
- }
-
- func (po PodOption) Apply(o *QueryOptions) {
- o.Level = LevelPod
- o.NamespacedResourcesFilter = po.NamespacedResourcesFilter
- o.ResourceFilter = po.ResourceFilter
- o.NodeName = po.NodeName
- o.Namespace = po.NamespaceName
- o.WorkloadKind = po.WorkloadKind
- o.OwnerName = po.WorkloadName
- o.PodName = po.PodName
- }
-
- func (co ControllerOption) Apply(o *QueryOptions) {
- o.Level = LevelController
- o.Namespace = co.Namespace
- o.WorkloadKind = co.Kind
- o.WorkloadName = co.WorkloadName
-
- }
-
- type ContainerOption struct {
- ResourceFilter string
- NamespaceName string
- PodName string
- ContainerName string
- }
-
- func (co ContainerOption) Apply(o *QueryOptions) {
- o.Level = LevelContainer
- o.ResourceFilter = co.ResourceFilter
- o.Namespace = co.NamespaceName
- o.PodName = co.PodName
- o.ContainerName = co.ContainerName
- }
-
- type PVCOption struct {
- ResourceFilter string
- NamespaceName string
- StorageClassName string
- PersistentVolumeClaimName string
- }
-
- func (po PVCOption) Apply(o *QueryOptions) {
- o.Level = LevelPVC
- o.ResourceFilter = po.ResourceFilter
- o.Namespace = po.NamespaceName
- o.StorageClassName = po.StorageClassName
- o.PersistentVolumeClaimName = po.PersistentVolumeClaimName
-
- // for meter
- o.PVCFilter = po.PersistentVolumeClaimName
- }
-
- type IngressOption struct {
- ResourceFilter string
- NamespaceName string
- Ingress string
- Job string
- Pod string
- Duration *time.Duration
- }
-
- func (no IngressOption) Apply(o *QueryOptions) {
- o.Level = LevelIngress
- o.ResourceFilter = no.ResourceFilter
- o.Namespace = no.NamespaceName
- o.Ingress = no.Ingress
- o.Job = no.Job
- o.PodName = no.Pod
- o.Duration = no.Duration
- }
-
- type ComponentOption struct{}
-
- func (_ ComponentOption) Apply(o *QueryOptions) {
- o.Level = LevelComponent
- }
-
- type MeterOption struct {
- Start time.Time
- End time.Time
- Step time.Duration
- }
-
- func (mo MeterOption) Apply(o *QueryOptions) {
- o.MeterOptions = &Meteroptions{
- Start: mo.Start,
- End: mo.End,
- Step: mo.Step,
- }
- }
|