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 substitution with non-empty env-var #1392

Merged

Conversation

thaJeztah
Copy link
Member

fixes #1391

Due to a typo introduced in #1249, substitution would not work if the given environment-variable was set.

Given the following docker compose file;

version: "3.7"

services:
  app:
    image: nginx:${version:-latest}

Deploying a stack with $version set would ignore the $version
environment variable, and use the default value instead;

version=alpine docker stack deploy -c docker-compose.yml foobar

Creating network foobar_default
Creating service foobar_app

docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
rskkjxe6sm0w        foobar_app          replicated          1/1                 nginx:latest

@thaJeztah
Copy link
Member Author

This should be cherry-picked in the 18.09 branch as well

ping @anokun7 @vdemeester @silvin-lubecki PTAL

Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

LGTM 🐸
(but test is red ?)

@thaJeztah
Copy link
Member Author

Oh, interesting; I tried my new test, but the existing ones broke 🤔 let me check why

@thaJeztah
Copy link
Member Author

Looks like the "soft default" doesn't actually check for empty values, so that's an existing bug as well (which went unnoticed because we never reached that part).

I'll have a look after dinner 😅

Due to a typo, substitution would not work if the given
environment-variable was set.

Given the following docker compose file;

```yaml
version: "3.7"

services:
  app:
    image: nginx:${version:-latest}
```

Deploying a stack with `$version` set would ignore the `$version`
environment variable, and use the default value instead;

```bash
version=alpine docker stack deploy -c docker-compose.yml foobar

Creating network foobar_default
Creating service foobar_app

docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
rskkjxe6sm0w        foobar_app          replicated          1/1                 nginx:latest
```

This patch also fixes "soft default" not detecting empty environment variables,
only non-set environment variables.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the fix_substitution_with_non_empty_value branch from 4e1d8ad to ec3daea Compare September 25, 2018 20:20
@codecov-io
Copy link

Codecov Report

Merging #1392 into master will increase coverage by 0.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master    #1392      +/-   ##
==========================================
+ Coverage   54.88%   54.89%   +0.01%     
==========================================
  Files         293      293              
  Lines       19428    19434       +6     
==========================================
+ Hits        10663    10669       +6     
  Misses       8089     8089              
  Partials      676      676

@thaJeztah
Copy link
Member Author

Fixed; should be good to go now

@@ -176,15 +176,21 @@ func extractVariable(value interface{}, pattern *regexp.Regexp) ([]extractedValu

// Soft default (fall back if unset or empty)
func softDefault(substitution string, mapping Mapping) (string, bool, error) {
return withDefault(substitution, mapping, "-:")
sep := ":-"
if !strings.Contains(substitution, sep) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it can be factorized with hardDefault but I don't see an obvious and/or clean way to do it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I was looking at that, but adding an (e.g.) checkIfSet func(val) bool argument felt like over engineering just to prevent just a few lines of duplicated code.

Copy link
Contributor

@silvin-lubecki silvin-lubecki left a comment

Choose a reason for hiding this comment

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

LGTM

@silvin-lubecki silvin-lubecki merged commit 23a8b6c into docker:master Sep 26, 2018
@GordonTheTurtle GordonTheTurtle added this to the 19.03.0 milestone Sep 26, 2018
@thaJeztah thaJeztah deleted the fix_substitution_with_non_empty_value branch September 26, 2018 13:18
lifubang pushed a commit to lifubang/cli that referenced this pull request Oct 12, 2018
…on_empty_value

Fix substitution with non-empty env-var

Signed-off-by: Lifubang <lifubang@acmcoder.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Regression] Environment variables are ignored during stack deployment
5 participants