Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
FROM golang:1.9
FROM golang:1.19

ENV CONSUL_VERSION=1.0.0
ENV GLIDE_VERSION=0.12.3
ENV CONSUL_VERSION=1.13.3

RUN apt-get update \
&& apt-get install -y unzip \
&& go get github.com/golang/lint/golint \
&& curl -Lo /tmp/glide.tgz "https://github.com/Masterminds/glide/releases/download/v${GLIDE_VERSION}/glide-v${GLIDE_VERSION}-linux-amd64.tar.gz" \
&& tar -C /usr/bin -xzf /tmp/glide.tgz --strip=1 linux-amd64/glide \
&& curl --fail -Lso consul.zip "https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip" \
&& unzip consul.zip -d /usr/bin
&& go install honnef.co/go/tools/cmd/staticcheck@latest

RUN export CONSUL_CHECKSUM=5370b0b5bf765530e28cb80f90dcb47bd7d6ba78176c1ab2430f56e460ed279c \
&& export archive=consul_${CONSUL_VERSION}_linux_amd64.zip \
&& curl -Lso /tmp/${archive} https://releases.hashicorp.com/consul/${CONSUL_VERSION}/${archive} \
&& echo "${CONSUL_CHECKSUM} /tmp/${archive}" | sha256sum -c \
&& cd /bin \
&& unzip /tmp/${archive} \
&& chmod +x /bin/consul \
&& rm /tmp/${archive}

ENV CGO_ENABLED 0
ENV GOPATH /go:/cp
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
// requests out to a ContainerPilot process's control socket.
type HTTPClient struct {
http.Client
socketPath string
// staticcheck U1000 field is unused
//socketPath string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If socketPath is unused I think it can just be removed.

}

var socketType = "unix"
Expand Down
12 changes: 6 additions & 6 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (c *Command) Run(pctx context.Context, bus *events.EventBus) {
defer log.Debugf("%s.Run end", c.Name)
if err := c.Cmd.Start(); err != nil {
log.Errorf("unable to start %s: %v", c.Name, err)
bus.Publish(events.Event{events.ExitFailed, c.Name})
bus.Publish(events.Event{events.Error, err.Error()})
bus.Publish(events.Event{Code: events.ExitFailed, Source: c.Name})
bus.Publish(events.Event{Code: events.Error, Source: err.Error()})
return
}

Expand All @@ -150,12 +150,12 @@ func (c *Command) Run(pctx context.Context, bus *events.EventBus) {
// we'll return from Wait() and publish events
if err := c.Cmd.Wait(); err != nil {
log.Errorf("%s exited with error: %v", c.Name, err)
bus.Publish(events.Event{events.ExitFailed, c.Name})
bus.Publish(events.Event{events.Error,
fmt.Errorf("%s: %s", c.Name, err).Error()})
bus.Publish(events.Event{Code: events.ExitFailed, Source: c.Name})
bus.Publish(events.Event{Code: events.Error,
Source: fmt.Errorf("%s: %s", c.Name, err).Error()})
} else {
log.Debugf("%s exited without error", c.Name)
bus.Publish(events.Event{events.ExitSuccess, c.Name})
bus.Publish(events.Event{Code: events.ExitSuccess, Source: c.Name})
}
}()
}
Expand Down
24 changes: 12 additions & 12 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestCommandRunWithTimeoutZero(t *testing.T) {
cmd, _ := NewCommand("sleep 2", time.Duration(0), nil)
got := runtestCommandRun(cmd)
timedout := events.Event{events.ExitFailed, "sleep"}
timedout := events.Event{Code: events.ExitFailed, Source: "sleep"}
if got[timedout] != 1 {
t.Fatalf("stopped command prior to test timeout, got events %v", got)
}
Expand All @@ -25,9 +25,9 @@ func TestCommandRunWithTimeoutKilled(t *testing.T) {
cmd, _ := NewCommand("sleep 2", time.Duration(100*time.Millisecond), nil)
cmd.Name = t.Name()
got := runtestCommandRun(cmd)
testTimeout := events.Event{events.TimerExpired, "DebugSubscriberTimeout"}
expired := events.Event{events.ExitFailed, t.Name()}
errMsg := events.Event{events.Error, fmt.Sprintf("%s: signal: killed", cmd.Name)}
testTimeout := events.Event{Code: events.TimerExpired, Source: "DebugSubscriberTimeout"}
expired := events.Event{Code: events.ExitFailed, Source: t.Name()}
errMsg := events.Event{Code: events.Error, Source: fmt.Sprintf("%s: signal: killed", cmd.Name)}
if got[testTimeout] > 0 || got[expired] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", expired, errMsg, got)
}
Expand All @@ -38,9 +38,9 @@ func TestCommandRunChildrenKilled(t *testing.T) {
time.Duration(100*time.Millisecond), nil)
cmd.Name = t.Name()
got := runtestCommandRun(cmd)
testTimeout := events.Event{events.TimerExpired, "DebugSubscriberTimeout"}
expired := events.Event{events.ExitFailed, t.Name()}
errMsg := events.Event{events.Error, fmt.Sprintf("%s: signal: killed", cmd.Name)}
testTimeout := events.Event{Code: events.TimerExpired, Source: "DebugSubscriberTimeout"}
expired := events.Event{Code: events.ExitFailed, Source: t.Name()}
errMsg := events.Event{Code: events.Error, Source: fmt.Sprintf("%s: signal: killed", cmd.Name)}
if got[testTimeout] > 0 || got[expired] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", expired, errMsg, got)
}
Expand All @@ -49,8 +49,8 @@ func TestCommandRunChildrenKilled(t *testing.T) {
func TestCommandRunExecFailed(t *testing.T) {
cmd, _ := NewCommand("./testdata/test.sh failStuff --debug", time.Duration(0), nil)
got := runtestCommandRun(cmd)
failed := events.Event{events.ExitFailed, "./testdata/test.sh"}
errMsg := events.Event{events.Error, "./testdata/test.sh: exit status 255"}
failed := events.Event{Code: events.ExitFailed, Source: "./testdata/test.sh"}
errMsg := events.Event{Code: events.Error, Source: "./testdata/test.sh: exit status 255"}
if got[failed] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", failed, errMsg, got)
}
Expand All @@ -59,9 +59,9 @@ func TestCommandRunExecFailed(t *testing.T) {
func TestCommandRunExecInvalid(t *testing.T) {
cmd, _ := NewCommand("./testdata/invalidCommand", time.Duration(0), nil)
got := runtestCommandRun(cmd)
failed := events.Event{events.ExitFailed, "./testdata/invalidCommand"}
errMsg := events.Event{events.Error,
"fork/exec ./testdata/invalidCommand: no such file or directory"}
failed := events.Event{Code: events.ExitFailed, Source: "./testdata/invalidCommand"}
errMsg := events.Event{Code: events.Error,
Source: "fork/exec ./testdata/invalidCommand: no such file or directory"}
if got[failed] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", failed, errMsg, got)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/testdata/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

trap 'exit 2' SIGTERM

Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/flynn/json5"
Expand Down Expand Up @@ -79,7 +79,7 @@ func RenderConfig(configFlag, renderFlag string) error {
fmt.Printf("%s", renderedConfig)
} else {
var err error
if err = ioutil.WriteFile(renderFlag, renderedConfig, 0644); err != nil {
if err = os.WriteFile(renderFlag, renderedConfig, 0644); err != nil {
return fmt.Errorf("could not write config file: %s", err)
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func loadConfigFile(configFlag string) ([]byte, error) {
if configFlag == "" {
return nil, errors.New("-config flag is required")
}
data, err := ioutil.ReadFile(configFlag)
data, err := os.ReadFile(configFlag)
if err != nil {
return nil, fmt.Errorf("could not read config file: %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -184,8 +183,8 @@ func TestRenderConfigFileStdout(t *testing.T) {
temp.Close()
os.Stdout = old

renderedOut, _ := ioutil.ReadFile(fname)
renderedFile, _ := ioutil.ReadFile("testJSON.json")
renderedOut, _ := os.ReadFile(fname)
renderedFile, _ := os.ReadFile("testJSON.json")
if string(renderedOut) != string(renderedFile) {
t.Fatalf("expected the rendered file and stdout to be identical")
}
Expand Down
2 changes: 1 addition & 1 deletion config/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func interfaceToString(raw interface{}) string {
}

func interfaceToStringArray(rawArray []interface{}) []string {
if rawArray == nil || len(rawArray) == 0 {
if len(rawArray) == 0 {
return nil
}
var stringArray []string
Expand Down
8 changes: 4 additions & 4 deletions config/logger/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (l *Config) Init() error {
}
level, err := logrus.ParseLevel(strings.ToLower(l.Level))
if err != nil {
return fmt.Errorf("Unknown log level '%s': %s", l.Level, err)
return fmt.Errorf("unknown log level '%s': %s", l.Level, err)
}
var formatter logrus.Formatter
var output io.Writer
Expand All @@ -65,19 +65,19 @@ func (l *Config) Init() error {
TimestampFormat: time.RFC3339Nano,
}
default:
return fmt.Errorf("Unknown log format '%s'", l.Format)
return fmt.Errorf("unknown log format '%s'", l.Format)
}
switch strings.ToLower(l.Output) {
case "stderr":
output = os.Stderr
case "stdout":
output = os.Stdout
case "":
return fmt.Errorf("Unknown output type '%s'", l.Output)
return fmt.Errorf("unknown output type '%s'", l.Output)
default:
f, err := reopen.NewFileWriter(l.Output)
if err != nil {
return fmt.Errorf("Error initializing log file '%s': %s", l.Output, err)
return fmt.Errorf("error initializing log file '%s': %s", l.Output, err)
}
initializeSignal(f)
output = f
Expand Down
3 changes: 1 addition & 2 deletions config/logger/logging_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logger

import (
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestFileLogger(t *testing.T) {
// write a log message
logMsg := "this is a test"
logrus.Info(logMsg)
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
t.Errorf("Did not expect error: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions config/services/ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func IPFromInterfaces(raw interface{}) (string, error) {
// GetIP determines the IP address of the container
func GetIP(specList []string) (string, error) {

if specList == nil || len(specList) == 0 {
if len(specList) == 0 {
// Use a sane default
specList = []string{"eth0:inet", "inet"}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func parseInterfaceSpec(spec string) (interfaceSpec, error) {
if _, err := strconv.Atoi(ip[1]); err != nil {
nip := net.ParseIP(ip[1])
if nip == nil {
return nil, fmt.Errorf("Unable to parse static ip %s in %s", ip[0], spec)
return nil, fmt.Errorf("unable to parse static ip %s in %s", ip[0], spec)
}
return staticInterfaceSpec{Spec: spec, Name: "static", IP: nip}, nil
}
Expand All @@ -204,7 +204,7 @@ func parseInterfaceSpec(spec string) (interfaceSpec, error) {
if index != "" {
i, err := strconv.Atoi(index)
if err != nil {
return nil, fmt.Errorf("Unable to parse index %s in %s", index, spec)
return nil, fmt.Errorf("unable to parse index %s in %s", index, spec)
}
return indexInterfaceSpec{Spec: spec, Name: name, Index: i}, nil
}
Expand All @@ -219,7 +219,7 @@ func parseInterfaceSpec(spec string) (interfaceSpec, error) {
if _, net, err := net.ParseCIDR(spec); err == nil {
return cidrInterfaceSpec{Spec: spec, Network: net}, nil
}
return nil, fmt.Errorf("Unable to parse interface spec: %s", spec)
return nil, fmt.Errorf("unable to parse interface spec: %s", spec)
}

type interfaceIP struct {
Expand Down
2 changes: 1 addition & 1 deletion config/services/ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestGetIp(t *testing.T) {
ip, _ := GetIP([]string{lo, "inet"})
assert.Equal(t, "127.0.0.1", ip, "expected to find loopback IP")

ip, err := GetIP([]string{"interface-does-not-exist"})
_, err := GetIP([]string{"interface-does-not-exist"})
assert.Error(t, err, "expected interface not found, but instead got an IP")

ip, _ = GetIP([]string{"static:192.168.1.100", lo})
Expand Down
4 changes: 2 additions & 2 deletions config/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func envFunc(env string) string {
}

func ensureInt(intv interface{}) (int, error) {
switch intv.(type) {
switch intv := intv.(type) {
case string:
ret, err := strconv.Atoi(intv.(string))
ret, err := strconv.Atoi(intv)
if err != nil {
return 0, err
}
Expand Down
4 changes: 2 additions & 2 deletions config/timing/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGetTimeout(t *testing.T) {

dur, err = GetTimeout("x")
expectDurationCompare(t, dur, time.Duration(0),
err, errors.New("time: invalid duration x"))
err, errors.New("time: invalid duration \"x\""))

dur, err = GetTimeout("0")
expectDurationCompare(t, dur, time.Duration(0), err, nil)
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestParseDuration(t *testing.T) {

// Some parse errors
expectError(t, "asf", "invalid duration")
expectError(t, "20yy", "unknown unit yy")
expectError(t, "20yy", "time: unknown unit \"yy\" in duration \"20yy\"")

// Fractional
expectError(t, 10.10, "unexpected duration of type float")
Expand Down
1 change: 0 additions & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (srv *HTTPServer) Run(pctx context.Context, bus *events.EventBus) {
go func() {
defer srv.Stop()
<-ctx.Done()
return
}()
}

Expand Down
7 changes: 3 additions & 4 deletions control/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -56,7 +55,7 @@ func (pw PostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// process. Returns empty response or HTTP422.
func (e Endpoints) PutEnviron(r *http.Request) (interface{}, int) {
var postEnv map[string]string
jsonBlob, err := ioutil.ReadAll(r.Body)
jsonBlob, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return nil, http.StatusUnprocessableEntity
Expand Down Expand Up @@ -110,7 +109,7 @@ func (e Endpoints) PostDisableMaintenanceMode(r *http.Request) (interface{}, int
// Returns empty response or HTTP422.
func (e Endpoints) PostMetric(r *http.Request) (interface{}, int) {
var postMetrics map[string]interface{}
jsonBlob, err := ioutil.ReadAll(r.Body)
jsonBlob, err := io.ReadAll(r.Body)

defer r.Body.Close()
if err != nil {
Expand All @@ -123,7 +122,7 @@ func (e Endpoints) PostMetric(r *http.Request) (interface{}, int) {
}
for metricKey, metricValue := range postMetrics {
eventVal := fmt.Sprintf("%v|%v", metricKey, metricValue)
e.bus.Publish(events.Event{events.Metric, eventVal})
e.bus.Publish(events.Event{Code: events.Metric, Source: eventVal})
}
return nil, http.StatusOK
}
Expand Down
10 changes: 5 additions & 5 deletions control/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package control
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestPostHandler(t *testing.T) {
ph.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
status := resp.StatusCode
return status, string(body)
}
Expand Down Expand Up @@ -130,15 +130,15 @@ func TestPostMetric(t *testing.T) {
})
t.Run("POST value", func(t *testing.T) {
body := "{\"mymetric\": 1.0}"
expected := map[events.Event]int{{events.Metric, "mymetric|1"}: 1}
expected := map[events.Event]int{{Code: events.Metric, Source: "mymetric|1"}: 1}
status := testFunc(t, expected, body)
assert.Equal(t, http.StatusOK, status, "status was not 200OK")
})
t.Run("POST multi-metric", func(t *testing.T) {
body := "{\"mymetric\": 1.5, \"myothermetric\": 2}"
status := testFunc(t, map[events.Event]int{
{events.Metric, "mymetric|1.5"}: 1,
{events.Metric, "myothermetric|2"}: 1,
{Code: events.Metric, Source: "mymetric|1.5"}: 1,
{Code: events.Metric, Source: "myothermetric|2"}: 1,
}, body)
assert.Equal(t, http.StatusOK, status, "status was not 200OK")
})
Expand Down
Loading