Skip to content

Commit 3e76752

Browse files
authored
Argo CD Agent: Add support for redis proxy (argoproj-labs#1788)
Signed-off-by: Jonathan West <jonwest@redhat.com>
1 parent d11787c commit 3e76752

File tree

9 files changed

+419
-21
lines changed

9 files changed

+419
-21
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ vet: ## Run go vet against code.
9999
go vet ./...
100100

101101
test: manifests generate fmt vet envtest ## Run tests.
102-
go test ./... -coverprofile cover.out
102+
REDIS_CONFIG_PATH="build/redis" go test ./... -coverprofile cover.out
103103

104104
##@ Build
105105

controllers/argocd/service.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ func getArgoServerServiceType(cr *argoproj.ArgoCD) corev1.ServiceType {
3636
return cr.Spec.Server.Service.Type
3737
}
3838

39-
// If Principal is enabled, use LoadBalancer service type
40-
if cr.Spec.ArgoCDAgent != nil && cr.Spec.ArgoCDAgent.Principal != nil && cr.Spec.ArgoCDAgent.Principal.IsEnabled() {
41-
return corev1.ServiceTypeLoadBalancer
42-
}
43-
4439
return corev1.ServiceTypeClusterIP
4540
}
4641

@@ -319,13 +314,7 @@ func (r *ReconcileArgoCD) reconcileRedisService(cr *argoproj.ArgoCD) error {
319314
return nil // Service found, do nothing
320315
}
321316

322-
// If Principal is enabled, use LoadBalancer service type
323-
if cr.Spec.ArgoCDAgent != nil && cr.Spec.ArgoCDAgent.Principal != nil && cr.Spec.ArgoCDAgent.Principal.IsEnabled() {
324-
svc.Spec.Type = corev1.ServiceTypeLoadBalancer
325-
} else {
326-
// TODO: Existing and current service is not compared and updated
327-
svc.Spec.Type = corev1.ServiceTypeClusterIP
328-
}
317+
svc.Spec.Type = corev1.ServiceTypeClusterIP
329318

330319
if cr.Spec.HA.Enabled || !cr.Spec.Redis.IsEnabled() {
331320
return nil //return as Ha is enabled do nothing

controllers/argocd/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,16 @@ func getRedisServerAddress(cr *argoproj.ArgoCD) string {
623623
if cr.Spec.Redis.Remote != nil && *cr.Spec.Redis.Remote != "" {
624624
return *cr.Spec.Redis.Remote
625625
}
626+
627+
// If principal is enabled, then Argo CD server/repo server should be configured to use redis proxy from principal (argo cd agent)
628+
if cr.Spec.ArgoCDAgent != nil && cr.Spec.ArgoCDAgent.Principal != nil && cr.Spec.ArgoCDAgent.Principal.IsEnabled() {
629+
return argoutil.GenerateAgentPrincipalRedisProxyServiceName(cr.Name) + "." + cr.Namespace + ".svc.cluster.local:6379"
630+
}
631+
626632
if cr.Spec.HA.Enabled {
627633
return getRedisHAProxyAddress(cr)
628634
}
635+
629636
return fqdnServiceRef(common.ArgoCDDefaultRedisSuffix, common.ArgoCDDefaultRedisPort, cr)
630637
}
631638

@@ -1991,6 +1998,11 @@ func (r *ReconcileArgoCD) reconcileArgoCDAgent(cr *argoproj.ArgoCD) error {
19911998
return err
19921999
}
19932000

2001+
log.Info("reconciling ArgoCD Agent redis proxy service")
2002+
if err := argocdagent.ReconcilePrincipalRedisProxyService(r.Client, compName, cr, r.Scheme); err != nil {
2003+
return err
2004+
}
2005+
19942006
log.Info("reconciling ArgoCD Agent deployment")
19952007
if err := argocdagent.ReconcilePrincipalDeployment(r.Client, compName, sa.Name, cr, r.Scheme); err != nil {
19962008
return err

controllers/argocdagent/deployment.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,15 @@ func buildPorts(compName string) []corev1.ContainerPort {
147147
{
148148
ContainerPort: 8443,
149149
Name: compName,
150-
}, {
150+
},
151+
{
151152
ContainerPort: 8000,
152153
Name: "metrics",
153154
},
155+
{
156+
ContainerPort: 6379,
157+
Name: "redis",
158+
},
154159
}
155160
}
156161

controllers/argocdagent/deployment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func TestReconcilePrincipalDeployment_VerifyDeploymentSpec(t *testing.T) {
357357
assert.Equal(t, corev1.SeccompProfileType("RuntimeDefault"), container.SecurityContext.SeccompProfile.Type)
358358

359359
// Verify ports configuration
360-
assert.Len(t, container.Ports, 2)
360+
assert.Len(t, container.Ports, 3)
361361
principalPort := container.Ports[0]
362362
assert.Equal(t, testCompName, principalPort.Name)
363363
assert.Equal(t, int32(8443), principalPort.ContainerPort)

controllers/argocdagent/service.go

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
174242
func 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

Comments
 (0)