Patches operation 'add' cannot add new keys as show in the JsonPatch example
Expected behavior
According to the patches documentation, we can use JsonPatches in profiles. According to the example in the JsonPatch documentation, it should be possible to add new keys to an object by using the add operation, simply by specifying the path to the key and the value I wanted to associate with it.
Actual behavior
Skaffold outputs the following:
parsing skaffold config:
failed to apply profiles to config "test" defined in file "/Users/gikkman/Development/test-buildargs/skaffold.yaml":
applying profile "load": invalid path: /build/artifacts/0/docker/buildArgs/profile.
There's an issue with one of the profiles defined in config "test" in file "/Users/gikkman/Development/test-buildargs/skaffold.yaml"; refer to the documentation on how to author valid profiles: https://skaffold.dev/docs/environment/profiles/.
Motivation
In my case, I want several profiles that add elements to the build.artifacts.0.docker.buildArgs object, so I can utilize multiple profiles to add the necessary buildArgs without having to write out all combinations I plan to use. It is possible to set a key with an empty value under buildArgs for each key I would like to patch, but according to the documentation it should be possible to add entirely new key-value pairs using add by just specifying the path where you would like to add the new key, and supply a value (see the add options in the JsonPatch example)
If this is not intended to be possible, I think the documentation should be updated to reflect that, so the next person don't have to spend as much time to experiment with this as I have :-)
Information
- Skaffold version: v1.23.0
- Operating system: macOS Catalina 10.15.7
- Installed via: Homebrew
- Contents of skaffold.yaml and Dockerfile:
Files
skaffold.yaml
apiVersion: skaffold/v2alpha3
kind: Config
metadata:
name: test
build:
artifacts:
- image: test-stuff
docker:
dockerfile: Dockerfile
buildArgs:
profiles:
- name: load
patches:
- op: add
path: /build/artifacts/0/docker/buildArgs/profile
value: "load"
- name: dev
patches:
- op: add
path: /build/artifacts/0/docker/buildArgs/build_env
value: "dev"
Dockerfile
FROM busybox
COPY . .
ARG build_env=<<<NONE>>>
RUN echo $build_env
ARG profile=<<<NONE>>>
RUN echo $profile
Run command:
skaffold build -p load,dev
hi @Gikkman , thanks for opening this issue! It seems the yaml-patch behavior here is pretty strange. It seems you can only reference paths like how you're doing if there is some preloaded data. For example:
apiVersion: skaffold/v2beta15
kind: Config
metadata:
name: test
build:
artifacts:
- image: test-stuff
docker:
dockerfile: Dockerfile
buildArgs: {"foo": "bar"}
profiles:
- name: load
patches:
- op: add
path: /build/artifacts/0/docker/buildArgs/profile
value: "load"
- name: dev
patches:
- op: add
path: /build/artifacts/0/docker/buildArgs/build
value: "dev"
seems to work, however there's then some extra values you won't want. Perhaps you can have some default value then use the replace op in your profile?
Hi Marlon, thank you for the reply. Yes, as a work around, your suggested approach works.
It is even possible to add empty values (or , well, default values if you have any):
build:
artifacts:
- image: test-stuff
docker:
dockerfile: Dockerfile
buildArgs: {"foo": "", "bar": ""}
Doing it like this, then you can use both the add and the replace operations (add seems to work the same as replace in these cases).
Another way you can do it I've noticed is by adding the entire node I've noticed:
patches:
- op: add
path: /build/artifacts/0/docker/buildArgs
value:
profile: load
The problem with this approach is that using two adds on the same node (in this case, buildArgs) is that they replace one another so this didn't work in my case.
I'm not completely sure, I'd have to do some more searching into the yaml-patch library, but unfortunately I don't have any super helpful answers for now. To anyone who comes across this issue, PRs welcome!
RFC 6902 has been around forever, can we get this fixed?
I looked into why this happens. The yaml-patch library handles add of a new key correctly (tested with v0.0.10 on its own). The problem is that applyProfile re-marshals the parsed SkaffoldConfig before patching (profiles.go#L273). Because fields like buildArgs and releases are omitempty, an empty buildArgs: {} or releases: [] is dropped, so the patch's parent path no longer exists and add fails with "invalid path". The same cause explains #8637 (appending with /- to an empty list).
I'd like to fix this. Two options:
- Patch the original YAML node instead of the re-marshaled struct, so empty maps and lists the user wrote are kept.
- For
addoperations only, create a missing parent map or list when its schema type is a map or list.
Option 1 is more faithful to JSON Patch; option 2 is smaller. Would you accept a PR, and which approach do you prefer?