func(t*tracker)Add(objruntime.Object)error{// 添加 List
ifmeta.IsListType(obj){returnt.addList(obj,false)}// 用来获取 namespace
objMeta,err:=meta.Accessor(obj)// 获取 gvk
gvks,_,err:=t.scheme.ObjectKinds(obj)for_,gvk:=rangegvks{// NOTE: UnsafeGuessKindToResource is a heuristic and default match. The
// actual registration in apiserver can specify arbitrary route for a
// gvk. If a test uses such objects, it cannot preset the tracker with
// objects via Add(). Instead, it should trigger the Create() function
// of the tracker, where an arbitrary gvr can be specified.
gvr,_:=meta.UnsafeGuessKindToResource(gvk)// Resource doesn't have the concept of "__internal" version, just set it to "".
ifgvr.Version==runtime.APIVersionInternal{gvr.Version=""}// 添加这个
err:=t.add(gvr,obj,objMeta.GetNamespace(),false)iferr!=nil{returnerr}}returnnil}
func(t*tracker)add(gvrschema.GroupVersionResource,objruntime.Object,nsstring,replaceExistingbool)error{t.lock.Lock()defert.lock.Unlock()gr:=gvr.GroupResource()// To avoid the object from being accidentally modified by caller
// after it's been added to the tracker, we always store the deep
// copy.
obj=obj.DeepCopyObject()newMeta,err:=meta.Accessor(obj)_,ok:=t.objects[gvr]if!ok{t.objects[gvr]=make(map[types.NamespacedName]runtime.Object)}// replaceExisting 策略来放入新对象
namespacedName:=types.NamespacedName{Namespace:newMeta.GetNamespace(),Name:newMeta.GetName()}if_,ok=t.objects[gvr][namespacedName];ok{ifreplaceExisting{for_,w:=ranget.getWatches(gvr,ns){// To avoid the object from being accidentally modified by watcher
// 最终操作: f.result <- Event{Modified, obj}
w.Modify(obj.DeepCopyObject())}// 覆盖原先的对象
t.objects[gvr][namespacedName]=objreturnnil}returnerrors.NewAlreadyExists(gr,newMeta.GetName())}ifreplaceExisting{// Tried to update but no matching object was found.
returnerrors.NewNotFound(gr,newMeta.GetName())}// 最终也就是放到这个 map 中
t.objects[gvr][namespacedName]=obj// 实现 objectTracker, 每添加一个新的对象,就会向 chan 中放入这个新对象
// 最终操作: f.result <- Event{Added, obj)
for_,w:=ranget.getWatches(gvr,ns){// To avoid the object from being accidentally modified by watcher
w.Add(obj.DeepCopyObject())}returnnil}