Skip to content

Commit 63c1830

Browse files
committed
Remove getters from ClusterBpfApplicationState and BpfApplicationState
API review: remove BpfApplicationState GetClientObject Method GetClientObject is not used and removing it removes the dependency on sigs.k8s.io/controller-runtime/pkg/client Signed-off-by: Andreas Karis <[email protected]>
1 parent b8b1652 commit 63c1830

13 files changed

+65
-92
lines changed

apis/v1alpha1/bpf_application_state_types.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package v1alpha1
1818

1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
metav1types "k8s.io/apimachinery/pkg/types"
22-
"sigs.k8s.io/controller-runtime/pkg/client"
2321
)
2422

2523
// +union
@@ -160,31 +158,3 @@ type BpfApplicationStateList struct {
160158
metav1.ListMeta `json:"metadata,omitempty"`
161159
Items []BpfApplicationState `json:"items"`
162160
}
163-
164-
func (an BpfApplicationState) GetName() string {
165-
return an.Name
166-
}
167-
168-
func (an BpfApplicationState) GetUID() metav1types.UID {
169-
return an.UID
170-
}
171-
172-
func (an BpfApplicationState) GetAnnotations() map[string]string {
173-
return an.Annotations
174-
}
175-
176-
func (an BpfApplicationState) GetLabels() map[string]string {
177-
return an.Labels
178-
}
179-
180-
func (an BpfApplicationState) GetConditions() []metav1.Condition {
181-
return an.Status.Conditions
182-
}
183-
184-
func (an BpfApplicationState) GetClientObject() client.Object {
185-
return &an
186-
}
187-
188-
func (anl BpfApplicationStateList) GetItems() []BpfApplicationState {
189-
return anl.Items
190-
}

apis/v1alpha1/cluster_bpf_application_state_types.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package v1alpha1
1818

1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
metav1types "k8s.io/apimachinery/pkg/types"
22-
"sigs.k8s.io/controller-runtime/pkg/client"
2321
)
2422

2523
// +union
@@ -211,31 +209,3 @@ type ClusterBpfApplicationStateList struct {
211209
metav1.ListMeta `json:"metadata,omitempty"`
212210
Items []ClusterBpfApplicationState `json:"items"`
213211
}
214-
215-
func (an ClusterBpfApplicationState) GetName() string {
216-
return an.Name
217-
}
218-
219-
func (an ClusterBpfApplicationState) GetUID() metav1types.UID {
220-
return an.UID
221-
}
222-
223-
func (an ClusterBpfApplicationState) GetAnnotations() map[string]string {
224-
return an.Annotations
225-
}
226-
227-
func (an ClusterBpfApplicationState) GetLabels() map[string]string {
228-
return an.Labels
229-
}
230-
231-
func (an ClusterBpfApplicationState) GetConditions() []metav1.Condition {
232-
return an.Status.Conditions
233-
}
234-
235-
func (an ClusterBpfApplicationState) GetClientObject() client.Object {
236-
return &an
237-
}
238-
239-
func (anl ClusterBpfApplicationStateList) GetItems() []ClusterBpfApplicationState {
240-
return anl.Items
241-
}

cmd/bpfman-operator/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func main() {
162162
}
163163
}
164164

165-
commonApp := bpfmanoperator.ReconcilerCommon[bpfmaniov1alpha1.ClusterBpfApplicationState, bpfmaniov1alpha1.ClusterBpfApplicationStateList]{
165+
commonApp := bpfmanoperator.ReconcilerCommon{
166166
Client: mgr.GetClient(),
167167
Scheme: mgr.GetScheme(),
168168
}
@@ -171,7 +171,7 @@ func main() {
171171
ReconcilerCommon: commonApp,
172172
}
173173

174-
commonNsApp := bpfmanoperator.ReconcilerCommon[bpfmaniov1alpha1.BpfApplicationState, bpfmaniov1alpha1.BpfApplicationStateList]{
174+
commonNsApp := bpfmanoperator.ReconcilerCommon{
175175
Client: mgr.GetClient(),
176176
Scheme: mgr.GetScheme(),
177177
}

controllers/bpfman-agent/cl_application_program.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (r *ClBpfApplicationReconciler) getBpfAppState(ctx context.Context) (*bpfma
535535

536536
opts := []client.ListOption{
537537
client.MatchingLabels{
538-
internal.BpfAppStateOwner: r.currentApp.GetName(),
538+
internal.BpfAppStateOwner: r.currentApp.Name,
539539
internal.K8sHostLabel: r.NodeName,
540540
},
541541
}
@@ -566,7 +566,7 @@ func (r *ClBpfApplicationReconciler) initBpfAppState() error {
566566
Name: generateUniqueName(r.currentApp.Name),
567567
Finalizers: []string{r.finalizer},
568568
Labels: map[string]string{
569-
internal.BpfAppStateOwner: r.currentApp.GetName(),
569+
internal.BpfAppStateOwner: r.currentApp.Name,
570570
internal.K8sHostLabel: r.NodeName,
571571
},
572572
},

controllers/bpfman-agent/ns_application_program.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ func (r *NsBpfApplicationReconciler) getBpfAppState(ctx context.Context) (*bpfma
478478

479479
opts := []client.ListOption{
480480
client.MatchingLabels{
481-
internal.BpfAppStateOwner: r.currentApp.GetName(),
481+
internal.BpfAppStateOwner: r.currentApp.Name,
482482
internal.K8sHostLabel: r.NodeName,
483483
},
484484
}
@@ -510,7 +510,7 @@ func (r *NsBpfApplicationReconciler) initBpfAppState() error {
510510
Namespace: r.currentApp.Namespace,
511511
Finalizers: []string{r.finalizer},
512512
Labels: map[string]string{
513-
internal.BpfAppStateOwner: r.currentApp.GetName(),
513+
internal.BpfAppStateOwner: r.currentApp.Name,
514514
internal.K8sHostLabel: r.NodeName,
515515
},
516516
},

controllers/bpfman-operator/cl_application_program_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func appProgramReconcile(t *testing.T, multiCondition bool) {
178178
// Create a fake client to mock API calls.
179179
cl := fake.NewClientBuilder().WithStatusSubresource(app).WithRuntimeObjects(objs...).Build()
180180

181-
rc := ReconcilerCommon[bpfmaniov1alpha1.ClusterBpfApplicationState, bpfmaniov1alpha1.ClusterBpfApplicationStateList]{
181+
rc := ReconcilerCommon{
182182
Client: cl,
183183
Scheme: s,
184184
}

controllers/bpfman-operator/cl_application_programs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type BpfApplicationReconciler struct {
4444
}
4545

4646
//lint:ignore U1000 Linter claims function unused, but generics confusing linter
47-
func (r *BpfApplicationReconciler) getRecCommon() *ReconcilerCommon[bpfmaniov1alpha1.ClusterBpfApplicationState, bpfmaniov1alpha1.ClusterBpfApplicationStateList] {
47+
func (r *BpfApplicationReconciler) getRecCommon() *ReconcilerCommon {
4848
return &r.ClusterApplicationReconciler.ReconcilerCommon
4949
}
5050

controllers/bpfman-operator/common.go

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,23 @@ type BpfProgListOper[T any] interface {
5555
GetItems() []T
5656
}
5757

58-
type ReconcilerCommon[T BpfProgOper, TL BpfProgListOper[T]] struct {
58+
type ReconcilerCommon struct {
5959
client.Client
6060
Scheme *runtime.Scheme
6161
Logger logr.Logger
6262
}
6363

6464
// ApplicationReconciler defines a k8s reconciler which can program bpfman.
65-
type ApplicationReconciler[T BpfProgOper, TL BpfProgListOper[T]] interface {
65+
type ApplicationReconciler interface {
6666
// BPF Cluster of Namespaced Reconciler
6767
getAppStateList(ctx context.Context,
6868
appName string,
6969
appNamespace string,
70-
) (*TL, error)
71-
containsFinalizer(bpfApplication *T, finalizer string) bool
70+
) (client.ObjectList, error)
71+
containsFinalizer(bpfApplication client.Object, finalizer string) bool
7272

7373
// *Program Reconciler
74-
getRecCommon() *ReconcilerCommon[T, TL]
74+
getRecCommon() *ReconcilerCommon
7575
updateStatus(ctx context.Context,
7676
namespace string,
7777
name string,
@@ -80,9 +80,23 @@ type ApplicationReconciler[T BpfProgOper, TL BpfProgListOper[T]] interface {
8080
getFinalizer() string
8181
}
8282

83-
func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
83+
// ValidReconciler encompasses BpfApplicationReconciler and BpfNSApplicationReconciler.
84+
type ValidReconciler interface {
85+
ValidReconcilerMethods
86+
*BpfApplicationReconciler | *BpfNsApplicationReconciler
87+
}
88+
89+
type ValidReconcilerMethods interface {
90+
getRecCommon() *ReconcilerCommon
91+
getAppStateList(ctx context.Context, appName string, appNamespace string) (client.ObjectList, error)
92+
updateStatus(ctx context.Context, namespace string, name string, cond bpfmaniov1alpha1.BpfApplicationConditionType, message string) (ctrl.Result, error)
93+
containsFinalizer(bpfApplication client.Object, finalizer string) bool
94+
getFinalizer() string
95+
}
96+
97+
func reconcileBpfApplication[T ValidReconciler](
8498
ctx context.Context,
85-
rec ApplicationReconciler[T, TL],
99+
rec T,
86100
app client.Object,
87101
) (ctrl.Result, error) {
88102
r := rec.getRecCommon()
@@ -103,6 +117,12 @@ func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
103117
r.Logger.Error(err, "failed to get freshPrograms for full reconcile")
104118
return ctrl.Result{}, nil
105119
}
120+
var bpfAppStateList []client.Object
121+
_ = meta.EachListItem(bpfAppStateObjs, func(obj runtime.Object) error {
122+
appState := obj.(client.Object)
123+
bpfAppStateList = append(bpfAppStateList, appState)
124+
return nil
125+
})
106126

107127
// List all nodes since a BpfApplicationState object will always be created for each
108128
nodes := &corev1.NodeList{}
@@ -116,7 +136,7 @@ func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
116136
if app.GetDeletionTimestamp().IsZero() {
117137
for _, node := range nodes.Items {
118138
nodeFound := false
119-
for _, appState := range (*bpfAppStateObjs).GetItems() {
139+
for _, appState := range bpfAppStateList {
120140
bpfProgramState := appState.GetLabels()[internal.K8sHostLabel]
121141
if node.Name == bpfProgramState {
122142
nodeFound = true
@@ -133,13 +153,15 @@ func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
133153
failedBpfApplications := []string{}
134154
finalApplied := []string{}
135155
// Make sure no BpfApplications had any issues in the loading or unloading process
136-
for _, bpfAppState := range (*bpfAppStateObjs).GetItems() {
137-
138-
if rec.containsFinalizer(&bpfAppState, rec.getFinalizer()) {
156+
for _, bpfAppState := range bpfAppStateList {
157+
if rec.containsFinalizer(bpfAppState, rec.getFinalizer()) {
139158
finalApplied = append(finalApplied, bpfAppState.GetName())
140159
}
141160

142-
conditions := bpfAppState.GetConditions()
161+
conditions, err := getBpfApplicationConditions(bpfAppState)
162+
if err != nil {
163+
return ctrl.Result{}, err
164+
}
143165
if bpfmanHelpers.IsBpfAppStateConditionFailure(conditions) {
144166
failedBpfApplications = append(failedBpfApplications, bpfAppState.GetName())
145167
} else if bpfmanHelpers.IsBpfAppStateConditionPending(conditions) {
@@ -169,7 +191,18 @@ func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
169191
return rec.updateStatus(ctx, appNamespace, appName, bpfmaniov1alpha1.BpfAppCondSuccess, "")
170192
}
171193

172-
func (r *ReconcilerCommon[T, TL]) removeFinalizer(ctx context.Context, bpfApp client.Object, finalizer string) (ctrl.Result, error) {
194+
func getBpfApplicationConditions(bpfAppState client.Object) ([]metav1.Condition, error) {
195+
switch v := bpfAppState.(type) {
196+
case *bpfmaniov1alpha1.ClusterBpfApplicationState:
197+
return v.Status.Conditions, nil
198+
case *bpfmaniov1alpha1.BpfApplicationState:
199+
return v.Status.Conditions, nil
200+
}
201+
return nil, fmt.Errorf("this error should never be triggered as only " +
202+
"*BpfApplicationReconciler | *BpfNsApplicationReconciler are valid types for rec")
203+
}
204+
205+
func (r *ReconcilerCommon) removeFinalizer(ctx context.Context, bpfApp client.Object, finalizer string) (ctrl.Result, error) {
173206
r.Logger.Info("Calling KubeAPI to delete Program Finalizer", "Type", bpfApp.GetObjectKind().GroupVersionKind().Kind, "Name", bpfApp.GetName())
174207

175208
if changed := controllerutil.RemoveFinalizer(bpfApp, finalizer); changed {
@@ -183,7 +216,7 @@ func (r *ReconcilerCommon[T, TL]) removeFinalizer(ctx context.Context, bpfApp cl
183216
return ctrl.Result{}, nil
184217
}
185218

186-
func (r *ReconcilerCommon[T, TL]) addFinalizer(ctx context.Context, app client.Object, finalizer string) (ctrl.Result, error) {
219+
func (r *ReconcilerCommon) addFinalizer(ctx context.Context, app client.Object, finalizer string) (ctrl.Result, error) {
187220
controllerutil.AddFinalizer(app, finalizer)
188221

189222
r.Logger.Info("Calling KubeAPI to add Program Finalizer", "Type", app.GetObjectKind().GroupVersionKind().Kind, "Name", app.GetName())
@@ -196,7 +229,7 @@ func (r *ReconcilerCommon[T, TL]) addFinalizer(ctx context.Context, app client.O
196229
return ctrl.Result{}, nil
197230
}
198231

199-
func (r *ReconcilerCommon[T, TL]) updateCondition(
232+
func (r *ReconcilerCommon) updateCondition(
200233
ctx context.Context,
201234
obj client.Object,
202235
conditions *[]metav1.Condition,

controllers/bpfman-operator/common_cluster.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ import (
3030
)
3131

3232
type ClusterApplicationReconciler struct {
33-
ReconcilerCommon[bpfmaniov1alpha1.ClusterBpfApplicationState, bpfmaniov1alpha1.ClusterBpfApplicationStateList]
33+
ReconcilerCommon
3434
}
3535

3636
//lint:ignore U1000 Linter claims function unused, but generics confusing linter
3737
func (r *ClusterApplicationReconciler) getAppStateList(
3838
ctx context.Context,
3939
appName string,
4040
_appNamespace string,
41-
) (*bpfmaniov1alpha1.ClusterBpfApplicationStateList, error) {
41+
) (client.ObjectList, error) {
4242

4343
appStateList := &bpfmaniov1alpha1.ClusterBpfApplicationStateList{}
4444

@@ -57,7 +57,7 @@ func (r *ClusterApplicationReconciler) getAppStateList(
5757

5858
//lint:ignore U1000 Linter claims function unused, but generics confusing linter
5959
func (r *ClusterApplicationReconciler) containsFinalizer(
60-
bpfAppState *bpfmaniov1alpha1.ClusterBpfApplicationState,
60+
bpfAppState client.Object,
6161
finalizer string,
6262
) bool {
6363
return controllerutil.ContainsFinalizer(bpfAppState, finalizer)

controllers/bpfman-operator/common_namespace.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ import (
3030
)
3131

3232
type NamespaceApplicationReconciler struct {
33-
ReconcilerCommon[bpfmaniov1alpha1.BpfApplicationState, bpfmaniov1alpha1.BpfApplicationStateList]
33+
ReconcilerCommon
3434
}
3535

3636
//lint:ignore U1000 Linter claims function unused, but generics confusing linter
3737
func (r *NamespaceApplicationReconciler) getAppStateList(
3838
ctx context.Context,
3939
appName string,
4040
appNamespace string,
41-
) (*bpfmaniov1alpha1.BpfApplicationStateList, error) {
41+
) (client.ObjectList, error) {
4242

4343
appStateList := &bpfmaniov1alpha1.BpfApplicationStateList{}
4444

@@ -58,7 +58,7 @@ func (r *NamespaceApplicationReconciler) getAppStateList(
5858

5959
//lint:ignore U1000 Linter claims function unused, but generics confusing linter
6060
func (r *NamespaceApplicationReconciler) containsFinalizer(
61-
bpfAppState *bpfmaniov1alpha1.BpfApplicationState,
61+
bpfAppState client.Object,
6262
finalizer string,
6363
) bool {
6464
return controllerutil.ContainsFinalizer(bpfAppState, finalizer)

0 commit comments

Comments
 (0)