-
Notifications
You must be signed in to change notification settings - Fork 8
/
helpers.go
328 lines (266 loc) · 9.43 KB
/
helpers.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright 2023.
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 helpers
import (
"context"
"fmt"
"time"
bpfmaniov1alpha1 "github.com/bpfman/bpfman-operator/apis/v1alpha1"
bpfmanclientset "github.com/bpfman/bpfman-operator/pkg/client/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
)
// Must match the internal bpfman-api mappings
type ProgramType int32
const (
Kprobe ProgramType = 2
Tc ProgramType = 3
Tracepoint ProgramType = 5
Xdp ProgramType = 6
Tracing ProgramType = 26
)
func (p ProgramType) Uint32() *uint32 {
progTypeInt := uint32(p)
return &progTypeInt
}
func FromString(p string) (*ProgramType, error) {
var programType ProgramType
switch p {
case "kprobe":
programType = Kprobe
case "tc":
programType = Tc
case "xdp":
programType = Xdp
case "tracepoint":
programType = Tracepoint
case "tracing":
programType = Tracing
default:
return nil, fmt.Errorf("unknown program type: %s", p)
}
return &programType, nil
}
func (p ProgramType) String() string {
switch p {
case Kprobe:
return "kprobe"
case Tc:
return "tc"
case Xdp:
return "xdp"
case Tracepoint:
return "tracepoint"
case Tracing:
return "tracing"
default:
return ""
}
}
type TcProgramDirection int32
const (
Ingress TcProgramDirection = 1
Egress TcProgramDirection = 2
)
func (t TcProgramDirection) String() string {
switch t {
case Ingress:
return "ingress"
case Egress:
return "egress"
default:
return ""
}
}
var log = ctrl.Log.WithName("bpfman-helpers")
// getk8sConfig gets a kubernetes config automatically detecting if it should
// be the in or out of cluster config. If this step fails panic.
func getk8sConfigOrDie() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
kubeConfig :=
clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
config, err = clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
panic(err)
}
log.Info("Program running from outside of the cluster, picking config from --kubeconfig flag")
} else {
log.Info("Program running inside the cluster, picking the in-cluster configuration")
}
return config
}
// GetClientOrDie gets the bpfman Kubernetes Client dynamically switching between in cluster and out of
// cluster config setup.
func GetClientOrDie() *bpfmanclientset.Clientset {
return bpfmanclientset.NewForConfigOrDie(getk8sConfigOrDie())
}
// Returns true if loaded. False if not. Also returns the condition type.
func isProgLoaded(conditions *[]metav1.Condition) (bool, string) {
// Get most recent condition
conLen := len(*conditions)
if conLen <= 0 {
return false, "None"
}
condition := (*conditions)[0]
if condition.Type != string(bpfmaniov1alpha1.ProgramReconcileSuccess) {
return false, condition.Type
}
return true, condition.Type
}
func isKprobebpfmanProgLoaded(c *bpfmanclientset.Clientset, progConfName string) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
log.Info(".") // progress bar!
bpfProgConfig, err := c.BpfmanV1alpha1().KprobePrograms().Get(ctx, progConfName, metav1.GetOptions{})
if err != nil {
return false, err
}
progLoaded, condType := isProgLoaded(&bpfProgConfig.Status.Conditions)
if !progLoaded {
log.Info("kprobeProgram: %s not ready with condition: %s, waiting until timeout", progConfName, condType)
return false, nil
}
return true, nil
}
}
func isFentrybpfmanProgLoaded(c *bpfmanclientset.Clientset, progConfName string) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
log.Info(".") // progress bar!
bpfProgConfig, err := c.BpfmanV1alpha1().FentryPrograms().Get(ctx, progConfName, metav1.GetOptions{})
if err != nil {
return false, err
}
progLoaded, condType := isProgLoaded(&bpfProgConfig.Status.Conditions)
if !progLoaded {
log.Info("fentryProgram: %s not ready with condition: %s, waiting until timeout", progConfName, condType)
return false, nil
}
return true, nil
}
}
func isTcbpfmanProgLoaded(c *bpfmanclientset.Clientset, progConfName string) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
log.Info(".") // progress bar!
bpfProgConfig, err := c.BpfmanV1alpha1().TcPrograms().Get(ctx, progConfName, metav1.GetOptions{})
if err != nil {
return false, err
}
progLoaded, condType := isProgLoaded(&bpfProgConfig.Status.Conditions)
if !progLoaded {
log.Info("tcProgram: %s not ready with condition: %s, waiting until timeout", progConfName, condType)
return false, nil
}
return true, nil
}
}
func isTracepointbpfmanProgLoaded(c *bpfmanclientset.Clientset, progConfName string) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
log.Info(".") // progress bar!
bpfProgConfig, err := c.BpfmanV1alpha1().TracepointPrograms().Get(ctx, progConfName, metav1.GetOptions{})
if err != nil {
return false, err
}
progLoaded, condType := isProgLoaded(&bpfProgConfig.Status.Conditions)
if !progLoaded {
log.Info("tracepointProgram: %s not ready with condition: %s, waiting until timeout", progConfName, condType)
return false, nil
}
return true, nil
}
}
func isXdpbpfmanProgLoaded(c *bpfmanclientset.Clientset, progConfName string) wait.ConditionWithContextFunc {
return func(ctx context.Context) (bool, error) {
log.Info(".") // progress bar!
bpfProgConfig, err := c.BpfmanV1alpha1().XdpPrograms().Get(ctx, progConfName, metav1.GetOptions{})
if err != nil {
return false, err
}
progLoaded, condType := isProgLoaded(&bpfProgConfig.Status.Conditions)
if !progLoaded {
log.Info("xdpProgram: %s not ready with condition: %s, waiting until timeout", progConfName, condType)
return false, nil
}
return true, nil
}
}
// WaitForBpfProgConfLoad ensures the Program object is loaded and deployed successfully, specifically
// it checks the config objects' conditions to look for the `Loaded` state.
func WaitForBpfProgConfLoad(c *bpfmanclientset.Clientset, progName string, timeout time.Duration, progType ProgramType) error {
ctx := context.Background()
switch progType {
case Kprobe:
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, true, isKprobebpfmanProgLoaded(c, progName))
case Tc:
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, true, isTcbpfmanProgLoaded(c, progName))
case Xdp:
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, true, isXdpbpfmanProgLoaded(c, progName))
case Tracepoint:
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, true, isTracepointbpfmanProgLoaded(c, progName))
case Tracing:
return wait.PollUntilContextTimeout(ctx, time.Second, timeout, true, isFentrybpfmanProgLoaded(c, progName))
// TODO: case Uprobe: not covered. Since Uprobe has the same ProgramType as
// Kprobe, we need a different way to distinguish them. Options include
// creating an internal ProgramType for Uprobe or using a different
// identifier such as the string representation of the program type.
// TODO: case Fexit: not covered. Since Fexit has the same ProgramType as
// Fentry, we need a different way to distinguish them. Options include
// creating an internal ProgramType for Fexit or using a different
// identifier such as the string representation of the program type.
default:
return fmt.Errorf("unknown bpf program type: %s", progType)
}
}
// IsBpfmanDeployed is used to check for the existence of bpfman in a Kubernetes cluster. Specifically it checks for
// the existence of the bpfman.io CRD api group within the apiserver. If getting the k8s config fails this will panic.
func IsBpfmanDeployed() bool {
config := getk8sConfigOrDie()
client, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
panic(err)
}
apiList, err := client.ServerGroups()
if err != nil {
log.Info("issue occurred while fetching ServerGroups")
panic(err)
}
for _, v := range apiList.Groups {
if v.Name == "bpfman.io" {
log.Info("bpfman.io found in apis, bpfman is deployed")
return true
}
}
return false
}
func IsBpfProgramConditionFailure(conditions *[]metav1.Condition) bool {
if conditions == nil || *conditions == nil || len(*conditions) == 0 {
return true
}
numConditions := len(*conditions)
if numConditions > 1 {
// We should only ever have one condition so log a message, but
// still look at (*conditions)[0].
log.Info("more than one BpfProgramCondition", "numConditions", numConditions)
}
if (*conditions)[0].Type == string(bpfmaniov1alpha1.BpfProgCondNotLoaded) ||
(*conditions)[0].Type == string(bpfmaniov1alpha1.BpfProgCondNotUnloaded) ||
(*conditions)[0].Type == string(bpfmaniov1alpha1.BpfProgCondMapOwnerNotFound) ||
(*conditions)[0].Type == string(bpfmaniov1alpha1.BpfProgCondMapOwnerNotLoaded) ||
(*conditions)[0].Type == string(bpfmaniov1alpha1.BpfProgCondBytecodeSelectorError) {
return true
}
return false
}