Skip to content

Commit

Permalink
Make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
jknipper committed Nov 20, 2024
1 parent 56d38a3 commit 105ab40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {

var labels []string
for _, label := range prometheusContainerLabels {
labels = append(labels, strings.Replace(label, ".", "_", -1))
labels = append(labels, strings.ReplaceAll(label, ".", "_"))
}
kubernetesCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "klog_pod_oomkill",
Expand All @@ -65,8 +65,8 @@ func main() {

go func() {
glog.Info("Starting prometheus metrics")
http.Handle("/metrics", promhttp.Handler())
glog.Warning(http.ListenAndServe(metricsAddr, nil))
http.Handle("/metrics", promhttp.Handler()) //permit

Check failure on line 68 in main.go

View workflow job for this annotation

GitHub Actions / Checks

use of `http.Handle` forbidden by pattern `^http\.Handle(?:Func)?$` (forbidigo)
glog.Warning(http.ListenAndServe(metricsAddr, nil)) //permit

Check failure on line 69 in main.go

View workflow job for this annotation

GitHub Actions / Checks

commentFormatting: put a space between `//` and comment text (gocritic)
}()

kmsgWatcher := kmsg.NewKmsgWatcher(types.WatcherConfig{Plugin: "kmsg"})
Expand All @@ -89,12 +89,16 @@ func main() {
}
}

func getContainerIDFromLog(log string) (string, string) {
func getContainerIDFromLog(log string) (podUID, containerID string) {
podUID = ""
containerID = ""

if matches := kmesgRE.FindStringSubmatch(log); matches != nil {
return matches[1], matches[2]
podUID = matches[1]
containerID = matches[2]
}

return "", ""
return
}

func getContainerLabels(containerID string, cli *containerd.Client) (map[string]string, error) {
Expand All @@ -111,8 +115,7 @@ func prometheusCount(containerLabels map[string]string) {
var counter prometheus.Counter
var err error

var labels map[string]string
labels = make(map[string]string)
labels := make(map[string]string)
for key, label := range prometheusContainerLabels {
labels[label] = containerLabels[key]
}
Expand Down
32 changes: 17 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -33,32 +34,33 @@ func parseMessage(input string) (kmsgparser.Message, error) {
// PRIORITY,SEQUENCE_NUM,TIMESTAMP,-;MESSAGE
parts := strings.SplitN(input, ";", 2)
if len(parts) != 2 {
return kmsgparser.Message{}, fmt.Errorf("invalid kmsg; must contain a ';'")
return kmsgparser.Message{}, errors.New("invalid kmsg; must contain a ';'")
}

metadata, message := parts[0], parts[1]

metadataParts := strings.Split(metadata, ",")
if len(metadataParts) < 3 {
return kmsgparser.Message{}, fmt.Errorf("invalid kmsg: must contain at least 3 ',' separated pieces at the start")
return kmsgparser.Message{}, errors.New("invalid kmsg: must contain at least 3 ',' separated pieces at the start")
}

return kmsgparser.Message{
Message: message,
}, nil
}

func getTestData() ([]string, []string, []string) {
return []string{
"6,22743,6115623303887,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,mems_allowed=0-1,oom_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task=prometheus-conf,pid=3401999,uid=0",
"6,23800,6780904484233,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,mems_allowed=0,oom_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice,task_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice/cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,task=stress,pid=255629,uid=0",
},
[]string{
"d11ab7b0-d6db-4a24-a7de-4a2faf1e6980",
"dfc377c9_c533_4d51_af9e_6e0e0b3db83b",
},
[]string{
"9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64",
"2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc",
}
func getTestData() (klog, podUIDs, containerIDs []string) {
klog = []string{
"6,22743,6115623303887,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,mems_allowed=0-1,oom_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task_memcg=/kubepods/burstable/podd11ab7b0-d6db-4a24-a7de-4a2faf1e6980/9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64,task=prometheus-conf,pid=3401999,uid=0",
"6,23800,6780904484233,-;oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,mems_allowed=0,oom_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice,task_memcg=/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-poddfc377c9_c533_4d51_af9e_6e0e0b3db83b.slice/cri-containerd-2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc.scope,task=stress,pid=255629,uid=0",
}
podUIDs = []string{
"d11ab7b0-d6db-4a24-a7de-4a2faf1e6980",
"dfc377c9_c533_4d51_af9e_6e0e0b3db83b",
}
containerIDs = []string{
"9f02d9fa0049eb2655fc83c765f142362b2cb403b57b70ba3185071015ca3b64",
"2260b35b008a15bd118e629c0c5d74e7f3a1fe18c724fbac61a54862fea196dc",
}
return
}

0 comments on commit 105ab40

Please sign in to comment.