Skip to content

Commit

Permalink
fix: resourceregistry merge ns
Browse files Browse the repository at this point in the history
Signed-off-by: JimDevil <[email protected]>
  • Loading branch information
JimDevil committed Jan 18, 2025
1 parent 4f86921 commit bb4f6a2
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/search/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ func (ctl *Controller) reconcile(util.QueueKey) error {
klog.Warningf("Resource %s is not enabled for cluster %s", resource.String(), cluster)
continue
}
resourcesByClusters[cluster.Name][resource] = multiNS
if ns, exist := resourcesByClusters[cluster.Name][resource]; !exist {
resourcesByClusters[cluster.Name][resource] = multiNS
} else {
resourcesByClusters[cluster.Name][resource] = ns.Merge(multiNS)
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/search/proxy/store/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ type MultiNamespace struct {
namespaces sets.Set[string]
}

// Merge merges multiNS into n.
func (n *MultiNamespace) Merge(multiNS *MultiNamespace) *MultiNamespace {
if n.allNamespaces || multiNS.allNamespaces {
n.allNamespaces = true
n.namespaces = nil
return n
}
if n.Equal(multiNS) {
return n
}
n.namespaces.Insert(multiNS.namespaces.Clone().UnsortedList()...)
return n
}

// NewMultiNamespace return a new empty MultiNamespace.
func NewMultiNamespace() *MultiNamespace {
return &MultiNamespace{
Expand Down
93 changes: 91 additions & 2 deletions pkg/search/proxy/store/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"testing"
"time"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
)

func Test_watchMux_StopBySelf(t *testing.T) {
Expand Down Expand Up @@ -1082,3 +1081,93 @@ func TestMultiNamespace_Equal(t *testing.T) {
})
}
}

func TestMultiNamespace_Merge(t *testing.T) {
type fields struct {
allNamespaces bool
namespaces sets.Set[string]
}
type args struct {
multiNS *MultiNamespace
}
tests := []struct {
name string
fields fields
args args
want *MultiNamespace
}{
{
name: "all ns merge all ns",
fields: fields{
allNamespaces: true,
},
args: args{
multiNS: &MultiNamespace{
allNamespaces: true,
},
},
want: &MultiNamespace{
allNamespaces: true,
},
},
{
name: "all ns merge not all",
fields: fields{
allNamespaces: true,
},
args: args{
multiNS: &MultiNamespace{
allNamespaces: false,
namespaces: sets.New[string]("foo"),
},
},
want: &MultiNamespace{
allNamespaces: true,
},
},
{
name: "not all ns merge all",
fields: fields{
allNamespaces: false,
namespaces: sets.New[string]("foo"),
},
args: args{
multiNS: &MultiNamespace{
allNamespaces: true,
},
},
want: &MultiNamespace{
allNamespaces: true,
},
},
{
name: "not all ns merge not all ns",
fields: fields{
allNamespaces: false,
namespaces: sets.New[string]("foo", "zoo"),
},
args: args{
multiNS: &MultiNamespace{
allNamespaces: false,
namespaces: sets.New[string]("bar"),
},
},
want: &MultiNamespace{
allNamespaces: false,
namespaces: sets.New[string]("foo", "bar", "zoo"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &MultiNamespace{
allNamespaces: tt.fields.allNamespaces,
namespaces: tt.fields.namespaces,
}
got := n.Merge(tt.args.multiNS)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MultiNamespace.Merge() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit bb4f6a2

Please sign in to comment.