Repository: istio/istio
File: pkg/revisions/tag_watcher.go (Go)

Code before:

// AddHandler registers a new handler for updates to tag changes.
func (p *tagWatcher) AddHandler(handler TagHandler) {
	p.handlers = append(p.handlers, handler)
}

func (p *tagWatcher) HasSynced() bool {
	return p.queue.HasSynced()
}

func (p *tagWatcher) IsMine(obj metav1.ObjectMeta) bool {
	selectedTag, ok := obj.Labels[label.IoIstioRev.Name]
	if !ok {
		ns := p.namespaces.Get(obj.Namespace, "")
		if ns == nil {
			return true
		}
		selectedTag = ns.Labels[label.IoIstioRev.Name]
	}
	myTags := p.GetMyTags()
	var weAreDefaultRevision bool
	currentDefaultRevision := p.vWebhooks.Get(defaultRevisionValidatingWebhookName, "")
	if currentDefaultRevision != nil {
		weAreDefaultRevision = currentDefaultRevision.Labels[label.IoIstioRev.Name] == p.revision
	}
	return myTags.Contains(selectedTag) || (selectedTag == "" && myTags.Contains("default")) || weAreDefaultRevision
}

func (p *tagWatcher) GetMyTags() sets.String {
	res := sets.New(p.revision)
	mwhTags := sets.New[string]()
	for _, wh := range p.webhooksIndex.Lookup(p.revision) {
		res.Insert(wh.GetLabels()[label.IoIstioTag.Name])
		mwhTags.Insert(wh.GetLabels()[label.IoIstioTag.Name])
	}
	for _, svc := range p.servicesIndex.Lookup(p.revision) {
		tagName := svc.GetLabels()[label.IoIstioTag.Name]
		res.Insert(tagName)

		if mwhTags.Contains(tagName) {
			log.Debugf("Tag name %s is already defined by a service. Delete the %s-%s MutatingWebhookConfiguration ", tagName, "istio-revision-tag", tagName)
			mwhTags.Delete(tagName)
		}
	}
	return res
}

// notifyHandlers notifies all registered handlers on tag change.
func (p *tagWatcher) notifyHandlers() {
	myTags := p.GetMyTags()
	log.Debugf("Current tags: %s", myTags)

Review comment (suggestion):
```suggestion
	return myTags.Contains(selectedTag) || selectedTag == "" && (myTags.Contains("default") || weAreDefaultRevision)
```


Code after:
	p.notifyHandlers()
	p.queue.Run(stopCh)
}

// AddHandler registers a new handler for updates to tag changes.
func (p *tagWatcher) AddHandler(handler TagHandler) {
	p.handlers = append(p.handlers, handler)
}

func (p *tagWatcher) HasSynced() bool {
	return p.queue.HasSynced()
}

func (p *tagWatcher) IsMine(obj metav1.ObjectMeta) bool {
	selectedTag, ok := obj.Labels[label.IoIstioRev.Name]
	if !ok {
		ns := p.namespaces.Get(obj.Namespace, "")
		if ns == nil {
			return true
		}
		selectedTag = ns.Labels[label.IoIstioRev.Name]
	}
	myTags := p.GetMyTags()
	// Figure out if there is a default tag that doesn't point to us
	var otherDefaultTagExists bool
	if !myTags.Contains("default") {
		defaultTag := p.webhooks.Get(defaultTagMutatingWebhookName, "")
		otherDefaultTagExists = defaultTag != nil
	}
	var weAreDefaultRevision bool
	currentDefaultRevision := p.vWebhooks.Get(defaultRevisionValidatingWebhookName, "")
	if currentDefaultRevision != nil {
		weAreDefaultRevision = currentDefaultRevision.Labels[label.IoIstioRev.Name] == p.revision
	}
	return myTags.Contains(selectedTag) ||
		selectedTag == "" && (myTags.Contains("default") ||
			(weAreDefaultRevision && !otherDefaultTagExists)) // Default tag takes precedence over default revision if they differ
}

func (p *tagWatcher) GetMyTags() sets.String {
	res := sets.New(p.revision)
	mwhTags := sets.New[string]()
	for _, wh := range p.webhooksIndex.Lookup(p.revision) {
		res.Insert(wh.GetLabels()[label.IoIstioTag.Name])
		mwhTags.Insert(wh.GetLabels()[label.IoIstioTag.Name])
	}
	for _, svc := range p.servicesIndex.Lookup(p.revision) {
		tagName := svc.GetLabels()[label.IoIstioTag.Name]
		res.Insert(tagName)

		if mwhTags.Contains(tagName) {