Skip to content

Commit

Permalink
Fixed matchExpressions node selector
Browse files Browse the repository at this point in the history
  • Loading branch information
p-strusiewiczsurmacki-mobica committed Nov 24, 2023
1 parent 30053eb commit eb5092c
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pkg/reconciler/layer2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"fmt"
"net"
"os"
"strings"

networkv1alpha1 "github.com/telekom/das-schiff-network-operator/api/v1alpha1"
"github.com/telekom/das-schiff-network-operator/pkg/healthcheck"
"github.com/telekom/das-schiff-network-operator/pkg/nl"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
)

Expand All @@ -36,12 +37,30 @@ func (r *reconcile) fetchLayer2(ctx context.Context) ([]networkv1alpha1.Layer2Ne
for i := range layer2List.Items {
item := &layer2List.Items[i]
if item.Spec.NodeSelector != nil {
selector, err := metav1.LabelSelectorAsSelector(item.Spec.NodeSelector)
if err != nil {
r.Logger.Error(err, "error converting nodeSelector of layer2 to selector", "layer2", item.ObjectMeta.Name)
return nil, fmt.Errorf("error converting nodeSelector of layer2 to selector: %w", err)
labelSelector := labels.NewSelector()
var reqs labels.Requirements

for labelKey, labelVal := range item.Spec.NodeSelector.MatchLabels {
requirement, err := labels.NewRequirement(labelKey, selection.Equals, []string{labelVal})
if err != nil {
r.Logger.Error(err, "error creating MatchLabel requirement")
return nil, err

Check failure on line 47 in pkg/reconciler/layer2.go

View workflow job for this annotation

GitHub Actions / lint

error returned from external package is unwrapped: sig: func k8s.io/apimachinery/pkg/labels.NewRequirement(key string, op k8s.io/apimachinery/pkg/selection.Operator, vals []string, opts ...k8s.io/apimachinery/pkg/util/validation/field.PathOption) (*k8s.io/apimachinery/pkg/labels.Requirement, error) (wrapcheck)
}
reqs = append(reqs, *requirement)
}
if !selector.Matches(labels.Set(node.ObjectMeta.Labels)) {

for _, req := range item.Spec.NodeSelector.MatchExpressions {
lowercaseOperator := selection.Operator(strings.ToLower(string(req.Operator)))
requirement, err := labels.NewRequirement(req.Key, lowercaseOperator, req.Values)
if err != nil {
r.Logger.Error(err, "error creating MatchExpression requirement")
return nil, err

Check failure on line 57 in pkg/reconciler/layer2.go

View workflow job for this annotation

GitHub Actions / lint

error returned from external package is unwrapped: sig: func k8s.io/apimachinery/pkg/labels.NewRequirement(key string, op k8s.io/apimachinery/pkg/selection.Operator, vals []string, opts ...k8s.io/apimachinery/pkg/util/validation/field.PathOption) (*k8s.io/apimachinery/pkg/labels.Requirement, error) (wrapcheck)
}
reqs = append(reqs, *requirement)
}
labelSelector = labelSelector.Add(reqs...)

if !labelSelector.Matches(labels.Set(node.ObjectMeta.Labels)) {
r.Logger.Info("local node does not match nodeSelector of layer2", "layer2", item.ObjectMeta.Name, "node", nodeName)
continue
}
Expand Down

0 comments on commit eb5092c

Please sign in to comment.