|
| 1 | +/* |
| 2 | +Copyright 2024 The Karmada 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 framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "regexp" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/prometheus/common/model" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + clientset "k8s.io/client-go/kubernetes" |
| 29 | + "k8s.io/component-base/metrics/testutil" |
| 30 | + "k8s.io/klog/v2" |
| 31 | + |
| 32 | + "github.com/karmada-io/karmada/pkg/util/names" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + karmadaNamespace = "karmada-system" |
| 37 | + metricsBindPort = 8080 |
| 38 | + leaderPodMetric = "leader_election_master_status" |
| 39 | + queryTimeout = 10 * time.Second |
| 40 | +) |
| 41 | + |
| 42 | +// following refers to https://github.com/kubernetes/kubernetes/blob/master/test/e2e/framework/metrics/metrics_grabber.go |
| 43 | + |
| 44 | +// Grabber is used to grab metrics from karmada components |
| 45 | +type Grabber struct { |
| 46 | + hostKubeClient clientset.Interface |
| 47 | + controllerManagerPods []string |
| 48 | + schedulerPods []string |
| 49 | + deschedulerPods []string |
| 50 | + metricsAdapterPods []string |
| 51 | + schedulerEstimatorPods []string |
| 52 | + webhookPods []string |
| 53 | +} |
| 54 | + |
| 55 | +// NewMetricsGrabber creates a new metrics grabber |
| 56 | +func NewMetricsGrabber(ctx context.Context, c clientset.Interface) (*Grabber, error) { |
| 57 | + grabber := Grabber{hostKubeClient: c} |
| 58 | + regKarmadaControllerManagerPods := regexp.MustCompile(names.KarmadaControllerManagerComponentName + "-.*") |
| 59 | + regKarmadaSchedulerPods := regexp.MustCompile(names.KarmadaSchedulerComponentName + "-.*") |
| 60 | + regKarmadaDeschedulerPods := regexp.MustCompile(names.KarmadaDeschedulerComponentName + "-.*") |
| 61 | + regKarmadaMetricsAdapterPods := regexp.MustCompile(names.KarmadaMetricsAdapterComponentName + "-.*") |
| 62 | + regKarmadaSchedulerEstimatorPods := regexp.MustCompile(names.KarmadaSchedulerEstimatorComponentName + "-" + ClusterNames()[0] + "-.*") |
| 63 | + regKarmadaWebhookPods := regexp.MustCompile(names.KarmadaWebhookComponentName + "-.*") |
| 64 | + |
| 65 | + podList, err := c.CoreV1().Pods(karmadaNamespace).List(ctx, metav1.ListOptions{}) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + if len(podList.Items) < 1 { |
| 70 | + klog.Warningf("Can't find any pods in namespace %s to grab metrics from", karmadaNamespace) |
| 71 | + } |
| 72 | + for _, pod := range podList.Items { |
| 73 | + if regKarmadaControllerManagerPods.MatchString(pod.Name) { |
| 74 | + grabber.controllerManagerPods = append(grabber.controllerManagerPods, pod.Name) |
| 75 | + continue |
| 76 | + } |
| 77 | + if regKarmadaDeschedulerPods.MatchString(pod.Name) { |
| 78 | + grabber.deschedulerPods = append(grabber.deschedulerPods, pod.Name) |
| 79 | + continue |
| 80 | + } |
| 81 | + if regKarmadaMetricsAdapterPods.MatchString(pod.Name) { |
| 82 | + grabber.metricsAdapterPods = append(grabber.metricsAdapterPods, pod.Name) |
| 83 | + continue |
| 84 | + } |
| 85 | + if regKarmadaSchedulerEstimatorPods.MatchString(pod.Name) { |
| 86 | + grabber.schedulerEstimatorPods = append(grabber.schedulerEstimatorPods, pod.Name) |
| 87 | + continue |
| 88 | + } |
| 89 | + if regKarmadaSchedulerPods.MatchString(pod.Name) { |
| 90 | + grabber.schedulerPods = append(grabber.schedulerPods, pod.Name) |
| 91 | + continue |
| 92 | + } |
| 93 | + if regKarmadaWebhookPods.MatchString(pod.Name) { |
| 94 | + grabber.webhookPods = append(grabber.webhookPods, pod.Name) |
| 95 | + } |
| 96 | + } |
| 97 | + return &grabber, nil |
| 98 | +} |
| 99 | + |
| 100 | +// GrabMetricsFromComponent fetch metrics from the leader of a specified Karmada component |
| 101 | +func (g *Grabber) GrabMetricsFromComponent(ctx context.Context, component string) (map[string]testutil.Metrics, error) { |
| 102 | + pods, fromLeader := make([]string, 0), false |
| 103 | + switch component { |
| 104 | + case names.KarmadaControllerManagerComponentName: |
| 105 | + pods, fromLeader = g.controllerManagerPods, true |
| 106 | + case names.KarmadaSchedulerComponentName: |
| 107 | + pods, fromLeader = g.schedulerPods, true |
| 108 | + case names.KarmadaDeschedulerComponentName: |
| 109 | + pods, fromLeader = g.deschedulerPods, true |
| 110 | + case names.KarmadaMetricsAdapterComponentName: |
| 111 | + pods = g.metricsAdapterPods |
| 112 | + case names.KarmadaSchedulerEstimatorComponentName: |
| 113 | + pods = g.schedulerEstimatorPods |
| 114 | + case names.KarmadaWebhookComponentName: |
| 115 | + pods = g.webhookPods |
| 116 | + } |
| 117 | + return g.grabMetricsFromPod(ctx, component, pods, fromLeader) |
| 118 | +} |
| 119 | + |
| 120 | +// grabMetricsFromPod fetch metrics from the leader pod |
| 121 | +func (g *Grabber) grabMetricsFromPod(ctx context.Context, component string, pods []string, fromLeader bool) (map[string]testutil.Metrics, error) { |
| 122 | + var output string |
| 123 | + var lastMetricsFetchErr error |
| 124 | + |
| 125 | + result := make(map[string]testutil.Metrics) |
| 126 | + for _, podName := range pods { |
| 127 | + if metricsWaitErr := wait.PollUntilContextTimeout(ctx, time.Second, queryTimeout, true, func(ctx context.Context) (bool, error) { |
| 128 | + output, lastMetricsFetchErr = GetMetricsFromPod(ctx, g.hostKubeClient, podName, karmadaNamespace, metricsBindPort) |
| 129 | + return lastMetricsFetchErr == nil, nil |
| 130 | + }); metricsWaitErr != nil { |
| 131 | + klog.Errorf("error waiting for %s to expose metrics: %v; %v", podName, metricsWaitErr, lastMetricsFetchErr) |
| 132 | + continue |
| 133 | + } |
| 134 | + |
| 135 | + podMetrics := testutil.Metrics{} |
| 136 | + metricsParseErr := testutil.ParseMetrics(output, &podMetrics) |
| 137 | + if metricsParseErr != nil { |
| 138 | + klog.Errorf("failed to parse metrics for %s: %v", podName, metricsParseErr) |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + // judge which pod is the leader pod |
| 143 | + if fromLeader && !isLeaderPod(podMetrics[leaderPodMetric]) { |
| 144 | + klog.Infof("skip fetch %s since it is not the leader pod", podName) |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + result[podName] = podMetrics |
| 149 | + klog.Infof("successfully grabbed metrics of %s", podName) |
| 150 | + } |
| 151 | + |
| 152 | + if len(result) == 0 { |
| 153 | + return nil, fmt.Errorf("failed to fetch metrics from the pod of %s", component) |
| 154 | + } |
| 155 | + return result, nil |
| 156 | +} |
| 157 | + |
| 158 | +// GetMetricsFromPod retrieves metrics data. |
| 159 | +func GetMetricsFromPod(ctx context.Context, client clientset.Interface, podName string, namespace string, port int) (string, error) { |
| 160 | + rawOutput, err := client.CoreV1().RESTClient().Get(). |
| 161 | + Namespace(namespace). |
| 162 | + Resource("pods"). |
| 163 | + SubResource("proxy"). |
| 164 | + Name(fmt.Sprintf("%s:%d", podName, port)). |
| 165 | + Suffix("metrics"). |
| 166 | + Do(ctx).Raw() |
| 167 | + if err != nil { |
| 168 | + return "", err |
| 169 | + } |
| 170 | + return string(rawOutput), nil |
| 171 | +} |
| 172 | + |
| 173 | +func isLeaderPod(samples model.Samples) bool { |
| 174 | + for _, sample := range samples { |
| 175 | + if sample.Value > 0 { |
| 176 | + return true |
| 177 | + } |
| 178 | + } |
| 179 | + return false |
| 180 | +} |
| 181 | + |
| 182 | +// PrintMetricSample prints the metric sample |
| 183 | +func PrintMetricSample(podName string, sample model.Samples) { |
| 184 | + if sample.Len() == 0 { |
| 185 | + return |
| 186 | + } |
| 187 | + if podName != "" { |
| 188 | + klog.Infof("metrics from pod: %s", podName) |
| 189 | + } |
| 190 | + for _, s := range sample { |
| 191 | + klog.Infof("metric: %v, value: %v, timestamp: %v", s.Metric, s.Value, s.Timestamp) |
| 192 | + } |
| 193 | +} |
0 commit comments