forked from inspektor-gadget/inspektor-gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (128 loc) · 4.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2019-2021 The Inspektor Gadget authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/apimachinery/pkg/types"
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
"github.com/inspektor-gadget/inspektor-gadget/pkg/k8sutil"
)
var (
kubeconfig = flag.String("kubeconfig", "", "kubeconfig")
node = flag.String("node", "", "Node name")
client *kubernetes.Clientset
cc *containercollection.ContainerCollection
)
func publishEvent(c *containercollection.Container, reason, message string) {
eventTime := metav1.NewTime(time.Now())
event := &api.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v.%x", c.Podname, time.Now().UnixNano()),
Namespace: c.Namespace,
},
Source: api.EventSource{
Component: "KubeContainerCollection",
Host: *node,
},
Count: 1,
ReportingController: "github.com/inspektor-gadget/inspektor-gadget",
ReportingInstance: os.Getenv("POD_NAME"), // pod name
FirstTimestamp: eventTime,
LastTimestamp: eventTime,
InvolvedObject: api.ObjectReference{
Kind: "Pod",
Namespace: c.Namespace,
Name: c.Podname,
UID: types.UID(c.PodUID),
},
Type: api.EventTypeNormal,
Reason: reason,
Message: message,
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := client.CoreV1().Events(c.Namespace).Create(ctx, event, metav1.CreateOptions{}); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create event: %s\n", err)
}
}
func callback(notif containercollection.PubSubEvent) {
switch notif.Type {
case containercollection.EventTypeAddContainer:
fmt.Printf("Container added: %v pid %d\n", notif.Container.ID, notif.Container.Pid)
if notif.Container.OciConfig != nil {
config, err := json.Marshal(notif.Container.OciConfig)
if err != nil {
publishEvent(notif.Container, "CannotMarshalContainerConfig", err.Error())
} else {
publishEvent(notif.Container, "NewContainerConfig", string(config))
}
} else {
publishEvent(notif.Container, "ContainerConfigNotFound", "")
}
case containercollection.EventTypeRemoveContainer:
fmt.Printf("Container removed: %v pid %d\n", notif.Container.ID, notif.Container.Pid)
default:
return
}
}
func main() {
flag.Parse()
if *kubeconfig == "" && os.Getenv("KUBECONFIG") != "" {
*kubeconfig = os.Getenv("KUBECONFIG")
}
if *node == "" && os.Getenv("NODE_NAME") != "" {
*node = os.Getenv("NODE_NAME")
}
config, err := k8sutil.NewKubeConfig(*kubeconfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get kubeconfig: %s\n", err)
os.Exit(1)
}
client, err = kubernetes.NewForConfig(config)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get Kubernetes client set: %s\n", err)
os.Exit(1)
}
containerEventFuncs := []containercollection.FuncNotify{callback}
opts := []containercollection.ContainerCollectionOption{
containercollection.WithPubSub(containerEventFuncs...),
containercollection.WithCgroupEnrichment(),
containercollection.WithKubernetesEnrichment(*node, config),
containercollection.WithRuncFanotify(),
}
cc = &containercollection.ContainerCollection{}
err = cc.Initialize(opts...)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize container collection: %s\n", err)
os.Exit(1)
}
fmt.Printf("Ready\n")
cc.ContainerRange(func(c *containercollection.Container) {
fmt.Printf("%+v\n", c)
})
// Graceful shutdown
exit := make(chan os.Signal, 1)
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM)
<-exit
}