Skip to content

Commit 6936c53

Browse files
c2ndevpoiana
authored andcommitted
fix(artifact): remove configMapKey, add configMap Indexer, restore artifact constants and types
Signed-off-by: cannarelladev <cannarella.dev@gmail.com>
1 parent ff5217c commit 6936c53

File tree

17 files changed

+266
-253
lines changed

17 files changed

+266
-253
lines changed

api/artifact/v1alpha1/rulesfile_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2025 The Falco Authors
1+
// Copyright (C) 2026 The Falco Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

api/common/v1alpha1/types.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2025 The Falco Authors
1+
// Copyright (C) 2026 The Falco Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -39,6 +39,11 @@ const (
3939
ConditionReconciled ConditionType = "ConditionReconciled"
4040
)
4141

42+
const (
43+
// ConfigMapRulesKey is the standard key used for rules data in ConfigMaps.
44+
ConfigMapRulesKey = "rules.yaml"
45+
)
46+
4247
// OCIArtifact defines the structure for specifying an OCI artifact reference.
4348
// +kubebuilder:object:generate=true
4449
type OCIArtifact struct {
@@ -72,8 +77,4 @@ type ConfigMapRef struct {
7277
// Name is the name of the ConfigMap.
7378
// +kubebuilder:validation:Required
7479
Name string `json:"name"`
75-
76-
// Key is the key in the ConfigMap to select.
77-
// +kubebuilder:validation:Required
78-
Key string `json:"key"`
7980
}

config/crd/bases/artifact.falcosecurity.dev_rulesfiles.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,10 @@ spec:
4343
description: ConfigMapRef specifies a reference to a ConfigMap containing
4444
the rules.
4545
properties:
46-
key:
47-
description: Key is the key in the ConfigMap to select.
48-
type: string
4946
name:
5047
description: Name is the name of the ConfigMap.
5148
type: string
5249
required:
53-
- key
5450
- name
5551
type: object
5652
inlineRules:

config/samples/artifact_v1alpha1_rulesfile_configmap.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ data:
3737
3838
---
3939
# Example Rulesfile using ConfigMap reference
40+
# Note: The ConfigMap must have a key named "rules.yaml" containing the rules content
4041
apiVersion: artifact.falcosecurity.dev/v1alpha1
4142
kind: Rulesfile
4243
metadata:
@@ -48,6 +49,5 @@ metadata:
4849
spec:
4950
configMapRef:
5051
name: custom-falco-rules
51-
key: rules.yaml
5252
priority: 60
5353

controllers/artifact/config/controller.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
//
1515
// SPDX-License-Identifier: Apache-2.0
1616

17-
// Package controller defines controllers' logic.
18-
1917
package config
2018

2119
import (

controllers/artifact/config/controller_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2025 The Falco Authors
1+
// Copyright (C) 2026 The Falco Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,8 +14,6 @@
1414
//
1515
// SPDX-License-Identifier: Apache-2.0
1616

17-
// Package controller defines controllers' logic.
18-
1917
package config
2018

2119
import (

controllers/artifact/config/suite_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2025 The Falco Authors
1+
// Copyright (C) 2026 The Falco Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,8 +14,6 @@
1414
//
1515
// SPDX-License-Identifier: Apache-2.0
1616

17-
// Package controller defines controllers' logic.
18-
1917
package config
2018

2119
import (

controllers/artifact/plugin/controller.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/falcosecurity/falco-operator/internal/pkg/artifact"
3434
"github.com/falcosecurity/falco-operator/internal/pkg/common"
3535
"github.com/falcosecurity/falco-operator/internal/pkg/controllerhelper"
36-
"github.com/falcosecurity/falco-operator/internal/pkg/filesystem"
3736
"github.com/falcosecurity/falco-operator/internal/pkg/priority"
3837
)
3938

@@ -278,8 +277,8 @@ type PluginsConfig struct {
278277
}
279278

280279
func (pc *PluginsConfig) addConfig(plugin *artifactv1alpha1.Plugin) {
281-
var config = PluginConfig{
282-
LibraryPath: artifact.Path(plugin.Name, priority.DefaultPriority, filesystem.MediumOCI, artifact.TypePlugin),
280+
config := PluginConfig{
281+
LibraryPath: artifact.Path(plugin.Name, priority.DefaultPriority, artifact.MediumOCI, artifact.TypePlugin),
283282
Name: plugin.Name,
284283
}
285284

@@ -359,16 +358,9 @@ func (pc *PluginsConfig) toString() (string, error) {
359358
if err != nil {
360359
return "", err
361360
}
362-
363-
// Convert the YAML to a string.
364-
yamlString := string(data)
365-
366-
return yamlString, nil
361+
return string(data), nil
367362
}
368363

369364
func (pc *PluginsConfig) isEmpty() bool {
370-
if len(pc.Configs) == 0 && len(pc.LoadPlugins) == 0 {
371-
return true
372-
}
373-
return false
365+
return len(pc.Configs) == 0 && len(pc.LoadPlugins) == 0
374366
}

controllers/artifact/rulesfile/controller.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
//
1515
// SPDX-License-Identifier: Apache-2.0
1616

17-
// Package controller defines controllers' logic.
18-
1917
package rulesfile
2018

2119
import (
@@ -25,12 +23,10 @@ import (
2523
apierrors "k8s.io/apimachinery/pkg/api/errors"
2624
"k8s.io/apimachinery/pkg/runtime"
2725
ctrl "sigs.k8s.io/controller-runtime"
28-
"sigs.k8s.io/controller-runtime/pkg/builder"
2926
"sigs.k8s.io/controller-runtime/pkg/client"
3027
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3128
"sigs.k8s.io/controller-runtime/pkg/handler"
3229
"sigs.k8s.io/controller-runtime/pkg/log"
33-
"sigs.k8s.io/controller-runtime/pkg/predicate"
3430
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3531

3632
artifactv1alpha1 "github.com/falcosecurity/falco-operator/api/artifact/v1alpha1"
@@ -42,6 +38,8 @@ import (
4238
const (
4339
// rulesfileFinalizerPrefix is the prefix for the finalizer name.
4440
rulesfileFinalizerPrefix = "rulesfile.artifact.falcosecurity.dev/finalizer"
41+
// configMapRefIndexField is the field used for indexing Rulesfiles by ConfigMap reference.
42+
configMapRefIndexField = ".spec.configMapRef.name"
4543
)
4644

4745
// NewRulesfileReconciler returns a new RulesfileReconciler.
@@ -115,41 +113,53 @@ func (r *RulesfileReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
115113

116114
// SetupWithManager sets up the controller with the Manager.
117115
func (r *RulesfileReconciler) SetupWithManager(mgr ctrl.Manager) error {
118-
// Filter ConfigMap watches to only the operator's namespace to reduce overhead.
119-
namespaceFilter := predicate.NewPredicateFuncs(func(object client.Object) bool {
120-
return object.GetNamespace() == r.namespace
121-
})
116+
// Create an index for Rulesfiles by ConfigMap reference for efficient lookups.
117+
if err := mgr.GetFieldIndexer().IndexField(
118+
context.Background(),
119+
&artifactv1alpha1.Rulesfile{},
120+
configMapRefIndexField,
121+
indexRulesfileByConfigMapRef,
122+
); err != nil {
123+
return err
124+
}
122125

123126
return ctrl.NewControllerManagedBy(mgr).
124127
For(&artifactv1alpha1.Rulesfile{}).
125128
Watches(
126129
&corev1.ConfigMap{},
127130
handler.EnqueueRequestsFromMapFunc(r.findRulesfilesForConfigMap),
128-
builder.WithPredicates(namespaceFilter),
129131
).
130132
Named("artifact-rulesfile").
131133
Complete(r)
132134
}
133135

134-
// findRulesfilesForConfigMap finds all Rulesfiles that reference a given ConfigMap.
136+
func indexRulesfileByConfigMapRef(obj client.Object) []string {
137+
rulesfile := obj.(*artifactv1alpha1.Rulesfile)
138+
if rulesfile.Spec.ConfigMapRef == nil {
139+
return nil
140+
}
141+
return []string{rulesfile.Namespace + "/" + rulesfile.Spec.ConfigMapRef.Name}
142+
}
143+
144+
// findRulesfilesForConfigMap finds all Rulesfiles that reference a given ConfigMap using the index.
135145
func (r *RulesfileReconciler) findRulesfilesForConfigMap(ctx context.Context, configMap client.Object) []reconcile.Request {
136146
logger := log.FromContext(ctx)
137147
rulesfileList := &artifactv1alpha1.RulesfileList{}
138148

139-
if err := r.List(ctx, rulesfileList, client.InNamespace(configMap.GetNamespace())); err != nil {
140-
logger.Error(err, "unable to list Rulesfiles")
149+
// Use the index to find Rulesfiles that reference this ConfigMap
150+
indexKey := configMap.GetNamespace() + "/" + configMap.GetName()
151+
if err := r.List(ctx, rulesfileList, client.MatchingFields{configMapRefIndexField: indexKey}); err != nil {
152+
logger.Error(err, "unable to list Rulesfiles by ConfigMap index")
141153
return []reconcile.Request{}
142154
}
143155

144-
var requests []reconcile.Request
156+
requests := make([]reconcile.Request, len(rulesfileList.Items))
145157
for i := range rulesfileList.Items {
146-
if rulesfileList.Items[i].Spec.ConfigMapRef != nil && rulesfileList.Items[i].Spec.ConfigMapRef.Name == configMap.GetName() {
147-
requests = append(requests, reconcile.Request{
148-
NamespacedName: client.ObjectKey{
149-
Name: rulesfileList.Items[i].Name,
150-
Namespace: rulesfileList.Items[i].Namespace,
151-
},
152-
})
158+
requests[i] = reconcile.Request{
159+
NamespacedName: client.ObjectKey{
160+
Name: rulesfileList.Items[i].Name,
161+
Namespace: rulesfileList.Items[i].Namespace,
162+
},
153163
}
154164
}
155165

@@ -188,7 +198,8 @@ func (r *RulesfileReconciler) ensureRulesfile(ctx context.Context, rulesfile *ar
188198
return err
189199
}
190200

191-
if err := r.artifactManager.StoreFromConfigMap(ctx, rulesfile.Name, p, rulesfile.Spec.ConfigMapRef, artifact.TypeRulesfile); err != nil {
201+
if err := r.artifactManager.StoreFromConfigMap(
202+
ctx, rulesfile.Name, rulesfile.Namespace, p, rulesfile.Spec.ConfigMapRef, artifact.TypeRulesfile); err != nil {
192203
return err
193204
}
194205

controllers/artifact/rulesfile/controller_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2025 The Falco Authors
1+
// Copyright (C) 2026 The Falco Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,8 +14,6 @@
1414
//
1515
// SPDX-License-Identifier: Apache-2.0
1616

17-
// Package controller defines controllers' logic.
18-
1917
package rulesfile
2018

2119
import (

0 commit comments

Comments
 (0)