Unverified Commit e97ff524 authored by Jacky Wu's avatar Jacky Wu Committed by GitHub
Browse files

feat: make selector support expression selectors. (#1277)

parent 05ce8b60
Showing with 780 additions and 9 deletions
+780 -9
......@@ -26,6 +26,9 @@ const (
PauseAnnotationKey = "experiment.chaos-mesh.org/pause"
)
// LabelSelectorRequirements is list of LabelSelectorRequirement
type LabelSelectorRequirements []metav1.LabelSelectorRequirement
// SelectorSpec defines the some selectors to select objects.
// If the all selectors are empty, all objects will be used in chaos experiment.
type SelectorSpec struct {
......@@ -59,6 +62,11 @@ type SelectorSpec struct {
// +optional
LabelSelectors map[string]string `json:"labelSelectors,omitempty"`
// a slice of label selector expressions that can be used to select objects.
// A list of selectors based on set-based label expressions.
// +optional
ExpressionSelectors LabelSelectorRequirements `json:"expressionSelectors,omitempty"`
// Map of string keys and values that can be used to select objects.
// A selector based on annotations.
// +optional
......
......@@ -1038,6 +1038,27 @@ func (in *KernelChaosStatus) DeepCopy() *KernelChaosStatus {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in LabelSelectorRequirements) DeepCopyInto(out *LabelSelectorRequirements) {
{
in := &in
*out = make(LabelSelectorRequirements, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LabelSelectorRequirements.
func (in LabelSelectorRequirements) DeepCopy() LabelSelectorRequirements {
if in == nil {
return nil
}
out := new(LabelSelectorRequirements)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *LossSpec) DeepCopyInto(out *LossSpec) {
*out = *in
......@@ -1728,6 +1749,13 @@ func (in *SelectorSpec) DeepCopyInto(out *SelectorSpec) {
(*out)[key] = val
}
}
if in.ExpressionSelectors != nil {
in, out := &in.ExpressionSelectors, &out.ExpressionSelectors
*out = make(LabelSelectorRequirements, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.AnnotationSelectors != nil {
in, out := &in.AnnotationSelectors, &out.AnnotationSelectors
*out = make(map[string]string, len(*in))
......
......@@ -86,6 +86,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -113,6 +113,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -181,6 +181,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -287,6 +287,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -139,6 +139,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -198,6 +198,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -268,6 +299,38 @@ spec:
description: Map of string keys and values that can be used
to select objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can
be used to select objects. A list of selectors based on set-based
label expressions.
items:
description: A label selector requirement is a selector that
contains values, a key, and an operator that relates the
key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are In, NotIn, Exists
and DoesNotExist.
type: string
values:
description: values is an array of string values. If the
operator is In or NotIn, the values array must be non-empty.
If the operator is Exists or DoesNotExist, the values
array must be empty. This array is replaced during a
strategic merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -95,6 +95,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -69,6 +69,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -85,6 +85,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -84,6 +84,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -339,6 +370,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -657,6 +719,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -1086,6 +1179,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -1426,6 +1550,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -1764,6 +1919,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -1834,6 +2020,38 @@ spec:
description: Map of string keys and values that can be used
to select objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can
be used to select objects. A list of selectors based on set-based
label expressions.
items:
description: A label selector requirement is a selector that
contains values, a key, and an operator that relates the
key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship
to a set of values. Valid operators are In, NotIn, Exists
and DoesNotExist.
type: string
values:
description: values is an array of string values. If the
operator is In or NotIn, the values array must be non-empty.
If the operator is Exists or DoesNotExist, the values
array must be empty. This array is replaced during a
strategic merge patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -2084,6 +2302,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -2762,6 +3011,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......@@ -3049,6 +3329,37 @@ spec:
description: Map of string keys and values that can be used to select
objects. A selector based on annotations.
type: object
expressionSelectors:
description: a slice of label selector expressions that can be used
to select objects. A list of selectors based on set-based label
expressions.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: object
type: array
fieldSelectors:
additionalProperties:
type: string
......
......@@ -65,6 +65,7 @@ func newEngine() *gin.Engine {
v.RegisterValidation("NameValid", apivalidator.NameValid)
v.RegisterValidation("NamespaceSelectorsValid", apivalidator.NamespaceSelectorsValid)
v.RegisterValidation("MapSelectorsValid", apivalidator.MapSelectorsValid)
v.RegisterValidation("RequirementSelectorsValid", apivalidator.RequirementSelectorsValid)
v.RegisterValidation("PhaseSelectorsValid", apivalidator.PhaseSelectorsValid)
v.RegisterValidation("CronValid", apivalidator.CronValid)
v.RegisterValidation("DurationValid", apivalidator.DurationValid)
......
......@@ -18,6 +18,7 @@ import (
"github.com/go-playground/validator/v10"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation"
)
......@@ -61,6 +62,40 @@ func MapSelectorsValid(fl validator.FieldLevel) bool {
return true
}
// RequirementSelectorsValid can be used to check whether label requirement selectors is valid.
func RequirementSelectorsValid(fl validator.FieldLevel) bool {
if fl.Field().IsNil() {
return true
}
rs, ok := fl.Field().Interface().([]metav1.LabelSelectorRequirement)
if !ok {
return false
}
for _, r := range rs {
if len(validation.IsQualifiedName(r.Key)) != 0 {
return false
}
switch r.Operator {
case metav1.LabelSelectorOpIn, metav1.LabelSelectorOpNotIn:
if len(r.Values) == 0 {
return false
}
case metav1.LabelSelectorOpExists, metav1.LabelSelectorOpDoesNotExist:
if len(r.Values) > 0 {
return false
}
default:
// unsupport operator
return false
}
}
return true
}
// PhaseSelectorsValid can be used to check whether phase selectors is valid.
func PhaseSelectorsValid(fl validator.FieldLevel) bool {
ph, ok := fl.Field().Interface().([]string)
......
......@@ -19,6 +19,8 @@ import (
"time"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ExperimentStore defines operations for working with experiments.
......@@ -109,11 +111,12 @@ type ScopeInfo struct {
// SelectorInfo defines the selector options of the Experiment.
type SelectorInfo struct {
NamespaceSelectors []string `json:"namespace_selectors" binding:"NamespaceSelectorsValid"`
LabelSelectors map[string]string `json:"label_selectors" binding:"MapSelectorsValid"`
AnnotationSelectors map[string]string `json:"annotation_selectors" binding:"MapSelectorsValid"`
FieldSelectors map[string]string `json:"field_selectors" binding:"MapSelectorsValid"`
PhaseSelector []string `json:"phase_selectors" binding:"PhaseSelectorsValid"`
NamespaceSelectors []string `json:"namespace_selectors" binding:"NamespaceSelectorsValid"`
LabelSelectors map[string]string `json:"label_selectors" binding:"MapSelectorsValid"`
ExpressionSelectors []metav1.LabelSelectorRequirement `json:"expression_selectors" binding:"RequirementSelectorsValid"`
AnnotationSelectors map[string]string `json:"annotation_selectors" binding:"MapSelectorsValid"`
FieldSelectors map[string]string `json:"field_selectors" binding:"MapSelectorsValid"`
PhaseSelector []string `json:"phase_selectors" binding:"PhaseSelectorsValid"`
// Pods is a map of string keys and a set values that used to select pods.
// The key defines the namespace which pods belong,
......@@ -134,6 +137,8 @@ func (s *SelectorInfo) ParseSelector() v1alpha1.SelectorSpec {
selector.LabelSelectors[key] = val
}
selector.ExpressionSelectors = append(selector.ExpressionSelectors, s.ExpressionSelectors...)
selector.AnnotationSelectors = make(map[string]string)
for key, val := range s.AnnotationSelectors {
selector.AnnotationSelectors[key] = val
......
......@@ -31,6 +31,7 @@ import (
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
......@@ -133,8 +134,16 @@ func SelectPods(ctx context.Context, c client.Client, r client.Reader, selector
if !clusterScoped {
listOptions.Namespace = targetNamespace
}
if len(selector.LabelSelectors) > 0 {
listOptions.LabelSelector = labels.SelectorFromSet(selector.LabelSelectors)
if len(selector.LabelSelectors) > 0 || len(selector.ExpressionSelectors) > 0 {
metav1Ls := &metav1.LabelSelector{
MatchLabels: selector.LabelSelectors,
MatchExpressions: selector.ExpressionSelectors,
}
ls, err := metav1.LabelSelectorAsSelector(metav1Ls)
if err != nil {
return nil, err
}
listOptions.LabelSelector = ls
}
listFunc := c.List
......@@ -274,8 +283,15 @@ func CheckPodMeetSelector(pod v1.Pod, selector v1alpha1.SelectorSpec) (bool, err
selector.LabelSelectors = make(map[string]string)
}
if len(selector.LabelSelectors) > 0 {
ls := labels.SelectorFromSet(selector.LabelSelectors)
if len(selector.LabelSelectors) > 0 || len(selector.ExpressionSelectors) > 0 {
metav1Ls := &metav1.LabelSelector{
MatchLabels: selector.LabelSelectors,
MatchExpressions: selector.ExpressionSelectors,
}
ls, err := metav1.LabelSelectorAsSelector(metav1Ls)
if err != nil {
return false, err
}
podLabels := labels.Set(pod.Labels)
if len(pod.Labels) == 0 || !ls.Matches(podLabels) {
return false, nil
......
......@@ -72,6 +72,33 @@ func TestSelectPods(t *testing.T) {
},
expectedPods: []v1.Pod{pods[5], pods[6]},
},
{
name: "filter pods by label expressions",
selector: v1alpha1.SelectorSpec{
ExpressionSelectors: []metav1.LabelSelectorRequirement{
{
Key: "l2",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"l2"},
},
},
},
expectedPods: []v1.Pod{pods[5], pods[6]},
},
{
name: "filter pods by label selectors and expression selectors",
selector: v1alpha1.SelectorSpec{
LabelSelectors: map[string]string{"l1": "l1"},
ExpressionSelectors: []metav1.LabelSelectorRequirement{
{
Key: "l2",
Operator: metav1.LabelSelectorOpIn,
Values: []string{"l2"},
},
},
},
expectedPods: nil,
},
{
name: "filter namespace and labels",
selector: v1alpha1.SelectorSpec{
......@@ -175,6 +202,35 @@ func TestCheckPodMeetSelector(t *testing.T) {
selector: v1alpha1.SelectorSpec{},
expectedValue: true,
},
{
name: "meet labels and meet expressions",
pod: newPod(PodArg{Name: "t1", Status: v1.PodPending, Labels: map[string]string{"app": "tikv", "ss": "t1"}}),
selector: v1alpha1.SelectorSpec{
LabelSelectors: map[string]string{"app": "tikv"},
ExpressionSelectors: []metav1.LabelSelectorRequirement{
{
Key: "ss",
Operator: metav1.LabelSelectorOpExists,
},
},
},
expectedValue: true,
},
{
name: "meet labels and not meet expressions",
pod: newPod(PodArg{Name: "t1", Status: v1.PodPending, Labels: map[string]string{"app": "tikv", "ss": "t1"}}),
selector: v1alpha1.SelectorSpec{
LabelSelectors: map[string]string{"app": "tikv"},
ExpressionSelectors: []metav1.LabelSelectorRequirement{
{
Key: "ss",
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{"t1"},
},
},
},
expectedValue: false,
},
{
name: "meet namespace",
pod: newPod(PodArg{Name: "t1"}),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment