@@ -14,23 +14,23 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package controllers
17
+ package featureflagconfiguration
18
18
19
19
import (
20
20
"context"
21
- "time"
22
21
23
22
"github.com/go-logr/logr"
23
+ "github.com/open-feature/open-feature-operator/controllers/common"
24
24
"github.com/open-feature/open-feature-operator/pkg/utils"
25
25
corev1 "k8s.io/api/core/v1"
26
26
"k8s.io/apimachinery/pkg/api/errors"
27
- "k8s.io/client-go/tools/record "
27
+ "sigs. k8s.io/controller-runtime/pkg/builder "
28
28
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
29
+ "sigs.k8s.io/controller-runtime/pkg/predicate"
29
30
30
31
"k8s.io/apimachinery/pkg/runtime"
31
32
ctrl "sigs.k8s.io/controller-runtime"
32
33
"sigs.k8s.io/controller-runtime/pkg/client"
33
- "sigs.k8s.io/controller-runtime/pkg/log"
34
34
35
35
corev1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
36
36
)
@@ -41,8 +41,6 @@ type FeatureFlagConfigurationReconciler struct {
41
41
42
42
// Scheme contains the scheme of this controller
43
43
Scheme * runtime.Scheme
44
- // Recorder contains the Recorder of this controller
45
- Recorder record.EventRecorder
46
44
// ReqLogger contains the Logger of this controller
47
45
Log logr.Logger
48
46
}
@@ -61,42 +59,36 @@ type FeatureFlagConfigurationReconciler struct {
61
59
// For more details, check Reconcile and its Result here:
62
60
// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile
63
61
64
- const (
65
- crdName = "FeatureFlagConfiguration"
66
- reconcileErrorInterval = 10 * time .Second
67
- reconcileSuccessInterval = 120 * time .Second
68
- finalizerName = "featureflagconfiguration.core.openfeature.dev/finalizer"
69
- )
62
+ const CrdName = "FeatureFlagConfiguration"
70
63
71
64
func (r * FeatureFlagConfigurationReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
72
- r .Log = log .FromContext (ctx )
73
- r .Log .Info ("Reconciling" + crdName )
65
+ r .Log .Info ("Reconciling" + CrdName )
74
66
75
67
ffconf := & corev1alpha1.FeatureFlagConfiguration {}
76
68
if err := r .Client .Get (ctx , req .NamespacedName , ffconf ); err != nil {
77
69
if errors .IsNotFound (err ) {
78
70
// taking down all associated K8s resources is handled by K8s
79
- r .Log .Info (crdName + " resource not found. Ignoring since object must be deleted" )
71
+ r .Log .Info (CrdName + " resource not found. Ignoring since object must be deleted" )
80
72
return r .finishReconcile (nil , false )
81
73
}
82
- r .Log .Error (err , "Failed to get the " + crdName )
74
+ r .Log .Error (err , "Failed to get the " + CrdName )
83
75
return r .finishReconcile (err , false )
84
76
}
85
77
86
78
if ffconf .ObjectMeta .DeletionTimestamp .IsZero () {
87
79
// The object is not being deleted, so if it does not have our finalizer,
88
80
// then lets add the finalizer and update the object. This is equivalent
89
81
// registering our finalizer.
90
- if ! utils .ContainsString (ffconf .GetFinalizers (), finalizerName ) {
91
- controllerutil .AddFinalizer (ffconf , finalizerName )
82
+ if ! utils .ContainsString (ffconf .GetFinalizers (), common . FinalizerName ) {
83
+ controllerutil .AddFinalizer (ffconf , common . FinalizerName )
92
84
if err := r .Update (ctx , ffconf ); err != nil {
93
85
return r .finishReconcile (err , false )
94
86
}
95
87
}
96
88
} else {
97
89
// The object is being deleted
98
- if utils .ContainsString (ffconf .GetFinalizers (), finalizerName ) {
99
- controllerutil .RemoveFinalizer (ffconf , finalizerName )
90
+ if utils .ContainsString (ffconf .GetFinalizers (), common . FinalizerName ) {
91
+ controllerutil .RemoveFinalizer (ffconf , common . FinalizerName )
100
92
if err := r .Update (ctx , ffconf ); err != nil {
101
93
return ctrl.Result {}, err
102
94
}
@@ -106,7 +98,7 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
106
98
}
107
99
108
100
// Check the provider on the FeatureFlagConfiguration
109
- if ffconf .Spec .ServiceProvider == nil {
101
+ if ! ffconf .Spec .ServiceProvider . IsSet () {
110
102
r .Log .Info ("No service provider specified for FeatureFlagConfiguration, using FlagD" )
111
103
ffconf .Spec .ServiceProvider = & corev1alpha1.FeatureFlagServiceProvider {
112
104
Name : "flagd" ,
@@ -161,28 +153,20 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
161
153
return r .finishReconcile (nil , false )
162
154
}
163
155
164
- // SetupWithManager sets up the controller with the Manager.
165
- func (r * FeatureFlagConfigurationReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
166
- return ctrl .NewControllerManagedBy (mgr ).
167
- For (& corev1alpha1.FeatureFlagConfiguration {}).
168
- Owns (& corev1.ConfigMap {}).
169
- Complete (r )
170
- }
171
-
172
156
func (r * FeatureFlagConfigurationReconciler ) finishReconcile (err error , requeueImmediate bool ) (ctrl.Result , error ) {
173
157
if err != nil {
174
- interval := reconcileErrorInterval
158
+ interval := common . ReconcileErrorInterval
175
159
if requeueImmediate {
176
160
interval = 0
177
161
}
178
- r .Log .Error (err , "Finished Reconciling " + crdName + " with error: %w" )
162
+ r .Log .Error (err , "Finished Reconciling " + CrdName + " with error: %w" )
179
163
return ctrl.Result {Requeue : true , RequeueAfter : interval }, err
180
164
}
181
- interval := reconcileSuccessInterval
165
+ interval := common . ReconcileSuccessInterval
182
166
if requeueImmediate {
183
167
interval = 0
184
168
}
185
- r .Log .Info ("Finished Reconciling " + crdName )
169
+ r .Log .Info ("Finished Reconciling " + CrdName )
186
170
return ctrl.Result {Requeue : true , RequeueAfter : interval }, nil
187
171
}
188
172
@@ -194,3 +178,11 @@ func (r *FeatureFlagConfigurationReconciler) featureFlagResourceIsOwner(ff *core
194
178
}
195
179
return false
196
180
}
181
+
182
+ // SetupWithManager sets up the controller with the Manager.
183
+ func (r * FeatureFlagConfigurationReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
184
+ return ctrl .NewControllerManagedBy (mgr ).
185
+ For (& corev1alpha1.FeatureFlagConfiguration {}, builder .WithPredicates (predicate.GenerationChangedPredicate {})).
186
+ Owns (& corev1.ConfigMap {}).
187
+ Complete (r )
188
+ }
0 commit comments