From 5fd0415985f9f55c0cec2632b25cdf25b4670526 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 20 Oct 2017 11:46:19 -0400 Subject: [PATCH] Add comments and fix common lint issues Signed-off-by: Michael Crosby --- client.go | 1 + cmd/containerd/main.go | 4 ++-- linux/bundle.go | 17 +++++++++-------- metadata/content.go | 8 ++------ plugin/context.go | 6 ++++++ runtime/task.go | 14 ++++++++++++++ runtime/task_list.go | 11 ++++++++++- server/config.go | 8 ++++---- services/healthcheck/service.go | 5 ++--- snapshot/btrfs/btrfs.go | 4 ++-- snapshot/btrfs/btrfs_test.go | 10 ++++------ snapshot/overlay/overlay.go | 2 +- snapshot/storage/bolt.go | 2 +- task.go | 1 + testutil/loopback_linux.go | 5 +---- 15 files changed, 60 insertions(+), 38 deletions(-) diff --git a/client.go b/client.go index 6f0eb28a2..2e253a08d 100644 --- a/client.go +++ b/client.go @@ -449,6 +449,7 @@ func (c *Client) DiffService() diff.Differ { return diffservice.NewDiffServiceFromClient(diffapi.NewDiffClient(c.conn)) } +// IntrospectionService returns the underlying Introspection Client func (c *Client) IntrospectionService() introspectionapi.IntrospectionClient { return introspectionapi.NewIntrospectionClient(c.conn) } diff --git a/cmd/containerd/main.go b/cmd/containerd/main.go index eed4356b2..a6ca3d078 100644 --- a/cmd/containerd/main.go +++ b/cmd/containerd/main.go @@ -110,7 +110,7 @@ func main() { } serverC <- server if config.Debug.Address != "" { - l, err := sys.GetLocalListener(config.Debug.Address, config.Debug.Uid, config.Debug.Gid) + l, err := sys.GetLocalListener(config.Debug.Address, config.Debug.UID, config.Debug.GID) if err != nil { return errors.Wrapf(err, "failed to get listener for debug endpoint") } @@ -124,7 +124,7 @@ func main() { serve(log.WithModule(ctx, "metrics"), l, server.ServeMetrics) } - l, err := sys.GetLocalListener(address, config.GRPC.Uid, config.GRPC.Gid) + l, err := sys.GetLocalListener(address, config.GRPC.UID, config.GRPC.GID) if err != nil { return errors.Wrapf(err, "failed to get listener for main endpoint") } diff --git a/linux/bundle.go b/linux/bundle.go index 3dbfef98d..29ab6ba5f 100644 --- a/linux/bundle.go +++ b/linux/bundle.go @@ -70,32 +70,33 @@ type bundle struct { workDir string } -type shimOpt func(*bundle, string, *runcopts.RuncOptions) (client.Config, client.ClientOpt) +// ShimOpt specifies shim options for initialization and connection +type ShimOpt func(*bundle, string, *runcopts.RuncOptions) (client.Config, client.ClientOpt) -// ShimRemote is a shimOpt for connecting and starting a remote shim -func ShimRemote(shim, daemonAddress, cgroup string, nonewns, debug bool, exitHandler func()) shimOpt { +// ShimRemote is a ShimOpt for connecting and starting a remote shim +func ShimRemote(shim, daemonAddress, cgroup string, nonewns, debug bool, exitHandler func()) ShimOpt { return func(b *bundle, ns string, ropts *runcopts.RuncOptions) (client.Config, client.ClientOpt) { return b.shimConfig(ns, ropts), client.WithStart(shim, b.shimAddress(ns), daemonAddress, cgroup, nonewns, debug, exitHandler) } } -// ShimLocal is a shimOpt for using an in process shim implementation -func ShimLocal(exchange *events.Exchange) shimOpt { +// ShimLocal is a ShimOpt for using an in process shim implementation +func ShimLocal(exchange *events.Exchange) ShimOpt { return func(b *bundle, ns string, ropts *runcopts.RuncOptions) (client.Config, client.ClientOpt) { return b.shimConfig(ns, ropts), client.WithLocal(exchange) } } -// ShimConnect is a shimOpt for connecting to an existing remote shim -func ShimConnect() shimOpt { +// ShimConnect is a ShimOpt for connecting to an existing remote shim +func ShimConnect() ShimOpt { return func(b *bundle, ns string, ropts *runcopts.RuncOptions) (client.Config, client.ClientOpt) { return b.shimConfig(ns, ropts), client.WithConnect(b.shimAddress(ns)) } } // NewShimClient connects to the shim managing the bundle and tasks creating it if needed -func (b *bundle) NewShimClient(ctx context.Context, namespace string, getClientOpts shimOpt, runcOpts *runcopts.RuncOptions) (*client.Client, error) { +func (b *bundle) NewShimClient(ctx context.Context, namespace string, getClientOpts ShimOpt, runcOpts *runcopts.RuncOptions) (*client.Client, error) { cfg, opt := getClientOpts(b, namespace, runcOpts) return client.New(ctx, cfg, opt) } diff --git a/metadata/content.go b/metadata/content.go index 05064fdec..293539aa1 100644 --- a/metadata/content.go +++ b/metadata/content.go @@ -566,7 +566,7 @@ func (cs *contentStore) garbageCollect(ctx context.Context) error { return err } - if err := cs.Store.Walk(ctx, func(info content.Info) error { + return cs.Store.Walk(ctx, func(info content.Info) error { if _, ok := seen[info.Digest.String()]; !ok { if err := cs.Store.Delete(ctx, info.Digest); err != nil { return err @@ -574,9 +574,5 @@ func (cs *contentStore) garbageCollect(ctx context.Context) error { log.G(ctx).WithField("digest", info.Digest).Debug("removed content") } return nil - }); err != nil { - return err - } - - return nil + }) } diff --git a/plugin/context.go b/plugin/context.go index 7fff5c6c6..849b1cb0f 100644 --- a/plugin/context.go +++ b/plugin/context.go @@ -61,10 +61,13 @@ type Plugin struct { err error // will be set if there was an error initializing the plugin } +// Err returns the errors during initialization. +// returns nil if not error was encountered func (p *Plugin) Err() error { return p.err } +// Instance returns the instance and any initialization error of the plugin func (p *Plugin) Instance() (interface{}, error) { return p.instance, p.err } @@ -80,12 +83,14 @@ type PluginSet struct { byTypeAndID map[Type]map[string]*Plugin } +// NewPluginSet returns an initialized plugin set func NewPluginSet() *PluginSet { return &PluginSet{ byTypeAndID: make(map[Type]map[string]*Plugin), } } +// Add a plugin to the set func (ps *PluginSet) Add(p *Plugin) error { if byID, typeok := ps.byTypeAndID[p.Registration.Type]; !typeok { ps.byTypeAndID[p.Registration.Type] = map[string]*Plugin{ @@ -109,6 +114,7 @@ func (ps *PluginSet) Get(t Type) (interface{}, error) { return nil, errors.Wrapf(errdefs.ErrNotFound, "no plugins registered for %s", t) } +// GetAll plugins in the set func (i *InitContext) GetAll() []*Plugin { return i.plugins.ordered } diff --git a/runtime/task.go b/runtime/task.go index 019d4c8cf..4c02455dc 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -7,6 +7,7 @@ import ( "github.com/gogo/protobuf/types" ) +// TaskInfo provides task specific information type TaskInfo struct { ID string Runtime string @@ -14,6 +15,7 @@ type TaskInfo struct { Namespace string } +// Process is a runtime object for an executing process inside a container type Process interface { ID() string // State returns the process state @@ -30,6 +32,7 @@ type Process interface { Wait(context.Context) (*Exit, error) } +// Task is the runtime object for an executing container type Task interface { Process @@ -55,27 +58,37 @@ type Task interface { Metrics(context.Context) (interface{}, error) } +// ExecOpts provides additional options for additional processes running in a task type ExecOpts struct { Spec *types.Any IO IO } +// ConsoleSize of a pty or windows terminal type ConsoleSize struct { Width uint32 Height uint32 } +// Status is the runtime status of a task and/or process type Status int const ( + // CreatedStatus when a process has been created CreatedStatus Status = iota + 1 + // RunningStatus when a process is running RunningStatus + // StoppedStatus when a process has stopped StoppedStatus + // DeletedStatus when a process has been deleted DeletedStatus + // PausedStatus when a process is paused PausedStatus + // PausingStatus when a process is currently pausing PausingStatus ) +// State information for a process type State struct { // Status is the current status of the container Status Status @@ -93,6 +106,7 @@ type State struct { Terminal bool } +// ProcessInfo holds platform specific process information type ProcessInfo struct { // Pid is the process ID Pid uint32 diff --git a/runtime/task_list.go b/runtime/task_list.go index 12062cef5..7c522655f 100644 --- a/runtime/task_list.go +++ b/runtime/task_list.go @@ -9,21 +9,26 @@ import ( ) var ( - ErrTaskNotExists = errors.New("task does not exist") + // ErrTaskNotExists is returned when a task does not exist + ErrTaskNotExists = errors.New("task does not exist") + // ErrTaskAlreadyExists is returned when a task already exists ErrTaskAlreadyExists = errors.New("task already exists") ) +// NewTaskList returns a new TaskList func NewTaskList() *TaskList { return &TaskList{ tasks: make(map[string]map[string]Task), } } +// TaskList holds and provides locking around tasks type TaskList struct { mu sync.Mutex tasks map[string]map[string]Task } +// Get a task func (l *TaskList) Get(ctx context.Context, id string) (Task, error) { l.mu.Lock() defer l.mu.Unlock() @@ -42,6 +47,7 @@ func (l *TaskList) Get(ctx context.Context, id string) (Task, error) { return t, nil } +// GetAll tasks under a namespace func (l *TaskList) GetAll(ctx context.Context) ([]Task, error) { namespace, err := namespaces.NamespaceRequired(ctx) if err != nil { @@ -58,6 +64,7 @@ func (l *TaskList) GetAll(ctx context.Context) ([]Task, error) { return o, nil } +// Add a task func (l *TaskList) Add(ctx context.Context, t Task) error { namespace, err := namespaces.NamespaceRequired(ctx) if err != nil { @@ -66,6 +73,7 @@ func (l *TaskList) Add(ctx context.Context, t Task) error { return l.AddWithNamespace(namespace, t) } +// AddWithNamespace adds a task with the provided namespace func (l *TaskList) AddWithNamespace(namespace string, t Task) error { l.mu.Lock() defer l.mu.Unlock() @@ -81,6 +89,7 @@ func (l *TaskList) AddWithNamespace(namespace string, t Task) error { return nil } +// Delete a task func (l *TaskList) Delete(ctx context.Context, t Task) { l.mu.Lock() defer l.mu.Unlock() diff --git a/server/config.go b/server/config.go index d6c2ca644..26af539ac 100644 --- a/server/config.go +++ b/server/config.go @@ -36,15 +36,15 @@ type Config struct { // GRPCConfig provides GRPC configuration for the socket type GRPCConfig struct { Address string `toml:"address"` - Uid int `toml:"uid"` - Gid int `toml:"gid"` + UID int `toml:"uid"` + GID int `toml:"gid"` } // Debug provides debug configuration type Debug struct { Address string `toml:"address"` - Uid int `toml:"uid"` - Gid int `toml:"gid"` + UID int `toml:"uid"` + GID int `toml:"gid"` Level string `toml:"level"` } diff --git a/services/healthcheck/service.go b/services/healthcheck/service.go index 85ffdab67..86c77e190 100644 --- a/services/healthcheck/service.go +++ b/services/healthcheck/service.go @@ -17,13 +17,12 @@ func init() { Type: plugin.GRPCPlugin, ID: "healthcheck", InitFn: func(*plugin.InitContext) (interface{}, error) { - return NewService() + return newService() }, }) } -// NewService returns the GRPC health server -func NewService() (*service, error) { +func newService() (*service, error) { return &service{ health.NewServer(), }, nil diff --git a/snapshot/btrfs/btrfs.go b/snapshot/btrfs/btrfs.go index 8bbe3fc80..32d66957e 100644 --- a/snapshot/btrfs/btrfs.go +++ b/snapshot/btrfs/btrfs.go @@ -107,8 +107,8 @@ func (b *snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, erro return info, nil } -func (o *snapshotter) Update(ctx context.Context, info snapshot.Info, fieldpaths ...string) (snapshot.Info, error) { - ctx, t, err := o.ms.TransactionContext(ctx, true) +func (b *snapshotter) Update(ctx context.Context, info snapshot.Info, fieldpaths ...string) (snapshot.Info, error) { + ctx, t, err := b.ms.TransactionContext(ctx, true) if err != nil { return snapshot.Info{}, err } diff --git a/snapshot/btrfs/btrfs_test.go b/snapshot/btrfs/btrfs_test.go index 54ff402c5..63c9b02af 100644 --- a/snapshot/btrfs/btrfs_test.go +++ b/snapshot/btrfs/btrfs_test.go @@ -46,12 +46,10 @@ func boltSnapshotter(t *testing.T) func(context.Context, string) (snapshot.Snaps return nil, nil, errors.Wrap(err, "failed to create new snapshotter") } - return snapshotter, func() (err error) { - merr := mount.UnmountAll(root, unix.MNT_DETACH) - if err = cleanupDevice(); err != nil { - return errors.Wrap(err, "device cleanup failed") - } else { - err = merr + return snapshotter, func() error { + err := mount.UnmountAll(root, unix.MNT_DETACH) + if cerr := cleanupDevice(); cerr != nil { + err = errors.Wrap(cerr, "device cleanup failed") } return err }, nil diff --git a/snapshot/overlay/overlay.go b/snapshot/overlay/overlay.go index c186c6ca7..8f0e0d09e 100644 --- a/snapshot/overlay/overlay.go +++ b/snapshot/overlay/overlay.go @@ -57,7 +57,7 @@ func NewSnapshotter(root string) (snapshot.Snapshotter, error) { return nil, err } if !supportsDType { - return nil, fmt.Errorf("%s does not support d_type. If the backing filesystem is xfs, please reformat with ftype=1 to enable d_type support.", root) + return nil, fmt.Errorf("%s does not support d_type. If the backing filesystem is xfs, please reformat with ftype=1 to enable d_type support", root) } ms, err := storage.NewMetaStore(filepath.Join(root, "metadata.db")) if err != nil { diff --git a/snapshot/storage/bolt.go b/snapshot/storage/bolt.go index 3ca3b879c..63dc1743d 100644 --- a/snapshot/storage/bolt.go +++ b/snapshot/storage/bolt.go @@ -582,7 +582,7 @@ func encodeSize(size int64) ([]byte, error) { func encodeID(id uint64) ([]byte, error) { var ( buf [binary.MaxVarintLen64]byte - idEncoded []byte = buf[:] + idEncoded = buf[:] ) idEncoded = idEncoded[:binary.PutUvarint(idEncoded, id)] diff --git a/task.go b/task.go index 6b9af1d41..b5f8450b3 100644 --- a/task.go +++ b/task.go @@ -51,6 +51,7 @@ type Status struct { ExitTime time.Time } +// ProcessInfo provides platform specific process information type ProcessInfo struct { // Pid is the process ID Pid uint32 diff --git a/testutil/loopback_linux.go b/testutil/loopback_linux.go index 1639c20de..635a6c31a 100644 --- a/testutil/loopback_linux.go +++ b/testutil/loopback_linux.go @@ -46,10 +46,7 @@ func NewLoopback(size int64) (string, func() error, error) { // remove file logrus.Debugf("Removing temporary file %s", file.Name()) - if err = os.Remove(file.Name()); err != nil { - return err - } - return nil + return os.Remove(file.Name()) } return deviceName, cleanup, nil