Skip to content

Commit

Permalink
fix: Modify PromQL to support IPv6
Browse files Browse the repository at this point in the history
Signed-off-by: qiuming520 <qiuming_yewu@cmss.chinamobile.com>
  • Loading branch information
qiuming520 committed May 21, 2024
1 parent c2c0533 commit f89f273
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
27 changes: 21 additions & 6 deletions pkg/controller/annotator/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"

policy "github.com/gocrane/crane-scheduler/pkg/plugins/apis/policy"

prom "github.com/gocrane/crane-scheduler/pkg/controller/prometheus"
utils "github.com/gocrane/crane-scheduler/pkg/utils"
"github.com/gocrane/crane-scheduler/pkg/plugins/apis/policy"
"github.com/gocrane/crane-scheduler/pkg/utils"
)

const (
Expand Down Expand Up @@ -99,10 +98,16 @@ func (n *nodeController) syncNode(key string) (bool, error) {
}

func annotateNodeLoad(promClient prom.PromClient, kubeClient clientset.Interface, node *v1.Node, key string) error {
value, err := promClient.QueryByNodeIP(key, getNodeInternalIP(node))
value, err := promClient.QueryByNodeIP(key, getNodeInternalIPv4(node))
if err == nil && len(value) > 0 {
return patchNodeAnnotation(kubeClient, node, key, value)
}

value, err = promClient.QueryByNodeIP(key, getNodeInternalIPv6(node))
if err == nil && len(value) > 0 {
return patchNodeAnnotation(kubeClient, node, key, value)
}

value, err = promClient.QueryByNodeName(key, getNodeName(node))
if err == nil && len(value) > 0 {
return patchNodeAnnotation(kubeClient, node, key, value)
Expand Down Expand Up @@ -176,9 +181,19 @@ func (n *nodeController) CreateMetricSyncTicker(stopCh <-chan struct{}) {
}
}

func getNodeInternalIP(node *v1.Node) string {
func getNodeInternalIPv4(node *v1.Node) string {
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP && utils.IsValidIPv4(addr.Address) {
return addr.Address
}
}

return node.Name
}

func getNodeInternalIPv6(node *v1.Node) string {
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
if addr.Type == v1.NodeInternalIP && utils.IsValidIPv6(addr.Address) {
return addr.Address
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/controller/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"k8s.io/klog/v2"

"github.com/gocrane/crane-scheduler/pkg/utils"
)

const (
Expand Down Expand Up @@ -57,7 +59,12 @@ func (p *promClient) QueryByNodeIP(metricName, ip string) (string, error) {
return result, nil
}

querySelector = fmt.Sprintf("%s{instance=~\"%s:.+\"} /100", metricName, ip)
if utils.IsValidIPv6(ip) {
querySelector = fmt.Sprintf("%s{instance=~\"\\\\[%s\\\\]:.+\"} /100", metricName, ip)
} else {
querySelector = fmt.Sprintf("%s{instance=~\"%s:.+\"} /100", metricName, ip)
}

result, err = p.query(querySelector)
if result != "" && err == nil {
return result, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"net"
"os"
"time"

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

return value
}

func IsValidIPv4(ip string) bool {
addr := net.ParseIP(ip)
return addr != nil && addr.To4() != nil
}

func IsValidIPv6(ip string) bool {
addr := net.ParseIP(ip)
return addr != nil && addr.To4() == nil
}

0 comments on commit f89f273

Please sign in to comment.