containrrr / watchtower

A process for automating Docker container base image updates.
https://containrrr.dev/watchtower/
Apache License 2.0
19.51k stars 862 forks source link

Can't filter logs by level in template (Incompatible types for comparison) #1969

Open OddMagnet opened 6 months ago

OddMagnet commented 6 months ago

Describe the bug

I'm trying to filter the logs in my template, since I don't need the info output.

I tried my template in the preview, there it works / I don't get errors This is my template:

{{- with .Report -}}
  {{len .Skipped}} Skipped, {{len .Scanned}} Scanned, {{len .Updated}} Updated, {{len .Failed}} Failed, {{len .Fresh}} Fresh, {{len .Stale}} Stale
  {{- range .Skipped -}} {{- println -}}
    - {{.State}}: {{.Name}} ({{.ImageName}}): {{.Error}}
  {{- end -}}
  {{- range .Failed -}} {{- println -}}
    - {{.State}}: {{.Name}}: {{.Error}}
  {{- end}}
  {{if (or .Skipped .Failed) -}} {{- println -}} Updates available for: {{- end}} 
  {{- range .Updated -}} {{- println -}}
    - {{.Name}} ({{.ImageName}}): {{.CurrentImageID.ShortID}} → a new image {{.LatestImageID.ShortID}}
  {{- end -}}
{{end}}
{{if (and .Entries .Report) -}} {{- println -}} Logs: {{- end}}
{{range .Entries -}}
  {{- if (ne .Level "info") -}}
    {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
  {{- end}}
{{- end}}

This is the part that causes the bug:

  {{- if (ne .Level "info") -}}
    {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
  {{- end}}

The error I get is: level=fatal msg="Notification template error" error="template: :17:10: executing \"\" at <ne .Level \"InfoLevel\">: error calling ne: incompatible types for comparison"

I also tried it like this:

  {{- if (ne .Level InfoLevel) -}}
    {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
  {{- end}}

Which gives me the error: level=error msg="Could not use configured notification template: template: :17: function \"InfoLevel\" not defined. Using default template"

Steps to reproduce

  1. Add the above template to the environment of your watchtower compose file:

    watchtower:
    container_name: watchtower
    image: containrrr/watchtower:latest
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
    environment:
      - WATCHTOWER_SCHEDULE=0 */2 * * * *   # For Debugging: check for updates every 2 minutes
      - WATCHTOWER_MONITOR_ONLY=true
      - WATCHTOWER_NOTIFICATION_URL=discord: ...
      - WATCHTOWER_NOTIFICATION_REPORT=true
      - | 
        WATCHTOWER_NOTIFICATION_TEMPLATE=
        {{- with .Report -}}
          {{len .Stale}} Stale, {{len .Skipped}} Skipped, {{len .Failed}} Failed
          {{- range .Skipped -}} {{- println -}}
            - {{.State}}: {{.Name}} ({{.ImageName}}): {{.Error}}
          {{- end -}}
          {{- range .Failed -}} {{- println -}}
            - {{.State}}: {{.Name}}: {{.Error}}
          {{- end}}
          {{if (or .Skipped .Failed) -}} {{- println -}} Updates available for: {{- end}} 
          {{- range .Stale -}} {{- println -}}
            - {{.Name}}: {{.CurrentImageID.ShortID}} has a new image {{.LatestImageID.ShortID}}
          {{- end -}}
        {{end}}
        {{if (and .Entries .Report) -}} {{- println -}} Logs: {{- end}}
        {{range .Entries -}}
          {{- if (ne .Level "info") -}}
            {{.Time.Format `02 Jan 06 15:04 MST`}} [{{printf `%7.7s` .Level}}] {{.Message}}{{println}}
          {{- end}}
        {{- end}}
  2. try to run Watchtower

Expected behavior

I'd expect either the preview on the website to give me the same error, or the Watchtower container to run with my template

Screenshots

No response

Environment

Your logs

With {{- if (ne .Level "info") -}}

time="2024-04-26T20:22:47+02:00" level=fatal msg="Notification template error" error="template: :17:10: executing \"\" at <ne .Level \"INFO\">: error calling ne: incompatible types for comparison" notify=no

With {{- if (ne .Level InfoLevel) -}}:

time="2024-04-26T20:17:31+02:00" level=error msg="Could not use configured notification template: template: :17: function \"InfoLevel\" not defined. Using default template"
github-actions[bot] commented 6 months ago

Hi there! 👋🏼 As you're new to this repo, we'd like to suggest that you read our code of conduct as well as our contribution guidelines. Thanks a bunch for opening your first issue! 🙏