@@ -21,6 +21,7 @@ package instanceset
2121
2222import (
2323 "encoding/json"
24+ "errors"
2425 "fmt"
2526 "regexp"
2627 "slices"
@@ -236,27 +237,55 @@ func parseProbeEventMessage(reqCtx intctrlutil.RequestCtx, event *corev1.Event)
236237 return nil
237238}
238239
239- // updatePodRoleLabel updates pod role label when internal container role changed
240- func updatePodRoleLabel ( cli client. Client , reqCtx intctrlutil. RequestCtx ,
241- its workloads. InstanceSet , pod * corev1. Pod , roleName string , version string ) error {
242- ctx : = reqCtx .Ctx
243- roleMap : = composeRoleMap (its )
244- // role not defined in CR, ignore it
245- roleName = strings . ToLower ( roleName )
246-
240+ func updatePodRoleLabel ( cli client. Client , reqCtx intctrlutil. RequestCtx , its workloads. InstanceSet ,
241+ pod * corev1. Pod , roleName string , version string ) error {
242+ var (
243+ ctx = reqCtx .Ctx
244+ roleMap = composeRoleMap (its )
245+ normalizedRoleName = strings . ToLower ( roleName )
246+ role , defined = roleMap [ normalizedRoleName ]
247+ )
247248 // update pod role label
248249 newPod := pod .DeepCopy ()
249- role , ok := roleMap [roleName ]
250- switch ok {
251- case true :
252- newPod .Labels [RoleLabelKey ] = role .Name
253- case false :
250+ if defined {
251+ newPod .Labels [RoleLabelKey ] = normalizedRoleName
252+ } else {
254253 delete (newPod .Labels , RoleLabelKey )
255254 }
256-
257255 if newPod .Annotations == nil {
258256 newPod .Annotations = map [string ]string {}
259257 }
260258 newPod .Annotations [constant .LastRoleSnapshotVersionAnnotationKey ] = version
261- return cli .Update (ctx , newPod )
259+ if err := cli .Update (ctx , newPod ); err != nil {
260+ return err
261+ }
262+
263+ if role .IsExclusive {
264+ return removeExclusiveRoleLabels (cli , reqCtx , its , pod .Name , normalizedRoleName )
265+ }
266+ return nil
267+ }
268+
269+ func removeExclusiveRoleLabels (cli client.Client , reqCtx intctrlutil.RequestCtx , its workloads.InstanceSet , newPodName , roleName string ) error {
270+ labels := getMatchLabels (its .Name )
271+ labels [RoleLabelKey ] = roleName
272+ var pods corev1.PodList
273+ if err := cli .List (reqCtx .Ctx , & pods , client .InNamespace (its .Namespace ), client .MatchingLabels (labels )); err != nil {
274+ return err
275+ }
276+
277+ var errs []error
278+ for i , pod := range pods .Items {
279+ if pod .Name == newPodName {
280+ continue
281+ }
282+ newPod := pods .Items [i ].DeepCopy ()
283+ delete (newPod .Labels , RoleLabelKey )
284+ if err := cli .Update (reqCtx .Ctx , newPod ); err != nil {
285+ errs = append (errs , err )
286+ } else {
287+ reqCtx .Log .Info ("remove exclusive role label" , "pod" , newPod .Name , "role" , roleName )
288+ }
289+ }
290+ return errors .Join (errs ... )
262291}
0 commit comments