Merge pull request #252 from satnam6502/master
A few minor Go style suggestions.
This commit is contained in:
commit
797a4986ff
@ -12,8 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Handler for /api/
|
||||
|
||||
// Package api provides a handler for /api/
|
||||
package api
|
||||
|
||||
import (
|
||||
@ -172,7 +171,8 @@ func getContainerInfoRequest(body io.ReadCloser) (*info.ContainerInfoRequest, er
|
||||
query.NumStats = 64
|
||||
|
||||
decoder := json.NewDecoder(body)
|
||||
if err := decoder.Decode(&query); err != nil && err != io.EOF {
|
||||
err := decoder.Decode(&query)
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, fmt.Errorf("unable to decode the json value: %s", err)
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// TODO(cAdvisor): Package comment.
|
||||
package cadvisor
|
||||
|
||||
import (
|
||||
|
@ -12,27 +12,33 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package container defines types for sub-container events and also
|
||||
// defines an interface for container operation handlers.
|
||||
package container
|
||||
|
||||
import "github.com/google/cadvisor/info"
|
||||
|
||||
// Listing types.
|
||||
const (
|
||||
LIST_SELF = iota
|
||||
LIST_RECURSIVE
|
||||
)
|
||||
|
||||
// ListType describes whether listing should be just for a
|
||||
// specific container or performed recursively.
|
||||
type ListType int
|
||||
|
||||
// SubcontainerEvent types.
|
||||
const (
|
||||
SUBCONTAINER_ADD = iota
|
||||
SUBCONTAINER_DELETE
|
||||
ListSelf ListType = iota
|
||||
ListRecursive
|
||||
)
|
||||
|
||||
// SubcontainerEventType indicates an addition or deletion event.
|
||||
type SubcontainerEventType int
|
||||
|
||||
const (
|
||||
SubcontainerAdd SubcontainerEventType = iota
|
||||
SubcontainerDelete
|
||||
)
|
||||
|
||||
// SubcontainerEvent represents a
|
||||
type SubcontainerEvent struct {
|
||||
// The type of event that occurred.
|
||||
EventType int
|
||||
EventType SubcontainerEventType
|
||||
|
||||
// The full container name of the container where the event occurred.
|
||||
Name string
|
||||
|
@ -12,6 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// TODO(cAdvisor): Package comment.
|
||||
package raw
|
||||
|
||||
import (
|
||||
@ -179,7 +180,7 @@ func listDirectories(dirpath string, parent string, recursive bool, output map[s
|
||||
func (self *rawContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) {
|
||||
containers := make(map[string]struct{})
|
||||
for _, subsystem := range self.cgroupSubsystems.mounts {
|
||||
err := listDirectories(path.Join(subsystem.Mountpoint, self.name), self.name, listType == container.LIST_RECURSIVE, containers)
|
||||
err := listDirectories(path.Join(subsystem.Mountpoint, self.name), self.name, listType == container.ListRecursive, containers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -230,16 +231,16 @@ func (self *rawContainerHandler) watchDirectory(dir string, containerName string
|
||||
|
||||
func (self *rawContainerHandler) processEvent(event *inotify.Event, events chan container.SubcontainerEvent) error {
|
||||
// Convert the inotify event type to a container create or delete.
|
||||
var eventType int
|
||||
var eventType container.SubcontainerEventType
|
||||
switch {
|
||||
case (event.Mask & inotify.IN_CREATE) > 0:
|
||||
eventType = container.SUBCONTAINER_ADD
|
||||
eventType = container.SubcontainerAdd
|
||||
case (event.Mask & inotify.IN_DELETE) > 0:
|
||||
eventType = container.SUBCONTAINER_DELETE
|
||||
eventType = container.SubcontainerDelete
|
||||
case (event.Mask & inotify.IN_MOVED_FROM) > 0:
|
||||
eventType = container.SUBCONTAINER_DELETE
|
||||
eventType = container.SubcontainerDelete
|
||||
case (event.Mask & inotify.IN_MOVED_TO) > 0:
|
||||
eventType = container.SUBCONTAINER_ADD
|
||||
eventType = container.SubcontainerAdd
|
||||
default:
|
||||
// Ignore other events.
|
||||
return nil
|
||||
@ -260,7 +261,7 @@ func (self *rawContainerHandler) processEvent(event *inotify.Event, events chan
|
||||
|
||||
// Maintain the watch for the new or deleted container.
|
||||
switch {
|
||||
case eventType == container.SUBCONTAINER_ADD:
|
||||
case eventType == container.SubcontainerAdd:
|
||||
// If we've already seen this event, return.
|
||||
if _, ok := self.watches[containerName]; ok {
|
||||
return nil
|
||||
@ -271,7 +272,7 @@ func (self *rawContainerHandler) processEvent(event *inotify.Event, events chan
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case eventType == container.SUBCONTAINER_DELETE:
|
||||
case eventType == container.SubcontainerDelete:
|
||||
// If we've already seen this event, return.
|
||||
if _, ok := self.watches[containerName]; !ok {
|
||||
return nil
|
||||
|
@ -12,8 +12,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Per-container manager.
|
||||
|
||||
package manager
|
||||
|
||||
import (
|
||||
@ -80,8 +78,7 @@ func (c *containerData) GetInfo() (*containerInfo, error) {
|
||||
// Make a copy of the info for the user.
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
ret := c.info
|
||||
return &ret, nil
|
||||
return &c.info, nil
|
||||
}
|
||||
|
||||
func newContainerData(containerName string, driver storage.StorageDriver, handler container.ContainerHandler) (*containerData, error) {
|
||||
@ -207,7 +204,7 @@ func (c *containerData) updateStats() error {
|
||||
}
|
||||
|
||||
func (c *containerData) updateSubcontainers() error {
|
||||
subcontainers, err := c.handler.ListContainers(container.LIST_SELF)
|
||||
subcontainers, err := c.handler.ListContainers(container.ListSelf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ func TestUpdateSubcontainers(t *testing.T) {
|
||||
{Name: "/container/something"},
|
||||
}
|
||||
cd, mockHandler, _ := newTestContainerData(t)
|
||||
mockHandler.On("ListContainers", container.LIST_SELF).Return(
|
||||
mockHandler.On("ListContainers", container.ListSelf).Return(
|
||||
subcontainers,
|
||||
nil,
|
||||
)
|
||||
@ -79,7 +79,7 @@ func TestUpdateSubcontainers(t *testing.T) {
|
||||
|
||||
func TestUpdateSubcontainersWithError(t *testing.T) {
|
||||
cd, mockHandler, _ := newTestContainerData(t)
|
||||
mockHandler.On("ListContainers", container.LIST_SELF).Return(
|
||||
mockHandler.On("ListContainers", container.ListSelf).Return(
|
||||
[]info.ContainerReference{},
|
||||
fmt.Errorf("some error"),
|
||||
)
|
||||
@ -143,7 +143,7 @@ func TestGetInfo(t *testing.T) {
|
||||
spec,
|
||||
nil,
|
||||
)
|
||||
mockHandler.On("ListContainers", container.LIST_SELF).Return(
|
||||
mockHandler.On("ListContainers", container.ListSelf).Return(
|
||||
subcontainers,
|
||||
nil,
|
||||
)
|
||||
|
@ -12,6 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// TODO(cAdvisor): Package comment.
|
||||
package manager
|
||||
|
||||
import (
|
||||
@ -30,6 +31,8 @@ import (
|
||||
|
||||
var globalHousekeepingInterval = flag.Duration("global_housekeeping_interval", 1*time.Minute, "Interval between global housekeepings")
|
||||
|
||||
// The Manager interface defines operations for starting a manager and getting
|
||||
// container and machine information.
|
||||
type Manager interface {
|
||||
// Start the manager.
|
||||
Start() error
|
||||
@ -50,6 +53,7 @@ type Manager interface {
|
||||
GetVersionInfo() (*info.VersionInfo, error)
|
||||
}
|
||||
|
||||
// New takes a driver and returns a new manager.
|
||||
func New(driver storage.StorageDriver) (Manager, error) {
|
||||
if driver == nil {
|
||||
return nil, fmt.Errorf("nil storage driver!")
|
||||
@ -253,13 +257,11 @@ func (self *manager) SubcontainersInfo(containerName string, query *info.Contain
|
||||
|
||||
func (m *manager) GetMachineInfo() (*info.MachineInfo, error) {
|
||||
// Copy and return the MachineInfo.
|
||||
ret := m.machineInfo
|
||||
return &ret, nil
|
||||
return &m.machineInfo, nil
|
||||
}
|
||||
|
||||
func (m *manager) GetVersionInfo() (*info.VersionInfo, error) {
|
||||
ret := m.versionInfo
|
||||
return &ret, nil
|
||||
return &m.versionInfo, nil
|
||||
}
|
||||
|
||||
// Create a container.
|
||||
@ -278,7 +280,7 @@ func (m *manager) createContainer(containerName string) error {
|
||||
m.containersLock.Lock()
|
||||
defer m.containersLock.Unlock()
|
||||
|
||||
// Check that the container didn't already exist
|
||||
// Check that the container didn't already exist\
|
||||
_, ok := m.containers[containerName]
|
||||
if ok {
|
||||
return true
|
||||
@ -335,9 +337,9 @@ func (m *manager) getContainersDiff(containerName string) (added []info.Containe
|
||||
// Get all subcontainers recursively.
|
||||
cont, ok := m.containers[containerName]
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("Failed to find container %q while checking for new containers", containerName)
|
||||
return nil, nil, fmt.Errorf("failed to find container %q while checking for new containers", containerName)
|
||||
}
|
||||
allContainers, err := cont.handler.ListContainers(container.LIST_RECURSIVE)
|
||||
allContainers, err := cont.handler.ListContainers(container.ListRecursive)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -396,8 +398,8 @@ func (m *manager) detectSubcontainers(containerName string) error {
|
||||
}
|
||||
|
||||
func (self *manager) processEvent(event container.SubcontainerEvent) error {
|
||||
var err error = nil
|
||||
return err
|
||||
// TODO(cAdvisor): Why does this method always return nil? [satnam6502]
|
||||
return nil
|
||||
}
|
||||
|
||||
// Watches for new containers started in the system. Runs forever unless there is a setup error.
|
||||
@ -410,7 +412,7 @@ func (self *manager) watchForNewContainers(quit chan error) error {
|
||||
root, ok = self.containers["/"]
|
||||
}()
|
||||
if !ok {
|
||||
return fmt.Errorf("root container does not exist when watching for new containers")
|
||||
return fmt.Errorf("Root container does not exist when watching for new containers")
|
||||
}
|
||||
|
||||
// Register for new subcontainers.
|
||||
@ -432,9 +434,9 @@ func (self *manager) watchForNewContainers(quit chan error) error {
|
||||
select {
|
||||
case event := <-events:
|
||||
switch {
|
||||
case event.EventType == container.SUBCONTAINER_ADD:
|
||||
case event.EventType == container.SubcontainerAdd:
|
||||
err = self.createContainer(event.Name)
|
||||
case event.EventType == container.SUBCONTAINER_DELETE:
|
||||
case event.EventType == container.SubcontainerDelete:
|
||||
err = self.destroyContainer(event.Name)
|
||||
}
|
||||
if err != nil {
|
||||
|
@ -83,7 +83,7 @@ func expectManagerWithContainers(containers []string, query *info.ContainerInfoR
|
||||
nil,
|
||||
)
|
||||
|
||||
h.On("ListContainers", container.LIST_SELF).Return(
|
||||
h.On("ListContainers", container.ListSelf).Return(
|
||||
[]info.ContainerReference(nil),
|
||||
nil,
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user