Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubectl wait --for=jsonpath parse #1224

Open
hholst80 opened this issue Jun 2, 2022 · 9 comments
Open

kubectl wait --for=jsonpath parse #1224

hholst80 opened this issue Jun 2, 2022 · 9 comments
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@hholst80
Copy link

hholst80 commented Jun 2, 2022

Background:

kubectl does not offer a direct way to wait for multiple signals.

https://stackoverflow.com/questions/55073453/wait-for-kubernetes-job-to-complete-on-either-failure-success-using-command-line

Workaround attempt:

Use jsonpath

❯ kubectl create job test --image=ubuntu:20.04 -- false
❯ kubectl wait '--for=jsonpath={.status.conditions[?(@.type=="Failed"||@.type=="Complete")].status}="True"'  job/test
error: jsonpath wait format must be --for=jsonpath='{.status.readyReplicas}'=3

However, this blew up, as seen above, in an unexpected way.

Side note this will likely not work anyway as a workaround to the original issue, since the output of a jsonpath might be many paths matching, it would have been easier to just have --for=..accepted many times and use the condition form.

@hholst80 hholst80 added the kind/bug Categorizes issue or PR as related to a bug. label Jun 2, 2022
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jun 2, 2022
@mpuckett159
Copy link
Contributor

Something to note is that kubectl uses a fairly basic implementation of the jsonpath spec. You can find out more about the specifics of it here. Specifically we do not support the || operator here. Unfortunately I believe that currently to support this specific use case you would need to use a shell function to wait on two separate fields like that.

To support this we would need to implement the usage of multiple -for flags, which should be doable, as well as support using contexts or goroutines to wait for these conditions in parallel, and finally a flag to specify whether to wait for all the conditions passed or only one of them.

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 22, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 20, 2022
@mpuckett159
Copy link
Contributor

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 21, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 20, 2022
@mpuckett159
Copy link
Contributor

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 20, 2022
@tommie
Copy link

tommie commented May 24, 2023

Even without ||, using ?(x==y) fails to parse, due to this split on =:

splitStr := strings.Split(condition, "=")

The jsonpath/ docs suggests this syntax should work.

I propose a simple improvement: Separate the splitting on equal sign into two phases:

  1. Remove the jsonpath= prefix.
  2. Do a single last-occurrence-split on = to separate the expression and condition value.

This would improve the situation for the case above, but would e.g. still not allow values with = in them.

A nicer solution would be to match curly brackets, but since RelaxedJSONPathExpression doesn't require them, that would also be prone to gotchas, AFAICT.

@nouaman
Copy link

nouaman commented May 25, 2023

A work around for a job is to use this version :

kubectl wait job job-test  --for jsonpath='{.status.conditions[*].status}'=True -o jsonpath='{.status.conditions[*].type}

This will return if the job is complete or failed.
It need the version 1.26 and higher to support missing fields.

andreaskaris added a commit to andreaskaris/kubectl that referenced this issue Jun 20, 2023
Make it possible to parse jsonpath filter expressions: After splitting
the condition for jsonpath, drop the first element containing the
prefix, use the last element as the condition and join all middle
elements as the expression.

Reported-at: kubernetes#1224
Signed-off-by: Andreas Karis <ak.karis@gmail.com>
andreaskaris added a commit to andreaskaris/kubernetes that referenced this issue Jun 21, 2023
Make it possible to parse jsonpath filter expressions: After splitting
the condition for jsonpath, drop the first element containing the
prefix, use the last element as the condition and join all middle
elements as the expression.

Reported-at: kubernetes/kubectl#1224
Signed-off-by: Andreas Karis <ak.karis@gmail.com>
andreaskaris added a commit to andreaskaris/kubernetes that referenced this issue Jun 21, 2023
Implement support for multiple --for statements in wait. By default,
multiple --for conditions are OR'ed. With the optional --for-all
flag, they can be AND'ed.

Fixes: kubernetes/kubectl#1224
Reported-at: kubernetes/kubectl#1224
Signed-off-by: Andreas Karis <ak.karis@gmail.com>
andreaskaris added a commit to andreaskaris/kubernetes that referenced this issue Jul 8, 2023
Make it possible to parse jsonpath filter expressions: Split
jsonpath expressions on single '=' only and leave '==' as part of the
string.

Reported-at: kubernetes/kubectl#1224
Signed-off-by: Andreas Karis <ak.karis@gmail.com>
andreaskaris added a commit to andreaskaris/kubernetes that referenced this issue Jul 8, 2023
Implement support for multiple --for statements in wait. By default,
multiple --for conditions are OR'ed. With the optional --for-all
flag, they can be AND'ed.

Fixes: kubernetes/kubectl#1224
Reported-at: kubernetes/kubectl#1224
Signed-off-by: Andreas Karis <ak.karis@gmail.com>
@ssuriyan7
Copy link

/assign

@luisdavim
Copy link

luisdavim commented Apr 22, 2024

another case that is not working is the following:

kubectl wait --for jsonpath='{.status.readyReplicas}={.spec.replicas}' KubeadmControlPlane -n vmsp-platform management-7qck2-8p7t6 -n vmsp-platform --timeout=20m

however, this works:

$ kubectl get KubeadmControlPlane -n vmsp-platform management-7qck2-8p7t6 -o jsonpath='{.status.readyReplicas}={.spec.replicas}'
3=3

and this also works:

$ kubectl wait --for jsonpath='{.status.readyReplicas}'=3 KubeadmControlPlane -n vmsp-platform management-7qck2-8p7t6 -n vmsp-platform --timeout=20m
kubeadmcontrolplane.controlplane.cluster.x-k8s.io/management-7qck2-8p7t6 condition met

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants