Skip to content

Commit f5e38b1

Browse files
Switch to golang due to bug in fluent-plugin-kubernetes-objects (#3)
* initial commit, still learning * testing another approach * fixed * print in json * Remove flag package * update README and docker image tag * switching docker to use multistage
1 parent d008ad0 commit f5e38b1

File tree

7 files changed

+90
-81
lines changed

7 files changed

+90
-81
lines changed

Dockerfile

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,11 @@
1-
FROM fluent/fluentd:v1.3.3-debian-1.0
2-
3-
USER root
4-
WORKDIR /home/fluent
5-
ENV PATH /fluentd/vendor/bundle/ruby/2.3.0/bin:$PATH
6-
ENV GEM_PATH /fluentd/vendor/bundle/ruby/2.3.0
7-
ENV GEM_HOME /fluentd/vendor/bundle/ruby/2.3.0
8-
# skip runtime bundler installation
9-
ENV FLUENTD_DISABLE_BUNDLER_INJECTION 1
10-
11-
COPY ./conf/Gemfile /fluentd/
12-
13-
RUN buildDeps="sudo make gcc g++ libc-dev ruby-dev libffi-dev" \
14-
&& apt-get update \
15-
&& apt-get upgrade -y \
16-
&& apt-get install \
17-
-y --no-install-recommends \
18-
$buildDeps net-tools libjemalloc1 \
19-
&& gem install bundler --version 1.16.2 \
20-
&& bundle config silence_root_warning true \
21-
&& bundle install --gemfile=/fluentd/Gemfile --path=/fluentd/vendor/bundle \
22-
&& SUDO_FORCE_REMOVE=yes \
23-
apt-get purge -y --auto-remove \
24-
-o APT::AutoRemove::RecommendsImportant=false \
25-
$buildDeps \
26-
&& rm -rf /var/lib/apt/lists/* \
27-
&& gem sources --clear-all \
28-
&& rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem
29-
30-
# Copy configuration files
31-
COPY ./conf/fluent.conf /fluentd/etc/
32-
RUN touch /fluentd/etc/disable.conf
33-
COPY ./conf/entrypoint.sh /fluentd/entrypoint.sh
34-
35-
# Environment variables
36-
ENV FLUENTD_OPT=""
37-
ENV FLUENTD_CONF="fluent.conf"
38-
39-
# See https://packages.debian.org/stretch/amd64/libjemalloc1/filelist
40-
ENV LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libjemalloc.so.1"
41-
42-
# Overwrite ENTRYPOINT to run fluentd as root for /var/log / /var/lib
43-
ENTRYPOINT ["tini", "--", "/fluentd/entrypoint.sh"]
1+
FROM golang:1.12.1
2+
WORKDIR /go/src/github.com/deliveryhero/k8s-event-logger
3+
COPY main.go .
4+
RUN go get -d -v ./...
5+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
6+
7+
FROM alpine:3.9.3
8+
RUN apk --no-cache add ca-certificates
9+
WORKDIR /root/
10+
COPY --from=0 /go/src/github.com/deliveryhero/k8s-event-logger/main k8s-event-logger
11+
CMD ["/root/k8s-event-logger"]

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ The problem is that these events are simply API objects in Kubernetes and are on
1111

1212
This simple container and [Helm](https://helm.sh/) chart will run in your cluster, watch for events and print them to stdout in JSON. The assumption is that you already have a daemonset for collecting all pod logs and sending them to a central system, e.g. ELK, Splunk, Graylog etc.
1313

14-
It's based on work in these 2 repositories:
15-
16-
- https://github.com/splunk/fluent-plugin-kubernetes-objects
17-
- https://github.com/fluent/fluentd-kubernetes-daemonset
18-
1914
### Installation
2015

2116
Use the [Helm](https://helm.sh/) chart:

chart/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
image:
22
repository: deliveryherotech/k8s-event-logger
3-
tag: "1.1"
3+
tag: "1.2"
44
pullPolicy: IfNotPresent
55

66
resources:

conf/Gemfile

Lines changed: 0 additions & 4 deletions
This file was deleted.

conf/entrypoint.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

conf/fluent.conf

Lines changed: 0 additions & 25 deletions
This file was deleted.

main.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"time"
8+
"os/user"
9+
"encoding/json"
10+
"k8s.io/api/core/v1"
11+
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/tools/clientcmd"
13+
"k8s.io/client-go/rest"
14+
"k8s.io/client-go/tools/cache"
15+
"k8s.io/apimachinery/pkg/fields"
16+
)
17+
18+
func main() {
19+
usr, err := user.Current()
20+
if err != nil {
21+
log.Fatal( err )
22+
}
23+
24+
var config *rest.Config
25+
26+
if k8s_port := os.Getenv("KUBERNETES_PORT"); k8s_port == "" {
27+
fmt.Println("Using local kubeconfig")
28+
var kubeconfig string
29+
home := usr.HomeDir
30+
if home != "" {
31+
kubeconfig = fmt.Sprintf("%s/.kube/config", home)
32+
} else {
33+
panic("home directory unknown")
34+
}
35+
36+
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
37+
if err != nil {
38+
panic(err.Error())
39+
}
40+
} else {
41+
fmt.Println("Using in cluster authentication")
42+
config, err = rest.InClusterConfig()
43+
if err != nil {
44+
panic(err.Error())
45+
}
46+
}
47+
48+
clientset, err := kubernetes.NewForConfig(config)
49+
if err != nil {
50+
panic(err.Error())
51+
}
52+
53+
watchlist := cache.NewListWatchFromClient(
54+
clientset.CoreV1().RESTClient(),
55+
"events",
56+
v1.NamespaceAll,
57+
fields.Everything(),
58+
)
59+
_, controller := cache.NewInformer(
60+
watchlist,
61+
&v1.Event{},
62+
0, //Duration is int64
63+
cache.ResourceEventHandlerFuncs{
64+
AddFunc: func(obj interface{}) {
65+
j, _ := json.Marshal(obj)
66+
fmt.Printf("%s\n", string(j))
67+
},
68+
},
69+
)
70+
71+
stop := make(chan struct{})
72+
defer close(stop)
73+
go controller.Run(stop)
74+
for {
75+
time.Sleep(time.Second)
76+
}
77+
78+
}

0 commit comments

Comments
 (0)