mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01:00
feat: include additional info in startup (#809)
This commit is contained in:
parent
5e17ef6014
commit
9fa2fd82a6
7 changed files with 185 additions and 55 deletions
|
|
@ -1,6 +1,9 @@
|
|||
package filters
|
||||
|
||||
import t "github.com/containrrr/watchtower/pkg/types"
|
||||
import (
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// WatchtowerContainersFilter filters only watchtower containers
|
||||
func WatchtowerContainersFilter(c t.FilterableContainer) bool { return c.IsWatchtower() }
|
||||
|
|
@ -68,19 +71,45 @@ func FilterByScope(scope string, baseFilter t.Filter) t.Filter {
|
|||
}
|
||||
|
||||
// BuildFilter creates the needed filter of containers
|
||||
func BuildFilter(names []string, enableLabel bool, scope string) t.Filter {
|
||||
func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, string) {
|
||||
sb := strings.Builder{}
|
||||
filter := NoFilter
|
||||
filter = FilterByNames(names, filter)
|
||||
|
||||
if len(names) > 0 {
|
||||
sb.WriteString("with name \"")
|
||||
for i, n := range names {
|
||||
sb.WriteString(n)
|
||||
if i < len(names)-1 {
|
||||
sb.WriteString(`" or "`)
|
||||
}
|
||||
}
|
||||
sb.WriteString(`", `)
|
||||
}
|
||||
|
||||
if enableLabel {
|
||||
// If label filtering is enabled, containers should only be considered
|
||||
// if the label is specifically set.
|
||||
filter = FilterByEnableLabel(filter)
|
||||
sb.WriteString("using enable label, ")
|
||||
}
|
||||
if scope != "" {
|
||||
// If a scope has been defined, containers should only be considered
|
||||
// if the scope is specifically set.
|
||||
filter = FilterByScope(scope, filter)
|
||||
sb.WriteString(`in scope "`)
|
||||
sb.WriteString(scope)
|
||||
sb.WriteString(`", `)
|
||||
}
|
||||
filter = FilterByDisabledLabel(filter)
|
||||
return filter
|
||||
|
||||
filterDesc := "Checking all containers (except explicitly disabled with label)"
|
||||
if sb.Len() > 0 {
|
||||
filterDesc = "Only checking containers " + sb.String()
|
||||
|
||||
// Remove the last ", "
|
||||
filterDesc = filterDesc[:len(filterDesc)-2]
|
||||
}
|
||||
|
||||
return filter, filterDesc
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,7 +114,8 @@ func TestBuildFilter(t *testing.T) {
|
|||
var names []string
|
||||
names = append(names, "test")
|
||||
|
||||
filter := BuildFilter(names, false, "")
|
||||
filter, desc := BuildFilter(names, false, "")
|
||||
assert.Contains(t, desc, "test")
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
container.On("Name").Return("Invalid")
|
||||
|
|
@ -150,7 +151,8 @@ func TestBuildFilterEnableLabel(t *testing.T) {
|
|||
var names []string
|
||||
names = append(names, "test")
|
||||
|
||||
filter := BuildFilter(names, true, "")
|
||||
filter, desc := BuildFilter(names, true, "")
|
||||
assert.Contains(t, desc, "using enable label")
|
||||
|
||||
container := new(mocks.FilterableContainer)
|
||||
container.On("Enabled").Return(false, false)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Notifier can send log output as notification to admins, with optional batching.
|
||||
|
|
@ -42,6 +43,26 @@ func NewNotifier(c *cobra.Command) *Notifier {
|
|||
return n
|
||||
}
|
||||
|
||||
func (n *Notifier) String() string {
|
||||
if len(n.types) < 1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
sb := strings.Builder{}
|
||||
for _, notif := range n.types {
|
||||
for _, name := range notif.GetNames() {
|
||||
sb.WriteString(name)
|
||||
sb.WriteString(", ")
|
||||
}
|
||||
}
|
||||
names := sb.String()
|
||||
|
||||
// remove the last separator
|
||||
names = names[:len(names)-2]
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// getNotificationTypes produces an array of notifiers from a list of types
|
||||
func (n *Notifier) getNotificationTypes(cmd *cobra.Command, levels []log.Level, types []string) []ty.Notifier {
|
||||
output := make([]ty.Notifier, 0)
|
||||
|
|
|
|||
|
|
@ -33,16 +33,29 @@ type shoutrrrTypeNotifier struct {
|
|||
done chan bool
|
||||
}
|
||||
|
||||
func (n *shoutrrrTypeNotifier) GetNames() []string {
|
||||
names := make([]string, len(n.Urls))
|
||||
for i, u := range n.Urls {
|
||||
schemeEnd := strings.Index(u, ":")
|
||||
if schemeEnd <= 0 {
|
||||
names[i] = "invalid"
|
||||
continue
|
||||
}
|
||||
names[i] = u[:schemeEnd]
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func newShoutrrrNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier {
|
||||
flags := c.PersistentFlags()
|
||||
urls, _ := flags.GetStringArray("notification-url")
|
||||
template := getShoutrrrTemplate(c)
|
||||
return createSender(urls, acceptedLogLevels, template)
|
||||
tpl := getShoutrrrTemplate(c)
|
||||
return createSender(urls, acceptedLogLevels, tpl)
|
||||
}
|
||||
|
||||
func newShoutrrrNotifierFromURL(c *cobra.Command, url string, levels []log.Level) t.Notifier {
|
||||
template := getShoutrrrTemplate(c)
|
||||
return createSender([]string{url}, levels, template)
|
||||
tpl := getShoutrrrTemplate(c)
|
||||
return createSender([]string{url}, levels, tpl)
|
||||
}
|
||||
|
||||
func createSender(urls []string, levels []log.Level, template *template.Template) t.Notifier {
|
||||
|
|
@ -83,54 +96,54 @@ func sendNotifications(n *shoutrrrTypeNotifier) {
|
|||
n.done <- true
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) buildMessage(entries []*log.Entry) string {
|
||||
func (n *shoutrrrTypeNotifier) buildMessage(entries []*log.Entry) string {
|
||||
var body bytes.Buffer
|
||||
if err := e.template.Execute(&body, entries); err != nil {
|
||||
if err := n.template.Execute(&body, entries); err != nil {
|
||||
fmt.Printf("Failed to execute Shoutrrrr template: %s\n", err.Error())
|
||||
}
|
||||
|
||||
return body.String()
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) sendEntries(entries []*log.Entry) {
|
||||
msg := e.buildMessage(entries)
|
||||
e.messages <- msg
|
||||
func (n *shoutrrrTypeNotifier) sendEntries(entries []*log.Entry) {
|
||||
msg := n.buildMessage(entries)
|
||||
n.messages <- msg
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) StartNotification() {
|
||||
if e.entries == nil {
|
||||
e.entries = make([]*log.Entry, 0, 10)
|
||||
func (n *shoutrrrTypeNotifier) StartNotification() {
|
||||
if n.entries == nil {
|
||||
n.entries = make([]*log.Entry, 0, 10)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) SendNotification() {
|
||||
if e.entries == nil || len(e.entries) <= 0 {
|
||||
func (n *shoutrrrTypeNotifier) SendNotification() {
|
||||
if n.entries == nil || len(n.entries) <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
e.sendEntries(e.entries)
|
||||
e.entries = nil
|
||||
n.sendEntries(n.entries)
|
||||
n.entries = nil
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) Close() {
|
||||
close(e.messages)
|
||||
func (n *shoutrrrTypeNotifier) Close() {
|
||||
close(n.messages)
|
||||
|
||||
// Use fmt so it doesn't trigger another notification.
|
||||
fmt.Println("Waiting for the notification goroutine to finish")
|
||||
|
||||
_ = <-e.done
|
||||
_ = <-n.done
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) Levels() []log.Level {
|
||||
return e.logLevels
|
||||
func (n *shoutrrrTypeNotifier) Levels() []log.Level {
|
||||
return n.logLevels
|
||||
}
|
||||
|
||||
func (e *shoutrrrTypeNotifier) Fire(entry *log.Entry) error {
|
||||
if e.entries != nil {
|
||||
e.entries = append(e.entries, entry)
|
||||
func (n *shoutrrrTypeNotifier) Fire(entry *log.Entry) error {
|
||||
if n.entries != nil {
|
||||
n.entries = append(n.entries, entry)
|
||||
} else {
|
||||
// Log output generated outside a cycle is sent immediately.
|
||||
e.sendEntries([]*log.Entry{entry})
|
||||
n.sendEntries([]*log.Entry{entry})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,6 @@ package types
|
|||
type Notifier interface {
|
||||
StartNotification()
|
||||
SendNotification()
|
||||
GetNames() []string
|
||||
Close()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue