@@ -20,7 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020package instanceset
2121
2222import (
23+ "context"
2324 "encoding/json"
25+ "errors"
2426 "fmt"
2527 "regexp"
2628 "slices"
@@ -236,27 +238,53 @@ func parseProbeEventMessage(reqCtx intctrlutil.RequestCtx, event *corev1.Event)
236238 return nil
237239}
238240
239- // updatePodRoleLabel updates pod role label when internal container role changed
240241func updatePodRoleLabel (cli client.Client , reqCtx intctrlutil.RequestCtx ,
241242 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-
243+ var (
244+ ctx = reqCtx .Ctx
245+ roleMap = composeRoleMap (its )
246+ normalizedRoleName = strings .ToLower (roleName )
247+ role , defined = roleMap [normalizedRoleName ]
248+ )
247249 // update pod role label
248250 newPod := pod .DeepCopy ()
249- role , ok := roleMap [roleName ]
250- switch ok {
251- case true :
252- newPod .Labels [RoleLabelKey ] = role .Name
253- case false :
251+ if defined {
252+ newPod .Labels [RoleLabelKey ] = normalizedRoleName
253+ } else {
254254 delete (newPod .Labels , RoleLabelKey )
255255 }
256-
257256 if newPod .Annotations == nil {
258257 newPod .Annotations = map [string ]string {}
259258 }
260259 newPod .Annotations [constant .LastRoleSnapshotVersionAnnotationKey ] = version
261- return cli .Update (ctx , newPod )
260+ if err := cli .Update (ctx , newPod ); err != nil {
261+ return err
262+ }
263+
264+ if role .IsExclusive {
265+ return removeExclusiveRoleLabels (ctx , cli , its , pod .Name , normalizedRoleName )
266+ }
267+ return nil
268+ }
269+
270+ func removeExclusiveRoleLabels (ctx context.Context , cli client.Client , its workloads.InstanceSet , newPodName , roleName string ) error {
271+ labels := getMatchLabels (its .Name )
272+ labels [RoleLabelKey ] = roleName
273+ var pods corev1.PodList
274+ if err := cli .List (ctx , & pods , client .InNamespace (its .Namespace ), client .MatchingLabels (labels )); err != nil {
275+ return err
276+ }
277+
278+ var errs []error
279+ for i , pod := range pods .Items {
280+ if pod .Name == newPodName {
281+ continue
282+ }
283+ newPod := pods .Items [i ].DeepCopy ()
284+ delete (newPod .Labels , RoleLabelKey )
285+ if err := cli .Update (ctx , newPod ); err != nil {
286+ errs = append (errs , err )
287+ }
288+ }
289+ return errors .Join (errs ... )
262290}
0 commit comments