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 image detection for kubelet metrics in inttests #3553

Merged
merged 1 commit into from
Oct 4, 2023
Merged
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
20 changes: 15 additions & 5 deletions inttest/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"io"
"regexp"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -294,25 +295,34 @@ func RetryWatchErrors(logf func(format string, args ...any)) watch.ErrorCallback
// VerifyKubeletMetrics checks whether we see container and image labels in kubelet metrics.
// It does it via polling as it takes some time for kubelet to start reporting metrics.
func VerifyKubeletMetrics(ctx context.Context, kc *kubernetes.Clientset, node string) error {
image := constant.KubeRouterCNIImage
if ver, hash, found := strings.Cut(constant.KubeRouterCNIImageVersion, "@"); found {
image = fmt.Sprintf("%s@%s", image, hash)
} else {
image = fmt.Sprintf("%s:%s", image, ver)
}

return Poll(ctx, func(ctx context.Context) (done bool, err error) {
re := fmt.Sprintf(`^container_cpu_usage_seconds_total\{container="kube-router".*image="%s"`, regexp.QuoteMeta(image))
containerRegex := regexp.MustCompile(re)

path := fmt.Sprintf("/api/v1/nodes/%s/proxy/metrics/cadvisor", node)

path := fmt.Sprintf("/api/v1/nodes/%s/proxy/metrics/cadvisor", node)
return Poll(ctx, func(ctx context.Context) (done bool, err error) {
metrics, err := kc.CoreV1().RESTClient().Get().AbsPath(path).Param("format", "text").DoRaw(ctx)
if err != nil {
return false, nil // do not return the error so we keep on polling
}

image := fmt.Sprintf("%s:%s", constant.KubeRouterCNIImage, constant.KubeRouterCNIImageVersion)
containerRegex := regexp.MustCompile(fmt.Sprintf(`container_cpu_usage_seconds_total{container="kube-router".*image="%s"`, image))

scanner := bufio.NewScanner(bytes.NewReader(metrics))
for scanner.Scan() {
line := scanner.Text()
if containerRegex.MatchString(line) {
return true, nil
}
}
if err := scanner.Err(); err != nil {
return false, err
}

return false, nil
})
Expand Down
Loading