s2irun.go 2.6 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"
Z
zryfish 已提交
25
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
26
	"kubesphere.io/kubesphere/pkg/server/params"
R
runzexia 已提交
27 28 29 30
	"sort"
)

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

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

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

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

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

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

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

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