Skip to content

Commit

Permalink
use metav1.LabelSelectorAsSelector() to convert label selector (#4709)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian authored Nov 8, 2024
1 parent 9c4a239 commit ad7138b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
25 changes: 5 additions & 20 deletions pkg/controller/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import (
"slices"
"strings"

"github.com/scylladb/go-set/strset"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

"github.com/scylladb/go-set/strset"

"github.com/kubeovn/kube-ovn/pkg/util"
)

Expand Down Expand Up @@ -128,9 +126,9 @@ func (c *Controller) handleAddNamespace(key string) error {

// bind subnet with namespaceLabelSeletcor which select the namespace
for _, nsSelector := range s.Spec.NamespaceSelectors {
matchSelector, err := mergeSelector(nsSelector)
matchSelector, err := metav1.LabelSelectorAsSelector(&nsSelector)
if err != nil {
klog.Errorf("failed to merge selector, %v", err)
klog.Errorf("failed to convert label selector, %v", err)
return err
}

Expand Down Expand Up @@ -230,19 +228,6 @@ func (c *Controller) handleAddNamespace(key string) error {
return err
}

func mergeSelector(nsSelector metav1.LabelSelector) (labels.Selector, error) {
matchSelector := labels.Set(nsSelector.MatchLabels).AsSelector()
for _, express := range nsSelector.MatchExpressions {
selectorRequirement, err := labels.NewRequirement(express.Key, selection.Operator(strings.ToLower(string(express.Operator))), express.Values)
if err != nil {
klog.Errorf("failed to get MatchExpressions selector, %v", err)
return matchSelector, err
}
matchSelector = matchSelector.Add(*selectorRequirement)
}
return matchSelector, nil
}

func (c *Controller) getNsExpectSubnets(newNs *v1.Namespace) ([]string, error) {
var expectSubnets []string

Expand All @@ -254,9 +239,9 @@ func (c *Controller) getNsExpectSubnets(newNs *v1.Namespace) ([]string, error) {
for _, subnet := range subnets {
// ns labels match subnet's selector
for _, nsSelector := range subnet.Spec.NamespaceSelectors {
matchSelector, err := mergeSelector(nsSelector)
matchSelector, err := metav1.LabelSelectorAsSelector(&nsSelector)
if err != nil {
klog.Errorf("failed to merge selector, %v", err)
klog.Errorf("failed to convert label selector, %v", err)
return expectSubnets, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,9 @@ func (c *Controller) reconcileNamespaces(subnet *kubeovnv1.Subnet) error {
func (c *Controller) getNamespacesBySelector(nsSelectors []metav1.LabelSelector) ([]string, error) {
var expectNss []string
for _, nsSelector := range nsSelectors {
matchSelector, err := mergeSelector(nsSelector)
matchSelector, err := metav1.LabelSelectorAsSelector(&nsSelector)
if err != nil {
klog.Errorf("failed to merge selector, %v", err)
klog.Errorf("failed to convert label selector, %v", err)
return expectNss, err
}

Expand Down

0 comments on commit ad7138b

Please sign in to comment.