Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 3fad6a8

Browse files
author
odacremolbap
committed
reconcile redis service endpoints
1 parent ff59907 commit 3fad6a8

File tree

4 files changed

+69
-31
lines changed

4 files changed

+69
-31
lines changed

pkg/apis/eventing/v1alpha1/redisbroker_lifecycle.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,24 @@ import (
1818
// RedisBrokerBroker refers to the TriggerMesh Broker that manages events on top of Redis.
1919

2020
const (
21-
RedisBrokerConditionReady = apis.ConditionReady
22-
RedisBrokerRedisDeployment apis.ConditionType = "RedisDeploymentReady"
23-
RedisBrokerRedisService apis.ConditionType = "RedisServiceReady"
24-
RedisBrokerRedisServiceConditionEndpointsReady apis.ConditionType = "RedisEndpointsReady"
25-
RedisBrokerBrokerDeployment apis.ConditionType = "BrokerDeploymentReady"
26-
RedisBrokerBrokerService apis.ConditionType = "BrokerServiceReady"
27-
28-
// RedisBrokerBrokerServiceConditionEndpointsReady has status True when a k8s Service Endpoints
29-
// are backed by at least one endpoint.
30-
RedisBrokerBrokerServiceConditionEndpointsReady apis.ConditionType = "BrokerEndpointsReady"
21+
RedisBrokerConditionReady = apis.ConditionReady
22+
RedisBrokerRedisDeployment apis.ConditionType = "RedisDeploymentReady"
23+
RedisBrokerRedisService apis.ConditionType = "RedisServiceReady"
24+
RedisBrokerRedisServiceEndpointsConditionReady apis.ConditionType = "RedisEndpointsReady"
25+
RedisBrokerBrokerDeployment apis.ConditionType = "BrokerDeploymentReady"
26+
RedisBrokerBrokerService apis.ConditionType = "BrokerServiceReady"
27+
RedisBrokerBrokerServiceEndpointsConditionReady apis.ConditionType = "BrokerEndpointsReady"
3128
RedisBrokerConfigSecret apis.ConditionType = "BrokerConfigSecretReady"
3229
RedisBrokerConditionAddressable apis.ConditionType = "Addressable"
3330
)
3431

3532
var redisBrokerCondSet = apis.NewLivingConditionSet(
3633
RedisBrokerRedisDeployment,
3734
RedisBrokerRedisService,
35+
RedisBrokerRedisServiceEndpointsConditionReady,
3836
RedisBrokerBrokerDeployment,
3937
RedisBrokerBrokerService,
40-
RedisBrokerBrokerServiceConditionEndpointsReady,
38+
RedisBrokerBrokerServiceEndpointsConditionReady,
4139
RedisBrokerConfigSecret,
4240

4341
// TODO RedisBrokerConditionAddressable,
@@ -155,6 +153,18 @@ func (bs *RedisBrokerStatus) MarkRedisServiceReady() {
155153
redisBrokerCondSet.Manage(bs).MarkTrue(RedisBrokerRedisService)
156154
}
157155

156+
func (bs *RedisBrokerStatus) MarkRedisEndpointsFailed(reason, messageFormat string, messageA ...interface{}) {
157+
redisBrokerCondSet.Manage(bs).MarkFalse(RedisBrokerRedisServiceEndpointsConditionReady, reason, messageFormat, messageA...)
158+
}
159+
160+
func (bs *RedisBrokerStatus) MarkRedisEndpointsUnknown(reason, messageFormat string, messageA ...interface{}) {
161+
redisBrokerCondSet.Manage(bs).MarkUnknown(RedisBrokerRedisServiceEndpointsConditionReady, reason, messageFormat, messageA...)
162+
}
163+
164+
func (bs *RedisBrokerStatus) MarkRedisEndpointsTrue() {
165+
redisBrokerCondSet.Manage(bs).MarkTrue(RedisBrokerRedisServiceEndpointsConditionReady)
166+
}
167+
158168
// Manage Redis broker state for
159169
// Deployment, Service and Endpoint
160170

@@ -196,13 +206,13 @@ func (bs *RedisBrokerStatus) MarkBrokerServiceReady() {
196206
}
197207

198208
func (bs *RedisBrokerStatus) MarkBrokerEndpointsFailed(reason, messageFormat string, messageA ...interface{}) {
199-
redisBrokerCondSet.Manage(bs).MarkFalse(RedisBrokerBrokerServiceConditionEndpointsReady, reason, messageFormat, messageA...)
209+
redisBrokerCondSet.Manage(bs).MarkFalse(RedisBrokerBrokerServiceEndpointsConditionReady, reason, messageFormat, messageA...)
200210
}
201211

202212
func (bs *RedisBrokerStatus) MarkBrokerEndpointsUnknown(reason, messageFormat string, messageA ...interface{}) {
203-
redisBrokerCondSet.Manage(bs).MarkUnknown(RedisBrokerBrokerServiceConditionEndpointsReady, reason, messageFormat, messageA...)
213+
redisBrokerCondSet.Manage(bs).MarkUnknown(RedisBrokerBrokerServiceEndpointsConditionReady, reason, messageFormat, messageA...)
204214
}
205215

206216
func (bs *RedisBrokerStatus) MarkBrokerEndpointsTrue() {
207-
redisBrokerCondSet.Manage(bs).MarkTrue(RedisBrokerBrokerServiceConditionEndpointsReady)
217+
redisBrokerCondSet.Manage(bs).MarkTrue(RedisBrokerBrokerServiceEndpointsConditionReady)
208218
}

pkg/reconciler/redisbroker/controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ func NewController(
6363
r := &Reconciler{
6464
kubeClientSet: kubeclient.Get(ctx),
6565
secretReconciler: newSecretReconciler(ctx, secretInformer.Lister(), trgInformer.Lister()),
66-
redisReconciler: newRedisReconciler(ctx, deploymentInformer.Lister(), serviceInformer.Lister(), env.RedisImage),
66+
redisReconciler: redisReconciler{
67+
client: kubeclient.Get(ctx),
68+
deploymentLister: deploymentInformer.Lister(),
69+
serviceLister: serviceInformer.Lister(),
70+
endpointsLister: endpointsInformer.Lister(),
71+
image: env.RedisImage,
72+
},
73+
6774
brokerReconciler: brokerReconciler{
6875
client: kubeclient.Get(ctx),
6976
deploymentLister: deploymentInformer.Lister(),

pkg/reconciler/redisbroker/reconcile_broker.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func (r *brokerReconciler) reconcile(ctx context.Context, rb *eventingv1alpha1.R
5555
return d, nil, err
5656
}
5757

58-
// update endpoint statuses
5958
_, err = r.reconcileEndpoints(ctx, svc, rb)
6059
if err != nil {
6160
return d, nil, err
@@ -221,12 +220,12 @@ func (r *brokerReconciler) reconcileEndpoints(ctx context.Context, service *core
221220
rb.Status.MarkBrokerEndpointsFailed(reconciler.ReasonUnavailableEndpoints, "Endpoints for broker service do not exist")
222221
return nil, pkgreconciler.NewEvent(corev1.EventTypeWarning, reconciler.ReasonUnavailableEndpoints,
223222
"Endpoints for broker service do not exist %s",
224-
types.NamespacedName{Namespace: ep.Namespace, Name: ep.Name})
223+
types.NamespacedName{Namespace: service.Namespace, Name: service.Name})
225224
}
226225

227-
fullname := types.NamespacedName{Namespace: ep.Namespace, Name: ep.Name}
228-
rb.Status.MarkBrokerEndpointsFailed(reconciler.ReasonFailedEndpointsGet, "Could not retrieve endpoints for broker service")
229-
logging.FromContext(ctx).Error("Unable to get the service endpoints", zap.String("endpoint", fullname.String()), zap.Error(err))
226+
fullname := types.NamespacedName{Namespace: service.Namespace, Name: service.Name}
227+
rb.Status.MarkBrokerEndpointsUnknown(reconciler.ReasonFailedEndpointsGet, "Could not retrieve endpoints for broker service")
228+
logging.FromContext(ctx).Error("Unable to get the broker service endpoints", zap.String("endpoint", fullname.String()), zap.Error(err))
230229
return nil, pkgreconciler.NewEvent(corev1.EventTypeWarning, reconciler.ReasonFailedEndpointsGet,
231230
"Failed to get broker service ednpoints %s: %w", fullname, err)
232231
}

pkg/reconciler/redisbroker/reconcile_redis.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"k8s.io/client-go/kubernetes"
1313
appsv1listers "k8s.io/client-go/listers/apps/v1"
1414
corev1listers "k8s.io/client-go/listers/core/v1"
15-
k8sclient "knative.dev/pkg/client/injection/kube/client"
15+
"knative.dev/eventing/pkg/apis/duck"
1616
"knative.dev/pkg/logging"
1717
pkgreconciler "knative.dev/pkg/reconciler"
1818

@@ -34,15 +34,6 @@ type redisReconciler struct {
3434
image string
3535
}
3636

37-
func newRedisReconciler(ctx context.Context, deploymentLister appsv1listers.DeploymentLister, serviceLister corev1listers.ServiceLister, image string) redisReconciler {
38-
return redisReconciler{
39-
client: k8sclient.Get(ctx),
40-
deploymentLister: deploymentLister,
41-
serviceLister: serviceLister,
42-
image: image,
43-
}
44-
}
45-
4637
func (r *redisReconciler) reconcile(ctx context.Context, rb *eventingv1alpha1.RedisBroker) (*appsv1.Deployment, *corev1.Service, error) {
4738
d, err := r.reconcileDeployment(ctx, rb)
4839
if err != nil {
@@ -54,7 +45,10 @@ func (r *redisReconciler) reconcile(ctx context.Context, rb *eventingv1alpha1.Re
5445
return d, nil, err
5546
}
5647

57-
// update endpoint statuses
48+
_, err = r.reconcileEndpoints(ctx, svc, rb)
49+
if err != nil {
50+
return d, nil, err
51+
}
5852

5953
return d, svc, nil
6054
}
@@ -184,3 +178,31 @@ func (r *redisReconciler) reconcileService(ctx context.Context, rb *eventingv1al
184178

185179
return current, nil
186180
}
181+
182+
func (r *redisReconciler) reconcileEndpoints(ctx context.Context, service *corev1.Service, rb *eventingv1alpha1.RedisBroker) (*corev1.Endpoints, error) {
183+
ep, err := r.endpointsLister.Endpoints(service.Namespace).Get(service.Name)
184+
switch {
185+
case err == nil:
186+
if duck.EndpointsAreAvailable(ep) {
187+
rb.Status.MarkRedisEndpointsTrue()
188+
return ep, nil
189+
}
190+
191+
rb.Status.MarkRedisEndpointsFailed(reconciler.ReasonUnavailableEndpoints, "Endpoints for redis service are not available")
192+
return nil, pkgreconciler.NewEvent(corev1.EventTypeWarning, reconciler.ReasonUnavailableEndpoints,
193+
"Endpoints for redis service are not available %s",
194+
types.NamespacedName{Namespace: ep.Namespace, Name: ep.Name})
195+
196+
case apierrs.IsNotFound(err):
197+
rb.Status.MarkRedisEndpointsFailed(reconciler.ReasonUnavailableEndpoints, "Endpoints for redis service do not exist")
198+
return nil, pkgreconciler.NewEvent(corev1.EventTypeWarning, reconciler.ReasonUnavailableEndpoints,
199+
"Endpoints for redis service do not exist %s",
200+
types.NamespacedName{Namespace: service.Namespace, Name: service.Name})
201+
}
202+
203+
fullname := types.NamespacedName{Namespace: service.Namespace, Name: service.Name}
204+
rb.Status.MarkRedisEndpointsUnknown(reconciler.ReasonFailedEndpointsGet, "Could not retrieve endpoints for redis service")
205+
logging.FromContext(ctx).Error("Unable to get the redis service endpoints", zap.String("endpoint", fullname.String()), zap.Error(err))
206+
return nil, pkgreconciler.NewEvent(corev1.EventTypeWarning, reconciler.ReasonFailedEndpointsGet,
207+
"Failed to get redis service ednpoints %s: %w", fullname, err)
208+
}

0 commit comments

Comments
 (0)