forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cAdvisor is started as a Kubelet dependency during startup of the Kubelet before the sync loops start.
- Loading branch information
Showing
7 changed files
with
204 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// +build cgo,linux | ||
|
||
/* | ||
Copyright 2015 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. | ||
*/ | ||
|
||
package cadvisor | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" | ||
"github.com/golang/glog" | ||
cadvisorHttp "github.com/google/cadvisor/http" | ||
cadvisorApi "github.com/google/cadvisor/info/v1" | ||
"github.com/google/cadvisor/manager" | ||
"github.com/google/cadvisor/storage/memory" | ||
"github.com/google/cadvisor/utils/sysfs" | ||
) | ||
|
||
type cadvisorClient struct { | ||
manager.Manager | ||
} | ||
|
||
var _ Interface = new(cadvisorClient) | ||
|
||
// TODO(vmarmol): Make configurable. | ||
// The number of stats to keep in memory. | ||
const statsToCache = 60 | ||
|
||
// Creates a cAdvisor and exports its API on the specified port if port > 0. | ||
func New(port uint) (Interface, error) { | ||
sysFs, err := sysfs.NewRealSysFs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create and start the cAdvisor container manager. | ||
m, err := manager.New(memory.New(statsToCache, nil), sysFs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = m.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cadvisorClient := &cadvisorClient{ | ||
Manager: m, | ||
} | ||
|
||
// Export the HTTP endpoint if a port was specified. | ||
if port > 0 { | ||
err = cadvisorClient.exportHTTP(port) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return cadvisorClient, nil | ||
} | ||
|
||
func (self *cadvisorClient) exportHTTP(port uint) error { | ||
mux := http.NewServeMux() | ||
err := cadvisorHttp.RegisterHandlers(mux, self, "", "", "", "", "/metrics") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
serv := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
Handler: mux, | ||
} | ||
|
||
// TODO(vmarmol): Remove this when the cAdvisor port is once again free. | ||
// If export failed, retry in the background until we are able to bind. | ||
// This allows an existing cAdvisor to be killed before this one registers. | ||
go func() { | ||
defer util.HandleCrash() | ||
|
||
err := serv.ListenAndServe() | ||
for err != nil { | ||
glog.Infof("Failed to register cAdvisor on port %d, retrying. Error: %v", port, err) | ||
time.Sleep(time.Minute) | ||
err = serv.ListenAndServe() | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (self *cadvisorClient) ContainerInfo(name string, req *cadvisorApi.ContainerInfoRequest) (*cadvisorApi.ContainerInfo, error) { | ||
return self.GetContainerInfo(name, req) | ||
} | ||
|
||
func (self *cadvisorClient) MachineInfo() (*cadvisorApi.MachineInfo, error) { | ||
return self.GetMachineInfo() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// +build !cgo !linux | ||
|
||
/* | ||
Copyright 2015 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. | ||
*/ | ||
|
||
package cadvisor | ||
|
||
import ( | ||
"errors" | ||
|
||
cadvisorApi "github.com/google/cadvisor/info/v1" | ||
) | ||
|
||
type cadvisorUnsupported struct { | ||
} | ||
|
||
var _ Interface = new(cadvisorUnsupported) | ||
|
||
func New(port uint) (Interface, error) { | ||
return &cadvisorUnsupported{}, nil | ||
} | ||
|
||
var unsupportedErr = errors.New("cAdvisor is unsupported in this build") | ||
|
||
func (self *cadvisorUnsupported) DockerContainer(name string, req *cadvisorApi.ContainerInfoRequest) (cadvisorApi.ContainerInfo, error) { | ||
return cadvisorApi.ContainerInfo{}, unsupportedErr | ||
} | ||
|
||
func (self *cadvisorUnsupported) ContainerInfo(name string, req *cadvisorApi.ContainerInfoRequest) (*cadvisorApi.ContainerInfo, error) { | ||
return nil, unsupportedErr | ||
} | ||
|
||
func (self *cadvisorUnsupported) MachineInfo() (*cadvisorApi.MachineInfo, error) { | ||
return nil, unsupportedErr | ||
} |