2017-05-08 13:36:04 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-05-08 13:36:04 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-05-12 07:18:48 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2022-08-27 08:49:24 -04:00
|
|
|
"github.com/moby/sys/sequential"
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-03-05 17:26:42 -05:00
|
|
|
// CreateOptions specifies some options that are used when creating a config.
|
|
|
|
type CreateOptions struct {
|
|
|
|
Name string
|
|
|
|
TemplateDriver string
|
|
|
|
File string
|
|
|
|
Labels opts.ListOpts
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
|
2019-03-05 17:26:42 -05:00
|
|
|
createOpts := CreateOptions{
|
Fix labels copying value from environment variables
This patch fixes a bug where labels use the same behavior as `--env`, resulting
in a value to be copied from environment variables with the same name as the
label if no value is set (i.e. a simple key, no `=` sign, no value).
An earlier pull request addressed similar cases for `docker run`;
2b17f4c8a8caad552025edb05a73db683fb8a5c6, but this did not address the
same situation for (e.g.) `docker service create`.
Digging in history for this bug, I found that use of the `ValidateEnv`
function for labels was added in the original implementation of the labels feature in
https://github.com/docker/docker/commit/abb5e9a0777469e64fe2c7ecfa66ea01083d2071#diff-ae476143d40e21ac0918630f7365ed3cR34
However, the design never intended it to expand environment variables,
and use of this function was either due to either a "copy/paste" of the
equivalent `--env` flags, or a misunderstanding (the name `ValidateEnv` does
not communicate that it also expands environment variables), and the existing
`ValidateLabel` was designed for _engine_ labels (which required a value to
be set).
Following the initial implementation, other parts of the code followed
the same (incorrect) approach, therefore leading the bug to be introduced
in services as well.
This patch:
- updates the `ValidateLabel` to match the expected validation
rules (this function is no longer used since 31dc5c0a9a8bdc11c7ad335aebb753ed527caa5a),
and the daemon has its own implementation)
- corrects various locations in the code where `ValidateEnv` was used instead of `ValidateLabel`.
Before this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker service inspect --format '{{json .Spec.Labels}}' test
{"SOME_ENV_VAR":"I_AM_SOME_ENV_VAR"}
```
After this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker container inspect --format '{{json .Config.Labels}}' test
{"SOME_ENV_VAR":""}
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-13 10:47:30 -05:00
|
|
|
Labels: opts.NewListOpts(opts.ValidateLabel),
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create [OPTIONS] CONFIG file|-",
|
2018-01-04 16:17:56 -05:00
|
|
|
Short: "Create a config from a file or STDIN",
|
2017-05-08 13:36:04 -04:00
|
|
|
Args: cli.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-03-05 17:26:42 -05:00
|
|
|
createOpts.Name = args[0]
|
|
|
|
createOpts.File = args[1]
|
2023-09-09 18:27:44 -04:00
|
|
|
return RunConfigCreate(cmd.Context(), dockerCli, createOpts)
|
2017-05-08 13:36:04 -04:00
|
|
|
},
|
2022-05-12 07:18:48 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2019-03-05 17:26:42 -05:00
|
|
|
flags.VarP(&createOpts.Labels, "label", "l", "Config labels")
|
|
|
|
flags.StringVar(&createOpts.TemplateDriver, "template-driver", "", "Template driver")
|
2019-03-21 11:19:06 -04:00
|
|
|
flags.SetAnnotation("template-driver", "version", []string{"1.37"})
|
2017-05-08 13:36:04 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-03-05 17:26:42 -05:00
|
|
|
// RunConfigCreate creates a config with the given options.
|
2023-09-09 18:27:44 -04:00
|
|
|
func RunConfigCreate(ctx context.Context, dockerCli command.Cli, options CreateOptions) error {
|
2017-05-08 13:36:04 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
var in io.Reader = dockerCli.In()
|
2019-03-05 17:26:42 -05:00
|
|
|
if options.File != "-" {
|
2022-08-27 08:49:24 -04:00
|
|
|
file, err := sequential.Open(options.File)
|
2017-05-08 13:36:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
in = file
|
|
|
|
defer file.Close()
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:04:43 -05:00
|
|
|
configData, err := io.ReadAll(in)
|
2017-05-08 13:36:04 -04:00
|
|
|
if err != nil {
|
2019-03-05 17:26:42 -05:00
|
|
|
return errors.Errorf("Error reading content from %q: %v", options.File, err)
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
spec := swarm.ConfigSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
2019-03-05 17:26:42 -05:00
|
|
|
Name: options.Name,
|
|
|
|
Labels: opts.ConvertKVStringsToMap(options.Labels.GetAll()),
|
2017-05-08 13:36:04 -04:00
|
|
|
},
|
|
|
|
Data: configData,
|
|
|
|
}
|
2019-03-05 17:26:42 -05:00
|
|
|
if options.TemplateDriver != "" {
|
2018-02-21 13:45:34 -05:00
|
|
|
spec.Templating = &swarm.Driver{
|
2019-03-05 17:26:42 -05:00
|
|
|
Name: options.TemplateDriver,
|
2018-02-21 13:45:34 -05:00
|
|
|
}
|
|
|
|
}
|
2017-05-08 13:36:04 -04:00
|
|
|
r, err := client.ConfigCreate(ctx, spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), r.ID)
|
|
|
|
return nil
|
|
|
|
}
|