handler.go 6.0 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 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 已提交
17 18 19 20
package v1alpha3

import (
	"github.com/emicklei/go-restful"
H
hongming 已提交
21
	"k8s.io/klog"
Z
zryfish 已提交
22 23 24
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/apiserver/query"
	"kubesphere.io/kubesphere/pkg/models/components"
H
hongming 已提交
25 26
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
	resourcev1alpha2 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha2/resource"
H
hongming 已提交
27
	resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
H
hongming 已提交
28 29
	"kubesphere.io/kubesphere/pkg/server/params"
	"strings"
Z
zryfish 已提交
30 31 32
)

type Handler struct {
H
hongming 已提交
33
	resourceGetterV1alpha3  *resourcev1alpha3.ResourceGetter
H
hongming 已提交
34 35
	resourcesGetterV1alpha2 *resourcev1alpha2.ResourceGetter
	componentsGetter        components.ComponentsGetter
Z
zryfish 已提交
36 37
}

H
hongming 已提交
38
func New(resourceGetterV1alpha3 *resourcev1alpha3.ResourceGetter, resourcesGetterV1alpha2 *resourcev1alpha2.ResourceGetter, componentsGetter components.ComponentsGetter) *Handler {
Z
zryfish 已提交
39
	return &Handler{
H
hongming 已提交
40 41 42
		resourceGetterV1alpha3:  resourceGetterV1alpha3,
		resourcesGetterV1alpha2: resourcesGetterV1alpha2,
		componentsGetter:        componentsGetter,
Z
zryfish 已提交
43 44 45
	}
}

Z
zhangmin 已提交
46 47 48 49 50
func (h *Handler) handleGetResources(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")
	resourceType := request.PathParameter("resources")
	name := request.PathParameter("name")

Z
Zack Zhang 已提交
51
	// use informers to retrieve resources
Z
zhangmin 已提交
52 53 54 55 56 57
	result, err := h.resourceGetterV1alpha3.Get(resourceType, namespace, name)
	if err == nil {
		response.WriteEntity(result)
		return
	}

H
hongming 已提交
58
	if err != resourcev1alpha3.ErrResourceNotSupported {
H
hongming 已提交
59
		klog.Error(err, resourceType)
Z
zhangmin 已提交
60 61 62 63 64 65 66
		api.HandleInternalError(response, nil, err)
		return
	}

	// fallback to v1alpha2
	resultV1alpha2, err := h.resourcesGetterV1alpha2.GetResource(namespace, resourceType, name)
	if err != nil {
H
hongming 已提交
67 68 69 70
		if err == resourcev1alpha2.ErrResourceNotSupported {
			api.HandleNotFound(response, request, err)
			return
		}
Z
zhangmin 已提交
71
		klog.Error(err)
H
hongming 已提交
72
		api.HandleError(response, request, err)
Z
zhangmin 已提交
73 74 75 76 77 78 79
		return
	}

	response.WriteEntity(resultV1alpha2)

}

H
hongming 已提交
80
// handleListResources retrieves resources
H
hongming 已提交
81
func (h *Handler) handleListResources(request *restful.Request, response *restful.Response) {
Z
zryfish 已提交
82
	query := query.ParseQueryParameter(request)
H
hongming 已提交
83
	resourceType := request.PathParameter("resources")
Z
zryfish 已提交
84 85
	namespace := request.PathParameter("namespace")

H
hongming 已提交
86 87 88 89 90 91
	result, err := h.resourceGetterV1alpha3.List(resourceType, namespace, query)
	if err == nil {
		response.WriteEntity(result)
		return
	}

H
hongming 已提交
92
	if err != resourcev1alpha3.ErrResourceNotSupported {
H
hongming 已提交
93
		klog.Error(err, resourceType)
H
hongming 已提交
94
		api.HandleInternalError(response, request, err)
H
hongming 已提交
95 96 97 98 99
		return
	}

	// fallback to v1alpha2
	result, err = h.fallback(resourceType, namespace, query)
Z
zryfish 已提交
100
	if err != nil {
H
hongming 已提交
101 102 103 104
		if err == resourcev1alpha2.ErrResourceNotSupported {
			api.HandleNotFound(response, request, err)
			return
		}
H
hongming 已提交
105
		klog.Error(err)
H
hongming 已提交
106
		api.HandleError(response, request, err)
Z
zryfish 已提交
107 108
		return
	}
H
hongming 已提交
109
	response.WriteEntity(result)
Z
zryfish 已提交
110 111
}

H
hongming 已提交
112 113 114 115
func (h *Handler) fallback(resourceType string, namespace string, q *query.Query) (*api.ListResult, error) {
	orderBy := string(q.SortBy)
	limit, offset := q.Pagination.Limit, q.Pagination.Offset
	reverse := !q.Ascending
H
hongming 已提交
116 117 118
	conditions := &params.Conditions{Match: make(map[string]string, 0), Fuzzy: make(map[string]string, 0)}
	for field, value := range q.Filters {
		switch field {
H
hongming 已提交
119
		case query.FieldName:
H
hongming 已提交
120
			conditions.Fuzzy[v1alpha2.Name] = string(value)
H
hongming 已提交
121
			break
H
hongming 已提交
122 123 124
		case query.FieldNames:
			conditions.Match[v1alpha2.Name] = string(value)
			break
H
hongming 已提交
125
		case query.FieldCreationTimeStamp:
H
hongming 已提交
126
			conditions.Match[v1alpha2.CreateTime] = string(value)
H
hongming 已提交
127 128
			break
		case query.FieldLastUpdateTimestamp:
H
hongming 已提交
129
			conditions.Match[v1alpha2.UpdateTime] = string(value)
H
hongming 已提交
130 131
			break
		case query.FieldLabel:
H
hongming 已提交
132
			values := strings.SplitN(string(value), ":", 2)
H
hongming 已提交
133 134 135 136 137 138 139
			if len(values) == 2 {
				conditions.Match[values[0]] = values[1]
			} else {
				conditions.Match[v1alpha2.Label] = values[0]
			}
			break
		case query.FieldAnnotation:
H
hongming 已提交
140
			values := strings.SplitN(string(value), ":", 2)
H
hongming 已提交
141 142 143 144 145 146 147
			if len(values) == 2 {
				conditions.Match[v1alpha2.Annotation] = values[1]
			} else {
				conditions.Match[v1alpha2.Annotation] = values[0]
			}
			break
		case query.FieldStatus:
H
hongming 已提交
148
			conditions.Match[v1alpha2.Status] = string(value)
H
hongming 已提交
149 150
			break
		case query.FieldOwnerReference:
H
hongming 已提交
151 152 153 154
			conditions.Match[v1alpha2.Owner] = string(value)
			break
		default:
			conditions.Match[string(field)] = string(value)
H
hongming 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
			break
		}
	}

	result, err := h.resourcesGetterV1alpha2.ListResources(namespace, resourceType, conditions, orderBy, reverse, limit, offset)
	if err != nil {
		klog.Error(err)
		return nil, err
	}

	return &api.ListResult{
		Items:      result.Items,
		TotalItems: result.TotalCount,
	}, nil
}

func (h *Handler) handleGetComponentStatus(request *restful.Request, response *restful.Response) {
Z
zryfish 已提交
172 173 174
	component := request.PathParameter("component")
	result, err := h.componentsGetter.GetComponentStatus(component)
	if err != nil {
H
hongming 已提交
175
		klog.Error(err)
Z
zryfish 已提交
176
		api.HandleInternalError(response, nil, err)
Z
zryfish 已提交
177 178
		return
	}
H
hongming 已提交
179
	response.WriteEntity(result)
Z
zryfish 已提交
180 181
}

H
hongming 已提交
182
func (h *Handler) handleGetSystemHealthStatus(request *restful.Request, response *restful.Response) {
Z
zryfish 已提交
183 184
	result, err := h.componentsGetter.GetSystemHealthStatus()
	if err != nil {
H
hongming 已提交
185
		klog.Error(err)
Z
zryfish 已提交
186
		api.HandleInternalError(response, nil, err)
Z
zryfish 已提交
187 188 189
		return
	}

H
hongming 已提交
190
	response.WriteEntity(result)
Z
zryfish 已提交
191 192 193
}

// get all componentsHandler
H
hongming 已提交
194
func (h *Handler) handleGetComponents(request *restful.Request, response *restful.Response) {
Z
zryfish 已提交
195 196
	result, err := h.componentsGetter.GetAllComponentsStatus()
	if err != nil {
H
hongming 已提交
197
		klog.Error(err)
Z
zryfish 已提交
198
		api.HandleInternalError(response, nil, err)
Z
zryfish 已提交
199 200 201
		return
	}

H
hongming 已提交
202
	response.WriteEntity(result)
Z
zryfish 已提交
203
}