Merge pull request #2104 from huanggze/monitoring
monitor: add platform metrics
This commit is contained in:
@@ -19,9 +19,18 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/klog"
|
||||
ksinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"kubesphere.io/kubesphere/pkg/models/monitoring/expressions"
|
||||
"kubesphere.io/kubesphere/pkg/models/openpitrix"
|
||||
"kubesphere.io/kubesphere/pkg/server/params"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/monitoring"
|
||||
opclient "kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -32,14 +41,26 @@ type MonitoringOperator interface {
|
||||
GetNamedMetricsOverTime(metrics []string, start, end time.Time, step time.Duration, opt monitoring.QueryOption) Metrics
|
||||
GetMetadata(namespace string) Metadata
|
||||
GetMetricLabelSet(metric, namespace string, start, end time.Time) MetricLabelSet
|
||||
|
||||
// TODO: refactor
|
||||
GetKubeSphereStats() Metrics
|
||||
GetWorkspaceStats(workspace string) Metrics
|
||||
}
|
||||
|
||||
type monitoringOperator struct {
|
||||
c monitoring.Interface
|
||||
c monitoring.Interface
|
||||
k8s kubernetes.Interface
|
||||
ks ksinformers.SharedInformerFactory
|
||||
op openpitrix.Interface
|
||||
}
|
||||
|
||||
func NewMonitoringOperator(client monitoring.Interface) MonitoringOperator {
|
||||
return &monitoringOperator{client}
|
||||
func NewMonitoringOperator(client monitoring.Interface, k8s kubernetes.Interface, factory informers.InformerFactory, opClient opclient.Client) MonitoringOperator {
|
||||
return &monitoringOperator{
|
||||
c: client,
|
||||
k8s: k8s,
|
||||
ks: factory.KubeSphereSharedInformerFactory(),
|
||||
op: openpitrix.NewOpenpitrixOperator(factory.KubernetesSharedInformerFactory(), opClient),
|
||||
}
|
||||
}
|
||||
|
||||
func (mo monitoringOperator) GetMetric(expr, namespace string, time time.Time) (monitoring.Metric, error) {
|
||||
@@ -94,3 +115,184 @@ func (mo monitoringOperator) GetMetricLabelSet(metric, namespace string, start,
|
||||
data := mo.c.GetMetricLabelSet(expr, start, end)
|
||||
return MetricLabelSet{Data: data}
|
||||
}
|
||||
|
||||
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)},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
wkList, err := mo.ks.Tenant().V1alpha1().Workspaces().Lister().List(labels.Everything())
|
||||
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))},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
tmpls, err := mo.op.ListApps(¶ms.Conditions{}, "", 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(tmpls.TotalCount)},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
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()}
|
||||
|
||||
nsList, err := mo.k8s.CoreV1().Namespaces().List(opt)
|
||||
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))},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
memberList, err := mo.ks.Iam().V1alpha2().WorkspaceRoleBindings().Lister().List(selector)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
package monitoring
|
||||
|
||||
const (
|
||||
KubeSphereWorkspaceCount = "kubesphere_workspace_count"
|
||||
KubeSphereUserCount = "kubesphere_user_count"
|
||||
KubeSphereClusterCount = "kubesphere_cluser_count"
|
||||
KubeSphereAppTmplCount = "kubesphere_app_template_count"
|
||||
|
||||
WorkspaceNamespaceCount = "workspace_namespace_count"
|
||||
WorkspaceDevopsCount = "workspace_devops_project_count"
|
||||
WorkspaceMemberCount = "workspace_member_count"
|
||||
WorkspaceRoleCount = "workspace_role_count"
|
||||
)
|
||||
|
||||
var ClusterMetrics = []string{
|
||||
"cluster_cpu_utilisation",
|
||||
"cluster_cpu_usage",
|
||||
|
||||
Reference in New Issue
Block a user