@@ -46,6 +46,12 @@ const (
4646 PrincipalMetricsServicePort = 8000
4747 // PrincipalMetricsServiceTargetPort is the target port for the principal metrics service
4848 PrincipalMetricsServiceTargetPort = 8000
49+ // PrincipalRedisProxyServicePortName is the name of the Redis proxy port
50+ PrincipalRedisProxyServicePortName = "redis"
51+ // PrincipalRedisProxyServicePort is the external port for the principal Redis proxy service
52+ PrincipalRedisProxyServicePort = 6379
53+ // PrincipalRedisProxyServiceTargetPort is the target port for the principal Redis proxy service
54+ PrincipalRedisProxyServiceTargetPort = 6379
4955)
5056
5157// ReconcilePrincipalService reconciles the principal service for the ArgoCD agent.
@@ -171,6 +177,68 @@ func ReconcilePrincipalMetricsService(client client.Client, compName string, cr
171177 return nil
172178}
173179
180+ // ReconcilePrincipalRedisProxyService reconciles the principal Redis proxy service for the ArgoCD agent.
181+ // It creates, updates, or deletes the Redis proxy service based on the principal configuration.
182+ func ReconcilePrincipalRedisProxyService (client client.Client , compName string , cr * argoproj.ArgoCD , scheme * runtime.Scheme ) error {
183+
184+ service := buildService (argoutil .GenerateAgentPrincipalRedisProxyServiceName (cr .Name ), compName , cr )
185+ expectedSpec := buildPrincipalRedisProxyServiceSpec (compName , cr )
186+
187+ // Check if the Redis proxy service already exists in the cluster
188+ exists := true
189+ if err := client .Get (context .TODO (), types.NamespacedName {Name : service .Name , Namespace : service .Namespace }, service ); err != nil {
190+ if ! errors .IsNotFound (err ) {
191+ return fmt .Errorf ("failed to get existing principal Redis proxy service %s in namespace %s: %v" , service .Name , service .Namespace , err )
192+ }
193+ exists = false
194+ }
195+
196+ // If Redis proxy service exists, handle updates or deletion
197+ if exists {
198+ if cr .Spec .ArgoCDAgent == nil || cr .Spec .ArgoCDAgent .Principal == nil || ! cr .Spec .ArgoCDAgent .Principal .IsEnabled () {
199+ argoutil .LogResourceDeletion (log , service , "principal Redis proxy service is being deleted as principal is disabled" )
200+ if err := client .Delete (context .TODO (), service ); err != nil {
201+ return fmt .Errorf ("failed to delete principal Redis proxy service %s: %v" , service .Name , err )
202+ }
203+ return nil
204+ }
205+
206+ if ! reflect .DeepEqual (service .Spec .Ports , expectedSpec .Ports ) ||
207+ ! reflect .DeepEqual (service .Spec .Selector , expectedSpec .Selector ) ||
208+ ! reflect .DeepEqual (service .Spec .Type , expectedSpec .Type ) {
209+
210+ service .Spec .Type = expectedSpec .Type
211+ service .Spec .Ports = expectedSpec .Ports
212+ service .Spec .Selector = expectedSpec .Selector
213+
214+ argoutil .LogResourceUpdate (log , service , "updating principal Redis proxy service spec" )
215+ if err := client .Update (context .TODO (), service ); err != nil {
216+ return fmt .Errorf ("failed to update principal Redis proxy service %s: %v" , service .Name , err )
217+ }
218+ }
219+ return nil
220+ }
221+
222+ // If Redis proxy service doesn't exist and principal is disabled, nothing to do
223+ if cr .Spec .ArgoCDAgent == nil || cr .Spec .ArgoCDAgent .Principal == nil || ! cr .Spec .ArgoCDAgent .Principal .IsEnabled () {
224+ return nil
225+ }
226+
227+ if err := controllerutil .SetControllerReference (cr , service , scheme ); err != nil {
228+ return fmt .Errorf ("failed to set ArgoCD CR %s as owner for service %s: %w" , cr .Name , service .Name , err )
229+ }
230+
231+ service .Spec .Type = expectedSpec .Type
232+ service .Spec .Ports = expectedSpec .Ports
233+ service .Spec .Selector = expectedSpec .Selector
234+
235+ argoutil .LogResourceCreation (log , service )
236+ if err := client .Create (context .TODO (), service ); err != nil {
237+ return fmt .Errorf ("failed to create principal Redis proxy service %s: %v" , service .Name , err )
238+ }
239+ return nil
240+ }
241+
174242func buildPrincipalServiceSpec (compName string , cr * argoproj.ArgoCD ) corev1.ServiceSpec {
175243 return corev1.ServiceSpec {
176244 Ports : []corev1.ServicePort {
@@ -201,7 +269,24 @@ func buildPrincipalMetricsServiceSpec(compName string, cr *argoproj.ArgoCD) core
201269 Selector : map [string ]string {
202270 common .ArgoCDKeyName : generateAgentResourceName (cr .Name , compName ),
203271 },
204- Type : corev1 .ServiceTypeLoadBalancer ,
272+ Type : corev1 .ServiceTypeClusterIP ,
273+ }
274+ }
275+
276+ func buildPrincipalRedisProxyServiceSpec (compName string , cr * argoproj.ArgoCD ) corev1.ServiceSpec {
277+ return corev1.ServiceSpec {
278+ Ports : []corev1.ServicePort {
279+ {
280+ Name : PrincipalRedisProxyServicePortName ,
281+ Port : PrincipalRedisProxyServicePort ,
282+ Protocol : corev1 .ProtocolTCP ,
283+ TargetPort : intstr .FromInt (PrincipalRedisProxyServiceTargetPort ),
284+ },
285+ },
286+ Selector : map [string ]string {
287+ common .ArgoCDKeyName : generateAgentResourceName (cr .Name , compName ),
288+ },
289+ Type : corev1 .ServiceTypeClusterIP ,
205290 }
206291}
207292
0 commit comments