Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modify PromQL to support IPv6 #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 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, getNodeInternalIP(node, utils.IPv4))
if err == nil && len(value) > 0 {
return patchNodeAnnotation(kubeClient, node, key, value)
}

value, err = promClient.QueryByNodeIP(key, getNodeInternalIP(node, utils.IPv6))
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,13 +181,23 @@ func (n *nodeController) CreateMetricSyncTicker(stopCh <-chan struct{}) {
}
}

func getNodeInternalIP(node *v1.Node) string {
func getNodeInternalIP(node *v1.Node, flag string) string {
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
return addr.Address
switch flag {
case utils.IPv6:
if utils.IsValidIPv6(addr.Address) {
return addr.Address
}
case utils.IPv4:
if utils.IsValidIPv4(addr.Address) {
return addr.Address
}
default:
return addr.Address
}
}
}

return node.Name
}

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
13 changes: 13 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 All @@ -11,6 +12,8 @@ const (
TimeFormat = "2006-01-02T15:04:05Z"
DefaultTimeZone = "Asia/Shanghai"
DefaultNamespace = "crane-system"
IPv4 = "IPv4"
IPv6 = "IPv6"
)

// IsDaemonsetPod judges if this pod belongs to one daemonset workload.
Expand Down Expand Up @@ -66,3 +69,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
}
Loading