@@ -184,7 +184,7 @@ func (r *Resources) EnsureServices(ctx context.Context, cachedStatus inspectorIn
184184 }
185185 }
186186 } else {
187- if changed , err := patcher .ServicePatcher (ctx , svcs , s , meta.PatchOptions {}, patcher .PatchServicePorts (clientServicePorts ), patcher .PatchServiceSelector (clientServiceSelectors )); err != nil {
187+ if changed , err := patcher .ServicePatcher (ctx , svcs , s , meta.PatchOptions {}, patcher .PatchServiceOnlyPorts (clientServicePorts ... ), patcher .PatchServiceSelector (clientServiceSelectors )); err != nil {
188188 log .Err (err ).Debug ("Failed to patch database client service" )
189189 return errors .WithStack (err )
190190 } else if changed {
@@ -254,9 +254,12 @@ func (r *Resources) ensureExternalAccessServices(ctx context.Context, cachedStat
254254 log := r .log .Str ("section" , "service-ea" ).Str ("role" , role ).Str ("service" , eaServiceName )
255255 createExternalAccessService := false
256256 deleteExternalAccessService := false
257+ owned := false
257258 eaServiceType := spec .GetType ().AsServiceType () // Note: Type auto defaults to ServiceTypeLoadBalancer
258259 if existing , exists := cachedStatus .Service ().V1 ().GetSimple (eaServiceName ); exists {
259260 // External access service exists
261+ owned = apiObject .OwnerOf (existing )
262+
260263 updateExternalAccessService := false
261264 loadBalancerIP := spec .GetLoadBalancerIP ()
262265 loadBalancerSourceRanges := spec .LoadBalancerSourceRanges
@@ -285,7 +288,7 @@ func (r *Resources) ensureExternalAccessServices(ctx context.Context, cachedStat
285288 } else if existing .Spec .Type == core .ServiceTypeLoadBalancer && (loadBalancerIP != "" && existing .Spec .LoadBalancerIP != loadBalancerIP ) {
286289 deleteExternalAccessService = true // LoadBalancerIP is wrong, remove the current and replace with proper one
287290 createExternalAccessService = true
288- } else if existing .Spec .Type == core .ServiceTypeNodePort && len (existing .Spec .Ports ) == 1 && (nodePort != 0 && existing .Spec .Ports [0 ].NodePort != int32 (nodePort )) {
291+ } else if existing .Spec .Type == core .ServiceTypeNodePort && len (existing .Spec .Ports ) < 1 || existing . Spec . Ports [ 0 ]. Name != shared . ServerPortName && (nodePort != 0 && existing .Spec .Ports [0 ].NodePort != int32 (nodePort )) {
289292 deleteExternalAccessService = true // NodePort is wrong, remove the current and replace with proper one
290293 createExternalAccessService = true
291294 }
@@ -300,7 +303,7 @@ func (r *Resources) ensureExternalAccessServices(ctx context.Context, cachedStat
300303 existing .Spec .LoadBalancerSourceRanges = loadBalancerSourceRanges
301304 }
302305 } else if spec .GetType ().IsNodePort () {
303- if existing .Spec .Type != core .ServiceTypeNodePort || len (existing .Spec .Ports ) != 1 || (nodePort != 0 && existing .Spec .Ports [0 ].NodePort != int32 (nodePort )) {
306+ if existing .Spec .Type != core .ServiceTypeNodePort || len (existing .Spec .Ports ) < 1 || existing . Spec . Ports [ 0 ]. Name != shared . ServerPortName || (nodePort != 0 && existing .Spec .Ports [0 ].NodePort != int32 (nodePort )) {
304307 deleteExternalAccessService = true // Remove the current and replace with proper one
305308 createExternalAccessService = true
306309 }
@@ -316,7 +319,9 @@ func (r *Resources) ensureExternalAccessServices(ctx context.Context, cachedStat
316319 }
317320 }
318321 if ! createExternalAccessService && ! deleteExternalAccessService {
319- if changed , err := patcher .ServicePatcher (ctx , svcs , existing , meta.PatchOptions {}, patcher .PatchServicePorts (eaPorts ), patcher .PatchServiceSelector (eaSelector )); err != nil {
322+ if changed , err := patcher .ServicePatcher (ctx , svcs , existing , meta.PatchOptions {},
323+ patcher .PatchServiceSelector (eaSelector ),
324+ patcher .Optional (patcher .PatchServiceOnlyPorts (eaPorts ... ), owned )); err != nil {
320325 log .Err (err ).Debug ("Failed to patch database client service" )
321326 return errors .WithStack (err )
322327 } else if changed {
@@ -331,13 +336,15 @@ func (r *Resources) ensureExternalAccessServices(ctx context.Context, cachedStat
331336 }
332337
333338 if deleteExternalAccessService {
334- log .Info ("Removing obsolete external access service" )
335- err := globals .GetGlobalTimeouts ().Kubernetes ().RunWithTimeout (ctx , func (ctxChild context.Context ) error {
336- return svcs .Delete (ctxChild , eaServiceName , meta.DeleteOptions {})
337- })
338- if err != nil {
339- log .Err (err ).Debug ("Failed to remove external access service" )
340- return errors .WithStack (err )
339+ if owned {
340+ log .Info ("Removing obsolete external access service" )
341+ err := globals .GetGlobalTimeouts ().Kubernetes ().RunWithTimeout (ctx , func (ctxChild context.Context ) error {
342+ return svcs .Delete (ctxChild , eaServiceName , meta.DeleteOptions {})
343+ })
344+ if err != nil {
345+ log .Err (err ).Debug ("Failed to remove external access service" )
346+ return errors .WithStack (err )
347+ }
341348 }
342349 }
343350 if createExternalAccessService {
@@ -368,7 +375,7 @@ func (r *Resources) ensureExternalAccessManagedServices(ctx context.Context, cac
368375
369376 apply := func (svc * core.Service ) (bool , error ) {
370377 return patcher .ServicePatcher (ctx , cachedStatus .ServicesModInterface ().V1 (), svc , meta.PatchOptions {},
371- patcher .PatchServicePorts (ports ),
378+ patcher .PatchServiceOnlyPorts (ports ... ),
372379 patcher .PatchServiceSelector (selectors ))
373380 }
374381
0 commit comments