Improve documentation, remove dead code plus some minor refactorings
This improves our code-to-comments ratio by a lot.
On the refactorings:
- Simplify the cluster join mode to just a single protobuf message -
a node can either join an existing cluster or bootstrap a new one.
All of the node-level setup like hostname and trust backend is done
using the setup call, since those are identical for both cases.
- We don't need a node name separate from the hostname. Ideally, we would
get rid of IP addresses for etcd as well.
- Google API design guidelines suggest the `List` term (vs. `Get`).
- Add username to comments for consistency. I think the names provide
useful context, but git blame is a thing. What do you think?
- Fixed or silenced some ignored error checks in preparation of using
an errcheck linter. Especially during early boot, many errors are
obviously not recoverable, but logging them can provide useful debugging info.
- Split up the common package into smaller subpackages.
- Remove the audit package (this will be a separate service that probably
uses it own database, rather than etcd).
- Move storage constants to storage package.
- Remove the unused KV type.
I also added a bunch of TODO comments with discussion points.
Added both of you as blocking reviewers - please comment if I
misunderstood any of your code.
Test Plan: Everything compiles and scripts:launch works (for whatever that's worth).
X-Origin-Diff: phab/D235
GitOrigin-RevId: 922fec5076e8d683e1138f26d2cb490de64a9777
diff --git a/core/internal/common/service/service.go b/core/internal/common/service/service.go
new file mode 100644
index 0000000..e093ff6
--- /dev/null
+++ b/core/internal/common/service/service.go
@@ -0,0 +1,104 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// 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 service
+
+import (
+ "errors"
+ "go.uber.org/zap"
+ "sync"
+)
+
+var (
+ ErrAlreadyRunning = errors.New("service is already running")
+ ErrNotRunning = errors.New("service is not running")
+)
+
+type (
+ // Service represents a subsystem of an application that can be used with a BaseService.
+ Service interface {
+ OnStart() error
+ OnStop() error
+ }
+
+ // BaseService implements utility functionality around a service.
+ BaseService struct {
+ impl Service
+ name string
+
+ Logger *zap.Logger
+
+ mutex sync.Mutex
+ running bool
+ }
+)
+
+func NewBaseService(name string, logger *zap.Logger, impl Service) *BaseService {
+ return &BaseService{
+ Logger: logger,
+ name: name,
+ impl: impl,
+ }
+}
+
+// Start starts the service. This is an atomic operation and should not be called on an already running service.
+func (b *BaseService) Start() error {
+ b.mutex.Lock()
+ defer b.mutex.Unlock()
+
+ if b.running {
+ return ErrAlreadyRunning
+ }
+
+ err := b.impl.OnStart()
+ if err != nil {
+ b.Logger.Error("Failed to start service", zap.String("service", b.name), zap.Error(err))
+ return err
+ }
+
+ b.running = true
+ b.Logger.Info("Started service", zap.String("service", b.name))
+ return nil
+}
+
+// Stop stops the service. This is an atomic operation and should only be called on a running service.
+func (b *BaseService) Stop() error {
+ b.mutex.Lock()
+ defer b.mutex.Unlock()
+
+ if !b.running {
+ return ErrNotRunning
+ }
+
+ err := b.impl.OnStart()
+ if err != nil {
+ b.Logger.Error("Failed to stop service", zap.String("service", b.name), zap.Error(err))
+
+ return err
+ }
+
+ b.running = false
+ b.Logger.Info("Stopped service", zap.String("service", b.name))
+ return nil
+}
+
+// IsRunning returns whether the service is currently running.
+func (b *BaseService) IsRunning() bool {
+ b.mutex.Lock()
+ defer b.mutex.Unlock()
+
+ return b.running
+}