@@ -41,6 +41,10 @@ type Clusters[T cluster.Cluster] struct {
4141 // an error.
4242 ErrorHandler func (error , string , ... any )
4343
44+ // EqualClusters is used to compare two clusters for equality when
45+ // adding or replacing clusters.
46+ EqualClusters func (a , b T ) bool
47+
4448 Lock sync.RWMutex
4549 Clusters map [string ]T
4650 Cancels map [string ]context.CancelFunc
@@ -59,9 +63,10 @@ type Index struct {
5963// New returns a new instance of Clusters.
6064func New [T cluster.Cluster ]() Clusters [T ] {
6165 return Clusters [T ]{
62- Clusters : make (map [string ]T ),
63- Cancels : make (map [string ]context.CancelFunc ),
64- Indexers : []Index {},
66+ EqualClusters : EqualClusters [T ],
67+ Clusters : make (map [string ]T ),
68+ Cancels : make (map [string ]context.CancelFunc ),
69+ Indexers : []Index {},
6570 }
6671}
6772
@@ -142,18 +147,25 @@ func (c *Clusters[T]) Remove(clusterName string) {
142147 delete (c .Clusters , clusterName )
143148}
144149
150+ // EqualClusters compares two clusters for equality based on their
151+ // configuration. It is the default implementation used by
152+ // Clusters.AddOrReplace.
153+ func EqualClusters [T cluster.Cluster ](a , b T ) bool {
154+ return cmp .Equal (a .GetConfig (), b .GetConfig ())
155+ }
156+
145157// AddOrReplace adds or replaces a cluster with the given name.
146158// If a cluster with the name already exists it compares the
147159// configuration as returned by cluster.GetConfig() to compare
148160// clusters.
149161func (c * Clusters [T ]) AddOrReplace (ctx context.Context , clusterName string , cl T , aware multicluster.Aware ) error {
150- existing , err := c .Get (ctx , clusterName )
162+ existing , err := c .GetTyped (ctx , clusterName )
151163 if err != nil {
152164 // Cluster does not exist, add it
153165 return c .Add (ctx , clusterName , cl , aware )
154166 }
155167
156- if cmp . Equal (existing . GetConfig () , cl . GetConfig () ) {
168+ if c . EqualClusters (existing , cl ) {
157169 // Cluster already exists with the same config, nothing to do
158170 return nil
159171 }
0 commit comments