15
15
package main
16
16
17
17
import (
18
- "bytes"
19
18
"log"
20
19
21
20
client "k8s.io/kubernetes/pkg/client/unversioned"
22
21
)
23
22
24
23
// Clients for making requests to a Heapster instance.
25
24
type HeapsterClient interface {
26
- // Creates a new GET http request to heapster.
27
- Get () * client.Request
25
+ // Creates a new GET HTTP request to heapster, specified by the path param, to the V1 API
26
+ // endpoint. The path param is without the API prefix, e.g.,
27
+ // /model/namespaces/default/pod-list/foo/metrics/memory-usage
28
+
29
+ Get (path string ) * client.Request
30
+ }
31
+
32
+ // In-cluster implementation of heapster client. Talks with heapster through service proxy.
33
+ type InClusterHeapsterClient struct {
34
+ client * client.Client
35
+ }
36
+
37
+ func (c InClusterHeapsterClient ) Get (path string ) * client.Request {
38
+ return c .client .Get ().Prefix ("proxy" ).
39
+ Namespace ("kube-system" ).
40
+ Resource ("services" ).
41
+ Name ("heapster" ).
42
+ Suffix ("/api/v1" + path )
43
+ }
44
+
45
+ // Remote heapster client based implementation. Talks with heapster through raw RESTClient.
46
+ type RemoteHeapsterClient struct {
47
+ client * client.RESTClient
48
+ }
49
+
50
+ func (c RemoteHeapsterClient ) Get (path string ) * client.Request {
51
+ return c .client .Get ().Suffix (path )
28
52
}
29
53
30
54
// Creates new Heapster REST client. When heapsterHost param is empty string the function
@@ -33,21 +57,16 @@ type HeapsterClient interface {
33
57
func CreateHeapsterRESTClient (heapsterHost string , apiclient * client.Client ) (
34
58
HeapsterClient , error ) {
35
59
36
- cfg := client.Config {}
37
-
38
60
if heapsterHost == "" {
39
- bufferProxyHost := bytes .NewBufferString ("http://" )
40
- bufferProxyHost .WriteString (apiclient .RESTClient .Get ().URL ().Host )
41
- cfg .Host = bufferProxyHost .String ()
42
- cfg .Prefix = "/api/v1/proxy/namespaces/kube-system/services/heapster/api"
61
+ log .Printf ("Creating in-cluster Heapster client" )
62
+ return InClusterHeapsterClient {client : apiclient }, nil
43
63
} else {
44
- cfg . Host = heapsterHost
45
- }
46
- log . Printf ( "Creating Heapster REST client for %s%s" , cfg . Host , cfg . Prefix )
47
- clientFactory := new ( ClientFactoryImpl )
48
- heapsterClient , err := clientFactory . New ( & cfg )
49
- if err != nil {
50
- return nil , err
64
+ cfg := & client. Config { Host : heapsterHost }
65
+ restClient , err := client . New ( cfg )
66
+ if err != nil {
67
+ return nil , err
68
+ }
69
+ log . Printf ( "Creating remote Heapster client for %s" , heapsterHost )
70
+ return RemoteHeapsterClient { client : restClient . RESTClient }, nil
51
71
}
52
- return heapsterClient .RESTClient , nil
53
72
}
0 commit comments