This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
/
pod_detail_collector.go
119 lines (103 loc) · 3.39 KB
/
pod_detail_collector.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright 2021 The KubeDiag 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 kubernetes
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kubediag/kubediag/pkg/executor"
"github.com/kubediag/kubediag/pkg/processors"
"github.com/kubediag/kubediag/pkg/processors/utils"
)
const (
ContextKeyPodDetail = "collector.kubernetes.pod.detail"
)
// podDetailCollector manages detail of a pod.
type podDetailCollector struct {
// Context carries values across API boundaries.
context.Context
// Logger represents the ability to log messages.
logr.Logger
// cache knows how to load Kubernetes objects.
cache cache.Cache
// podCollectorEnabled indicates whether podListCollector and podDetailCollector is enabled.
podCollectorEnabled bool
}
// NewPodDetailCollector creates a new podDetailCollector.
func NewPodDetailCollector(
ctx context.Context,
logger logr.Logger,
cache cache.Cache,
nodeName string,
podCollectorEnabled bool,
) processors.Processor {
return &podDetailCollector{
Context: ctx,
Logger: logger,
cache: cache,
podCollectorEnabled: podCollectorEnabled,
}
}
// Handler handles http requests for pod information.
func (pc *podDetailCollector) Handler(w http.ResponseWriter, r *http.Request) {
if !pc.podCollectorEnabled {
http.Error(w, fmt.Sprintf("pod collector is not enabled"), http.StatusUnprocessableEntity)
return
}
switch r.Method {
case "POST":
contexts, err := utils.ExtractParametersFromHTTPContext(r)
if err != nil {
pc.Error(err, "extract contexts failed")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if contexts[executor.PodNamespaceTelemetryKey] == "" ||
contexts[executor.PodNameTelemetryKey] == "" {
pc.Error(err, "extract contexts lack of pod namespace and name")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pod := corev1.Pod{}
err = pc.cache.Get(pc.Context,
client.ObjectKey{
Namespace: contexts[executor.PodNamespaceTelemetryKey],
Name: contexts[executor.PodNameTelemetryKey],
}, &pod)
if err != nil {
http.Error(w, fmt.Sprintf("failed to get pod: %v", err), http.StatusInternalServerError)
return
}
raw, err := json.Marshal(pod)
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal pod: %v", err), http.StatusInternalServerError)
return
}
result := make(map[string]string)
result[ContextKeyPodDetail] = string(raw)
data, err := json.Marshal(result)
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal result: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(data)
default:
http.Error(w, fmt.Sprintf("method %s is not supported", r.Method), http.StatusMethodNotAllowed)
}
}