MCPcopy
hub / github.com/istio/istio / newDiscoveryNamespacesFilter

Function newDiscoveryNamespacesFilter

pkg/kube/namespace/filter.go:49–112  ·  view source on GitHub ↗
(
	namespaces kclient.Client[*corev1.Namespace],
	mesh mesh.Watcher,
	stop <-chan struct{},
	wait bool,
)

Source from the content-addressed store, hash-verified

47}
48
49func newDiscoveryNamespacesFilter(
50 namespaces kclient.Client[*corev1.Namespace],
51 mesh mesh.Watcher,
52 stop <-chan struct{},
53 wait bool,
54) *discoveryNamespacesFilter {
55 // convert LabelSelectors to Selectors
56 f := &discoveryNamespacesFilter{
57 namespaces: namespaces,
58 discoveryNamespaces: sets.New[string](),
59 }
60 reg := mesh.AddMeshHandler(func() {
61 f.selectorsChanged(mesh.Mesh().GetDiscoverySelectors(), true)
62 })
63
64 // Clean up mesh handler on stop
65 go func() {
66 <-stop
67 mesh.DeleteMeshHandler(reg)
68 }()
69
70 namespaces.AddEventHandler(controllers.EventHandler[*corev1.Namespace]{
71 AddFunc: func(ns *corev1.Namespace) {
72 f.lock.Lock()
73 created := f.namespaceCreatedLocked(ns.ObjectMeta)
74 f.lock.Unlock()
75 // In rare cases, a namespace may be created after objects in the namespace, because there is no synchronization between watches
76 // So we need to notify if we started selecting namespace
77 if created {
78 f.notifyHandlers(sets.New(ns.Name), nil)
79 }
80 },
81 UpdateFunc: func(oldObj, newObj *corev1.Namespace) {
82 f.lock.Lock()
83 membershipChanged, namespaceAdded := f.namespaceUpdatedLocked(oldObj.ObjectMeta, newObj.ObjectMeta)
84 f.lock.Unlock()
85 if membershipChanged {
86 added := sets.New(newObj.Name)
87 var removed sets.String
88 if !namespaceAdded {
89 removed = added
90 added = nil
91 }
92 f.notifyHandlers(added, removed)
93 }
94 },
95 DeleteFunc: func(ns *corev1.Namespace) {
96 f.lock.Lock()
97 defer f.lock.Unlock()
98 // No need to notify handlers for deletes. The namespace was deleted, so the object will be as well (and a delete could not de-select).
99 // Note that specifically for the edge case of a Namespace watcher that is filtering, this will ignore deletes we should
100 // otherwise send.
101 // See kclient.applyDynamicFilter for rationale.
102 f.namespaceDeletedLocked(ns.ObjectMeta)
103 },
104 })
105 // Start namespaces and wait for it to be ready now. This is required for subsequent users, so we want to block
106 namespaces.Start(stop)

Calls 12

selectorsChangedMethod · 0.95
notifyHandlersMethod · 0.95
NewFunction · 0.92
WaitForCacheSyncFunction · 0.92
AddMeshHandlerMethod · 0.65
MeshMethod · 0.65
DeleteMeshHandlerMethod · 0.65
AddEventHandlerMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…