66 "path"
77
88 "go.uber.org/zap"
9+
910 appsv1 "k8s.io/api/apps/v1"
1011 corev1 "k8s.io/api/core/v1"
1112 apierrs "k8s.io/apimachinery/pkg/api/errors"
@@ -14,7 +15,7 @@ import (
1415 "k8s.io/client-go/kubernetes"
1516 appsv1listers "k8s.io/client-go/listers/apps/v1"
1617 corev1listers "k8s.io/client-go/listers/core/v1"
17- k8sclient "knative.dev/pkg/client/injection/kube/client "
18+ "knative.dev/eventing/ pkg/apis/duck "
1819 "knative.dev/pkg/logging"
1920 pkgreconciler "knative.dev/pkg/reconciler"
2021
@@ -39,18 +40,10 @@ type brokerReconciler struct {
3940 client kubernetes.Interface
4041 deploymentLister appsv1listers.DeploymentLister
4142 serviceLister corev1listers.ServiceLister
43+ endpointsLister corev1listers.EndpointsLister
4244 image string
4345}
4446
45- func newBrokerReconciler (ctx context.Context , deploymentLister appsv1listers.DeploymentLister , serviceLister corev1listers.ServiceLister , image string ) brokerReconciler {
46- return brokerReconciler {
47- client : k8sclient .Get (ctx ),
48- deploymentLister : deploymentLister ,
49- serviceLister : serviceLister ,
50- image : image ,
51- }
52- }
53-
5447func (r * brokerReconciler ) reconcile (ctx context.Context , rb * eventingv1alpha1.RedisBroker , redis * corev1.Service , secret * corev1.Secret ) (* appsv1.Deployment , * corev1.Service , error ) {
5548 d , err := r .reconcileDeployment (ctx , rb , redis , secret )
5649 if err != nil {
@@ -62,6 +55,12 @@ func (r *brokerReconciler) reconcile(ctx context.Context, rb *eventingv1alpha1.R
6255 return d , nil , err
6356 }
6457
58+ // update endpoint statuses
59+ _ , err = r .reconcileEndpoints (ctx , svc , rb )
60+ if err != nil {
61+ return d , nil , err
62+ }
63+
6564 return d , svc , nil
6665}
6766
@@ -76,7 +75,7 @@ func buildBrokerDeployment(rb *eventingv1alpha1.RedisBroker, redis *corev1.Servi
7675
7776 return resources .NewDeployment (rb .Namespace , rb .Name + "-" + brokerResourceSuffix ,
7877 resources .DeploymentWithMetaOptions (
79- resources .MetaAddLabel ("app" , appAnnotationValue ),
78+ resources .MetaAddLabel (appAnnotation , appAnnotationValue ),
8079 resources .MetaAddLabel ("component" , brokerResourceSuffix ),
8180 resources .MetaAddLabel (resourceNameAnnotation , rb .Name + "-" + brokerResourceSuffix ),
8281 resources .MetaAddOwner (rb , rb .GetGroupVersionKind ())),
@@ -145,7 +144,7 @@ func (r *brokerReconciler) reconcileDeployment(ctx context.Context, rb *eventing
145144func buildBrokerService (rb * eventingv1alpha1.RedisBroker ) * corev1.Service {
146145 return resources .NewService (rb .Namespace , rb .Name + "-" + brokerResourceSuffix ,
147146 resources .ServiceWithMetaOptions (
148- resources .MetaAddLabel ("app" , appAnnotationValue ),
147+ resources .MetaAddLabel (appAnnotation , appAnnotationValue ),
149148 resources .MetaAddLabel ("component" , brokerResourceSuffix ),
150149 resources .MetaAddLabel (resourceNameAnnotation , rb .Name + "-" + brokerResourceSuffix ),
151150 resources .MetaAddOwner (rb , rb .GetGroupVersionKind ())),
@@ -159,6 +158,7 @@ func (r *brokerReconciler) reconcileService(ctx context.Context, rb *eventingv1a
159158 current , err := r .serviceLister .Services (desired .Namespace ).Get (desired .Name )
160159 switch {
161160 case err == nil :
161+ // Set Status
162162 // Compare current object with desired, update if needed.
163163 if ! semantic .Semantic .DeepEqual (desired , current ) {
164164 desired .Status = current .Status
@@ -202,3 +202,31 @@ func (r *brokerReconciler) reconcileService(ctx context.Context, rb *eventingv1a
202202
203203 return current , nil
204204}
205+
206+ func (r * brokerReconciler ) reconcileEndpoints (ctx context.Context , service * corev1.Service , rb * eventingv1alpha1.RedisBroker ) (* corev1.Endpoints , error ) {
207+ ep , err := r .endpointsLister .Endpoints (service .Namespace ).Get (service .Name )
208+ switch {
209+ case err == nil :
210+ if duck .EndpointsAreAvailable (ep ) {
211+ rb .Status .MarkBrokerEndpointsTrue ()
212+ return ep , nil
213+ }
214+
215+ rb .Status .MarkBrokerEndpointsFailed (reconciler .ReasonUnavailableEndpoints , "Endpoints for broker service are not available" )
216+ return nil , pkgreconciler .NewEvent (corev1 .EventTypeWarning , reconciler .ReasonUnavailableEndpoints ,
217+ "Endpoints for broker service are not available %s" ,
218+ types.NamespacedName {Namespace : ep .Namespace , Name : ep .Name })
219+
220+ case apierrs .IsNotFound (err ):
221+ rb .Status .MarkBrokerEndpointsFailed (reconciler .ReasonUnavailableEndpoints , "Endpoints for broker service do not exist" )
222+ return nil , pkgreconciler .NewEvent (corev1 .EventTypeWarning , reconciler .ReasonUnavailableEndpoints ,
223+ "Endpoints for broker service do not exist %s" ,
224+ types.NamespacedName {Namespace : ep .Namespace , Name : ep .Name })
225+ }
226+
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 ))
230+ return nil , pkgreconciler .NewEvent (corev1 .EventTypeWarning , reconciler .ReasonFailedEndpointsGet ,
231+ "Failed to get broker service ednpoints %s: %w" , fullname , err )
232+ }
0 commit comments