From b93a0802f07d06f3213036c2bbeaae1d9ac50fdd Mon Sep 17 00:00:00 2001 From: hongming Date: Fri, 2 Aug 2019 11:17:53 +0800 Subject: [PATCH] support empty label filter Signed-off-by: hongming --- pkg/models/resources/clusterroles.go | 5 +++-- pkg/models/resources/configmaps.go | 5 +++-- pkg/models/resources/cronjobs.go | 5 +++-- pkg/models/resources/daemonsets.go | 5 +++-- pkg/models/resources/deployments.go | 5 +++-- pkg/models/resources/ingresses.go | 5 +++-- pkg/models/resources/jobs.go | 5 +++-- pkg/models/resources/namespaces.go | 5 +++-- pkg/models/resources/nodes.go | 5 +++-- pkg/models/resources/persistentvolumeclaims.go | 5 +++-- pkg/models/resources/pods.go | 5 +++-- pkg/models/resources/resources.go | 16 ++++++++-------- pkg/models/resources/roles.go | 5 +++-- pkg/models/resources/s2ibuilder.go | 5 +++-- pkg/models/resources/s2ibuildertemplate.go | 5 +++-- pkg/models/resources/s2irun.go | 5 +++-- pkg/models/resources/secrets.go | 5 +++-- pkg/models/resources/services.go | 5 +++-- pkg/models/resources/statefulsets.go | 5 +++-- pkg/models/resources/storageclasses.go | 5 +++-- pkg/models/resources/workspaces.go | 5 +++-- pkg/models/tenant/namespaces.go | 3 ++- pkg/models/tenant/workspaces.go | 3 ++- pkg/params/params.go | 18 ++++++++++++------ 24 files changed, 84 insertions(+), 56 deletions(-) diff --git a/pkg/models/resources/clusterroles.go b/pkg/models/resources/clusterroles.go index 6f6353fc..f0eade05 100644 --- a/pkg/models/resources/clusterroles.go +++ b/pkg/models/resources/clusterroles.go @@ -65,7 +65,8 @@ func (*clusterRoleSearcher) match(match map[string]string, item *rbac.ClusterRol } } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -91,7 +92,7 @@ func (*clusterRoleSearcher) fuzzy(fuzzy map[string]string, item *rbac.ClusterRol } return false default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/configmaps.go b/pkg/models/resources/configmaps.go index c59b1720..5f76bce0 100644 --- a/pkg/models/resources/configmaps.go +++ b/pkg/models/resources/configmaps.go @@ -50,7 +50,8 @@ func (*configMapSearcher) match(match map[string]string, item *v1.ConfigMap) boo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -80,7 +81,7 @@ func (*configMapSearcher) fuzzy(fuzzy map[string]string, item *v1.ConfigMap) boo return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/cronjobs.go b/pkg/models/resources/cronjobs.go index 16971cc0..5092cd55 100644 --- a/pkg/models/resources/cronjobs.go +++ b/pkg/models/resources/cronjobs.go @@ -62,7 +62,8 @@ func (*cronJobSearcher) match(match map[string]string, item *v1beta1.CronJob) bo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -92,7 +93,7 @@ func (*cronJobSearcher) fuzzy(fuzzy map[string]string, item *v1beta1.CronJob) bo return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/daemonsets.go b/pkg/models/resources/daemonsets.go index cb2bac63..b8e93311 100644 --- a/pkg/models/resources/daemonsets.go +++ b/pkg/models/resources/daemonsets.go @@ -64,7 +64,8 @@ func (*daemonSetSearcher) match(match map[string]string, item *v1.DaemonSet) boo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -94,7 +95,7 @@ func (*daemonSetSearcher) fuzzy(fuzzy map[string]string, item *v1.DaemonSet) boo return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/deployments.go b/pkg/models/resources/deployments.go index 5657748d..0feaf68b 100644 --- a/pkg/models/resources/deployments.go +++ b/pkg/models/resources/deployments.go @@ -68,7 +68,8 @@ func (*deploymentSearcher) match(match map[string]string, item *v1.Deployment) b return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -98,7 +99,7 @@ func (*deploymentSearcher) fuzzy(fuzzy map[string]string, item *v1.Deployment) b return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/ingresses.go b/pkg/models/resources/ingresses.go index b81cedee..71211eeb 100644 --- a/pkg/models/resources/ingresses.go +++ b/pkg/models/resources/ingresses.go @@ -51,7 +51,8 @@ func (*ingressSearcher) match(match map[string]string, item *extensions.Ingress) return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -81,7 +82,7 @@ func (*ingressSearcher) fuzzy(fuzzy map[string]string, item *extensions.Ingress) return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/jobs.go b/pkg/models/resources/jobs.go index 506e4fa1..050a1f5e 100644 --- a/pkg/models/resources/jobs.go +++ b/pkg/models/resources/jobs.go @@ -76,7 +76,8 @@ func (*jobSearcher) match(match map[string]string, item *batchv1.Job) bool { return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -106,7 +107,7 @@ func (*jobSearcher) fuzzy(fuzzy map[string]string, item *batchv1.Job) bool { return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/namespaces.go b/pkg/models/resources/namespaces.go index d34aa01b..0007c230 100644 --- a/pkg/models/resources/namespaces.go +++ b/pkg/models/resources/namespaces.go @@ -50,7 +50,8 @@ func (*namespaceSearcher) match(match map[string]string, item *v1.Namespace) boo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -80,7 +81,7 @@ func (*namespaceSearcher) fuzzy(fuzzy map[string]string, item *v1.Namespace) boo return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/nodes.go b/pkg/models/resources/nodes.go index 38e607f0..ba3673af 100644 --- a/pkg/models/resources/nodes.go +++ b/pkg/models/resources/nodes.go @@ -56,7 +56,8 @@ func (*nodeSearcher) match(match map[string]string, item *v1.Node) bool { return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -86,7 +87,7 @@ func (*nodeSearcher) fuzzy(fuzzy map[string]string, item *v1.Node) bool { return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/persistentvolumeclaims.go b/pkg/models/resources/persistentvolumeclaims.go index a27e0ccb..379b3607 100644 --- a/pkg/models/resources/persistentvolumeclaims.go +++ b/pkg/models/resources/persistentvolumeclaims.go @@ -71,7 +71,8 @@ func (*persistentVolumeClaimSearcher) match(match map[string]string, item *v1.Pe return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -101,7 +102,7 @@ func (*persistentVolumeClaimSearcher) fuzzy(fuzzy map[string]string, item *v1.Pe return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/pods.go b/pkg/models/resources/pods.go index 95cdefa2..7670b301 100644 --- a/pkg/models/resources/pods.go +++ b/pkg/models/resources/pods.go @@ -180,7 +180,8 @@ func (*podSearcher) match(match map[string]string, item *v1.Pod) bool { return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -210,7 +211,7 @@ func (*podSearcher) fuzzy(fuzzy map[string]string, item *v1.Pod) bool { return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/resources.go b/pkg/models/resources/resources.go index 4017cced..356c4601 100644 --- a/pkg/models/resources/resources.go +++ b/pkg/models/resources/resources.go @@ -158,14 +158,14 @@ func ListResources(namespace, resource string, conditions *params.Conditions, or } func searchFuzzy(m map[string]string, key, value string) bool { - for k, v := range m { - if key == "" { - if strings.Contains(k, value) || strings.Contains(v, value) { - return true - } - } else if k == key && strings.Contains(v, value) { - return true - } + + val, exist := m[key] + + if value == "" && (!exist || val == "") { + return true + } else if value != "" && strings.Contains(val, value) { + return true } + return false } diff --git a/pkg/models/resources/roles.go b/pkg/models/resources/roles.go index 2787c50f..d203834a 100644 --- a/pkg/models/resources/roles.go +++ b/pkg/models/resources/roles.go @@ -50,7 +50,8 @@ func (*roleSearcher) match(match map[string]string, item *rbac.Role) bool { return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -76,7 +77,7 @@ func (*roleSearcher) fuzzy(fuzzy map[string]string, item *rbac.Role) bool { } return false default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/s2ibuilder.go b/pkg/models/resources/s2ibuilder.go index 5f72f550..58616d03 100644 --- a/pkg/models/resources/s2ibuilder.go +++ b/pkg/models/resources/s2ibuilder.go @@ -50,7 +50,8 @@ func (*s2iBuilderSearcher) match(match map[string]string, item *v1alpha1.S2iBuil return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -80,7 +81,7 @@ func (*s2iBuilderSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iBuil return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/s2ibuildertemplate.go b/pkg/models/resources/s2ibuildertemplate.go index 9d156cd4..dd9b5734 100644 --- a/pkg/models/resources/s2ibuildertemplate.go +++ b/pkg/models/resources/s2ibuildertemplate.go @@ -50,7 +50,8 @@ func (*s2iBuilderTemplateSearcher) match(match map[string]string, item *v1alpha1 return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -76,7 +77,7 @@ func (*s2iBuilderTemplateSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1 } return false default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/s2irun.go b/pkg/models/resources/s2irun.go index eb7245cd..d82cb548 100644 --- a/pkg/models/resources/s2irun.go +++ b/pkg/models/resources/s2irun.go @@ -57,7 +57,8 @@ func (*s2iRunSearcher) match(match map[string]string, item *v1alpha1.S2iRun) boo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -87,7 +88,7 @@ func (*s2iRunSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iRun) boo return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/secrets.go b/pkg/models/resources/secrets.go index 7fbd5871..a864c9fd 100644 --- a/pkg/models/resources/secrets.go +++ b/pkg/models/resources/secrets.go @@ -54,7 +54,8 @@ func (*secretSearcher) match(match map[string]string, item *v1.Secret) bool { return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -84,7 +85,7 @@ func (*secretSearcher) fuzzy(fuzzy map[string]string, item *v1.Secret) bool { return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/services.go b/pkg/models/resources/services.go index f231c33c..58878b77 100644 --- a/pkg/models/resources/services.go +++ b/pkg/models/resources/services.go @@ -50,7 +50,8 @@ func (*serviceSearcher) match(match map[string]string, item *v1.Service) bool { return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -80,7 +81,7 @@ func (*serviceSearcher) fuzzy(fuzzy map[string]string, item *v1.Service) bool { return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/statefulsets.go b/pkg/models/resources/statefulsets.go index 69ff8c33..6460ce0f 100644 --- a/pkg/models/resources/statefulsets.go +++ b/pkg/models/resources/statefulsets.go @@ -67,7 +67,8 @@ func (*statefulSetSearcher) match(match map[string]string, item *v1.StatefulSet) return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -97,7 +98,7 @@ func (*statefulSetSearcher) fuzzy(fuzzy map[string]string, item *v1.StatefulSet) return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/storageclasses.go b/pkg/models/resources/storageclasses.go index 1ba6df52..978d3b5e 100644 --- a/pkg/models/resources/storageclasses.go +++ b/pkg/models/resources/storageclasses.go @@ -50,7 +50,8 @@ func (*storageClassesSearcher) match(match map[string]string, item *v1.StorageCl return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -76,7 +77,7 @@ func (*storageClassesSearcher) fuzzy(fuzzy map[string]string, item *v1.StorageCl } return false default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/resources/workspaces.go b/pkg/models/resources/workspaces.go index b5919ca3..e152ebdc 100644 --- a/pkg/models/resources/workspaces.go +++ b/pkg/models/resources/workspaces.go @@ -50,7 +50,8 @@ func (*workspaceSearcher) match(match map[string]string, item *tenantv1alpha1.Wo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } @@ -80,7 +81,7 @@ func (*workspaceSearcher) fuzzy(fuzzy map[string]string, item *tenantv1alpha1.Wo return false } default: - if !searchFuzzy(item.Labels, k, v) && !searchFuzzy(item.Annotations, k, v) { + if !searchFuzzy(item.Labels, k, v) { return false } } diff --git a/pkg/models/tenant/namespaces.go b/pkg/models/tenant/namespaces.go index 121a5726..36ad27d3 100644 --- a/pkg/models/tenant/namespaces.go +++ b/pkg/models/tenant/namespaces.go @@ -49,7 +49,8 @@ func (*namespaceSearcher) match(match map[string]string, item *v1.Namespace) boo return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } diff --git a/pkg/models/tenant/workspaces.go b/pkg/models/tenant/workspaces.go index 524bde1d..e42271e7 100644 --- a/pkg/models/tenant/workspaces.go +++ b/pkg/models/tenant/workspaces.go @@ -48,7 +48,8 @@ func (*workspaceSearcher) match(match map[string]string, item *v1alpha1.Workspac return false } default: - if item.Labels[k] != v { + // label not exist or value not equal + if val, ok := item.Labels[k]; !ok || val != v { return false } } diff --git a/pkg/params/params.go b/pkg/params/params.go index 3e14c3f1..0d2ace41 100644 --- a/pkg/params/params.go +++ b/pkg/params/params.go @@ -56,15 +56,21 @@ func ParseConditions(conditionsStr string) (*Conditions, error) { return conditions, nil } + // ?conditions=key1=value1,key2~value2,key3= for _, item := range strings.Split(conditionsStr, ",") { - if strings.Count(item, "=") > 1 || strings.Count(item, "~") > 1 { - return nil, fmt.Errorf("invalid conditions") - } - if groups := regexp.MustCompile(`(\S+)([=~])(\S+)`).FindStringSubmatch(item); len(groups) == 4 { + // exact query: key=value, if value is empty means label value must be "" + // 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[2] == "=" { - conditions.Match[groups[1]] = groups[3] + conditions.Match[groups[1]] = value } else { - conditions.Fuzzy[groups[1]] = groups[3] + conditions.Fuzzy[groups[1]] = value } } else { return nil, fmt.Errorf("invalid conditions") -- GitLab