GoogleContainerTools / skaffold

Easy and Repeatable Kubernetes Development
https://skaffold.dev/
Apache License 2.0
15.01k stars 1.62k forks source link

Override via patches doesn't respect index of array #6775

Closed katsew closed 2 years ago

katsew commented 2 years ago

Hi, I'm using skaffold with helm. I have 2 helm releases need to remove when it is a dev environment. I try removing them via patches, but It shows an error applying profile "dev": Unable to remove invalid index: 2.. Here is the patch config that I try to apply.

    patches:
      - op: remove
        path: /deploy/helm/releases/1
      - op: remove
        path: /deploy/helm/releases/2

Then I try the following config.

    patches:
      - op: remove
        path: /deploy/helm/releases/1
      - op: remove
        path: /deploy/helm/releases/1

It works and continue running.

Expected behavior

Respect the index in skaffold.yaml

Actual behavior

It seems that patches mutates object immediately and does not respect the index in skaffold.yaml.

Information

apiVersion: skaffold/v2beta24
kind: Config
metadata:
  name: skaffold-bug-reproduction
build:
  artifacts:
  - image: alpine
    docker:
      dockerfile: Dockerfile
deploy:
  helm:
    releases:
      - name: alpine
        chartPath: "./deploy/alpine"
        createNamespace: true
      - name: prom
        repo: https://prometheus-community.github.io/helm-charts
        remoteChart: prometheus
        createNamespace: true
      - name: grafana
        repo: https://grafana.github.io/helm-charts
        remoteChart: grafana
        createNamespace: true

profiles:
  - name: local
    activation:
      - kubeContext: minikube
    build:
      local:
        push: false
  - name: dev
    patches:
      - op: remove
        path: /deploy/helm/releases/1
      - op: remove
        path: /deploy/helm/releases/2
    activation:
      - kubeContext: gke
    build:
      googleCloudBuild:
        projectId: my-project-id

Steps to reproduce the behavior

  1. use the skaffold.yaml I pasted above
  2. skaffold build -p dev
DEBU[0000] skaffold API not starting as it's not requested  subtask=-1 task=DevLoop
INFO[0000] Skaffold &{Version:v1.33.0 ConfigVersion:skaffold/v2beta24 GitVersion: GitCommit:251c0406b3f9ef149cfa084d912874fd080e1e97 BuildDate:2021-10-07T19:33:12Z GoVersion:go1.14.14 Compiler:gc Platform:darwin/amd64 User:}  subtask=-1 task=DevLoop
INFO[0000] Loaded Skaffold defaults from "/Users/yu-kato/.skaffold/config"  subtask=-1 task=DevLoop
DEBU[0000] parsed 1 configs from configuration file /private/tmp/skaffold-bug/skaffold.yaml  subtask=-1 task=DevLoop
INFO[0000] applying profile: local                       subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field Build  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field artifacts  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field insecureRegistries  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field tagPolicy  subtask=-1 task=DevLoop
INFO[0000] no values found in profile for field TagPolicy, using original config values  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field BuildType  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field Test   subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field Deploy  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field DeployType  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field docker  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field helm   subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kpt    subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kubectl  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kustomize  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field statusCheck  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field statusCheckDeadlineSeconds  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kubeContext  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field logs   subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field prefix  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field -      subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field PortForward  subtask=-1 task=DevLoop
INFO[0000] applying profile: dev                         subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field Build  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field artifacts  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field insecureRegistries  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field tagPolicy  subtask=-1 task=DevLoop
INFO[0000] no values found in profile for field TagPolicy, using original config values  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field BuildType  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field Test   subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field Deploy  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field DeployType  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field docker  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field helm   subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kpt    subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kubectl  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kustomize  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field statusCheck  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field statusCheckDeadlineSeconds  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field kubeContext  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field logs   subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field prefix  subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field -      subtask=-1 task=DevLoop
DEBU[0000] overlaying profile on config for field PortForward  subtask=-1 task=DevLoop
DEBU[0000] Running command: [tput colors]                subtask=-1 task=DevLoop
DEBU[0000] Command output: [256
]                        subtask=-1 task=DevLoop
parsing skaffold config: failed to apply profiles to config "skaffold-bug-reproduction" defined in file "/private/tmp/skaffold-bug/skaffold.yaml": applying profile "dev": Unable to remove invalid index: 2. There's an issue with one of the profiles defined in config "skaffold-bug-reproduction" in file "/private/tmp/skaffold-bug/skaffold.yaml"; refer to the documentation on how to author valid profiles: https://skaffold.dev/docs/environment/profiles/.
katsew commented 2 years ago

According to the doc, my issue is an expected behaviour.

http://jsonpatch.com/

The operations are applied in order: if any of them fail then the whole patch operation should abort.

https://datatracker.ietf.org/doc/html/rfc6902#page-6

If removing an element from an array, any elements above the specified index are shifted one position to the left.

I'll close the issue, sorry for my misunderstanding.