github.com/oam-dev/kubevela@v1.9.11/pkg/multicluster/o11n.go (about) 1 /* 2 Copyright 2020-2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package multicluster 18 19 import ( 20 "context" 21 22 metricsV1beta1api "k8s.io/metrics/pkg/apis/metrics/v1beta1" 23 24 "github.com/pkg/errors" 25 corev1 "k8s.io/api/core/v1" 26 storagev1 "k8s.io/api/storage/v1" 27 "k8s.io/apimachinery/pkg/api/resource" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 ) 30 31 // ClusterMetrics describes the metrics of a cluster 32 type ClusterMetrics struct { 33 IsConnected bool 34 ClusterInfo *ClusterInfo 35 ClusterUsageMetrics *ClusterUsageMetrics 36 } 37 38 // ClusterInfo describes the basic information of a cluster 39 type ClusterInfo struct { 40 Nodes *corev1.NodeList 41 WorkerNumber int 42 MasterNumber int 43 MemoryCapacity resource.Quantity 44 CPUCapacity resource.Quantity 45 PodCapacity resource.Quantity 46 MemoryAllocatable resource.Quantity 47 CPUAllocatable resource.Quantity 48 PodAllocatable resource.Quantity 49 StorageClasses *storagev1.StorageClassList 50 } 51 52 // ClusterUsageMetrics describes the usage metrics of a cluster 53 type ClusterUsageMetrics struct { 54 CPUUsage resource.Quantity 55 MemoryUsage resource.Quantity 56 } 57 58 // GetClusterInfo retrieves current cluster info from cluster 59 func GetClusterInfo(_ctx context.Context, k8sClient client.Client, clusterName string) (*ClusterInfo, error) { 60 ctx := ContextWithClusterName(_ctx, clusterName) 61 nodes := &corev1.NodeList{} 62 if err := k8sClient.List(ctx, nodes); err != nil { 63 return nil, errors.Wrapf(err, "failed to list cluster nodes") 64 } 65 var workerNumber, masterNumber int 66 var memoryCapacity, cpuCapacity, podCapacity, memoryAllocatable, cpuAllocatable, podAllocatable resource.Quantity 67 for _, node := range nodes.Items { 68 if _, ok := node.Labels["node-role.kubernetes.io/master"]; ok { 69 masterNumber++ 70 } else { 71 workerNumber++ 72 } 73 capacity := node.Status.Capacity 74 memoryCapacity.Add(*capacity.Memory()) 75 cpuCapacity.Add(*capacity.Cpu()) 76 podCapacity.Add(*capacity.Pods()) 77 allocatable := node.Status.Allocatable 78 memoryAllocatable.Add(*allocatable.Memory()) 79 cpuAllocatable.Add(*allocatable.Cpu()) 80 podAllocatable.Add(*allocatable.Pods()) 81 } 82 storageClasses := &storagev1.StorageClassList{} 83 if err := k8sClient.List(ctx, storageClasses); err != nil { 84 return nil, errors.Wrapf(err, "failed to list storage classes") 85 } 86 return &ClusterInfo{ 87 Nodes: nodes, 88 WorkerNumber: workerNumber, 89 MasterNumber: masterNumber, 90 MemoryCapacity: memoryCapacity, 91 CPUCapacity: cpuCapacity, 92 PodCapacity: podCapacity, 93 MemoryAllocatable: memoryAllocatable, 94 CPUAllocatable: cpuAllocatable, 95 PodAllocatable: podAllocatable, 96 StorageClasses: storageClasses, 97 }, nil 98 } 99 100 // GetClusterMetricsFromMetricsAPI retrieves current cluster metrics based on GetNodeMetricsFromMetricsAPI 101 func GetClusterMetricsFromMetricsAPI(ctx context.Context, k8sClient client.Client, clusterName string) (*ClusterUsageMetrics, error) { 102 nodeMetricsList := metricsV1beta1api.NodeMetricsList{} 103 if err := k8sClient.List(ContextWithClusterName(ctx, clusterName), &nodeMetricsList); err != nil { 104 return nil, errors.Wrapf(err, "failed to list node metrics") 105 } 106 var memoryUsage, cpuUsage resource.Quantity 107 for _, nm := range nodeMetricsList.Items { 108 cpuUsage.Add(*nm.Usage.Cpu()) 109 memoryUsage.Add(*nm.Usage.Memory()) 110 } 111 return &ClusterUsageMetrics{ 112 CPUUsage: cpuUsage, 113 MemoryUsage: memoryUsage, 114 }, nil 115 }