Skip to content

Commit d791fa3

Browse files
committed
Remove getters from ClusterBpfApplicationState and BpfApplicationState
Signed-off-by: Andreas Karis <[email protected]>
1 parent da74a9c commit d791fa3

File tree

11 files changed

+61
-60
lines changed

11 files changed

+61
-60
lines changed

apis/v1alpha1/bpf_application_state_types.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,3 @@ type BpfApplicationStateList struct {
158158
metav1.ListMeta `json:"metadata,omitempty"`
159159
Items []BpfApplicationState `json:"items"`
160160
}
161-
162-
func (an BpfApplicationState) GetName() string {
163-
return an.Name
164-
}
165-
166-
func (an BpfApplicationState) GetLabels() map[string]string {
167-
return an.Labels
168-
}
169-
170-
func (an BpfApplicationState) GetConditions() []metav1.Condition {
171-
return an.Status.Conditions
172-
}
173-
174-
func (anl BpfApplicationStateList) GetItems() []BpfApplicationState {
175-
return anl.Items
176-
}

apis/v1alpha1/cluster_bpf_application_state_types.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,3 @@ type ClusterBpfApplicationStateList struct {
209209
metav1.ListMeta `json:"metadata,omitempty"`
210210
Items []ClusterBpfApplicationState `json:"items"`
211211
}
212-
213-
func (an ClusterBpfApplicationState) GetName() string {
214-
return an.Name
215-
}
216-
217-
func (an ClusterBpfApplicationState) GetLabels() map[string]string {
218-
return an.Labels
219-
}
220-
221-
func (an ClusterBpfApplicationState) GetConditions() []metav1.Condition {
222-
return an.Status.Conditions
223-
}
224-
225-
func (anl ClusterBpfApplicationStateList) GetItems() []ClusterBpfApplicationState {
226-
return anl.Items
227-
}

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-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
@@ -129,17 +149,30 @@ func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
129149
}
130150
}
131151

152+
getBpfApplicationConditions := func(bpfAppState client.Object) ([]metav1.Condition, error) {
153+
switch v := bpfAppState.(type) {
154+
case *bpfmaniov1alpha1.ClusterBpfApplicationState:
155+
return v.Status.Conditions, nil
156+
case *bpfmaniov1alpha1.BpfApplicationState:
157+
return v.Status.Conditions, nil
158+
}
159+
return nil, fmt.Errorf("this error should never be triggered as only " +
160+
"*BpfApplicationReconciler | *BpfNsApplicationReconciler are valid types for rec")
161+
}
162+
132163
pendingBpfApplications := []string{}
133164
failedBpfApplications := []string{}
134165
finalApplied := []string{}
135166
// 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()) {
167+
for _, bpfAppState := range bpfAppStateList {
168+
if rec.containsFinalizer(bpfAppState, rec.getFinalizer()) {
139169
finalApplied = append(finalApplied, bpfAppState.GetName())
140170
}
141171

142-
conditions := bpfAppState.GetConditions()
172+
conditions, err := getBpfApplicationConditions(bpfAppState)
173+
if err != nil {
174+
return ctrl.Result{}, err
175+
}
143176
if bpfmanHelpers.IsBpfAppStateConditionFailure(conditions) {
144177
failedBpfApplications = append(failedBpfApplications, bpfAppState.GetName())
145178
} else if bpfmanHelpers.IsBpfAppStateConditionPending(conditions) {
@@ -169,7 +202,7 @@ func reconcileBpfApplication[T BpfProgOper, TL BpfProgListOper[T]](
169202
return rec.updateStatus(ctx, appNamespace, appName, bpfmaniov1alpha1.BpfAppCondSuccess, "")
170203
}
171204

172-
func (r *ReconcilerCommon[T, TL]) removeFinalizer(ctx context.Context, bpfApp client.Object, finalizer string) (ctrl.Result, error) {
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)

controllers/bpfman-operator/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func setupTestEnvironment(isOpenShift bool, recorder record.EventRecorder) (*Bpf
250250
// Set development Logger so we can see all logs in tests.
251251
logf.SetLogger(zap.New(zap.UseFlagOptions(&zap.Options{Development: true})))
252252

253-
rc := ReconcilerCommon[v1alpha1.ClusterBpfApplicationState, v1alpha1.ClusterBpfApplicationStateList]{
253+
rc := ReconcilerCommon{
254254
Client: cl,
255255
Scheme: s,
256256
}

controllers/bpfman-operator/ns_application_program_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func appNsProgramReconcile(t *testing.T, multiCondition bool) {
137137
// Create a fake client to mock API calls.
138138
cl := fake.NewClientBuilder().WithStatusSubresource(App).WithRuntimeObjects(objs...).Build()
139139

140-
rc := ReconcilerCommon[bpfmaniov1alpha1.BpfApplicationState, bpfmaniov1alpha1.BpfApplicationStateList]{
140+
rc := ReconcilerCommon{
141141
Client: cl,
142142
Scheme: s,
143143
}

0 commit comments

Comments
 (0)