s2irun.go 3.7 KB
Newer Older
R
runzexia 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*

 Copyright 2019 The KubeSphere Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

*/

Z
zryfish 已提交
19
package s2irun
R
runzexia 已提交
20 21

import (
22 23 24
	"k8s.io/apimachinery/pkg/labels"
	"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
H
hongming 已提交
25
	"kubesphere.io/kubesphere/pkg/constants"
Z
zryfish 已提交
26
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
27
	"kubesphere.io/kubesphere/pkg/server/params"
H
hongming 已提交
28
	"kubesphere.io/kubesphere/pkg/utils/sliceutil"
R
runzexia 已提交
29 30 31 32 33
	"sort"
	"strings"
)

type s2iRunSearcher struct {
Z
zryfish 已提交
34 35 36 37 38
	informers externalversions.SharedInformerFactory
}

func NewS2iRunSearcher(informers externalversions.SharedInformerFactory) v1alpha2.Interface {
	return &s2iRunSearcher{informers: informers}
R
runzexia 已提交
39 40
}

Z
zryfish 已提交
41 42
func (s *s2iRunSearcher) Get(namespace, name string) (interface{}, error) {
	return s.informers.Devops().V1alpha1().S2iRuns().Lister().S2iRuns(namespace).Get(name)
H
hongming 已提交
43 44
}

R
runzexia 已提交
45 46 47 48
// exactly Match
func (*s2iRunSearcher) match(match map[string]string, item *v1alpha1.S2iRun) bool {
	for k, v := range match {
		switch k {
Z
zryfish 已提交
49
		case v1alpha2.Name:
H
hongming 已提交
50 51
			names := strings.Split(v, "|")
			if !sliceutil.HasString(names, item.Name) {
R
runzexia 已提交
52
				return false
R
runzexia 已提交
53
			}
Z
zryfish 已提交
54
		case v1alpha2.Status:
H
hongming 已提交
55 56 57
			if string(item.Status.RunState) != v {
				return false
			}
Z
zryfish 已提交
58 59
		case v1alpha2.Keyword:
			if !strings.Contains(item.Name, v) && !v1alpha2.SearchFuzzy(item.Labels, "", v) && !v1alpha2.SearchFuzzy(item.Annotations, "", v) {
R
runzexia 已提交
60
				return false
R
runzexia 已提交
61 62
			}
		default:
H
hongming 已提交
63 64
			// label not exist or value not equal
			if val, ok := item.Labels[k]; !ok || val != v {
H
hongming 已提交
65 66
				return false
			}
R
runzexia 已提交
67 68 69 70 71 72 73 74 75
		}
	}
	return true
}

// Fuzzy searchInNamespace
func (*s2iRunSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iRun) bool {
	for k, v := range fuzzy {
		switch k {
Z
zryfish 已提交
76
		case v1alpha2.Name:
H
hongming 已提交
77
			if !strings.Contains(item.Name, v) && !strings.Contains(item.Annotations[constants.DisplayNameAnnotationKey], v) {
R
runzexia 已提交
78 79
				return false
			}
Z
zryfish 已提交
80 81
		case v1alpha2.Label:
			if !v1alpha2.SearchFuzzy(item.Labels, "", v) {
R
runzexia 已提交
82 83
				return false
			}
Z
zryfish 已提交
84 85
		case v1alpha2.Annotation:
			if !v1alpha2.SearchFuzzy(item.Annotations, "", v) {
R
runzexia 已提交
86 87 88 89
				return false
			}
			return false
		default:
Z
zryfish 已提交
90
			if !v1alpha2.SearchFuzzy(item.Labels, k, v) {
R
runzexia 已提交
91 92 93 94 95 96 97 98 99
				return false
			}
		}
	}
	return true
}

func (*s2iRunSearcher) compare(a, b *v1alpha1.S2iRun, orderBy string) bool {
	switch orderBy {
Z
zryfish 已提交
100
	case v1alpha2.CreateTime:
R
runzexia 已提交
101
		return a.CreationTimestamp.Time.Before(b.CreationTimestamp.Time)
Z
zryfish 已提交
102
	case v1alpha2.Name:
R
runzexia 已提交
103 104 105 106 107 108
		fallthrough
	default:
		return strings.Compare(a.Name, b.Name) <= 0
	}
}

Z
zryfish 已提交
109 110
func (s *s2iRunSearcher) Search(namespace string, conditions *params.Conditions, orderBy string, reverse bool) ([]interface{}, error) {
	s2iRuns, err := s.informers.Devops().V1alpha1().S2iRuns().Lister().S2iRuns(namespace).List(labels.Everything())
R
runzexia 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

	if err != nil {
		return nil, err
	}

	result := make([]*v1alpha1.S2iRun, 0)

	if len(conditions.Match) == 0 && len(conditions.Fuzzy) == 0 {
		result = s2iRuns
	} else {
		for _, item := range s2iRuns {
			if s.match(conditions.Match, item) && s.fuzzy(conditions.Fuzzy, item) {
				result = append(result, item)
			}
		}
	}
	sort.Slice(result, func(i, j int) bool {
		if reverse {
			tmp := i
			i = j
			j = tmp
		}
		return s.compare(result[i], result[j], orderBy)
	})

	r := make([]interface{}, 0)
	for _, i := range result {
		r = append(r, i)
	}
	return r, nil
}