Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken block parsing when label prefix contains dots #610

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Vigilans
Copy link

@Vigilans Vigilans commented May 2, 2024

When CADDY_DOCKER_LABEL_PREFIX contains dots, parsing of blocks will break.

e.g. With CADDY_DOCKER_LABEL_PREFIX=mydomain.gitlab.caddy, cdp will yield error:

[ERROR]  Removing invalid block: Caddyfile:2: unrecognized global option: gitlab\n{\n\tgitlab {\n\t\tcaddy http://redis.gitlab.mydomain {\n\t\t\treverse_proxy 127.0.0.1:5540\n\t\t}\n\t}\n}\n\n

Then wrongly parsed block is:

{
        gitlab {
                caddy http://redis.gitlab.mydomain {
                        reverse_proxy 127.0.0.1:5540
                }
        }
}

It is because gitlab.caddy in mydomain.gitlab.caddy also gets parsed as path.

Using dots in prefix conforms to what docker has adopted in their labels, e.g.:

            "com.docker.compose.project": "...",
            "com.docker.compose.project.config_files": "...",
            "com.docker.compose.project.working_dir": "...",

So using dot-separated string in CADDY_DOCKER_LABEL_PREFIX should be a good way to keep naming style consistent with other labels and should be supported.

This PR fixes it by canonicalizing the label prefix to "caddy", so meta characters in user provided label prefix will not affect cdp's block parsing.

@@ -38,7 +39,7 @@ type CaddyfileGenerator struct {

// CreateGenerator creates a new generator
func CreateGenerator(dockerClients []docker.Client, dockerUtils docker.Utils, options *config.Options) *CaddyfileGenerator {
var labelRegexString = fmt.Sprintf("^%s(_\\d+)?(\\.|$)", options.LabelPrefix)
var labelRegexString = fmt.Sprintf("^%s(_\\d+)?(\\.|$)", regexp.QuoteMeta(options.LabelPrefix))
Copy link
Author

Choose a reason for hiding this comment

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

Here uses regexp.QuoteMeta to escapes all regular expression metacharacters in the user-provided label prefix, to make it a literal text in the labelRegex.

@Vigilans
Copy link
Author

Vigilans commented Aug 29, 2024

@lucaslorentz @francislavoie Ping for a review, I've used it for months and it proves to work usefully.

Use cases:

services:
  cs2-ingress:
    image: .../caddy:latest # with caddy-docker-proxy built
    environment:
      CADDY_DOCKER_LABEL_PREFIX: game.cs2.caddy
    labels:
      game.cs2.caddy: http://
      game.cs2.caddy.handle_path: /fastdl/*
      game.cs2.caddy.handle_path.file_server: browse
      game.cs2.caddy.handle_path.file_server.root: /srv/www


  ds3:
    image: timleonarduk/ds3os:latest
    labels:
      game.ds3.caddy: http://
      game.ds3.caddy.reverse_proxy: 127.0.0.1:50005
services:
  gitlab-postgres-webui:
    image: sosedoff/pgweb:latest
    labels:
      nas.gitlab.caddy: http://postgres.gitlab.mydomain
      nas.gitlab.caddy.reverse_proxy: 127.0.0.1:5433

  harbor-redis-webui:
    image: ghcr.io/joeferner/redis-commander:latest
    labels:
      nas.harbor.caddy: https://redis.harbor.mydomain
      nas.harbor.caddy.import: tls
      nas.harbor.caddy.reverse_proxy: 127.0.0.1:6380

nas.gitlab.caddy prefix is better than nas-gitlab-caddy because it is in consistence with labels like com.docker.compose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant