|
| 1 | +package collect |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/pkg/errors" |
| 10 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/client-go/kubernetes" |
| 13 | + "k8s.io/client-go/rest" |
| 14 | + "k8s.io/klog/v2" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + summaryUrlTemplate = "/api/v1/nodes/%s/proxy/stats/summary" |
| 19 | +) |
| 20 | + |
| 21 | +type CollectNodeMetrics struct { |
| 22 | + Collector *troubleshootv1beta2.NodeMetrics |
| 23 | + BundlePath string |
| 24 | + ClientConfig *rest.Config |
| 25 | + Client kubernetes.Interface |
| 26 | + Context context.Context |
| 27 | + RBACErrors |
| 28 | +} |
| 29 | + |
| 30 | +func (c *CollectNodeMetrics) Title() string { |
| 31 | + return getCollectorName(c) |
| 32 | +} |
| 33 | + |
| 34 | +func (c *CollectNodeMetrics) IsExcluded() (bool, error) { |
| 35 | + return isExcluded(c.Collector.Exclude) |
| 36 | +} |
| 37 | + |
| 38 | +func (c *CollectNodeMetrics) Collect(progressChan chan<- interface{}) (CollectorResult, error) { |
| 39 | + output := NewResult() |
| 40 | + nodesMap := c.constructNodesMap() |
| 41 | + if len(nodesMap) == 0 { |
| 42 | + klog.V(2).Info("no nodes found to collect metrics for") |
| 43 | + return output, nil |
| 44 | + } |
| 45 | + |
| 46 | + nodeNames := make([]string, 0, len(nodesMap)) |
| 47 | + for nodeName := range nodesMap { |
| 48 | + nodeNames = append(nodeNames, nodeName) |
| 49 | + } |
| 50 | + |
| 51 | + klog.V(2).Infof("collecting node metrics for [%s] nodes", strings.Join(nodeNames, ", ")) |
| 52 | + |
| 53 | + for nodeName, endpoint := range nodesMap { |
| 54 | + // Equivalent to `kubectl get --raw "/api/v1/nodes/<nodeName>/proxy/stats/summary"` |
| 55 | + klog.V(2).Infof("querying: %+v\n", endpoint) |
| 56 | + response, err := c.Client.CoreV1().RESTClient().Get().AbsPath(endpoint).DoRaw(c.Context) |
| 57 | + if err != nil { |
| 58 | + return output, errors.Wrapf(err, "could not query endpoint %s", endpoint) |
| 59 | + } |
| 60 | + err = output.SaveResult(c.BundlePath, fmt.Sprintf("node-metrics/%s.json", nodeName), bytes.NewBuffer(response)) |
| 61 | + if err != nil { |
| 62 | + klog.Errorf("failed to save node metrics for %s: %v", nodeName, err) |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + return output, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (c *CollectNodeMetrics) constructNodesMap() map[string]string { |
| 70 | + nodesMap := map[string]string{} |
| 71 | + |
| 72 | + if c.Collector.NodeNames == nil && c.Collector.Selector == nil { |
| 73 | + // If no node names or selectors are provided, collect all nodes |
| 74 | + nodes, err := c.Client.CoreV1().Nodes().List(c.Context, metav1.ListOptions{}) |
| 75 | + if err != nil { |
| 76 | + klog.Errorf("failed to list nodes: %v", err) |
| 77 | + } |
| 78 | + for _, node := range nodes.Items { |
| 79 | + nodesMap[node.Name] = fmt.Sprintf(summaryUrlTemplate, node.Name) |
| 80 | + } |
| 81 | + return nodesMap |
| 82 | + } |
| 83 | + |
| 84 | + for _, nodeName := range c.Collector.NodeNames { |
| 85 | + nodesMap[nodeName] = fmt.Sprintf(summaryUrlTemplate, nodeName) |
| 86 | + } |
| 87 | + |
| 88 | + // Find nodes by label selector |
| 89 | + if c.Collector.Selector != nil { |
| 90 | + nodes, err := c.Client.CoreV1().Nodes().List(c.Context, metav1.ListOptions{ |
| 91 | + LabelSelector: strings.Join(c.Collector.Selector, ","), |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + klog.Errorf("failed to list nodes by label selector: %v", err) |
| 95 | + } |
| 96 | + for _, node := range nodes.Items { |
| 97 | + nodesMap[node.Name] = fmt.Sprintf(summaryUrlTemplate, node.Name) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return nodesMap |
| 102 | +} |
0 commit comments