s2irun.go 2.6 KB
Newer Older
R
runzexia 已提交
1
/*
H
hongming 已提交
2
Copyright 2019 The KubeSphere Authors.
R
runzexia 已提交
3

H
hongming 已提交
4 5 6
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
R
runzexia 已提交
7

H
hongming 已提交
8
    http://www.apache.org/licenses/LICENSE-2.0
R
runzexia 已提交
9

H
hongming 已提交
10 11 12 13 14
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.
R
runzexia 已提交
15 16
*/

Z
zryfish 已提交
17
package s2irun
R
runzexia 已提交
18 19

import (
20 21 22
	"k8s.io/apimachinery/pkg/labels"
	"kubesphere.io/kubesphere/pkg/apis/devops/v1alpha1"
	"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
Z
zryfish 已提交
23
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
24
	"kubesphere.io/kubesphere/pkg/server/params"
R
runzexia 已提交
25 26 27 28
	"sort"
)

type s2iRunSearcher struct {
Z
zryfish 已提交
29 30 31 32 33
	informers externalversions.SharedInformerFactory
}

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

Z
zryfish 已提交
36 37
func (s *s2iRunSearcher) Get(namespace, name string) (interface{}, error) {
	return s.informers.Devops().V1alpha1().S2iRuns().Lister().S2iRuns(namespace).Get(name)
H
hongming 已提交
38 39
}

R
runzexia 已提交
40 41 42
func (*s2iRunSearcher) match(match map[string]string, item *v1alpha1.S2iRun) bool {
	for k, v := range match {
		switch k {
Z
zryfish 已提交
43
		case v1alpha2.Status:
H
hongming 已提交
44 45 46
			if string(item.Status.RunState) != v {
				return false
			}
R
runzexia 已提交
47
		default:
H
hongming 已提交
48
			if !v1alpha2.ObjectMetaExactlyMath(k, v, item.ObjectMeta) {
H
hongming 已提交
49 50
				return false
			}
R
runzexia 已提交
51 52 53 54 55 56 57
		}
	}
	return true
}

func (*s2iRunSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.S2iRun) bool {
	for k, v := range fuzzy {
H
hongming 已提交
58
		if !v1alpha2.ObjectMetaFuzzyMath(k, v, item.ObjectMeta) {
R
runzexia 已提交
59 60 61 62 63 64
			return false
		}
	}
	return true
}

Z
zryfish 已提交
65 66
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 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	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 {
H
hongming 已提交
85
			i, j = j, i
R
runzexia 已提交
86
		}
H
hongming 已提交
87
		return v1alpha2.ObjectMetaCompare(result[i].ObjectMeta, result[j].ObjectMeta, orderBy)
R
runzexia 已提交
88 89 90 91 92 93 94 95
	})

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