@@ -36,9 +36,10 @@ import (
3636)
3737
3838const (
39- deploymentNameIndexField = "spec.deployment"
40- secretNameIndexField = "spec.secret"
41- finalizerName = "agent.finalizers.lightrun.com"
39+ deploymentNameIndexField = "spec.deployment"
40+ statefulSetNameIndexField = "spec.statefulset"
41+ secretNameIndexField = "spec.secret"
42+ finalizerName = "agent.finalizers.lightrun.com"
4243)
4344
4445var err error
@@ -56,19 +57,40 @@ type LightrunJavaAgentReconciler struct {
5657//+kubebuilder:rbac:groups=agents.lightrun.com,resources=lightrunjavaagents/finalizers,verbs=update
5758//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
5859//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;watch;list;patch
60+ //+kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;watch;list;patch
5961//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;watch;list
6062
6163func (r * LightrunJavaAgentReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
6264 log := r .Log .WithValues ("lightrunJavaAgent" , req .NamespacedName )
63- fieldManager := "lightrun-conrtoller"
6465 lightrunJavaAgent := & agentv1beta.LightrunJavaAgent {}
6566 if err = r .Get (ctx , req .NamespacedName , lightrunJavaAgent ); err != nil {
6667 return ctrl.Result {}, client .IgnoreNotFound (err )
6768 }
6869
70+ // Determine which workload type to reconcile
71+ if lightrunJavaAgent .Spec .DeploymentName != "" && lightrunJavaAgent .Spec .StatefulSetName != "" {
72+ log .Error (nil , "Both DeploymentName and StatefulSetName are set. Only one should be specified" )
73+ return r .errorStatus (ctx , lightrunJavaAgent , errors .New ("both deployment and statefulset specified" ))
74+ } else if lightrunJavaAgent .Spec .DeploymentName != "" {
75+ // Handle Deployment reconciliation (existing code)
76+ return r .reconcileDeployment (ctx , lightrunJavaAgent , req .Namespace )
77+ } else if lightrunJavaAgent .Spec .StatefulSetName != "" {
78+ // Handle StatefulSet reconciliation (to be implemented)
79+ return r .reconcileStatefulSet (ctx , lightrunJavaAgent , req .Namespace )
80+ } else {
81+ log .Error (nil , "Neither DeploymentName nor StatefulSetName is set" )
82+ return r .errorStatus (ctx , lightrunJavaAgent , errors .New ("no workload specified" ))
83+ }
84+ }
85+
86+ // reconcileDeployment handles the reconciliation logic for Deployment workloads
87+ func (r * LightrunJavaAgentReconciler ) reconcileDeployment (ctx context.Context , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , namespace string ) (ctrl.Result , error ) {
88+ log := r .Log .WithValues ("lightrunJavaAgent" , lightrunJavaAgent .Name , "deployment" , lightrunJavaAgent .Spec .DeploymentName )
89+ fieldManager := "lightrun-conrtoller"
90+
6991 deplNamespacedObj := client.ObjectKey {
7092 Name : lightrunJavaAgent .Spec .DeploymentName ,
71- Namespace : req . Namespace ,
93+ Namespace : namespace ,
7294 }
7395 originalDeployment := & appsv1.Deployment {}
7496 err = r .Get (ctx , deplNamespacedObj , originalDeployment )
@@ -106,7 +128,7 @@ func (r *LightrunJavaAgentReconciler) Reconcile(ctx context.Context, req ctrl.Re
106128 log .V (2 ).Info ("Searching for secret" , "Name" , lightrunJavaAgent .Spec .SecretName )
107129 secretNamespacedObj := client.ObjectKey {
108130 Name : lightrunJavaAgent .Spec .SecretName ,
109- Namespace : req . Namespace ,
131+ Namespace : namespace ,
110132 }
111133 secret = & corev1.Secret {}
112134 err = r .Get (ctx , secretNamespacedObj , secret )
@@ -289,6 +311,15 @@ func (r *LightrunJavaAgentReconciler) Reconcile(ctx context.Context, req ctrl.Re
289311 return r .successStatus (ctx , lightrunJavaAgent , reconcileTypeReady )
290312}
291313
314+ // reconcileStatefulSet handles the reconciliation logic for StatefulSet workloads
315+ func (r * LightrunJavaAgentReconciler ) reconcileStatefulSet (ctx context.Context , lightrunJavaAgent * agentv1beta.LightrunJavaAgent , namespace string ) (ctrl.Result , error ) {
316+ log := r .Log .WithValues ("lightrunJavaAgent" , lightrunJavaAgent .Name , "statefulSet" , lightrunJavaAgent .Spec .StatefulSetName )
317+
318+ // This is a placeholder for Phase 2 implementation
319+ log .Info ("StatefulSet reconciliation not yet implemented" , "StatefulSet" , lightrunJavaAgent .Spec .StatefulSetName )
320+ return r .errorStatus (ctx , lightrunJavaAgent , errors .New ("statefulset reconciliation not yet implemented" ))
321+ }
322+
292323// SetupWithManager sets up the controller with the Manager.
293324func (r * LightrunJavaAgentReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
294325 // Add spec.container_selector.deployment field to cache for future filtering
@@ -310,6 +341,25 @@ func (r *LightrunJavaAgentReconciler) SetupWithManager(mgr ctrl.Manager) error {
310341 return err
311342 }
312343
344+ // Add spec.container_selector.statefulset field to cache for future filtering
345+ err = mgr .GetFieldIndexer ().IndexField (
346+ context .Background (),
347+ & agentv1beta.LightrunJavaAgent {},
348+ statefulSetNameIndexField ,
349+ func (object client.Object ) []string {
350+ lightrunJavaAgent := object .(* agentv1beta.LightrunJavaAgent )
351+
352+ if lightrunJavaAgent .Spec .StatefulSetName == "" {
353+ return nil
354+ }
355+
356+ return []string {lightrunJavaAgent .Spec .StatefulSetName }
357+ })
358+
359+ if err != nil {
360+ return err
361+ }
362+
313363 // Add spec.container_selector.secret field to cache for future filtering
314364 err = mgr .GetFieldIndexer ().IndexField (
315365 context .Background (),
@@ -336,6 +386,10 @@ func (r *LightrunJavaAgentReconciler) SetupWithManager(mgr ctrl.Manager) error {
336386 & appsv1.Deployment {},
337387 handler .EnqueueRequestsFromMapFunc (r .mapDeploymentToAgent ),
338388 ).
389+ Watches (
390+ & appsv1.StatefulSet {},
391+ handler .EnqueueRequestsFromMapFunc (r .mapStatefulSetToAgent ),
392+ ).
339393 Watches (
340394 & corev1.Secret {},
341395 handler .EnqueueRequestsFromMapFunc (r .mapSecretToAgent ),
0 commit comments