Skip to content

Commit

Permalink
Merge pull request #37 from anfredette/bpfprogram-name
Browse files Browse the repository at this point in the history
More concise naming scheme for BpfPrograms
  • Loading branch information
mergify[bot] authored Jul 9, 2024
2 parents 2a8ce1b + 5b85c5d commit 3906a80
Show file tree
Hide file tree
Showing 27 changed files with 283 additions and 170 deletions.
61 changes: 34 additions & 27 deletions controllers/bpfman-agent/application-program.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,22 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
var err error
var complete bool

namePrefix := func(
buildProgramName := func(
app bpfmaniov1alpha1.BpfApplication,
prog bpfmaniov1alpha1.BpfApplicationProgram) string {
return app.Name + "-" + strings.ToLower(string(prog.Type)) + "-"
return app.Name + "-" + strings.ToLower(string(prog.Type))
}

for i, a := range appPrograms.Items {
var appProgramMap = make(map[string]bool)
for j, p := range a.Spec.Programs {
switch p.Type {
case bpfmaniov1alpha1.ProgTypeFentry:
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), sanitize(p.Fentry.FunctionName))
fentryProgram := bpfmaniov1alpha1.FentryProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + sanitize(p.Fentry.FunctionName),
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.FentryProgramSpec{
FentryProgramInfo: *p.Fentry,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -94,15 +95,16 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
fentryObjects := []client.Object{&fentryProgram}
appProgramMap[fentryProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile FentryProgram.
complete, res, err = r.reconcileCommon(ctx, rec, fentryObjects)

case bpfmaniov1alpha1.ProgTypeFexit:
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), sanitize(p.Fexit.FunctionName))
fexitProgram := bpfmaniov1alpha1.FexitProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + sanitize(p.Fexit.FunctionName),
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.FexitProgramSpec{
FexitProgramInfo: *p.Fexit,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -115,16 +117,17 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
fexitObjects := []client.Object{&fexitProgram}
appProgramMap[fexitProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile FexitProgram.
complete, res, err = r.reconcileCommon(ctx, rec, fexitObjects)

case bpfmaniov1alpha1.ProgTypeKprobe,
bpfmaniov1alpha1.ProgTypeKretprobe:
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), sanitize(p.Kprobe.FunctionName))
kprobeProgram := bpfmaniov1alpha1.KprobeProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + sanitize(p.Kprobe.FunctionName),
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.KprobeProgramSpec{
KprobeProgramInfo: *p.Kprobe,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -137,16 +140,17 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
kprobeObjects := []client.Object{&kprobeProgram}
appProgramMap[kprobeProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile KprobeProgram or KpretprobeProgram.
complete, res, err = r.reconcileCommon(ctx, rec, kprobeObjects)

case bpfmaniov1alpha1.ProgTypeUprobe,
bpfmaniov1alpha1.ProgTypeUretprobe:
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), sanitize(p.Uprobe.FunctionName))
uprobeProgram := bpfmaniov1alpha1.UprobeProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + sanitize(p.Uprobe.FunctionName),
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.UprobeProgramSpec{
UprobeProgramInfo: *p.Uprobe,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -159,15 +163,16 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
uprobeObjects := []client.Object{&uprobeProgram}
appProgramMap[uprobeProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile UprobeProgram or UpretprobeProgram.
complete, res, err = r.reconcileCommon(ctx, rec, uprobeObjects)

case bpfmaniov1alpha1.ProgTypeTracepoint:
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), sanitize(p.Tracepoint.Names[0]))
tracepointProgram := bpfmaniov1alpha1.TracepointProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + sanitize(p.Tracepoint.Names[0]),
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.TracepointProgramSpec{
TracepointProgramInfo: *p.Tracepoint,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -180,7 +185,7 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
tracepointObjects := []client.Object{&tracepointProgram}
appProgramMap[tracepointProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile TracepointProgram.
complete, res, err = r.reconcileCommon(ctx, rec, tracepointObjects)

Expand All @@ -192,10 +197,11 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
"app program name", a.Name, "program index", j)
continue
}
appProgramId := fmt.Sprintf("%s-%s-%s", strings.ToLower(string(p.Type)), p.TC.Direction, interfaces[0])
tcProgram := bpfmaniov1alpha1.TcProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + p.TC.Direction + "-" + interfaces[0],
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.TcProgramSpec{
TcProgramInfo: *p.TC,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -208,7 +214,7 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
tcObjects := []client.Object{&tcProgram}
appProgramMap[tcProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile TcProgram.
complete, res, err = r.reconcileCommon(ctx, rec, tcObjects)

Expand All @@ -219,10 +225,11 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
"app program name", a.Name, "program index", j)
continue
}
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), interfaces[0])
xdpProgram := bpfmaniov1alpha1.XdpProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + interfaces[0],
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},
Spec: bpfmaniov1alpha1.XdpProgramSpec{
XdpProgramInfo: *p.XDP,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -235,7 +242,7 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
rec.appOwner = &a
xdpObjects := []client.Object{&xdpProgram}
appProgramMap[xdpProgram.Name] = true
appProgramMap[appProgramId] = true
// Reconcile XdpProgram.
complete, res, err = r.reconcileCommon(ctx, rec, xdpObjects)

Expand All @@ -260,15 +267,15 @@ func (r *BpfApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
bpfPrograms := &bpfmaniov1alpha1.BpfProgramList{}
bpfDeletedPrograms := &bpfmaniov1alpha1.BpfProgramList{}
// find programs that need to be deleted and delete them
opts := []client.ListOption{client.MatchingLabels{internal.BpfProgramOwnerLabel: a.Name}}
opts := []client.ListOption{client.MatchingLabels{internal.BpfProgramOwner: a.Name}}
if err := r.List(ctx, bpfPrograms, opts...); err != nil {
ctxLogger.Error(err, "failed to get freshPrograms for full reconcile")
return ctrl.Result{}, err
}
for _, bpfProgram := range bpfPrograms.Items {
progName := bpfProgram.Labels[internal.BpfParentProgram]
if _, ok := appProgramMap[progName]; !ok {
ctxLogger.Info("Deleting BpfProgram", "BpfProgram", progName)
id := bpfProgram.Labels[internal.AppProgramId]
if _, ok := appProgramMap[id]; !ok {
ctxLogger.Info("Deleting BpfProgram", "AppProgramId", id, "BpfProgram", bpfProgram.Name)
bpfDeletedPrograms.Items = append(bpfDeletedPrograms.Items, bpfProgram)
}
}
Expand Down
23 changes: 12 additions & 11 deletions controllers/bpfman-agent/application-program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
// fentry program config
bpfFentryFunctionName = "fentry_test"
fentryFunctionName = "do_unlinkat"
fentryBpfProgName = fmt.Sprintf("%s-%s-%s-%s-%s", name, "fentry", "do-unlinkat", fakeNode.Name, "do-unlinkat")
fentryAppProgramId = fmt.Sprintf("%s-%s", "fentry", sanitize(fentryFunctionName))
fentryAttachPoint = sanitize(fentryFunctionName)
fentryBpfProg = &bpfmaniov1alpha1.BpfProgram{}
fentryFakeUID = "ef71d42c-aa21-48e8-a697-82391d801a81"
// kprobe program config
bpfKprobeFunctionName = "kprobe_test"
kprobeFunctionName = "try_to_wake_up"
kprobeBpfProgName = fmt.Sprintf("%s-%s-%s-%s-%s", name, "kprobe", "try-to-wake-up", fakeNode.Name, "try-to-wake-up")
kprobeAppProgramId = fmt.Sprintf("%s-%s", "kprobe", sanitize(kprobeFunctionName))
kprobeAttachPoint = sanitize(kprobeFunctionName)
kprobeBpfProg = &bpfmaniov1alpha1.BpfProgram{}
kprobeFakeUID = "ef71d42c-aa21-48e8-a697-82391d801a82"
kprobeOffset = 0
Expand Down Expand Up @@ -133,14 +135,14 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
}

// Check the BpfProgram Object was created successfully
err = cl.Get(ctx, types.NamespacedName{Name: fentryBpfProgName, Namespace: metav1.NamespaceAll}, fentryBpfProg)
err = rc.getBpfProgram(ctx, name, fentryAppProgramId, fentryAttachPoint, fentryBpfProg)
require.NoError(t, err)

require.NotEmpty(t, fentryBpfProg)
// Finalizer is written
require.Equal(t, internal.BpfApplicationControllerFinalizer, fentryBpfProg.Finalizers[0])
// owningConfig Label was correctly set
require.Equal(t, name, fentryBpfProg.Labels[internal.BpfProgramOwnerLabel])
require.Equal(t, name, fentryBpfProg.Labels[internal.BpfProgramOwner])
// node Label was correctly set
require.Equal(t, fakeNode.Name, fentryBpfProg.Labels[internal.K8sHostLabel])
// fentry function Annotation was correctly set
Expand Down Expand Up @@ -184,7 +186,7 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
}

// Check that the bpfProgram's programs was correctly updated
err = cl.Get(ctx, types.NamespacedName{Name: fentryBpfProgName, Namespace: metav1.NamespaceAll}, fentryBpfProg)
err = rc.getBpfProgram(ctx, name, fentryAppProgramId, fentryAttachPoint, fentryBpfProg)
require.NoError(t, err)

// prog ID should already have been set
Expand All @@ -208,7 +210,7 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
require.False(t, res.Requeue)

// Check that the bpfProgram's status was correctly updated
err = cl.Get(ctx, types.NamespacedName{Name: fentryBpfProgName, Namespace: metav1.NamespaceAll}, fentryBpfProg)
err = rc.getBpfProgram(ctx, name, fentryAppProgramId, fentryAttachPoint, fentryBpfProg)
require.NoError(t, err)

require.Equal(t, string(bpfmaniov1alpha1.BpfProgCondLoaded), fentryBpfProg.Status.Conditions[0].Type)
Expand All @@ -220,14 +222,14 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
t.Fatalf("reconcile: (%v)", err)
}

err = cl.Get(ctx, types.NamespacedName{Name: kprobeBpfProgName, Namespace: metav1.NamespaceAll}, kprobeBpfProg)
err = rc.getBpfProgram(ctx, name, kprobeAppProgramId, kprobeAttachPoint, kprobeBpfProg)
require.NoError(t, err)

require.NotEmpty(t, kprobeBpfProg)
// Finalizer is written
require.Equal(t, internal.BpfApplicationControllerFinalizer, kprobeBpfProg.Finalizers[0])
// owningConfig Label was correctly set
require.Equal(t, name, kprobeBpfProg.Labels[internal.BpfProgramOwnerLabel])
require.Equal(t, name, kprobeBpfProg.Labels[internal.BpfProgramOwner])
// node Label was correctly set
require.Equal(t, fakeNode.Name, kprobeBpfProg.Labels[internal.K8sHostLabel])
// fentry function Annotation was correctly set
Expand Down Expand Up @@ -273,7 +275,7 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
}

// Check that the bpfProgram's programs was correctly updated
err = cl.Get(ctx, types.NamespacedName{Name: kprobeBpfProgName, Namespace: metav1.NamespaceAll}, kprobeBpfProg)
err = rc.getBpfProgram(ctx, name, kprobeAppProgramId, kprobeAttachPoint, kprobeBpfProg)
require.NoError(t, err)

// prog ID should already have been set
Expand All @@ -297,9 +299,8 @@ func TestBpfApplicationControllerCreate(t *testing.T) {
require.False(t, res.Requeue)

// Check that the bpfProgram's status was correctly updated
err = cl.Get(ctx, types.NamespacedName{Name: kprobeBpfProgName, Namespace: metav1.NamespaceAll}, kprobeBpfProg)
err = rc.getBpfProgram(ctx, name, kprobeAppProgramId, kprobeAttachPoint, kprobeBpfProg)
require.NoError(t, err)

require.Equal(t, string(bpfmaniov1alpha1.BpfProgCondLoaded), kprobeBpfProg.Status.Conditions[0].Type)

}
Loading

0 comments on commit 3906a80

Please sign in to comment.