@@ -87,7 +87,22 @@ func (r *TypesenseClusterReconciler) ReconcileServices(ctx context.Context, ts t
8787 }
8888 }
8989
90- if int32 (ts .Spec .ApiPort ) != svc .Spec .Ports [0 ].Port || ! apiequality .Semantic .DeepEqual (svc .Annotations , ts .Spec .ServiceAnnotations ) {
90+ // Remove in 0.5.0
91+ annotations := getMergedAnnotations (& ts )
92+ svcType := v1 .ServiceTypeClusterIP
93+ if ts .Spec .Service != nil {
94+ svcType = ts .Spec .Service .Type
95+ }
96+
97+ svcExternalTrafficPolicy , err := r .invalidateExternalTrafficPolicy (svcType , ts .Spec .Service )
98+ if err != nil {
99+ return err
100+ }
101+
102+ if int32 (ts .Spec .ApiPort ) != svc .Spec .Ports [0 ].Port ||
103+ ! apiequality .Semantic .DeepEqual (svc .Annotations , annotations ) ||
104+ svc .Spec .Type != svcType ||
105+ ! externalTrafficPolicyEqual (svc .Spec .ExternalTrafficPolicy , svcExternalTrafficPolicy ) {
91106 r .logger .V (debugLevel ).Info ("updating resolver service" , "service" , svcObjectKey .Name )
92107
93108 err := r .updateService (ctx , svc , & ts )
@@ -144,7 +159,7 @@ func (r *TypesenseClusterReconciler) updateHeadlessService(ctx context.Context,
144159
145160func (r * TypesenseClusterReconciler ) createService (ctx context.Context , key client.ObjectKey , ts * tsv1alpha1.TypesenseCluster ) (* v1.Service , error ) {
146161 svc := & v1.Service {
147- ObjectMeta : getObjectMeta (ts , & key .Name , ts . Spec . ServiceAnnotations ),
162+ ObjectMeta : getObjectMeta (ts , & key .Name , getMergedAnnotations ( ts ) ),
148163 Spec : v1.ServiceSpec {
149164 Type : v1 .ServiceTypeClusterIP ,
150165 Selector : getLabels (ts ),
@@ -163,10 +178,17 @@ func (r *TypesenseClusterReconciler) createService(ctx context.Context, key clie
163178 },
164179 }
165180
166- svcSpec := ts .Spec .Service
167- if svcSpec != nil {
168- svc .Spec .Type = svcSpec .Type
169- svc .Spec .ExternalTrafficPolicy = svcSpec .ExternalTrafficPolicy
181+ if ts .Spec .Service != nil {
182+ svcType := ts .Spec .Service .Type
183+ svcExternalTrafficPolicy , err := r .invalidateExternalTrafficPolicy (svcType , ts .Spec .Service )
184+ if err != nil {
185+ return nil , err
186+ }
187+
188+ svc .Spec .Type = svcType
189+ if svcExternalTrafficPolicy != nil {
190+ svc .Spec .ExternalTrafficPolicy = * svcExternalTrafficPolicy
191+ }
170192 }
171193
172194 err := ctrl .SetControllerReference (ts , svc , r .Scheme )
@@ -184,12 +206,29 @@ func (r *TypesenseClusterReconciler) createService(ctx context.Context, key clie
184206
185207func (r * TypesenseClusterReconciler ) updateService (ctx context.Context , svc * v1.Service , ts * tsv1alpha1.TypesenseCluster ) error {
186208 patch := client .MergeFrom (svc .DeepCopy ())
209+
187210 svc .Spec .Ports [0 ].Port = int32 (ts .Spec .ApiPort )
188211
212+ svcType := v1 .ServiceTypeClusterIP
213+ if ts .Spec .Service != nil {
214+ svcType = ts .Spec .Service .Type
215+ }
216+ svc .Spec .Type = svcType
217+
218+ svcExternalTrafficPolicy , err := r .invalidateExternalTrafficPolicy (svcType , ts .Spec .Service )
219+ if err != nil {
220+ return err
221+ }
222+
223+ svc .Spec .ExternalTrafficPolicy = ""
224+ if svcExternalTrafficPolicy != nil {
225+ svc .Spec .ExternalTrafficPolicy = * svcExternalTrafficPolicy
226+ }
227+
189228 if svc .ObjectMeta .Annotations == nil {
190229 svc .ObjectMeta .Annotations = map [string ]string {}
191230 }
192- svc .ObjectMeta .Annotations = ts . Spec . ServiceAnnotations
231+ svc .ObjectMeta .Annotations = getMergedAnnotations ( ts )
193232
194233 if err := r .Patch (ctx , svc , patch ); err != nil {
195234 return err
@@ -206,3 +245,18 @@ func (r *TypesenseClusterReconciler) deleteService(ctx context.Context, svc *v1.
206245
207246 return nil
208247}
248+
249+ func (r * TypesenseClusterReconciler ) invalidateExternalTrafficPolicy (
250+ svcType v1.ServiceType ,
251+ service * tsv1alpha1.ServiceSpec ,
252+ ) (* v1.ServiceExternalTrafficPolicyType , error ) {
253+ if service == nil || service .ExternalTrafficPolicy == nil {
254+ return nil , nil
255+ }
256+
257+ if svcType == v1 .ServiceTypeClusterIP {
258+ return nil , fmt .Errorf ("externalTrafficPolicy may only be set for externally-accessible services" )
259+ }
260+
261+ return service .ExternalTrafficPolicy , nil
262+ }
0 commit comments