/
get.go
72 lines (56 loc) · 1.8 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020-2023 Project Capsule Authors.
// SPDX-License-Identifier: Apache-2.0
package metric
import (
"net/http"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/projectcapsule/capsule-proxy/internal/modules"
"github.com/projectcapsule/capsule-proxy/internal/modules/errors"
"github.com/projectcapsule/capsule-proxy/internal/modules/utils"
"github.com/projectcapsule/capsule-proxy/internal/request"
"github.com/projectcapsule/capsule-proxy/internal/tenant"
)
type get struct {
client client.Reader
log logr.Logger
gk schema.GroupKind
}
func Get(client client.Reader) modules.Module {
return &get{
client: client,
log: ctrl.Log.WithName("metric_get"),
gk: schema.GroupKind{
Group: corev1.GroupName,
Kind: "nodes",
},
}
}
func (g get) Path() string {
return "/apis/metrics.k8s.io/{version}/nodes/{name}"
}
func (g get) Methods() []string {
return []string{}
}
func (g get) Handle(proxyTenants []*tenant.ProxyTenant, proxyRequest request.Request) (selector labels.Selector, err error) {
httpRequest := proxyRequest.GetHTTPRequest()
selectors := utils.GetNodeSelectors(httpRequest, proxyTenants)
name := mux.Vars(httpRequest)["name"]
nl := &corev1.NodeList{}
if err = g.client.List(httpRequest.Context(), nl, client.MatchingLabels{"kubernetes.io/hostname": name}); err != nil {
return nil, errors.NewBadRequest(err, g.gk)
}
var r *labels.Requirement
if r, err = utils.GetNodeSelector(nl, selectors); err == nil {
return labels.NewSelector().Add(*r), nil
}
if httpRequest.Method == http.MethodGet {
return nil, errors.NewNotFoundError(name, g.gk)
}
return nil, nil
}