Skip to content

Commit

Permalink
Merge pull request #11 from NUCsimple/master
Browse files Browse the repository at this point in the history
fix pod-OOM event type is pod event
  • Loading branch information
ringtail authored Jan 4, 2021
2 parents 160499f + df01c15 commit 71bd5f8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.11.0
FROM golang:1.12.0
LABEL maintainer="Andy Xie <[email protected]>"

ENV GOPATH /gopath/
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ require (
k8s.io/test-infra v0.0.0-20190914015041-e1cbc3ccd91c
)

replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999
replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999
6 changes: 5 additions & 1 deletion pkg/exporters/k8sexporter/k8s_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func NewExporterOrDie(npdo *options.NodeProblemDetectorOptions) types.Exporter {

func (ke *k8sExporter) ExportProblems(status *types.Status) {
for _, event := range status.Events {
ke.client.Eventf(util.ConvertToAPIEventType(event.Severity), status.Source, event.Reason, event.Message)
if util.PodOOMRegex.MatchString(event.Message) {
ke.client.PodEventf(util.ConvertToAPIEventType(event.Severity), status.Source, event.Reason, event.Message)
} else {
ke.client.Eventf(util.ConvertToAPIEventType(event.Severity), status.Source, event.Reason, event.Message)
}
}
for _, cdt := range status.Conditions {
ke.conditionManager.UpdateCondition(cdt)
Expand Down
26 changes: 23 additions & 3 deletions pkg/exporters/k8sexporter/problemclient/problem_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package problemclient
import (
"encoding/json"
"fmt"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/node-problem-detector/pkg/util"
"net/url"
"os"
"path/filepath"

typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/api/legacyscheme"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -48,6 +48,7 @@ type Client interface {
// Eventf reports the event.
Eventf(eventType string, source, reason, messageFmt string, args ...interface{})
// GetNode returns the Node object of the node on which the
PodEventf(eventType string, source, reason, messageFmt string, args ...interface{})
// node-problem-detector runs.
GetNode() (*v1.Node, error)
}
Expand Down Expand Up @@ -119,6 +120,25 @@ func (c *nodeProblemClient) Eventf(eventType, source, reason, messageFmt string,
recorder.Eventf(c.nodeRef, eventType, reason, messageFmt, args...)
}


func (c *nodeProblemClient) PodEventf(eventType, source, reason, messageFmt string, args ...interface{}) {
recorder, found := c.recorders[source]
if !found {
recorder = getEventRecorder(c.client, c.nodeName, source)
c.recorders[source] = recorder
}
rst := util.PodOOMRegex.FindStringSubmatch(messageFmt)

podRef := &v1.ObjectReference{
Kind: "Pod",
Name: rst[2],
UID: types.UID(rst[2]),
Namespace: rst[3],
}

recorder.Eventf(podRef, eventType, reason, messageFmt, args...)
}

func (c *nodeProblemClient) GetNode() (*v1.Node, error) {
return c.client.Nodes().Get(c.nodeName, metav1.GetOptions{})
}
Expand Down
26 changes: 14 additions & 12 deletions pkg/systemlogmonitor/log_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ package systemlogmonitor
import (
"encoding/json"
"io/ioutil"
"k8s.io/heapster/common/kubernetes"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/heapster/common/kubernetes"
"k8s.io/node-problem-detector/cmd/options"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"fmt"
"time"

"github.com/golang/glog"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/node-problem-detector/pkg/problemdaemon"
"k8s.io/node-problem-detector/pkg/problemmetrics"
"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers"
Expand All @@ -42,7 +44,6 @@ import (
"k8s.io/node-problem-detector/pkg/util"
"k8s.io/node-problem-detector/pkg/util/tomb"
"k8s.io/node-problem-detector/pkg/version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -51,8 +52,8 @@ const (
)

var (
uuidRegx *regexp.Regexp
k8sClient *clientset.Clientset
uuidRegx *regexp.Regexp
k8sClient *clientset.Clientset
)

func init() {
Expand All @@ -78,7 +79,7 @@ type logMonitor struct {
tomb *tomb.Tomb
}

func InitK8sClientOrDie(options *options.NodeProblemDetectorOptions) *clientset.Clientset{
func InitK8sClientOrDie(options *options.NodeProblemDetectorOptions) *clientset.Clientset {
uri, _ := url.Parse(options.ApiServerOverride)
cfg, err := kubernetes.GetKubeClientConfig(uri)
if err != nil {
Expand Down Expand Up @@ -201,9 +202,10 @@ func (l *logMonitor) generateStatus(logs []*logtypes.Log, rule systemlogtypes.Ru
// We use the timestamp of the first log line as the timestamp of the status.
timestamp := logs[0].Timestamp
message := generateMessage(logs)
if rule.Reason == OOMREASON && k8sClient != nil{
if rule.Reason == OOMREASON && k8sClient != nil {
uuid := string(uuidRegx.Find([]byte(message)))
uuid = strings.ReplaceAll(uuid,"_","-")

uuid = strings.ReplaceAll(uuid, "_", "-")
pl, err := k8sClient.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
glog.Error("Error in getting pods: %v", err.Error())
Expand All @@ -222,10 +224,10 @@ func (l *logMonitor) generateStatus(logs []*logtypes.Log, rule systemlogtypes.Ru
if rule.Type == types.Temp {
// For temporary error only generate event
events = append(events, types.Event{
Severity: types.Warn,
Timestamp: timestamp,
Reason: rule.Reason,
Message: message,
Severity: types.Warn,
Timestamp: timestamp,
Reason: rule.Reason,
Message: message,
})
} else {
// For permanent error changes the condition
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package util

import (
"fmt"
"regexp"
"syscall"
"time"

Expand All @@ -27,6 +28,8 @@ import (

var osReleasePath = "/etc/os-release"

var PodOOMRegex = regexp.MustCompile(`node:(\S+)\s+pod:(\S+)\s+namespace:(\S+)\s+uuid:(\S+)`)

// GenerateConditionChangeEvent generates an event for condition change.
func GenerateConditionChangeEvent(t string, status types.ConditionStatus, reason string, timestamp time.Time, msg string) types.Event {
return types.Event{
Expand Down

0 comments on commit 71bd5f8

Please sign in to comment.