Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More concise naming scheme for BpfPrograms #37

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}
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))

Check warning on line 103 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L103

Added line #L103 was not covered by tests
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}},

Check warning on line 107 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L106-L107

Added lines #L106 - L107 were not covered by tests
Spec: bpfmaniov1alpha1.FexitProgramSpec{
FexitProgramInfo: *p.Fexit,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -115,16 +117,17 @@
}
rec.appOwner = &a
fexitObjects := []client.Object{&fexitProgram}
appProgramMap[fexitProgram.Name] = true
appProgramMap[appProgramId] = true

Check warning on line 120 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L120

Added line #L120 was not covered by tests
// 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 @@
}
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))

Check warning on line 149 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L149

Added line #L149 was not covered by tests
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}},

Check warning on line 153 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L152-L153

Added lines #L152 - L153 were not covered by tests
Spec: bpfmaniov1alpha1.UprobeProgramSpec{
UprobeProgramInfo: *p.Uprobe,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -159,15 +163,16 @@
}
rec.appOwner = &a
uprobeObjects := []client.Object{&uprobeProgram}
appProgramMap[uprobeProgram.Name] = true
appProgramMap[appProgramId] = true

Check warning on line 166 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L166

Added line #L166 was not covered by tests
// 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]))

Check warning on line 171 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L171

Added line #L171 was not covered by tests
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}},

Check warning on line 175 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L174-L175

Added lines #L174 - L175 were not covered by tests
Spec: bpfmaniov1alpha1.TracepointProgramSpec{
TracepointProgramInfo: *p.Tracepoint,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -180,7 +185,7 @@
}
rec.appOwner = &a
tracepointObjects := []client.Object{&tracepointProgram}
appProgramMap[tracepointProgram.Name] = true
appProgramMap[appProgramId] = true

Check warning on line 188 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L188

Added line #L188 was not covered by tests
// Reconcile TracepointProgram.
complete, res, err = r.reconcileCommon(ctx, rec, tracepointObjects)

Expand All @@ -192,10 +197,11 @@
"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])

Check warning on line 200 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L200

Added line #L200 was not covered by tests
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}},

Check warning on line 204 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L203-L204

Added lines #L203 - L204 were not covered by tests
Spec: bpfmaniov1alpha1.TcProgramSpec{
TcProgramInfo: *p.TC,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -208,7 +214,7 @@
}
rec.appOwner = &a
tcObjects := []client.Object{&tcProgram}
appProgramMap[tcProgram.Name] = true
appProgramMap[appProgramId] = true

Check warning on line 217 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L217

Added line #L217 was not covered by tests
// Reconcile TcProgram.
complete, res, err = r.reconcileCommon(ctx, rec, tcObjects)

Expand All @@ -219,10 +225,11 @@
"app program name", a.Name, "program index", j)
continue
}
appProgramId := fmt.Sprintf("%s-%s", strings.ToLower(string(p.Type)), interfaces[0])

Check warning on line 228 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L228

Added line #L228 was not covered by tests
xdpProgram := bpfmaniov1alpha1.XdpProgram{
ObjectMeta: metav1.ObjectMeta{
Name: namePrefix(a, p) + interfaces[0],
},
Name: buildProgramName(a, p),
Labels: map[string]string{internal.AppProgramId: appProgramId}},

Check warning on line 232 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L231-L232

Added lines #L231 - L232 were not covered by tests
Spec: bpfmaniov1alpha1.XdpProgramSpec{
XdpProgramInfo: *p.XDP,
BpfAppCommon: a.Spec.BpfAppCommon,
Expand All @@ -235,7 +242,7 @@
}
rec.appOwner = &a
xdpObjects := []client.Object{&xdpProgram}
appProgramMap[xdpProgram.Name] = true
appProgramMap[appProgramId] = true

Check warning on line 245 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L245

Added line #L245 was not covered by tests
// Reconcile XdpProgram.
complete, res, err = r.reconcileCommon(ctx, rec, xdpObjects)

Expand All @@ -260,15 +267,15 @@
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}}

Check warning on line 270 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L270

Added line #L270 was not covered by tests
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)

Check warning on line 278 in controllers/bpfman-agent/application-program.go

View check run for this annotation

Codecov / codecov/patch

controllers/bpfman-agent/application-program.go#L276-L278

Added lines #L276 - L278 were not covered by tests
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
Loading