monitoring.go 18.2 KB
Newer Older
G
Guangzhe Huang 已提交
1
/*
H
hongming 已提交
2
Copyright 2019 The KubeSphere Authors.
G
Guangzhe Huang 已提交
3

H
hongming 已提交
4 5 6
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
G
Guangzhe Huang 已提交
7

H
hongming 已提交
8
    http://www.apache.org/licenses/LICENSE-2.0
G
Guangzhe Huang 已提交
9

H
hongming 已提交
10 11 12 13 14
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
G
Guangzhe Huang 已提交
15 16 17 18 19
*/

package monitoring

import (
H
hongming 已提交
20
	"context"
Y
yunkunrao 已提交
21 22 23
	"fmt"
	"math"
	"strings"
R
Roland.Ma 已提交
24 25
	"time"

H
huanggze 已提交
26 27
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
R
Roland.Ma 已提交
28
	"k8s.io/apimachinery/pkg/selection"
H
huanggze 已提交
29
	"k8s.io/client-go/kubernetes"
H
huanggze 已提交
30
	"k8s.io/klog"
R
Roland.Ma 已提交
31
	"kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
Y
yunkunrao 已提交
32
	"kubesphere.io/kubesphere/pkg/apiserver/query"
H
huanggze 已提交
33 34 35
	ksinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
H
huanggze 已提交
36
	"kubesphere.io/kubesphere/pkg/models/monitoring/expressions"
H
huanggze 已提交
37
	"kubesphere.io/kubesphere/pkg/models/openpitrix"
Y
yunkunrao 已提交
38 39
	resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
	"kubesphere.io/kubesphere/pkg/server/errors"
H
huanggze 已提交
40
	"kubesphere.io/kubesphere/pkg/server/params"
G
Guangzhe Huang 已提交
41
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring"
Y
yunkunrao 已提交
42 43
	"sigs.k8s.io/application/api/v1beta1"
	appv1beta1 "sigs.k8s.io/application/api/v1beta1"
G
Guangzhe Huang 已提交
44 45 46
)

type MonitoringOperator interface {
H
huanggze 已提交
47 48
	GetMetric(expr, namespace string, time time.Time) (monitoring.Metric, error)
	GetMetricOverTime(expr, namespace string, start, end time.Time, step time.Duration) (monitoring.Metric, error)
Z
zryfish 已提交
49 50
	GetNamedMetrics(metrics []string, time time.Time, opt monitoring.QueryOption) Metrics
	GetNamedMetricsOverTime(metrics []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) Metrics
H
huanggze 已提交
51
	GetMetadata(namespace string) Metadata
H
huanggze 已提交
52
	GetMetricLabelSet(metric, namespace string, start, end time.Time) MetricLabelSet
H
huanggze 已提交
53

H
huanggze 已提交
54
	// TODO: expose KubeSphere self metrics in Prometheus format
H
huanggze 已提交
55 56
	GetKubeSphereStats() Metrics
	GetWorkspaceStats(workspace string) Metrics
Y
yunkunrao 已提交
57 58 59 60 61 62

	// meter
	GetNamedMetersOverTime(metrics []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) (Metrics, error)
	GetNamedMeters(metrics []string, time time.Time, opt monitoring.QueryOption) (Metrics, error)
	GetAppComponentsMap(ns string, apps []string) map[string][]string
	GetSerivePodsMap(ns string, services []string) map[string][]string
G
Guangzhe Huang 已提交
63 64 65
}

type monitoringOperator struct {
Y
yunkunrao 已提交
66 67 68 69 70 71
	prometheus     monitoring.Interface
	metricsserver  monitoring.Interface
	k8s            kubernetes.Interface
	ks             ksinformers.SharedInformerFactory
	op             openpitrix.Interface
	resourceGetter *resourcev1alpha3.ResourceGetter
G
Guangzhe Huang 已提交
72 73
}

L
LiHui 已提交
74
func NewMonitoringOperator(monitoringClient monitoring.Interface, metricsClient monitoring.Interface, k8s kubernetes.Interface, factory informers.InformerFactory, resourceGetter *resourcev1alpha3.ResourceGetter) MonitoringOperator {
H
huanggze 已提交
75
	return &monitoringOperator{
Y
yunkunrao 已提交
76 77 78 79 80
		prometheus:     monitoringClient,
		metricsserver:  metricsClient,
		k8s:            k8s,
		ks:             factory.KubeSphereSharedInformerFactory(),
		resourceGetter: resourceGetter,
H
huanggze 已提交
81
	}
G
Guangzhe Huang 已提交
82 83
}

H
huanggze 已提交
84
func (mo monitoringOperator) GetMetric(expr, namespace string, time time.Time) (monitoring.Metric, error) {
J
junotx 已提交
85 86 87 88 89 90 91 92 93 94
	if namespace != "" {
		// Different monitoring backend implementations have different ways to enforce namespace isolation.
		// Each implementation should register itself to `ReplaceNamespaceFns` during init().
		// We hard code "prometheus" here because we only support this datasource so far.
		// In the future, maybe the value should be returned from a method like `mo.c.GetMonitoringServiceName()`.
		var err error
		expr, err = expressions.ReplaceNamespaceFns["prometheus"](expr, namespace)
		if err != nil {
			return monitoring.Metric{}, err
		}
H
huanggze 已提交
95
	}
R
root 已提交
96
	return mo.prometheus.GetMetric(expr, time), nil
G
Guangzhe Huang 已提交
97 98
}

H
huanggze 已提交
99
func (mo monitoringOperator) GetMetricOverTime(expr, namespace string, start, end time.Time, step time.Duration) (monitoring.Metric, error) {
J
junotx 已提交
100 101 102 103 104 105 106 107 108 109
	if namespace != "" {
		// Different monitoring backend implementations have different ways to enforce namespace isolation.
		// Each implementation should register itself to `ReplaceNamespaceFns` during init().
		// We hard code "prometheus" here because we only support this datasource so far.
		// In the future, maybe the value should be returned from a method like `mo.c.GetMonitoringServiceName()`.
		var err error
		expr, err = expressions.ReplaceNamespaceFns["prometheus"](expr, namespace)
		if err != nil {
			return monitoring.Metric{}, err
		}
H
huanggze 已提交
110
	}
R
root 已提交
111
	return mo.prometheus.GetMetricOverTime(expr, start, end, step), nil
G
Guangzhe Huang 已提交
112 113
}

Z
zryfish 已提交
114
func (mo monitoringOperator) GetNamedMetrics(metrics []string, time time.Time, opt monitoring.QueryOption) Metrics {
R
root 已提交
115 116
	ress := mo.prometheus.GetNamedMetrics(metrics, time, opt)

117 118 119 120
	if mo.metricsserver != nil {

		//Merge edge node metrics data
		edgeMetrics := make(map[string]monitoring.MetricData)
R
root 已提交
121

122
		for i, ressMetric := range ress {
Z
zhu733756 已提交
123
			metricName := ressMetric.MetricName
124 125 126 127 128 129 130 131 132 133
			ressMetricValues := ressMetric.MetricData.MetricValues
			if len(ressMetricValues) == 0 {
				// this metric has no prometheus metrics data
				if len(edgeMetrics) == 0 {
					// start to request monintoring metricsApi data
					mr := mo.metricsserver.GetNamedMetrics(metrics, time, opt)
					for _, mrMetric := range mr {
						edgeMetrics[mrMetric.MetricName] = mrMetric.MetricData
					}
				}
Z
zhu733756 已提交
134
				if val, ok := edgeMetrics[metricName]; ok {
135 136
					ress[i].MetricData.MetricValues = append(ress[i].MetricData.MetricValues, val.MetricValues...)
				}
137
			}
138

R
root 已提交
139 140 141
		}
	}

Z
zryfish 已提交
142
	return Metrics{Results: ress}
G
Guangzhe Huang 已提交
143 144
}

Z
zryfish 已提交
145
func (mo monitoringOperator) GetNamedMetricsOverTime(metrics []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) Metrics {
R
root 已提交
146 147
	ress := mo.prometheus.GetNamedMetricsOverTime(metrics, start, end, step, opt)

148 149 150 151
	if mo.metricsserver != nil {

		//Merge edge node metrics data
		edgeMetrics := make(map[string]monitoring.MetricData)
R
root 已提交
152

153
		for i, ressMetric := range ress {
Z
zhu733756 已提交
154
			metricName := ressMetric.MetricName
155 156 157 158 159 160 161 162 163 164
			ressMetricValues := ressMetric.MetricData.MetricValues
			if len(ressMetricValues) == 0 {
				// this metric has no prometheus metrics data
				if len(edgeMetrics) == 0 {
					// start to request monintoring metricsApi data
					mr := mo.metricsserver.GetNamedMetricsOverTime(metrics, start, end, step, opt)
					for _, mrMetric := range mr {
						edgeMetrics[mrMetric.MetricName] = mrMetric.MetricData
					}
				}
Z
zhu733756 已提交
165
				if val, ok := edgeMetrics[metricName]; ok {
166 167
					ress[i].MetricData.MetricValues = append(ress[i].MetricData.MetricValues, val.MetricValues...)
				}
168
			}
R
root 已提交
169 170 171
		}
	}

Z
zryfish 已提交
172
	return Metrics{Results: ress}
G
Guangzhe Huang 已提交
173
}
H
huanggze 已提交
174 175

func (mo monitoringOperator) GetMetadata(namespace string) Metadata {
R
root 已提交
176
	data := mo.prometheus.GetMetadata(namespace)
H
huanggze 已提交
177 178
	return Metadata{Data: data}
}
H
huanggze 已提交
179 180

func (mo monitoringOperator) GetMetricLabelSet(metric, namespace string, start, end time.Time) MetricLabelSet {
J
junotx 已提交
181 182 183 184 185 186 187 188 189 190 191 192
	var expr = metric
	var err error
	if namespace != "" {
		// Different monitoring backend implementations have different ways to enforce namespace isolation.
		// Each implementation should register itself to `ReplaceNamespaceFns` during init().
		// We hard code "prometheus" here because we only support this datasource so far.
		// In the future, maybe the value should be returned from a method like `mo.c.GetMonitoringServiceName()`.
		expr, err = expressions.ReplaceNamespaceFns["prometheus"](metric, namespace)
		if err != nil {
			klog.Error(err)
			return MetricLabelSet{}
		}
H
huanggze 已提交
193
	}
R
root 已提交
194
	data := mo.prometheus.GetMetricLabelSet(expr, start, end)
H
huanggze 已提交
195 196
	return MetricLabelSet{Data: data}
}
H
huanggze 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

func (mo monitoringOperator) GetKubeSphereStats() Metrics {
	var res Metrics
	now := float64(time.Now().Unix())

	clusterList, err := mo.ks.Cluster().V1alpha1().Clusters().Lister().List(labels.Everything())
	clusterTotal := len(clusterList)
	if clusterTotal == 0 {
		clusterTotal = 1
	}
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: KubeSphereClusterCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: KubeSphereClusterCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(clusterTotal)},
					},
				},
			},
		})
	}

226
	wkList, err := mo.ks.Tenant().V1alpha2().WorkspaceTemplates().Lister().List(labels.Everything())
H
huanggze 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: KubeSphereWorkspaceCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: KubeSphereWorkspaceCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(len(wkList))},
					},
				},
			},
		})
	}

	usrList, err := mo.ks.Iam().V1alpha2().Users().Lister().List(labels.Everything())
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: KubeSphereUserCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: KubeSphereUserCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(len(usrList))},
					},
				},
			},
		})
	}

266 267 268 269 270 271
	cond := &params.Conditions{
		Match: map[string]string{
			openpitrix.Status: openpitrix.StatusActive,
			openpitrix.RepoId: openpitrix.BuiltinRepoId,
		},
	}
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	if mo.op != nil {
		tmpl, err := mo.op.ListApps(cond, "", false, 0, 0)
		if err != nil {
			res.Results = append(res.Results, monitoring.Metric{
				MetricName: KubeSphereAppTmplCount,
				Error:      err.Error(),
			})
		} else {
			res.Results = append(res.Results, monitoring.Metric{
				MetricName: KubeSphereAppTmplCount,
				MetricData: monitoring.MetricData{
					MetricType: monitoring.MetricTypeVector,
					MetricValues: []monitoring.MetricValue{
						{
							Sample: &monitoring.Point{now, float64(tmpl.TotalCount)},
						},
H
huanggze 已提交
288 289
					},
				},
290 291
			})
		}
H
huanggze 已提交
292 293 294 295 296 297 298 299 300 301 302 303
	}

	return res
}

func (mo monitoringOperator) GetWorkspaceStats(workspace string) Metrics {
	var res Metrics
	now := float64(time.Now().Unix())

	selector := labels.SelectorFromSet(labels.Set{constants.WorkspaceLabelKey: workspace})
	opt := metav1.ListOptions{LabelSelector: selector.String()}

H
hongming 已提交
304
	nsList, err := mo.k8s.CoreV1().Namespaces().List(context.Background(), opt)
H
huanggze 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceNamespaceCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceNamespaceCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(len(nsList.Items))},
					},
				},
			},
		})
	}

	devopsList, err := mo.ks.Devops().V1alpha3().DevOpsProjects().Lister().List(selector)
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceDevopsCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceDevopsCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(len(devopsList))},
					},
				},
			},
		})
	}

R
Roland.Ma 已提交
344 345 346
	r, _ := labels.NewRequirement(v1alpha2.UserReferenceLabel, selection.Exists, nil)
	memberSelector := selector.DeepCopySelector().Add(*r)
	memberList, err := mo.ks.Iam().V1alpha2().WorkspaceRoleBindings().Lister().List(memberSelector)
H
huanggze 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceMemberCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceMemberCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(len(memberList))},
					},
				},
			},
		})
	}

	roleList, err := mo.ks.Iam().V1alpha2().WorkspaceRoles().Lister().List(selector)
	if err != nil {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceRoleCount,
			Error:      err.Error(),
		})
	} else {
		res.Results = append(res.Results, monitoring.Metric{
			MetricName: WorkspaceRoleCount,
			MetricData: monitoring.MetricData{
				MetricType: monitoring.MetricTypeVector,
				MetricValues: []monitoring.MetricValue{
					{
						Sample: &monitoring.Point{now, float64(len(roleList))},
					},
				},
			},
		})
	}

	return res
}
Y
yunkunrao 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

/*
	meter related methods
*/

func (mo monitoringOperator) getNamedMetersWithHourInterval(meters []string, t time.Time, opt monitoring.QueryOption) Metrics {

	var opts []monitoring.QueryOption

	opts = append(opts, opt)
	opts = append(opts, monitoring.MeterOption{
		Step: 1 * time.Hour,
	})

	ress := mo.prometheus.GetNamedMeters(meters, t, opts)

	return Metrics{Results: ress}
}

func generateScalingFactorMap(step time.Duration) map[string]float64 {
	scalingMap := make(map[string]float64)

	for k := range MeterResourceMap {
		scalingMap[k] = step.Hours()
	}
	return scalingMap
}

func (mo monitoringOperator) GetNamedMetersOverTime(meters []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) (metrics Metrics, err error) {

	if step.Hours() < 1 {
		klog.Warning("step should be longer than one hour")
		step = 1 * time.Hour
	}
	if end.Sub(start).Hours() > 30*24 {
		if step.Hours() < 24 {
			err = errors.New("step should be larger than 24 hours")
			return
		}
	}
	if math.Mod(step.Hours(), 1.0) > 0 {
		err = errors.New("step should be integer hours")
		return
	}

	// query time range: (start, end], so here we need to exclude start itself.
	if start.Add(step).After(end) {
		start = end
	} else {
		start = start.Add(step)
	}

	var opts []monitoring.QueryOption

	opts = append(opts, opt)
	opts = append(opts, monitoring.MeterOption{
		Start: start,
		End:   end,
		Step:  step,
	})

	ress := mo.prometheus.GetNamedMetersOverTime(meters, start, end, step, opts)
	sMap := generateScalingFactorMap(step)

	for i, _ := range ress {
		ress[i].MetricData = updateMetricStatData(ress[i], sMap)
	}

	return Metrics{Results: ress}, nil
}

func (mo monitoringOperator) GetNamedMeters(meters []string, time time.Time, opt monitoring.QueryOption) (Metrics, error) {

	metersPerHour := mo.getNamedMetersWithHourInterval(meters, time, opt)

	for metricIndex, _ := range metersPerHour.Results {

		res := metersPerHour.Results[metricIndex]

		metersPerHour.Results[metricIndex].MetricData = updateMetricStatData(res, nil)
	}

	return metersPerHour, nil
}

func (mo monitoringOperator) GetAppComponentsMap(ns string, apps []string) map[string][]string {

	componentsMap := make(map[string][]string)
	applicationList := []*appv1beta1.Application{}

	result, err := mo.resourceGetter.List("applications", ns, query.New())
	if err != nil {
		klog.Error(err)
		return nil
	}

	for _, obj := range result.Items {
		app, ok := obj.(*appv1beta1.Application)
		if !ok {
			continue
		}

		applicationList = append(applicationList, app)
	}

	getAppFullName := func(appObject *v1beta1.Application) (name string) {
		name = appObject.Labels[constants.ApplicationName]
		if appObject.Labels[constants.ApplicationVersion] != "" {
			name += fmt.Sprintf(":%v", appObject.Labels[constants.ApplicationVersion])
		}
		return
	}

	appFilter := func(appObject *v1beta1.Application) bool {

		for _, app := range apps {
			var applicationName, applicationVersion string
			tmp := strings.Split(app, ":")

			if len(tmp) >= 1 {
				applicationName = tmp[0]
			}
			if len(tmp) == 2 {
				applicationVersion = tmp[1]
			}

			if applicationName != "" && appObject.Labels[constants.ApplicationName] != applicationName {
				return false
			}
			if applicationVersion != "" && appObject.Labels[constants.ApplicationVersion] != applicationVersion {
				return false
			}
			return true
		}

		return true
	}

	for _, appObj := range applicationList {
		if appFilter(appObj) {
			for _, com := range appObj.Status.ComponentList.Objects {
				kind := strings.Title(com.Kind)
				name := com.Name
				componentsMap[getAppFullName((appObj))] = append(componentsMap[getAppFullName(appObj)], kind+":"+name)
			}
		}
	}

	return componentsMap
}

func (mo monitoringOperator) getApplicationPVCs(appObject *v1beta1.Application) []string {

	var pvcList []string

	ns := appObject.Namespace
	for _, com := range appObject.Status.ComponentList.Objects {

		switch strings.Title(com.Kind) {
		case "Deployment":
			deployObj, err := mo.k8s.AppsV1().Deployments(ns).Get(context.Background(), com.Name, metav1.GetOptions{})
			if err != nil {
				klog.Error(err.Error())
				return nil
			}

			for _, vol := range deployObj.Spec.Template.Spec.Volumes {
				pvcList = append(pvcList, vol.PersistentVolumeClaim.ClaimName)
			}
		case "Statefulset":
			stsObj, err := mo.k8s.AppsV1().StatefulSets(ns).Get(context.Background(), com.Name, metav1.GetOptions{})
			if err != nil {
				klog.Error(err.Error())
				return nil
			}
			for _, vol := range stsObj.Spec.Template.Spec.Volumes {
				pvcList = append(pvcList, vol.PersistentVolumeClaim.ClaimName)
			}
		}

	}

	return pvcList

}

func (mo monitoringOperator) GetSerivePodsMap(ns string, services []string) map[string][]string {
	var svcPodsMap = make(map[string][]string)

	for _, svc := range services {
		svcObj, err := mo.k8s.CoreV1().Services(ns).Get(context.Background(), svc, metav1.GetOptions{})
		if err != nil {
			klog.Error(err.Error())
			return svcPodsMap
		}

		svcSelector := svcObj.Spec.Selector
		if len(svcSelector) == 0 {
			return svcPodsMap
		}

		svcLabels := labels.Set{}
		for key, value := range svcSelector {
			svcLabels[key] = value
		}

		selector := labels.SelectorFromSet(svcLabels)
		opt := metav1.ListOptions{LabelSelector: selector.String()}

		podList, err := mo.k8s.CoreV1().Pods(ns).List(context.Background(), opt)
		if err != nil {
			klog.Error(err.Error())
			return svcPodsMap
		}

		for _, pod := range podList.Items {
			svcPodsMap[svc] = append(svcPodsMap[svc], pod.Name)
		}

	}
	return svcPodsMap
}