未验证 提交 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
}
}
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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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
}
}
......
......@@ -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")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册