Skip to content

Commit c7bcc1d

Browse files
committed
fix: Modify PromQL to support IPv6
Signed-off-by: qiuming520 <[email protected]>
1 parent c2c0533 commit c7bcc1d

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

pkg/controller/annotator/node.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414
"k8s.io/client-go/util/workqueue"
1515
"k8s.io/klog/v2"
1616

17-
policy "github.com/gocrane/crane-scheduler/pkg/plugins/apis/policy"
18-
1917
prom "github.com/gocrane/crane-scheduler/pkg/controller/prometheus"
20-
utils "github.com/gocrane/crane-scheduler/pkg/utils"
18+
"github.com/gocrane/crane-scheduler/pkg/plugins/apis/policy"
19+
"github.com/gocrane/crane-scheduler/pkg/utils"
2120
)
2221

2322
const (
@@ -99,10 +98,16 @@ func (n *nodeController) syncNode(key string) (bool, error) {
9998
}
10099

101100
func annotateNodeLoad(promClient prom.PromClient, kubeClient clientset.Interface, node *v1.Node, key string) error {
102-
value, err := promClient.QueryByNodeIP(key, getNodeInternalIP(node))
101+
value, err := promClient.QueryByNodeIP(key, getNodeInternalIPv4(node))
103102
if err == nil && len(value) > 0 {
104103
return patchNodeAnnotation(kubeClient, node, key, value)
105104
}
105+
106+
value, err = promClient.QueryByNodeIP(key, getNodeInternalIPv6(node))
107+
if err == nil && len(value) > 0 {
108+
return patchNodeAnnotation(kubeClient, node, key, value)
109+
}
110+
106111
value, err = promClient.QueryByNodeName(key, getNodeName(node))
107112
if err == nil && len(value) > 0 {
108113
return patchNodeAnnotation(kubeClient, node, key, value)
@@ -176,9 +181,19 @@ func (n *nodeController) CreateMetricSyncTicker(stopCh <-chan struct{}) {
176181
}
177182
}
178183

179-
func getNodeInternalIP(node *v1.Node) string {
184+
func getNodeInternalIPv4(node *v1.Node) string {
185+
for _, addr := range node.Status.Addresses {
186+
if addr.Type == v1.NodeInternalIP && utils.IsValidIPv4(addr.Address) {
187+
return addr.Address
188+
}
189+
}
190+
191+
return node.Name
192+
}
193+
194+
func getNodeInternalIPv6(node *v1.Node) string {
180195
for _, addr := range node.Status.Addresses {
181-
if addr.Type == v1.NodeInternalIP {
196+
if addr.Type == v1.NodeInternalIP && utils.IsValidIPv6(addr.Address) {
182197
return addr.Address
183198
}
184199
}

pkg/controller/prometheus/prometheus.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
1212
"github.com/prometheus/common/model"
1313
"k8s.io/klog/v2"
14+
15+
"github.com/gocrane/crane-scheduler/pkg/utils"
1416
)
1517

1618
const (
@@ -57,7 +59,12 @@ func (p *promClient) QueryByNodeIP(metricName, ip string) (string, error) {
5759
return result, nil
5860
}
5961

60-
querySelector = fmt.Sprintf("%s{instance=~\"%s:.+\"} /100", metricName, ip)
62+
if utils.IsValidIPv4(ip) {
63+
querySelector = fmt.Sprintf("%s{instance=~\"%s:.+\"} /100", metricName, ip)
64+
} else {
65+
querySelector = fmt.Sprintf("%s{instance=~\"\\\\[%s\\\\]:.+\"} /100", metricName, ip)
66+
}
67+
6168
result, err = p.query(querySelector)
6269
if result != "" && err == nil {
6370
return result, nil

pkg/utils/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"net"
45
"os"
56
"time"
67

@@ -66,3 +67,13 @@ func NormalizeScore(value, max, min int64) int64 {
6667

6768
return value
6869
}
70+
71+
func IsValidIPv4(ip string) bool {
72+
addr := net.ParseIP(ip)
73+
return addr != nil && addr.To4() != nil
74+
}
75+
76+
func IsValidIPv6(ip string) bool {
77+
addr := net.ParseIP(ip)
78+
return addr != nil && addr.To4() == nil
79+
}

0 commit comments

Comments
 (0)