未验证 提交 b93a0802 编写于 作者: H hongming

support empty label filter

Signed-off-by: Nhongming <talonwan@yunify.com>
上级 bfa81db9
...@@ -65,7 +65,8 @@ func (*clusterRoleSearcher) match(match map[string]string, item *rbac.ClusterRol ...@@ -65,7 +65,8 @@ func (*clusterRoleSearcher) match(match map[string]string, item *rbac.ClusterRol
} }
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -91,7 +92,7 @@ func (*clusterRoleSearcher) fuzzy(fuzzy map[string]string, item *rbac.ClusterRol ...@@ -91,7 +92,7 @@ func (*clusterRoleSearcher) fuzzy(fuzzy map[string]string, item *rbac.ClusterRol
} }
return false return false
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*configMapSearcher) match(match map[string]string, item *v1.ConfigMap) boo ...@@ -50,7 +50,8 @@ func (*configMapSearcher) match(match map[string]string, item *v1.ConfigMap) boo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -80,7 +81,7 @@ func (*configMapSearcher) fuzzy(fuzzy map[string]string, item *v1.ConfigMap) boo ...@@ -80,7 +81,7 @@ func (*configMapSearcher) fuzzy(fuzzy map[string]string, item *v1.ConfigMap) boo
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -62,7 +62,8 @@ func (*cronJobSearcher) match(match map[string]string, item *v1beta1.CronJob) bo ...@@ -62,7 +62,8 @@ func (*cronJobSearcher) match(match map[string]string, item *v1beta1.CronJob) bo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -92,7 +93,7 @@ func (*cronJobSearcher) fuzzy(fuzzy map[string]string, item *v1beta1.CronJob) bo ...@@ -92,7 +93,7 @@ func (*cronJobSearcher) fuzzy(fuzzy map[string]string, item *v1beta1.CronJob) bo
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -64,7 +64,8 @@ func (*daemonSetSearcher) match(match map[string]string, item *v1.DaemonSet) boo ...@@ -64,7 +64,8 @@ func (*daemonSetSearcher) match(match map[string]string, item *v1.DaemonSet) boo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -94,7 +95,7 @@ func (*daemonSetSearcher) fuzzy(fuzzy map[string]string, item *v1.DaemonSet) boo ...@@ -94,7 +95,7 @@ func (*daemonSetSearcher) fuzzy(fuzzy map[string]string, item *v1.DaemonSet) boo
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -68,7 +68,8 @@ func (*deploymentSearcher) match(match map[string]string, item *v1.Deployment) b ...@@ -68,7 +68,8 @@ func (*deploymentSearcher) match(match map[string]string, item *v1.Deployment) b
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -98,7 +99,7 @@ func (*deploymentSearcher) fuzzy(fuzzy map[string]string, item *v1.Deployment) b ...@@ -98,7 +99,7 @@ func (*deploymentSearcher) fuzzy(fuzzy map[string]string, item *v1.Deployment) b
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -51,7 +51,8 @@ func (*ingressSearcher) match(match map[string]string, item *extensions.Ingress) ...@@ -51,7 +51,8 @@ func (*ingressSearcher) match(match map[string]string, item *extensions.Ingress)
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -81,7 +82,7 @@ func (*ingressSearcher) fuzzy(fuzzy map[string]string, item *extensions.Ingress) ...@@ -81,7 +82,7 @@ func (*ingressSearcher) fuzzy(fuzzy map[string]string, item *extensions.Ingress)
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -76,7 +76,8 @@ func (*jobSearcher) match(match map[string]string, item *batchv1.Job) bool { ...@@ -76,7 +76,8 @@ func (*jobSearcher) match(match map[string]string, item *batchv1.Job) bool {
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -106,7 +107,7 @@ func (*jobSearcher) fuzzy(fuzzy map[string]string, item *batchv1.Job) bool { ...@@ -106,7 +107,7 @@ func (*jobSearcher) fuzzy(fuzzy map[string]string, item *batchv1.Job) bool {
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*namespaceSearcher) match(match map[string]string, item *v1.Namespace) boo ...@@ -50,7 +50,8 @@ func (*namespaceSearcher) match(match map[string]string, item *v1.Namespace) boo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -80,7 +81,7 @@ func (*namespaceSearcher) fuzzy(fuzzy map[string]string, item *v1.Namespace) boo ...@@ -80,7 +81,7 @@ func (*namespaceSearcher) fuzzy(fuzzy map[string]string, item *v1.Namespace) boo
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -56,7 +56,8 @@ func (*nodeSearcher) match(match map[string]string, item *v1.Node) bool { ...@@ -56,7 +56,8 @@ func (*nodeSearcher) match(match map[string]string, item *v1.Node) bool {
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -86,7 +87,7 @@ func (*nodeSearcher) fuzzy(fuzzy map[string]string, item *v1.Node) bool { ...@@ -86,7 +87,7 @@ func (*nodeSearcher) fuzzy(fuzzy map[string]string, item *v1.Node) bool {
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -71,7 +71,8 @@ func (*persistentVolumeClaimSearcher) match(match map[string]string, item *v1.Pe ...@@ -71,7 +71,8 @@ func (*persistentVolumeClaimSearcher) match(match map[string]string, item *v1.Pe
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -101,7 +102,7 @@ func (*persistentVolumeClaimSearcher) fuzzy(fuzzy map[string]string, item *v1.Pe ...@@ -101,7 +102,7 @@ func (*persistentVolumeClaimSearcher) fuzzy(fuzzy map[string]string, item *v1.Pe
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -180,7 +180,8 @@ func (*podSearcher) match(match map[string]string, item *v1.Pod) bool { ...@@ -180,7 +180,8 @@ func (*podSearcher) match(match map[string]string, item *v1.Pod) bool {
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -210,7 +211,7 @@ func (*podSearcher) fuzzy(fuzzy map[string]string, item *v1.Pod) bool { ...@@ -210,7 +211,7 @@ func (*podSearcher) fuzzy(fuzzy map[string]string, item *v1.Pod) bool {
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -158,14 +158,14 @@ func ListResources(namespace, resource string, conditions *params.Conditions, or ...@@ -158,14 +158,14 @@ func ListResources(namespace, resource string, conditions *params.Conditions, or
} }
func searchFuzzy(m map[string]string, key, value string) bool { func searchFuzzy(m map[string]string, key, value string) bool {
for k, v := range m {
if key == "" { val, exist := m[key]
if strings.Contains(k, value) || strings.Contains(v, value) {
if value == "" && (!exist || val == "") {
return true return true
} } else if value != "" && strings.Contains(val, value) {
} else if k == key && strings.Contains(v, value) {
return true return true
} }
}
return false return false
} }
...@@ -50,7 +50,8 @@ func (*roleSearcher) match(match map[string]string, item *rbac.Role) bool { ...@@ -50,7 +50,8 @@ func (*roleSearcher) match(match map[string]string, item *rbac.Role) bool {
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -76,7 +77,7 @@ func (*roleSearcher) fuzzy(fuzzy map[string]string, item *rbac.Role) bool { ...@@ -76,7 +77,7 @@ func (*roleSearcher) fuzzy(fuzzy map[string]string, item *rbac.Role) bool {
} }
return false return false
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*s2iBuilderSearcher) match(match map[string]string, item *v1alpha1.S2iBuil ...@@ -50,7 +50,8 @@ func (*s2iBuilderSearcher) match(match map[string]string, item *v1alpha1.S2iBuil
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -80,7 +81,7 @@ func (*s2iBuilderSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iBuil ...@@ -80,7 +81,7 @@ func (*s2iBuilderSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iBuil
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*s2iBuilderTemplateSearcher) match(match map[string]string, item *v1alpha1 ...@@ -50,7 +50,8 @@ func (*s2iBuilderTemplateSearcher) match(match map[string]string, item *v1alpha1
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -76,7 +77,7 @@ func (*s2iBuilderTemplateSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1 ...@@ -76,7 +77,7 @@ func (*s2iBuilderTemplateSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1
} }
return false return false
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -57,7 +57,8 @@ func (*s2iRunSearcher) match(match map[string]string, item *v1alpha1.S2iRun) boo ...@@ -57,7 +57,8 @@ func (*s2iRunSearcher) match(match map[string]string, item *v1alpha1.S2iRun) boo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -87,7 +88,7 @@ func (*s2iRunSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iRun) boo ...@@ -87,7 +88,7 @@ func (*s2iRunSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iRun) boo
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -54,7 +54,8 @@ func (*secretSearcher) match(match map[string]string, item *v1.Secret) bool { ...@@ -54,7 +54,8 @@ func (*secretSearcher) match(match map[string]string, item *v1.Secret) bool {
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -84,7 +85,7 @@ func (*secretSearcher) fuzzy(fuzzy map[string]string, item *v1.Secret) bool { ...@@ -84,7 +85,7 @@ func (*secretSearcher) fuzzy(fuzzy map[string]string, item *v1.Secret) bool {
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*serviceSearcher) match(match map[string]string, item *v1.Service) bool { ...@@ -50,7 +50,8 @@ func (*serviceSearcher) match(match map[string]string, item *v1.Service) bool {
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -80,7 +81,7 @@ func (*serviceSearcher) fuzzy(fuzzy map[string]string, item *v1.Service) bool { ...@@ -80,7 +81,7 @@ func (*serviceSearcher) fuzzy(fuzzy map[string]string, item *v1.Service) bool {
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -67,7 +67,8 @@ func (*statefulSetSearcher) match(match map[string]string, item *v1.StatefulSet) ...@@ -67,7 +67,8 @@ func (*statefulSetSearcher) match(match map[string]string, item *v1.StatefulSet)
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -97,7 +98,7 @@ func (*statefulSetSearcher) fuzzy(fuzzy map[string]string, item *v1.StatefulSet) ...@@ -97,7 +98,7 @@ func (*statefulSetSearcher) fuzzy(fuzzy map[string]string, item *v1.StatefulSet)
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*storageClassesSearcher) match(match map[string]string, item *v1.StorageCl ...@@ -50,7 +50,8 @@ func (*storageClassesSearcher) match(match map[string]string, item *v1.StorageCl
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -76,7 +77,7 @@ func (*storageClassesSearcher) fuzzy(fuzzy map[string]string, item *v1.StorageCl ...@@ -76,7 +77,7 @@ func (*storageClassesSearcher) fuzzy(fuzzy map[string]string, item *v1.StorageCl
} }
return false return false
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -50,7 +50,8 @@ func (*workspaceSearcher) match(match map[string]string, item *tenantv1alpha1.Wo ...@@ -50,7 +50,8 @@ func (*workspaceSearcher) match(match map[string]string, item *tenantv1alpha1.Wo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
...@@ -80,7 +81,7 @@ func (*workspaceSearcher) fuzzy(fuzzy map[string]string, item *tenantv1alpha1.Wo ...@@ -80,7 +81,7 @@ func (*workspaceSearcher) fuzzy(fuzzy map[string]string, item *tenantv1alpha1.Wo
return false return false
} }
default: default:
if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { if !searchFuzzy(item.Labels, k, v) {
return false return false
} }
} }
......
...@@ -49,7 +49,8 @@ func (*namespaceSearcher) match(match map[string]string, item *v1.Namespace) boo ...@@ -49,7 +49,8 @@ func (*namespaceSearcher) match(match map[string]string, item *v1.Namespace) boo
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
......
...@@ -48,7 +48,8 @@ func (*workspaceSearcher) match(match map[string]string, item *v1alpha1.Workspac ...@@ -48,7 +48,8 @@ func (*workspaceSearcher) match(match map[string]string, item *v1alpha1.Workspac
return false return false
} }
default: default:
if item.Labels[k] != v { // label not exist or value not equal
if val, ok := item.Labels[k]; !ok || val != v {
return false return false
} }
} }
......
...@@ -56,15 +56,21 @@ func ParseConditions(conditionsStr string) (*Conditions, error) { ...@@ -56,15 +56,21 @@ func ParseConditions(conditionsStr string) (*Conditions, error) {
return conditions, nil return conditions, nil
} }
// ?conditions=key1=value1,key2~value2,key3=
for _, item := range strings.Split(conditionsStr, ",") { for _, item := range strings.Split(conditionsStr, ",") {
if strings.Count(item, "=") > 1 || strings.Count(item, "~") > 1 { // exact query: key=value, if value is empty means label value must be ""
return nil, fmt.Errorf("invalid conditions") // fuzzy query: key~value, if value is empty means label value is "" or label key not exist
if groups := regexp.MustCompile(`(\S+)([=~])(\S+)?`).FindStringSubmatch(item); len(groups) >= 3 {
value := ""
if len(groups) > 3 {
value = groups[3]
} }
if groups := regexp.MustCompile(`(\S+)([=~])(\S+)`).FindStringSubmatch(item); len(groups) == 4 {
if groups[2] == "=" { if groups[2] == "=" {
conditions.Match[groups[1]] = groups[3] conditions.Match[groups[1]] = value
} else { } else {
conditions.Fuzzy[groups[1]] = groups[3] conditions.Fuzzy[groups[1]] = value
} }
} else { } else {
return nil, fmt.Errorf("invalid conditions") return nil, fmt.Errorf("invalid conditions")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册