-
Notifications
You must be signed in to change notification settings - Fork 188
/
interface.go
172 lines (141 loc) · 4.62 KB
/
interface.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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 gadgets
import (
"errors"
"fmt"
"sync"
"github.com/cilium/ebpf"
gadgetv1alpha1 "github.com/inspektor-gadget/inspektor-gadget/pkg/apis/gadget/v1alpha1"
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
log "github.com/sirupsen/logrus"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type TraceFactory interface {
// Initialize gives the Helpers and the Client to the gadget. Gadgets
// don't need to implement this method if they use BaseFactory as an
// anonymous field.
Initialize(Helpers GadgetHelpers, Client client.Client)
// Delete request a gadget to release the information it has about a
// trace. BaseFactory implements this method, so gadgets who embed
// BaseFactory don't need to implement it.
Delete(name string)
// Operations gives the list of operations on a gadget that users can
// call via the gadget.kinvolk.io/operation annotation.
Operations() map[gadgetv1alpha1.Operation]TraceOperation
// OutputModesSupported returns the set of OutputMode supported by the
// gadget.
OutputModesSupported() map[gadgetv1alpha1.TraceOutputMode]struct{}
}
type TraceFactoryWithScheme interface {
TraceFactory
// AddToScheme let gadgets inform the Trace controller of any scheme
// they want to use
AddToScheme(*apimachineryruntime.Scheme)
}
type TraceFactoryWithDocumentation interface {
Description() string
}
// TraceOperation packages an operation on a gadget that users can call via the
// annotation gadget.kinvolk.io/operation.
type TraceOperation struct {
// Operation is the function called by the controller
Operation func(name string, trace *gadgetv1alpha1.Trace)
// Doc documents the operation. It is used to generate the
// documentation.
Doc string
// Order controls the ordering of the operation in the documentation.
// It's only needed when ordering alphabetically is not suitable.
Order int
}
// GadgetHelpers provides different functions that are used in the
// gadgets implementation.
type GadgetHelpers interface {
containercollection.ContainerResolver
gadgets.DataEnricher
PublishEvent(tracerID string, line string) error
TracerMountNsMap(tracerID string) (*ebpf.Map, error)
ContainersMap() *ebpf.Map
}
type BaseFactory struct {
Helpers GadgetHelpers
Client client.Client
// DeleteTrace is optionally set by gadgets if they need to do
// specialised clean up. Example:
//
// func NewFactory() gadgets.TraceFactory {
// return &TraceFactory{
// BaseFactory: gadgets.BaseFactory{DeleteTrace: deleteTrace},
// }
// }
DeleteTrace func(name string, trace interface{})
mu sync.Mutex
traces map[string]interface{}
}
func (f *BaseFactory) Initialize(r GadgetHelpers, c client.Client) {
f.Helpers = r
f.Client = c
}
func (f *BaseFactory) LookupOrCreate(name string, newTrace func() interface{}) interface{} {
f.mu.Lock()
defer f.mu.Unlock()
if f.traces == nil {
f.traces = make(map[string]interface{})
} else {
trace, ok := f.traces[name]
if ok {
return trace
}
}
if newTrace == nil {
return nil
}
trace := newTrace()
f.traces[name] = trace
return trace
}
func (f *BaseFactory) Lookup(name string) (interface{}, error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.traces == nil {
return nil, errors.New("traces map is nil")
}
trace, ok := f.traces[name]
if !ok {
return nil, fmt.Errorf("no trace for name %q", name)
}
return trace, nil
}
func (f *BaseFactory) Delete(name string) {
log.Infof("Deleting %s", name)
f.mu.Lock()
defer f.mu.Unlock()
trace, ok := f.traces[name]
if !ok {
log.Infof("Deleting %s: does not exist", name)
return
}
if f.DeleteTrace != nil {
f.DeleteTrace(name, trace)
}
delete(f.traces, name)
}
func (f *BaseFactory) Operations() map[gadgetv1alpha1.Operation]TraceOperation {
return map[gadgetv1alpha1.Operation]TraceOperation{}
}
func (f *BaseFactory) OutputModesSupported() map[gadgetv1alpha1.TraceOutputMode]struct{} {
return map[gadgetv1alpha1.TraceOutputMode]struct{}{}
}