| 352 | } |
| 353 | |
| 354 | func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config *container.Config, hostConfig *container.HostConfig) (*container.Config, *container.HostConfig, error) { |
| 355 | logger := common.Logger(ctx) |
| 356 | input := cr.input |
| 357 | |
| 358 | if input.Options == "" { |
| 359 | return config, hostConfig, nil |
| 360 | } |
| 361 | |
| 362 | // parse configuration from CLI container.options |
| 363 | flags := pflag.NewFlagSet("container_flags", pflag.ContinueOnError) |
| 364 | copts := addFlags(flags) |
| 365 | |
| 366 | optionsArgs, err := shellquote.Split(input.Options) |
| 367 | if err != nil { |
| 368 | return nil, nil, fmt.Errorf("Cannot split container options: '%s': '%w'", input.Options, err) |
| 369 | } |
| 370 | |
| 371 | err = flags.Parse(optionsArgs) |
| 372 | if err != nil { |
| 373 | return nil, nil, fmt.Errorf("Cannot parse container options: '%s': '%w'", input.Options, err) |
| 374 | } |
| 375 | |
| 376 | if len(copts.netMode.Value()) == 0 { |
| 377 | if err = copts.netMode.Set(cr.input.NetworkMode); err != nil { |
| 378 | return nil, nil, fmt.Errorf("Cannot parse networkmode=%s. This is an internal error and should not happen: '%w'", cr.input.NetworkMode, err) |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | containerConfig, err := parse(flags, copts, runtime.GOOS) |
| 383 | if err != nil { |
| 384 | return nil, nil, fmt.Errorf("Cannot process container options: '%s': '%w'", input.Options, err) |
| 385 | } |
| 386 | |
| 387 | logger.Debugf("Custom container.Config from options ==> %+v", containerConfig.Config) |
| 388 | |
| 389 | err = mergo.Merge(config, containerConfig.Config, mergo.WithOverride) |
| 390 | if err != nil { |
| 391 | return nil, nil, fmt.Errorf("Cannot merge container.Config options: '%s': '%w'", input.Options, err) |
| 392 | } |
| 393 | logger.Debugf("Merged container.Config ==> %+v", config) |
| 394 | |
| 395 | logger.Debugf("Custom container.HostConfig from options ==> %+v", containerConfig.HostConfig) |
| 396 | |
| 397 | hostConfig.Binds = append(hostConfig.Binds, containerConfig.HostConfig.Binds...) |
| 398 | hostConfig.Mounts = append(hostConfig.Mounts, containerConfig.HostConfig.Mounts...) |
| 399 | binds := hostConfig.Binds |
| 400 | mounts := hostConfig.Mounts |
| 401 | err = mergo.Merge(hostConfig, containerConfig.HostConfig, mergo.WithOverride) |
| 402 | if err != nil { |
| 403 | return nil, nil, fmt.Errorf("Cannot merge container.HostConfig options: '%s': '%w'", input.Options, err) |
| 404 | } |
| 405 | hostConfig.Binds = binds |
| 406 | hostConfig.Mounts = mounts |
| 407 | logger.Debugf("Merged container.HostConfig ==> %+v", hostConfig) |
| 408 | |
| 409 | return config, hostConfig, nil |
| 410 | } |
| 411 | |