在Kubernetes中,”delete”是否幂等?

huangapple go评论42阅读模式
英文:

Is delete in Kubernetes idempotent

问题

调用 Kubernetes Go API 中的 Delete 方法是否是幂等操作,即可以安全地调用两次?
如果是,是否有任何定义这一属性的文档?
Go 代码只是说明

Delete deletes the given obj from Kubernetes cluster.

本质上,这个说明与在查看代码时人们所期望的一致。

英文:

Is calling Delete in the Kubernetes go API an idempotent operation, i.e. can it safely be called twice?
If so, is there any documentation defining this property?
The go code just states
> Delete deletes the given obj from Kubernetes cluster.

Essentially, this statement is what one would expect anyway when looking at the code.

答案1

得分: 2

API服务基于HTTP,所以你可以查看RFC 7231了解相关信息。

> 如果使用某个请求方法进行多次相同请求,其对服务器的预期影响与单个请求的影响相同,则称该请求方法为“幂等”。根据本规范定义的请求方法中,PUT、DELETE以及安全请求方法都是幂等的。

如果你使用kubectl,在第二次运行时删除命令会失败,因为资源找不到。你可以通过使用--ignore-not-found标志来防止它失败。

$ kubectl run nginx --image nginx
pod/nginx created
$ kubectl delete pod nginx
pod "nginx" deleted 
$ kubectl delete pod nginx
Error from server (NotFound): pods "nginx" not found
$ kubectl delete pod nginx --ignore-not-found

因此,在服务器端它是幂等的,但在客户端不是。

英文:

The api service is based on http, so you can check RFC 7231 about that

> A request method is considered "idempotent" if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request. Of the request methods defined by this specification, PUT, DELETE, and safe request methods are idempotent.

If your're using kubectl the delete command will fail on the second run, because the resource can not be found. You can prevent it from failing by using the --ignore-not-found flag.

$ kubectl run nginx --image nginx
pod/nginx created
$ kubectl delete pod nginx
pod "nginx" deleted 
$ kubectl delete pod nginx
Error from server (NotFound): pods "nginx" not found
$ kubectl delete pod nginx --ignore-not-found

So it's idempotent on the server but not on the client.

huangapple
  • 本文由 发表于 2023年2月6日 20:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定