@@ -3,7 +3,6 @@ package redisbroker
33import (
44 "context"
55 "fmt"
6- "path"
76
87 "go.uber.org/zap"
98
@@ -32,21 +31,27 @@ const (
3231 brokerResourceSuffix = "redisbroker-broker"
3332)
3433
35- var (
36- configMountedPath = path .Join (configSecretPath , configSecretFile )
37- )
34+ // var (
35+ // configMountedPath = path.Join(configSecretPath, configSecretFile)
36+ // )
3837
3938type brokerReconciler struct {
4039 client kubernetes.Interface
4140 deploymentLister appsv1listers.DeploymentLister
41+ serviceAccountLister corev1listers.ServiceAccountLister
4242 serviceLister corev1listers.ServiceLister
4343 endpointsLister corev1listers.EndpointsLister
4444 image string
4545 pullPolicy corev1.PullPolicy
4646}
4747
4848func (r * brokerReconciler ) reconcile (ctx context.Context , rb * eventingv1alpha1.RedisBroker , redis * corev1.Service , secret * corev1.Secret ) (* appsv1.Deployment , * corev1.Service , error ) {
49- d , err := r .reconcileDeployment (ctx , rb , redis , secret )
49+ sa , err := r .reconcileServiceAccount (ctx , rb )
50+ if err != nil {
51+ return nil , nil , err
52+ }
53+
54+ d , err := r .reconcileDeployment (ctx , rb ,sa , redis , secret )
5055 if err != nil {
5156 return nil , nil , err
5257 }
@@ -64,12 +69,57 @@ func (r *brokerReconciler) reconcile(ctx context.Context, rb *eventingv1alpha1.R
6469 return d , svc , nil
6570}
6671
67- func buildBrokerDeployment (rb * eventingv1alpha1.RedisBroker , redis * corev1.Service , secret * corev1.Secret , image string , pullPolicy corev1.PullPolicy ) * appsv1.Deployment {
72+ func buildBrokerServiceAccount (rb * eventingv1alpha1.RedisBroker ) * corev1.ServiceAccount {
73+ return resources .NewServiceAccount (rb .Namespace , rb .Name + "-" + brokerResourceSuffix ,
74+ resources .ServiceAccountWithMetaOptions (
75+ resources .MetaAddLabel (appAnnotation , appAnnotationValue ),
76+ resources .MetaAddLabel ("component" , brokerResourceSuffix ),
77+ resources .MetaAddLabel (resourceNameAnnotation , rb .Name + "-" + brokerResourceSuffix ),
78+ resources .MetaAddOwner (rb , rb .GetGroupVersionKind ())))
79+ }
80+
81+ func (r * brokerReconciler ) reconcileServiceAccount (ctx context.Context , rb * eventingv1alpha1.RedisBroker ) (* corev1.ServiceAccount , error ) {
82+ desired := buildBrokerServiceAccount (rb )
83+ current , err := r .serviceAccountLister .ServiceAccounts (desired .Namespace ).Get (desired .Name )
84+
85+ switch {
86+ case err == nil :
87+ // TODO check RoleBinding
88+
89+ case ! apierrs .IsNotFound (err ):
90+ // An error occurred retrieving current object.
91+ fullname := types.NamespacedName {Namespace : desired .Namespace , Name : desired .Name }
92+ logging .FromContext (ctx ).Error ("Unable to get broker ServiceAccount" , zap .String ("serviceAccount" , fullname .String ()), zap .Error (err ))
93+ rb .Status .MarkBrokerServiceAccountFailed (reconciler .ReasonFailedServiceAccountGet , "Failed to get broker ServiceAccount" )
94+
95+ return nil , pkgreconciler .NewEvent (corev1 .EventTypeWarning , reconciler .ReasonFailedServiceAccountGet ,
96+ "Failed to get broker ServiceAccount %s: %w" , fullname , err )
97+
98+ default :
99+ // The ServiceAccount has not been found, create it.
100+ current , err = r .client .CoreV1 ().ServiceAccounts (desired .Namespace ).Create (ctx , desired , metav1.CreateOptions {})
101+ if err != nil {
102+ fullname := types.NamespacedName {Namespace : desired .Namespace , Name : desired .Name }
103+ logging .FromContext (ctx ).Error ("Unable to create broker ServiceAccount" , zap .String ("serviceAccount" , fullname .String ()), zap .Error (err ))
104+ rb .Status .MarkBrokerServiceAccountFailed (reconciler .ReasonFailedServiceAccountCreate , "Failed to create broker ServiceAccount" )
105+
106+ return nil , pkgreconciler .NewEvent (corev1 .EventTypeWarning , reconciler .ReasonFailedServiceAccountCreate ,
107+ "Failed to create broker ServiceAccount %s: %w" , fullname , err )
108+ }
109+ }
110+
111+ // Update status
112+ rb .Status .MarkBrokerServiceAccountReady ()
113+
114+ return current , nil
115+ }
116+
117+ func buildBrokerDeployment (rb * eventingv1alpha1.RedisBroker ,sa * corev1.ServiceAccount , redis * corev1.Service , secret * corev1.Secret , image string , pullPolicy corev1.PullPolicy ) * appsv1.Deployment {
68118
69- v := resources .NewVolume ("config" ,
70- resources .VolumeFromSecretOption (secret .Name , configSecretKey , configSecretFile ))
71- vm := resources .NewVolumeMount ("config" , configSecretPath ,
72- resources .VolumeMountWithReadOnlyOption (true ))
119+ // v := resources.NewVolume("config",
120+ // resources.VolumeFromSecretOption(secret.Name, configSecretKey, configSecretFile))
121+ // vm := resources.NewVolumeMount("config", configSecretPath,
122+ // resources.VolumeMountWithReadOnlyOption(true))
73123
74124 var stream string
75125 if rb .Spec .Redis != nil && rb .Spec .Redis .Stream != nil && * rb .Spec .Redis .Stream != "" {
@@ -80,8 +130,13 @@ func buildBrokerDeployment(rb *eventingv1alpha1.RedisBroker, redis *corev1.Servi
80130
81131 opts := []resources.ContainerOption {
82132 resources .ContainerAddArgs ("start" ),
83- resources .ContainerAddVolumeMount (vm ),
84- resources .ContainerAddEnvFromValue ("BROKER_CONFIG_PATH" , configMountedPath ),
133+ // resources.ContainerAddVolumeMount(vm),
134+ // resources.ContainerAddEnvFromValue("BROKER_CONFIG_PATH", configMountedPath),
135+ resources .ContainerAddEnvFromFieldRef ("KUBERNETES_NAMESPACE" , "metadata.namespace" ),
136+ // resources.ContainerAddEnvFromValue("KUBERNETES_NAMESPACE", rb.Namespace),
137+ resources .ContainerAddEnvFromValue ("BROKER_CONFIG_KUBERNETES_SECRET_NAME" , secret .Name ),
138+ resources .ContainerAddEnvFromValue ("BROKER_CONFIG_KUBERNETES_SECRET_KEY" , configSecretKey ),
139+
85140 resources .ContainerAddEnvFromValue ("REDIS_STREAM" , stream ),
86141 resources .ContainerWithImagePullPolicy (pullPolicy ),
87142 }
@@ -127,13 +182,13 @@ func buildBrokerDeployment(rb *eventingv1alpha1.RedisBroker, redis *corev1.Servi
127182 resources .DeploymentAddSelectorForTemplate (resourceNameAnnotation , rb .Name + "-" + brokerResourceSuffix ),
128183 resources .DeploymentSetReplicas (1 ),
129184 resources .DeploymentWithTemplateOptions (
130- resources .PodSpecAddVolume (v ),
185+ // resources.PodSpecAddVolume(v),
131186 resources .PodSpecAddContainer (
132187 resources .NewContainer ("broker" , image , opts ... ))))
133188}
134189
135- func (r * brokerReconciler ) reconcileDeployment (ctx context.Context , rb * eventingv1alpha1.RedisBroker , redis * corev1.Service , secret * corev1.Secret ) (* appsv1.Deployment , error ) {
136- desired := buildBrokerDeployment (rb , redis , secret , r .image , r .pullPolicy )
190+ func (r * brokerReconciler ) reconcileDeployment (ctx context.Context , rb * eventingv1alpha1.RedisBroker ,sa * corev1. ServiceAccount , redis * corev1.Service , secret * corev1.Secret ) (* appsv1.Deployment , error ) {
191+ desired := buildBrokerDeployment (rb ,sa , redis , secret , r .image , r .pullPolicy )
137192 current , err := r .deploymentLister .Deployments (desired .Namespace ).Get (desired .Name )
138193 switch {
139194 case err == nil :
@@ -154,7 +209,7 @@ func (r *brokerReconciler) reconcileDeployment(ctx context.Context, rb *eventing
154209 }
155210
156211 case ! apierrs .IsNotFound (err ):
157- // An error ocurred retrieving current deployment.
212+ // An error occurred retrieving current deployment.
158213 fullname := types.NamespacedName {Namespace : desired .Namespace , Name : desired .Name }
159214 logging .FromContext (ctx ).Error ("Unable to get broker deployment" , zap .String ("deployment" , fullname .String ()), zap .Error (err ))
160215 rb .Status .MarkBrokerDeploymentFailed (reconciler .ReasonFailedDeploymentGet , "Failed to get broker deployment" )
@@ -216,7 +271,7 @@ func (r *brokerReconciler) reconcileService(ctx context.Context, rb *eventingv1a
216271 }
217272
218273 case ! apierrs .IsNotFound (err ):
219- // An error ocurred retrieving current object.
274+ // An error occurred retrieving current object.
220275 fullname := types.NamespacedName {Namespace : desired .Namespace , Name : desired .Name }
221276 logging .FromContext (ctx ).Error ("Unable to get the service" , zap .String ("service" , fullname .String ()), zap .Error (err ))
222277 rb .Status .MarkBrokerServiceFailed (reconciler .ReasonFailedServiceGet , "Failed to get broker service" )
0 commit comments