-
Notifications
You must be signed in to change notification settings - Fork 341
/
nodes.go
100 lines (83 loc) · 3.14 KB
/
nodes.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
/*
Copyright 2018 Heptio Inc.
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 discovery
import (
"encoding/json"
"os"
"path"
"github.com/heptio/sonobuoy/pkg/config"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type nodeData struct {
APIResource v1.Node `json:"apiResource,omitempty"`
ConfigzOutput map[string]interface{} `json:"configzOutput,omitempty"`
HealthzStatus int `json:"healthzStatus,omitempty"`
}
// gatherNodeData collects non-resource information about a node through the
// kubernetes API. That is, its `healthz` and `configz` endpoints, which are
// not "resources" per se, although they are accessible through the apiserver.
func gatherNodeData(kubeClient kubernetes.Interface, cfg *config.Config) error {
logrus.Info("Collecting Node Configuration and Health...")
nodelist, err := kubeClient.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return err
}
for _, node := range nodelist.Items {
// We hit the master on /api/v1/proxy/nodes/<node> to gather node
// information without having to reinvent auth
proxypath := "/api/v1/proxy/nodes/" + node.Name
restclient := kubeClient.CoreV1().RESTClient()
out := path.Join(cfg.OutputDir(), HostsLocation, node.Name)
logrus.Infof("Creating host results for %v under %v\n", node.Name, out)
if err = os.MkdirAll(out, 0755); err != nil {
return err
}
_, err = untypedQuery(out, "configz.json", func() (interface{}, error) {
var configz map[string]interface{}
// Get the configz endpoint, put the result in the nodeData
request := restclient.Get().RequestURI(proxypath + "/configz")
if result, err := request.Do().Raw(); err == nil {
json.Unmarshal(result, &configz)
} else {
logrus.Warningf("Could not get configz endpoint for node %v: %v", node.Name, err)
}
return configz, err
})
if err != nil {
return err
}
_, err = untypedQuery(out, "healthz.json", func() (interface{}, error) {
// Since health is just an int, we wrap it in a JSON object that looks like
// `{"status":200}`
health := make(map[string]interface{})
var healthstatus int
// Get the healthz endpoint too. We care about the response code in this
// case, not the body.
request := restclient.Get().RequestURI(proxypath + "/healthz")
if result := request.Do(); result.Error() == nil {
result.StatusCode(&healthstatus)
health["status"] = healthstatus
} else {
logrus.Warningf("Could not get healthz endpoint for node %v: %v", node.Name, result.Error())
}
return health, err
})
if err != nil {
return err
}
}
return err
}