Skip to content
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
60 changes: 0 additions & 60 deletions Dockerfile.dind

This file was deleted.

8 changes: 7 additions & 1 deletion builder/Dockerfile.dind → Dockerfile.muti
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ ARG BUILDPLATFORM
ARG BUILDARCH
ARG TARGETARCH

# default is a wildcard path that shouldn't match anything in COPY command. REF: https://github.com/kubernetes/node-problem-detector/blob/master/Makefile#L105
ARG LOGCOUNTER=*dont-include-log-counter

RUN echo "I am running on $BUILDPLATFORM $BUILDARCH, building for $TARGETPLATFORM $TARGETARCH"

####### go env #######
Expand Down Expand Up @@ -62,10 +65,13 @@ RUN make build-tar LINUX_PLATFORMS=linux_$TARGETARCH ENABLE_JOURNALD=1

#######################################################

# default is a wildcard path that shouldn't match anything in COPY command. REF: https://github.com/kubernetes/node-problem-detector/blob/master/Makefile#L105
ARG LOGCOUNTER=*dont-include-log-counter

FROM --platform=$TARGETPLATFORM centos:centos8
FROM --platform=$TARGETPLATFORM registry-cn-hangzhou.ack.aliyuncs.com/dev/alinux:3-update
MAINTAINER KeyOfSpectator <shichun.fsc@alibaba-inc.com>

RUN yum install systemd -y && yum install pciutils -y && rm -rf /var/cache/yum/
RUN systemctl --version

# RUN clean-install util-linux libsystemd0 systemd bash lsof curl
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ output/windows_amd64/test/bin/%.exe: $(PKG_SOURCES)
-tags "$(WINDOWS_BUILD_TAGS)" \
./test/e2e/$(subst -,,$*)

# =x86_64-linux-gnu-gcc need yum install gcc-x86_64-linux-gnu.x86_64

output/linux_amd64/bin/%: $(PKG_SOURCES)
GOOS=linux GOARCH=amd64 CGO_ENABLED=$(CGO_ENABLED) GO111MODULE=on \
# CC=x86_64-linux-gnu-gcc go build \
Expand Down
87 changes: 87 additions & 0 deletions builder/Dockerfile.muti
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
ARG GO_VERSION=1.17
FROM --platform=$TARGETPLATFORM golang:${GO_VERSION} as gobase
ENV GOBIN /usr/local/go/bin

FROM --platform=$TARGETPLATFORM centos:centos8 AS builder
ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG BUILDARCH
ARG TARGETARCH

# default is a wildcard path that shouldn't match anything in COPY command. REF: https://github.com/kubernetes/node-problem-detector/blob/master/Makefile#L105
ARG LOGCOUNTER=*dont-include-log-counter

RUN echo "I am running on $BUILDPLATFORM $BUILDARCH, building for $TARGETPLATFORM $TARGETARCH"

####### go env #######

ENV GOLANG_VERSION ${GO_VERSION}
ENV GOBIN /usr/local/go/bin
ENV GOROOT /usr/local/go
ENV PATH /usr/bin:/usr/local/bin:/usr/local/go/bin:/sbin:/bin
COPY --from=gobase /usr/local/go /usr/local/go

####### end o env #######

RUN sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*
RUN yum -y install yum-utils device-mapper-persistent-data lvm2 iptables openssl net-tools binutils wget git unzip\
&& yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo\
#&& yum install docker -b current -y\
&& yum install docker-ce-19.03.15 -y\
&& yum clean all && rm -rf /var/cache/yum\
&& useradd -M -s /bin/false dockremap\
&& echo "dockremap:524288:65536" >> /etc/subuid\
&& echo "dockremap:524288:65536" >> /etc/subgid

RUN yum -y update; yum clean all; \
yum -y install systemd; yum clean all; \
yum -y install libsystemd-dev; yum clean all;\
yum -y install systemd-devel; yum clean all;\
yum -y install gcc-x86_64-linux-gnu.x86_64; yum clean all;\
yum -y install docker-ce-19.03.15; yum clean all;\
yum -y install make automake gcc gcc-c++ kernel-devel; yum clean all;



#######################################################

ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct

ADD . /src/github.com/AliyunContainerService/node-problem-detector
ENV GOPATH /:/src/github.com/AliyunContainerService/node-problem-detector/vendor
WORKDIR /src/github.com/AliyunContainerService/node-problem-detector

RUN # VERSION="{{.VERSION}}"
RUN # IMAGES="{{.VERSION}}"
RUN # REGIONS="{{.REGIONS}}"
RUN # LOG_LEVEL="{{.LOG_LEVEL}}"
# RUN ls /src/github.com/AliyunContainerService/node-problem-detector/
# RUN cd /src/github.com/AliyunContainerService/node-problem-detector/

RUN make build-binaries LINUX_PLATFORMS=linux_$TARGETARCH ENABLE_JOURNALD=1
RUN make build-tar LINUX_PLATFORMS=linux_$TARGETARCH ENABLE_JOURNALD=1

#######################################################

# default is a wildcard path that shouldn't match anything in COPY command. REF: https://github.com/kubernetes/node-problem-detector/blob/master/Makefile#L105
ARG LOGCOUNTER=*dont-include-log-counter

FROM --platform=$TARGETPLATFORM registry-cn-hangzhou.ack.aliyuncs.com/dev/alinux:3-update
MAINTAINER KeyOfSpectator <shichun.fsc@alibaba-inc.com>

RUN yum install systemd -y && yum install pciutils -y && rm -rf /var/cache/yum/
RUN systemctl --version

# RUN clean-install util-linux libsystemd0 systemd bash lsof curl

# Avoid symlink of /etc/localtime.
RUN test -h /etc/localtime && rm -f /etc/localtime && cp /usr/share/zoneinfo/UTC /etc/localtime || true

COPY --from=builder /src/github.com/AliyunContainerService/node-problem-detector/bin/node-problem-detector /node-problem-detector
COPY --from=builder /src/github.com/AliyunContainerService/node-problem-detector/bin/health-checker ${LOGCOUNTER} /home/kubernetes/bin/

COPY --from=builder /src/github.com/AliyunContainerService/node-problem-detector/config /config
RUN chmod +x /config/plugin/*.sh
ENTRYPOINT ["/node-problem-detector", "--config.system-log-monitor=/config/kernel-monitor.json"]
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/hpcloud/tail v1.0.0
github.com/onsi/ginkgo v1.10.3
github.com/onsi/gomega v1.7.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4
github.com/prometheus/common v0.4.1
github.com/prometheus/procfs v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mo
github.com/opencontainers/selinux v1.3.3/go.mod h1:yTcKuYAh6R95iDpefGLQaPaRwJFwyzAJufJyiTt7s0g=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pborman/uuid v0.0.0-20150824212802-cccd189d45f7/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
github.com/pborman/uuid v0.0.0-20170612153648-e790cca94e6c/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
Expand Down
102 changes: 63 additions & 39 deletions pkg/systemlogmonitor/log_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,34 @@ package systemlogmonitor
import (
"regexp"
"strings"
"time"

"k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
)

// LogBuffer buffers the logs and supports match in the log buffer with regular expression.
type LogBuffer interface {
// Push pushes log into the log buffer.
// Push pushes log into the log buffer. It will panic if the buffer is full.
Push(*types.Log)
// Poll poll log from the log buffer. It returns nil if no log is available.
Poll() *types.Log
// Match with regular expression in the log buffer.
Match(string) []*types.Log
// String returns a concatenated string of the buffered logs.
String() string

Clean()

SetLookback(lookback *time.Duration)
}

type logBuffer struct {
// buffer is a simple ring buffer.
buffer []*types.Log
msg []string
max int
current int
buffer []*types.Log
msg []string
max int
write int
read int
size int
lookback *time.Duration
}

// NewLogBuffer creates log buffer with max line number limit. Because we only match logs
Expand All @@ -50,50 +58,66 @@ func NewLogBuffer(maxLines int) *logBuffer {
buffer: make([]*types.Log, maxLines, maxLines),
msg: make([]string, maxLines, maxLines),
max: maxLines,
write: 0,
read: 0,
size: 0,
}
}

func (b *logBuffer) SetLookback(lookback *time.Duration) {
b.lookback = lookback
}

func (b *logBuffer) Push(log *types.Log) {
b.buffer[b.current%b.max] = log
b.msg[b.current%b.max] = log.Message
b.current++
if b.size == b.max {
b.read = (b.read + 1) % b.max
b.size--
}
b.buffer[b.write] = log
b.msg[b.write] = log.Message
b.write = (b.write + 1) % b.max
b.size++
}

// TODO(random-liu): Cache regexp if garbage collection becomes a problem someday.
func (b *logBuffer) Match(expr string) []*types.Log {
// The expression should be checked outside, and it must match to the end.
reg := regexp.MustCompile(expr + `\z`)
log := b.String()
loc := reg.FindStringIndex(log)
if loc == nil {
// No match
func (b *logBuffer) Poll() *types.Log {
if b.size == 0 {
return nil
}
// reverse index
s := len(log) - loc[0] - 1
total := 0
matched := []*types.Log{}
for i := b.tail(); i >= b.current && b.buffer[i%b.max] != nil; i-- {
matched = append(matched, b.buffer[i%b.max])
total += len(b.msg[i%b.max]) + 1 // Add '\n'
if total > s {
break
}
}
for i := 0; i < len(matched)/2; i++ {
matched[i], matched[len(matched)-i-1] = matched[len(matched)-i-1], matched[i]
}
return matched
item := b.buffer[b.read]
b.read = (b.read + 1) % b.max
b.size--
return item
}

func (b *logBuffer) String() string {
logs := append(b.msg[b.current%b.max:], b.msg[:b.current%b.max]...)
return concatLogs(logs)
func (b *logBuffer) Clean() {
b.read = b.write
b.size = 0
}

// tail returns current tail index.
func (b *logBuffer) tail() int {
return b.current + b.max - 1
func (b *logBuffer) IsEmpty() bool {
return b.size == 0
}

func (b *logBuffer) IsFull() bool {
return b.size == b.max
}

// TODO(random-liu): Cache regexp if garbage collection becomes a problem someday.
func (b *logBuffer) Match(expr string) []*types.Log {
// The expression should be checked outside
reg := regexp.MustCompile(expr + `\z`)
var matched []*types.Log

for i := b.read; i != b.write; i = (i + 1) % b.max {
if b.lookback != nil && b.buffer[i] != nil && b.buffer[i].Timestamp.Before(time.Now().Add(-*b.lookback)) {
// not ontime log
continue
}
if b.buffer[i] != nil && reg.MatchString(b.buffer[i].Message) {
matched = append(matched, b.buffer[i])
}
}
return matched
}

// concatLogs concatenates multiple lines of logs into one string.
Expand Down
Loading