mirror of
https://github.com/containrrr/watchtower.git
synced 2026-02-02 13:41:49 +01:00
http report wip
This commit is contained in:
parent
e3dd8d688a
commit
efaf7190ee
25 changed files with 350 additions and 284 deletions
|
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/containrrr/watchtower/pkg/session"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
|
@ -10,8 +11,9 @@ const tokenMissingMsg = "api token is empty or has not been set. exiting"
|
|||
|
||||
// API is the http server responsible for serving the HTTP API endpoints
|
||||
type API struct {
|
||||
Token string
|
||||
hasHandlers bool
|
||||
Token string
|
||||
hasHandlers bool
|
||||
latestReport session.Report
|
||||
}
|
||||
|
||||
// New is a factory function creating a new API instance
|
||||
|
|
@ -74,3 +76,7 @@ func runHTTPServer() {
|
|||
log.Info("Serving HTTP")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func (api *API) UpdateReport(report session.Report) {
|
||||
api.latestReport = report
|
||||
}
|
||||
|
|
|
|||
13
pkg/api/api_suite_test.go
Normal file
13
pkg/api/api_suite_test.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package api_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestAPI(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "API Suite")
|
||||
}
|
||||
23
pkg/api/json.go
Normal file
23
pkg/api/json.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// WriteJsonOrError writes the supplied response to the http.ResponseWriter, handling any errors by logging and
|
||||
// returning an Internal Server Error response (status 500)
|
||||
func WriteJsonOrError(writer http.ResponseWriter, response interface{}) {
|
||||
data, err := json.MarshalIndent(response, "", " ")
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("failed to create json payload")
|
||||
writer.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
_, err = writer.Write(data)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("failed to write response")
|
||||
}
|
||||
}
|
||||
32
pkg/api/metrics.go
Normal file
32
pkg/api/metrics.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/metrics"
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// MetricsHandler is a HTTP handler for serving metric data
|
||||
type MetricsHandler struct {
|
||||
Path string
|
||||
Handle http.HandlerFunc
|
||||
Metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
// NewMetricsHandler is a factory function creating a new Metrics instance
|
||||
func NewMetricsHandler() *MetricsHandler {
|
||||
m := metrics.Default()
|
||||
handler := promhttp.Handler()
|
||||
|
||||
return &MetricsHandler{
|
||||
Path: "/v1/metrics",
|
||||
Handle: handler.ServeHTTP,
|
||||
Metrics: m,
|
||||
}
|
||||
}
|
||||
|
||||
func MetricsEndpoint() (path string, handler http.HandlerFunc) {
|
||||
mh := NewMetricsHandler()
|
||||
return mh.Path, mh.Handle
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/metrics"
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// Handler is an HTTP handle for serving metric data
|
||||
type Handler struct {
|
||||
Path string
|
||||
Handle http.HandlerFunc
|
||||
Metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
// New is a factory function creating a new Metrics instance
|
||||
func New() *Handler {
|
||||
m := metrics.Default()
|
||||
handler := promhttp.Handler()
|
||||
|
||||
return &Handler{
|
||||
Path: "/v1/metrics",
|
||||
Handle: handler.ServeHTTP,
|
||||
Metrics: m,
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package metrics_test
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -6,10 +6,8 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/containrrr/watchtower/pkg/api"
|
||||
metricsAPI "github.com/containrrr/watchtower/pkg/api/metrics"
|
||||
"github.com/containrrr/watchtower/pkg/metrics"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
|
@ -21,11 +19,6 @@ const (
|
|||
getURL = "http://localhost:8080/v1/metrics"
|
||||
)
|
||||
|
||||
func TestMetrics(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Metrics Suite")
|
||||
}
|
||||
|
||||
func getWithToken(handler http.Handler) map[string]string {
|
||||
metricMap := map[string]string{}
|
||||
respWriter := httptest.NewRecorder()
|
||||
|
|
@ -51,7 +44,7 @@ func getWithToken(handler http.Handler) map[string]string {
|
|||
|
||||
var _ = Describe("the metrics API", func() {
|
||||
httpAPI := api.New(token)
|
||||
m := metricsAPI.New()
|
||||
m := api.NewMetricsHandler()
|
||||
|
||||
handleReq := httpAPI.RequireToken(m.Handle)
|
||||
tryGetMetrics := func() map[string]string { return getWithToken(handleReq) }
|
||||
14
pkg/api/report.go
Normal file
14
pkg/api/report.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/session"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ReportEndpoint(reportPtr **session.Report) (path string, handler http.HandlerFunc) {
|
||||
path = "/v1/report"
|
||||
handler = func(writer http.ResponseWriter, request *http.Request) {
|
||||
WriteJsonOrError(writer, *reportPtr)
|
||||
}
|
||||
return path, handler
|
||||
}
|
||||
62
pkg/api/update.go
Normal file
62
pkg/api/update.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/metrics"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
lock chan bool
|
||||
)
|
||||
|
||||
// NewUpdateHandler is a factory function creating a new Handler instance
|
||||
func NewUpdateHandler(updateFn func() *metrics.Metric, updateLock chan bool) *UpdateHandler {
|
||||
if updateLock != nil {
|
||||
lock = updateLock
|
||||
} else {
|
||||
lock = make(chan bool, 1)
|
||||
lock <- true
|
||||
}
|
||||
|
||||
return &UpdateHandler{
|
||||
fn: updateFn,
|
||||
Path: "/v1/update",
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateEndpoint(updateFn func() *metrics.Metric, updateLock chan bool) (path string, handler http.HandlerFunc) {
|
||||
uh := NewUpdateHandler(updateFn, updateLock)
|
||||
return uh.Path, uh.Handle
|
||||
}
|
||||
|
||||
// UpdateHandler is an API handler used for triggering container update scans
|
||||
type UpdateHandler struct {
|
||||
fn func() *metrics.Metric
|
||||
Path string
|
||||
}
|
||||
|
||||
// Handle is the actual http.Handle function doing all the heavy lifting
|
||||
func (handler *UpdateHandler) Handle(w http.ResponseWriter, _ *http.Request) {
|
||||
log.Info("Updates triggered by HTTP API request.")
|
||||
|
||||
result := updateResult{}
|
||||
|
||||
select {
|
||||
case chanValue := <-lock:
|
||||
defer func() { lock <- chanValue }()
|
||||
metric := handler.fn()
|
||||
metrics.RegisterScan(metric)
|
||||
result.Result = metric
|
||||
result.Skipped = false
|
||||
default:
|
||||
log.Debug("Skipped. Another update already running.")
|
||||
result.Skipped = true
|
||||
}
|
||||
WriteJsonOrError(w, result)
|
||||
}
|
||||
|
||||
type updateResult struct {
|
||||
Skipped bool
|
||||
Result *metrics.Metric
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
package update
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
lock chan bool
|
||||
)
|
||||
|
||||
// New is a factory function creating a new Handler instance
|
||||
func New(updateFn func(), updateLock chan bool) *Handler {
|
||||
if updateLock != nil {
|
||||
lock = updateLock
|
||||
} else {
|
||||
lock = make(chan bool, 1)
|
||||
lock <- true
|
||||
}
|
||||
|
||||
return &Handler{
|
||||
fn: updateFn,
|
||||
Path: "/v1/update",
|
||||
}
|
||||
}
|
||||
|
||||
// Handler is an API handler used for triggering container update scans
|
||||
type Handler struct {
|
||||
fn func()
|
||||
Path string
|
||||
}
|
||||
|
||||
// Handle is the actual http.Handle function doing all the heavy lifting
|
||||
func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
log.Info("Updates triggered by HTTP API request.")
|
||||
|
||||
_, err := io.Copy(os.Stdout, r.Body)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case chanValue := <-lock:
|
||||
defer func() { lock <- chanValue }()
|
||||
handle.fn()
|
||||
default:
|
||||
log.Debug("Skipped. Another update already running.")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
"github.com/containrrr/watchtower/pkg/session"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
|
@ -26,12 +26,12 @@ type Metrics struct {
|
|||
}
|
||||
|
||||
// NewMetric returns a Metric with the counts taken from the appropriate types.Report fields
|
||||
func NewMetric(report types.Report) *Metric {
|
||||
func NewMetric(report *session.Report) *Metric {
|
||||
return &Metric{
|
||||
Scanned: len(report.Scanned()),
|
||||
Scanned: len(report.Scanned),
|
||||
// Note: This is for backwards compatibility. ideally, stale containers should be counted separately
|
||||
Updated: len(report.Updated()) + len(report.Stale()),
|
||||
Failed: len(report.Failed()),
|
||||
Updated: len(report.Updated) + len(report.Stale),
|
||||
Failed: len(report.Failed),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package notifications
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/session"
|
||||
ty "github.com/containrrr/watchtower/pkg/types"
|
||||
"github.com/johntdyer/slackrus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
|
@ -8,8 +9,16 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
// Notifier is the interface that all notification services have in common
|
||||
type Notifier interface {
|
||||
StartNotification()
|
||||
SendNotification(report *session.Report)
|
||||
GetNames() []string
|
||||
Close()
|
||||
}
|
||||
|
||||
// NewNotifier creates and returns a new Notifier, using global configuration.
|
||||
func NewNotifier(c *cobra.Command) ty.Notifier {
|
||||
func NewNotifier(c *cobra.Command) Notifier {
|
||||
f := c.PersistentFlags()
|
||||
|
||||
level, _ := f.GetString("notifications-level")
|
||||
|
|
|
|||
|
|
@ -3,13 +3,13 @@ package notifications
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/containrrr/watchtower/pkg/session"
|
||||
stdlog "log"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/containrrr/shoutrrr"
|
||||
"github.com/containrrr/shoutrrr/pkg/types"
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
|
@ -18,7 +18,7 @@ const (
|
|||
shoutrrrDefaultTemplate = `{{- with .Report -}}
|
||||
{{len .Scanned}} Scanned, {{len .Updated}} Updated, {{len .Failed}} Failed
|
||||
{{range .Updated -}}
|
||||
- {{.Name}} ({{.ImageName}}): {{.CurrentImageID.ShortID}} updated to {{.LatestImageID.ShortID}}
|
||||
- {{.Name}} ({{.ImageName}}): {{.OldImageID.ShortID}} updated to {{.NewImageID.ShortID}}
|
||||
{{end -}}
|
||||
{{range .Fresh -}}
|
||||
- {{.Name}} ({{.ImageName}}): {{.State}}
|
||||
|
|
@ -66,7 +66,7 @@ func (n *shoutrrrTypeNotifier) GetNames() []string {
|
|||
return names
|
||||
}
|
||||
|
||||
func newShoutrrrNotifier(tplString string, acceptedLogLevels []log.Level, legacy bool, urls ...string) t.Notifier {
|
||||
func newShoutrrrNotifier(tplString string, acceptedLogLevels []log.Level, legacy bool, urls ...string) Notifier {
|
||||
|
||||
notifier := createNotifier(urls, acceptedLogLevels, tplString, legacy)
|
||||
log.AddHook(notifier)
|
||||
|
|
@ -129,7 +129,7 @@ func (n *shoutrrrTypeNotifier) buildMessage(data Data) string {
|
|||
return body.String()
|
||||
}
|
||||
|
||||
func (n *shoutrrrTypeNotifier) sendEntries(entries []*log.Entry, report t.Report) {
|
||||
func (n *shoutrrrTypeNotifier) sendEntries(entries []*log.Entry, report *session.Report) {
|
||||
msg := n.buildMessage(Data{entries, report})
|
||||
n.messages <- msg
|
||||
}
|
||||
|
|
@ -140,11 +140,7 @@ func (n *shoutrrrTypeNotifier) StartNotification() {
|
|||
}
|
||||
}
|
||||
|
||||
func (n *shoutrrrTypeNotifier) SendNotification(report t.Report) {
|
||||
//if n.entries == nil || len(n.entries) <= 0 {
|
||||
// return
|
||||
//}
|
||||
|
||||
func (n *shoutrrrTypeNotifier) SendNotification(report *session.Report) {
|
||||
n.sendEntries(n.entries, report)
|
||||
n.entries = nil
|
||||
}
|
||||
|
|
@ -205,5 +201,5 @@ func getShoutrrrTemplate(tplString string, legacy bool) (tpl *template.Template,
|
|||
// Data is the notification template data model
|
||||
type Data struct {
|
||||
Entries []*log.Entry
|
||||
Report t.Report
|
||||
Report *session.Report
|
||||
}
|
||||
|
|
|
|||
|
|
@ -223,5 +223,7 @@ func getTemplatedResult(tplString string, legacy bool, data Data) (string, error
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return notifier.buildMessage(data), err
|
||||
msg := notifier.buildMessage(data)
|
||||
println(msg)
|
||||
return msg, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package session
|
||||
|
||||
import wt "github.com/containrrr/watchtower/pkg/types"
|
||||
import (
|
||||
wt "github.com/containrrr/watchtower/pkg/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// State indicates what the current state is of the container
|
||||
type State int
|
||||
|
|
@ -19,51 +22,17 @@ const (
|
|||
|
||||
// ContainerStatus contains the container state during a session
|
||||
type ContainerStatus struct {
|
||||
containerID wt.ContainerID
|
||||
oldImage wt.ImageID
|
||||
newImage wt.ImageID
|
||||
containerName string
|
||||
imageName string
|
||||
error
|
||||
state State
|
||||
ID wt.ContainerID
|
||||
Name string
|
||||
OldImageID wt.ImageID
|
||||
NewImageID wt.ImageID
|
||||
ImageName string
|
||||
Error error
|
||||
State State
|
||||
}
|
||||
|
||||
// ID returns the container ID
|
||||
func (u *ContainerStatus) ID() wt.ContainerID {
|
||||
return u.containerID
|
||||
}
|
||||
|
||||
// Name returns the container name
|
||||
func (u *ContainerStatus) Name() string {
|
||||
return u.containerName
|
||||
}
|
||||
|
||||
// CurrentImageID returns the image ID that the container used when the session started
|
||||
func (u *ContainerStatus) CurrentImageID() wt.ImageID {
|
||||
return u.oldImage
|
||||
}
|
||||
|
||||
// LatestImageID returns the newest image ID found during the session
|
||||
func (u *ContainerStatus) LatestImageID() wt.ImageID {
|
||||
return u.newImage
|
||||
}
|
||||
|
||||
// ImageName returns the name:tag that the container uses
|
||||
func (u *ContainerStatus) ImageName() string {
|
||||
return u.imageName
|
||||
}
|
||||
|
||||
// Error returns the error (if any) that was encountered for the container during a session
|
||||
func (u *ContainerStatus) Error() string {
|
||||
if u.error == nil {
|
||||
return ""
|
||||
}
|
||||
return u.error.Error()
|
||||
}
|
||||
|
||||
// State returns the current State that the container is in
|
||||
func (u *ContainerStatus) State() string {
|
||||
switch u.state {
|
||||
func (state State) String() string {
|
||||
switch state {
|
||||
case SkippedState:
|
||||
return "Skipped"
|
||||
case ScannedState:
|
||||
|
|
@ -80,3 +49,12 @@ func (u *ContainerStatus) State() string {
|
|||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON marshals State as a string
|
||||
func (state State) MarshalJSON() ([]byte, error) {
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString(`"`)
|
||||
sb.WriteString(state.String())
|
||||
sb.WriteString(`"`)
|
||||
return []byte(sb.String()), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,19 +10,19 @@ type Progress map[types.ContainerID]*ContainerStatus
|
|||
// UpdateFromContainer sets various status fields from their corresponding container equivalents
|
||||
func UpdateFromContainer(cont types.Container, newImage types.ImageID, state State) *ContainerStatus {
|
||||
return &ContainerStatus{
|
||||
containerID: cont.ID(),
|
||||
containerName: cont.Name(),
|
||||
imageName: cont.ImageName(),
|
||||
oldImage: cont.SafeImageID(),
|
||||
newImage: newImage,
|
||||
state: state,
|
||||
ID: cont.ID(),
|
||||
Name: cont.Name(),
|
||||
ImageName: cont.ImageName(),
|
||||
OldImageID: cont.SafeImageID(),
|
||||
NewImageID: newImage,
|
||||
State: state,
|
||||
}
|
||||
}
|
||||
|
||||
// AddSkipped adds a container to the Progress with the state set as skipped
|
||||
func (m Progress) AddSkipped(cont types.Container, err error) {
|
||||
update := UpdateFromContainer(cont, cont.SafeImageID(), SkippedState)
|
||||
update.error = err
|
||||
update.Error = err
|
||||
m.Add(update)
|
||||
}
|
||||
|
||||
|
|
@ -35,22 +35,17 @@ func (m Progress) AddScanned(cont types.Container, newImage types.ImageID) {
|
|||
func (m Progress) UpdateFailed(failures map[types.ContainerID]error) {
|
||||
for id, err := range failures {
|
||||
update := m[id]
|
||||
update.error = err
|
||||
update.state = FailedState
|
||||
update.Error = err
|
||||
update.State = FailedState
|
||||
}
|
||||
}
|
||||
|
||||
// Add a container to the map using container ID as the key
|
||||
func (m Progress) Add(update *ContainerStatus) {
|
||||
m[update.containerID] = update
|
||||
m[update.ID] = update
|
||||
}
|
||||
|
||||
// MarkForUpdate marks the container identified by containerID for update
|
||||
func (m Progress) MarkForUpdate(containerID types.ContainerID) {
|
||||
m[containerID].state = UpdatedState
|
||||
}
|
||||
|
||||
// Report creates a new Report from a Progress instance
|
||||
func (m Progress) Report() types.Report {
|
||||
return NewReport(m)
|
||||
m[containerID].State = UpdatedState
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,90 +1,78 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type report struct {
|
||||
scanned []types.ContainerReport
|
||||
updated []types.ContainerReport
|
||||
failed []types.ContainerReport
|
||||
skipped []types.ContainerReport
|
||||
stale []types.ContainerReport
|
||||
fresh []types.ContainerReport
|
||||
}
|
||||
|
||||
func (r *report) Scanned() []types.ContainerReport {
|
||||
return r.scanned
|
||||
}
|
||||
func (r *report) Updated() []types.ContainerReport {
|
||||
return r.updated
|
||||
}
|
||||
func (r *report) Failed() []types.ContainerReport {
|
||||
return r.failed
|
||||
}
|
||||
func (r *report) Skipped() []types.ContainerReport {
|
||||
return r.skipped
|
||||
}
|
||||
func (r *report) Stale() []types.ContainerReport {
|
||||
return r.stale
|
||||
}
|
||||
func (r *report) Fresh() []types.ContainerReport {
|
||||
return r.fresh
|
||||
type Report struct {
|
||||
Started time.Time
|
||||
Ended time.Time
|
||||
Trigger Trigger
|
||||
Scanned []*ContainerStatus
|
||||
Updated []*ContainerStatus
|
||||
Failed []*ContainerStatus
|
||||
Skipped []*ContainerStatus
|
||||
Stale []*ContainerStatus
|
||||
Fresh []*ContainerStatus
|
||||
}
|
||||
|
||||
// NewReport creates a types.Report from the supplied Progress
|
||||
func NewReport(progress Progress) types.Report {
|
||||
report := &report{
|
||||
scanned: []types.ContainerReport{},
|
||||
updated: []types.ContainerReport{},
|
||||
failed: []types.ContainerReport{},
|
||||
skipped: []types.ContainerReport{},
|
||||
stale: []types.ContainerReport{},
|
||||
fresh: []types.ContainerReport{},
|
||||
// s.Started, time.Now().UTC(), s.Trigger, s.Progress
|
||||
func NewReport(started, ended time.Time, trigger Trigger, progress Progress) *Report {
|
||||
report := &Report{
|
||||
Started: started,
|
||||
Ended: ended,
|
||||
Trigger: trigger,
|
||||
Scanned: []*ContainerStatus{},
|
||||
Updated: []*ContainerStatus{},
|
||||
Failed: []*ContainerStatus{},
|
||||
Skipped: []*ContainerStatus{},
|
||||
Stale: []*ContainerStatus{},
|
||||
Fresh: []*ContainerStatus{},
|
||||
}
|
||||
|
||||
for _, update := range progress {
|
||||
if update.state == SkippedState {
|
||||
report.skipped = append(report.skipped, update)
|
||||
if update.State == SkippedState {
|
||||
report.Skipped = append(report.Skipped, update)
|
||||
continue
|
||||
}
|
||||
|
||||
report.scanned = append(report.scanned, update)
|
||||
if update.newImage == update.oldImage {
|
||||
update.state = FreshState
|
||||
report.fresh = append(report.fresh, update)
|
||||
report.Scanned = append(report.Scanned, update)
|
||||
if update.NewImageID == update.OldImageID {
|
||||
update.State = FreshState
|
||||
report.Fresh = append(report.Fresh, update)
|
||||
continue
|
||||
}
|
||||
|
||||
switch update.state {
|
||||
switch update.State {
|
||||
case UpdatedState:
|
||||
report.updated = append(report.updated, update)
|
||||
report.Updated = append(report.Updated, update)
|
||||
case FailedState:
|
||||
report.failed = append(report.failed, update)
|
||||
report.Failed = append(report.Failed, update)
|
||||
default:
|
||||
update.state = StaleState
|
||||
report.stale = append(report.stale, update)
|
||||
update.State = StaleState
|
||||
report.Stale = append(report.Stale, update)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Sort(sortableContainers(report.scanned))
|
||||
sort.Sort(sortableContainers(report.updated))
|
||||
sort.Sort(sortableContainers(report.failed))
|
||||
sort.Sort(sortableContainers(report.skipped))
|
||||
sort.Sort(sortableContainers(report.stale))
|
||||
sort.Sort(sortableContainers(report.fresh))
|
||||
sort.Sort(sortableContainers(report.Scanned))
|
||||
sort.Sort(sortableContainers(report.Updated))
|
||||
sort.Sort(sortableContainers(report.Failed))
|
||||
sort.Sort(sortableContainers(report.Skipped))
|
||||
sort.Sort(sortableContainers(report.Stale))
|
||||
sort.Sort(sortableContainers(report.Fresh))
|
||||
|
||||
return report
|
||||
}
|
||||
|
||||
type sortableContainers []types.ContainerReport
|
||||
type sortableContainers []*ContainerStatus
|
||||
|
||||
// Len implements sort.Interface.Len
|
||||
func (s sortableContainers) Len() int { return len(s) }
|
||||
|
||||
// Less implements sort.Interface.Less
|
||||
func (s sortableContainers) Less(i, j int) bool { return s[i].ID() < s[j].ID() }
|
||||
func (s sortableContainers) Less(i, j int) bool { return s[i].ID < s[j].ID }
|
||||
|
||||
// Swap implements sort.Interface.Swap
|
||||
func (s sortableContainers) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
|
|
|||
24
pkg/session/session.go
Normal file
24
pkg/session/session.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
Trigger Trigger
|
||||
Started time.Time
|
||||
Progress Progress
|
||||
}
|
||||
|
||||
func New(trigger Trigger) *Session {
|
||||
return &Session{
|
||||
Started: time.Now().UTC(),
|
||||
Trigger: trigger,
|
||||
Progress: Progress{},
|
||||
}
|
||||
}
|
||||
|
||||
// Report creates a new Report from a Session instance
|
||||
func (s Session) Report() *Report {
|
||||
return NewReport(s.Started, time.Now().UTC(), s.Trigger, s.Progress)
|
||||
}
|
||||
34
pkg/session/trigger.go
Normal file
34
pkg/session/trigger.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package session
|
||||
|
||||
import "strings"
|
||||
|
||||
type Trigger int
|
||||
|
||||
const (
|
||||
SchedulerTrigger Trigger = iota
|
||||
APITrigger
|
||||
StartupTrigger
|
||||
)
|
||||
|
||||
// String returns a string representation of the Trigger
|
||||
func (trigger Trigger) String() string {
|
||||
switch trigger {
|
||||
case SchedulerTrigger:
|
||||
return "Scheduler"
|
||||
case APITrigger:
|
||||
return "API"
|
||||
case StartupTrigger:
|
||||
return "Startup"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON marshals Trigger as a quoted string
|
||||
func (trigger Trigger) MarshalJSON() ([]byte, error) {
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString(`"`)
|
||||
sb.WriteString(trigger.String())
|
||||
sb.WriteString(`"`)
|
||||
return []byte(sb.String()), nil
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
package types
|
||||
|
||||
// Notifier is the interface that all notification services have in common
|
||||
type Notifier interface {
|
||||
StartNotification()
|
||||
SendNotification(Report)
|
||||
GetNames() []string
|
||||
Close()
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
package types
|
||||
|
||||
// Report contains reports for all the containers processed during a session
|
||||
type Report interface {
|
||||
Scanned() []ContainerReport
|
||||
Updated() []ContainerReport
|
||||
Failed() []ContainerReport
|
||||
Skipped() []ContainerReport
|
||||
Stale() []ContainerReport
|
||||
Fresh() []ContainerReport
|
||||
}
|
||||
|
||||
// ContainerReport represents a container that was included in watchtower session
|
||||
type ContainerReport interface {
|
||||
ID() ContainerID
|
||||
Name() string
|
||||
CurrentImageID() ImageID
|
||||
LatestImageID() ImageID
|
||||
ImageName() string
|
||||
Error() string
|
||||
State() string
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue