forked from kubernetes-retired/heapster
/
cadvisor.go
189 lines (167 loc) · 4.72 KB
/
cadvisor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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.
// This file is dedicated to heapster running outside of kubernetes. Heapster
// will poll a file to get the hosts that it needs to monitor and will collect
// stats from cadvisor running on those hosts.
package sources
import (
"fmt"
"net/url"
"strconv"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/heapster/extpoints"
"k8s.io/heapster/sinks/cache"
"k8s.io/heapster/sources/api"
"k8s.io/heapster/sources/datasource"
"k8s.io/heapster/sources/nodes"
)
const (
defaultCadvisorPort = 8080
defaultStandalone = false
defaultHostsFile = "/var/run/heapster/hosts"
)
var defaultFleetEndpoints = []string{"http://127.0.0.1:4001"}
type cadvisorSource struct {
cadvisorPort int
cadvisorApi datasource.Cadvisor
nodesApi nodes.NodesApi
}
func init() {
extpoints.SourceFactories.Register(NewCadvisorSources, "cadvisor")
}
func (self *cadvisorSource) GetInfo(start, end time.Time, resolution time.Duration, align bool) (api.AggregateData, error) {
var (
lock sync.Mutex
wg sync.WaitGroup
)
nodeList, err := self.nodesApi.List()
if err != nil {
return api.AggregateData{}, err
}
result := api.AggregateData{}
for hostname, info := range nodeList.Items {
wg.Add(1)
go func(hostname string, info nodes.Info) {
defer wg.Done()
host := datasource.Host{
IP: info.InternalIP,
Port: self.cadvisorPort,
}
rawSubcontainers, node, err := self.cadvisorApi.GetAllContainers(host, start, end, resolution, align)
if err != nil {
glog.Error(err)
return
}
subcontainers := []api.Container{}
for _, cont := range rawSubcontainers {
if cont != nil {
cont.Hostname = hostname
subcontainers = append(subcontainers, *cont)
}
}
lock.Lock()
defer lock.Unlock()
result.Containers = append(result.Containers, subcontainers...)
if node != nil {
node.Hostname = hostname
result.Machine = append(result.Machine, *node)
}
}(string(hostname), info)
}
wg.Wait()
return result, nil
}
func (self *cadvisorSource) DebugInfo() string {
desc := "Source type: Cadvisor\n"
// TODO(rjnagal): Cache config?
nodeList, err := self.nodesApi.List()
if err != nil {
desc += fmt.Sprintf("\tFailed to read host config: %s", err)
}
desc += fmt.Sprintf("\tNodeList: %+v\n", *nodeList)
desc += fmt.Sprintf("\t%s\n", self.nodesApi.DebugInfo())
desc += "\n"
return desc
}
func (cs *cadvisorSource) Name() string {
return "Cadvisor Source"
}
func NewCadvisorSources(uri *url.URL, _ cache.Cache) ([]api.Source, error) {
switch uri.Path {
case "coreos", "fleet":
return newCoreosSources(uri.Query())
case "external":
return newExternalSources(uri.Query())
default:
return nil, fmt.Errorf("Unknown cadvisor source: %s", uri.Path)
}
}
func newExternalSources(options map[string][]string) ([]api.Source, error) {
hostsFile := defaultHostsFile
if len(options["hostsFile"]) > 0 {
hostsFile = options["hostsFile"][0]
}
standalone := defaultStandalone
if len(options["standalone"]) > 0 {
standaloneOption, err := strconv.ParseBool(options["standalone"][0])
if err != nil {
return nil, err
}
standalone = standaloneOption
}
nodesApi, err := nodes.NewExternalNodes(standalone, hostsFile)
if err != nil {
return nil, err
}
cadvisorPort := defaultCadvisorPort
if len(options["cadvisorPort"]) > 0 {
cadvisorPort, err = strconv.Atoi(options["cadvisorPort"][0])
if err != nil {
return nil, err
}
}
return []api.Source{
&cadvisorSource{
cadvisorApi: datasource.NewCadvisor(),
nodesApi: nodesApi,
cadvisorPort: cadvisorPort,
},
}, nil
}
func newCoreosSources(options map[string][]string) ([]api.Source, error) {
fleetEndpoints := defaultFleetEndpoints
if len(options["fleetEndpoint"]) > 0 {
fleetEndpoints = options["fleetEndpoint"]
}
nodesApi, err := nodes.NewCoreOSNodes(fleetEndpoints)
if err != nil {
return nil, err
}
cadvisorPort := defaultCadvisorPort
if len(options["cadvisorPort"]) > 0 {
cadvisorPort, err = strconv.Atoi(options["cadvisorPort"][0])
if err != nil {
return nil, err
}
}
return []api.Source{
&cadvisorSource{
cadvisorApi: datasource.NewCadvisor(),
nodesApi: nodesApi,
cadvisorPort: cadvisorPort,
},
}, nil
}