resources.go 4.9 KB
Newer Older
J
jeff 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*

 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.

*/
package resources

import (
H
hongming 已提交
21 22 23
	"fmt"
	"kubesphere.io/kubesphere/pkg/models"
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
24
	"kubesphere.io/kubesphere/pkg/utils/sliceutil"
J
jeff 已提交
25 26 27 28
	"strings"
)

func init() {
H
hongming 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	resources[ConfigMaps] = &configMapSearcher{}
	resources[CronJobs] = &cronJobSearcher{}
	resources[DaemonSets] = &daemonSetSearcher{}
	resources[Deployments] = &deploymentSearcher{}
	resources[Ingresses] = &ingressSearcher{}
	resources[Jobs] = &jobSearcher{}
	resources[PersistentVolumeClaims] = &persistentVolumeClaimSearcher{}
	resources[Secrets] = &secretSearcher{}
	resources[Services] = &serviceSearcher{}
	resources[StatefulSets] = &statefulSetSearcher{}
	resources[Pods] = &podSearcher{}
	resources[Roles] = &roleSearcher{}
	resources[S2iBuilders] = &s2iBuilderSearcher{}
	resources[S2iRuns] = &s2iRunSearcher{}

	resources[Nodes] = &nodeSearcher{}
	resources[Namespaces] = &namespaceSearcher{}
	resources[ClusterRoles] = &clusterRoleSearcher{}
	resources[StorageClasses] = &storageClassesSearcher{}
	resources[S2iBuilderTemplates] = &s2iBuilderTemplateSearcher{}
	resources[Workspaces] = &workspaceSearcher{}
J
jeff 已提交
50 51
}

H
hongming 已提交
52 53 54 55
var (
	resources        = make(map[string]resourceSearchInterface)
	clusterResources = []string{Nodes, Workspaces, Namespaces, ClusterRoles, StorageClasses, S2iBuilderTemplates}
)
J
jeff 已提交
56 57

const (
H
hongming 已提交
58
	Name                   = "name"
J
jeff 已提交
59
	label                  = "label"
H
hongming 已提交
60 61
	ownerKind              = "ownerKind"
	ownerName              = "ownerName"
H
hongming 已提交
62
	CreateTime             = "createTime"
J
jeff 已提交
63
	updateTime             = "updateTime"
H
hongming 已提交
64
	lastScheduleTime       = "lastScheduleTime"
J
jeff 已提交
65 66 67
	chart                  = "chart"
	release                = "release"
	annotation             = "annotation"
H
hongming 已提交
68
	Keyword                = "keyword"
J
jeff 已提交
69 70 71 72 73 74 75 76 77 78 79
	status                 = "status"
	running                = "running"
	paused                 = "paused"
	updating               = "updating"
	stopped                = "stopped"
	failed                 = "failed"
	complete               = "complete"
	app                    = "app"
	Deployments            = "deployments"
	DaemonSets             = "daemonsets"
	Roles                  = "roles"
H
hongming 已提交
80 81
	Workspaces             = "workspaces"
	WorkspaceRoles         = "workspaceroles"
J
jeff 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94
	CronJobs               = "cronjobs"
	ConfigMaps             = "configmaps"
	Ingresses              = "ingresses"
	Jobs                   = "jobs"
	PersistentVolumeClaims = "persistentvolumeclaims"
	Pods                   = "pods"
	Secrets                = "secrets"
	Services               = "services"
	StatefulSets           = "statefulsets"
	Nodes                  = "nodes"
	Namespaces             = "namespaces"
	StorageClasses         = "storageclasses"
	ClusterRoles           = "clusterroles"
R
runzexia 已提交
95 96 97
	S2iBuilderTemplates    = "s2ibuildertemplates"
	S2iBuilders            = "s2ibuilders"
	S2iRuns                = "s2iruns"
J
jeff 已提交
98 99
)

H
hongming 已提交
100 101
type resourceSearchInterface interface {
	get(namespace, name string) (interface{}, error)
H
hongming 已提交
102
	search(namespace string, conditions *params.Conditions, orderBy string, reverse bool) ([]interface{}, error)
J
jeff 已提交
103 104
}

H
hongming 已提交
105
func GetResource(namespace, resource, name string) (interface{}, error) {
H
hongming 已提交
106
	if searcher, ok := resources[resource]; ok {
H
hongming 已提交
107 108 109
		resource, err := searcher.get(namespace, name)
		if err != nil {
			return nil, err
J
jeff 已提交
110
		}
H
hongming 已提交
111
		return resource, nil
J
jeff 已提交
112
	}
H
hongming 已提交
113
	return nil, fmt.Errorf("resource %s not found", resource)
J
jeff 已提交
114 115
}

H
hongming 已提交
116
func ListResources(namespace, resource string, conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
J
jeff 已提交
117 118
	items := make([]interface{}, 0)
	var err error
H
hongming 已提交
119
	var result []interface{}
J
jeff 已提交
120

H
hongming 已提交
121 122 123
	// none namespace resource
	if namespace != "" && sliceutil.HasString(clusterResources, resource) {
		return nil, fmt.Errorf("not found")
J
jeff 已提交
124 125
	}

H
hongming 已提交
126 127
	if searcher, ok := resources[resource]; ok {
		result, err = searcher.search(namespace, conditions, orderBy, reverse)
J
jeff 已提交
128
	} else {
H
hongming 已提交
129
		return nil, fmt.Errorf("not found")
J
jeff 已提交
130 131 132
	}

	if err != nil {
H
hongming 已提交
133
		return nil, err
J
jeff 已提交
134 135 136
	}

	for i, d := range result {
H
hongming 已提交
137
		if i >= offset && (limit == -1 || len(items) < limit) {
J
jeff 已提交
138 139 140 141
			items = append(items, d)
		}
	}

H
hongming 已提交
142
	return &models.PageableResponse{TotalCount: len(result), Items: items}, nil
J
jeff 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156
}

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
		}
	}
	return false
}