0

I'm trying to determine internal IP of a docker container and use that as an value in an yaml config file. The command for determining the IP is;

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' controller-control-plane

And, for editing the config in place, I'm using;

yq -i '.kubeslice.controller.endpoint = "https://<output_from_previous_command>:6443"'  values.yaml

Now, if I want to use the 1st command as an argument like this;

yq '.kubeslice.controller.endpoint = "https://$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' controller-control-plane):6443"'  values.yaml

Then the output is not coming as intended.

kubeslice:
  controller:
    loglevel: info
    rbacResourcePrefix: kubeslice-rbac
    projectnsPrefix: kubeslice
    endpoint: https://$(docker inspect -f {{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}} controller-control-plane):6443

How to do this correctly so that end point gets the correct IP value instead of the command string?

Kusalananda
  • 333,661

3 Answers3

1

Assuming you have to use double quotes in the x = "y" you passed to yq:

yq -i ".kubeslice.controller.endpoint = \"https://$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' controller-control-plane):6443\"" values.yaml

Tom Yan
  • 731
1

The main issue is that you are trying to use a single-quoted string and a command substitution within a single-quoted string. A single-quoted string can't contain single quotes, and substitutions, in general, are ignored within single-quoted strings.

A follow-on issue is that you seem to want to inject the result of the command substitution directly into the YAML that you are modifying. This is inappropriate as the data may need to be quoted and/or encoded.

Instead, assuming you are using Andrey Kislyuk's yq from https://kislyuk.github.io/yq/, extract the data with your docker inspect command and initialize an internal variable that yq may use to set the value of the endpoint URL.

yq -y -i --arg addr "$(
    docker inspect \
        -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
        controller-control-plane
    )" \
    '.kubeslice.controller.endpoint = "https://" + $addr + ":6443"' values.yaml

If you're using Mike Farah's yq, you may instead round-trip via jq to properly insert the data:

yq -o json values.yaml |
jq --arg addr "$(
    docker inspect \
        -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
        controller-control-plane
    )" \
    '.kubeslice.controller.endpoint = "https://" + $addr + ":6443"' |
yq -P -o yaml >values.yaml.new &&
mv values.yaml.new values.yaml

This uses Mike's yq just for converting the YAML to and from JSON. Any other program that can do this could be used for this, for example, Stephen Levine's yj.

Kusalananda
  • 333,661
0

We can use xargs to forward the output from the 1st command as an argument for 2nd command that edits the config.

docker inspect -f \
'{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
controller-control-plane | xargs -I{} yq \
'.kubeslice.controller.endpoint = "https://{}:6443"' values.yaml

output:

kubeslice:
  controller:
    loglevel: info
    rbacResourcePrefix: kubeslice-rbac
    projectnsPrefix: kubeslice
    endpoint: https://172.18.0.3:6443