mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 06:01:50 +00:00
Make updates atomic from the client side.
This commit is contained in:
@@ -192,12 +192,10 @@ func (c *Client) CreatePod(pod api.Pod) (result api.Pod, err error) {
|
||||
|
||||
// UpdatePod takes the representation of a pod to update. Returns the server's representation of the pod, and an error, if it occurs
|
||||
func (c *Client) UpdatePod(pod api.Pod) (result api.Pod, err error) {
|
||||
var prev api.Pod
|
||||
err = c.Get().Path("pod").Path(pod.ID).Do().Into(&prev)
|
||||
if err != nil {
|
||||
if pod.ResourceVersion == 0 {
|
||||
err = fmt.Errorf("invalid update object, missing resource version: %v", pod)
|
||||
return
|
||||
}
|
||||
pod.ResourceVersion = prev.ResourceVersion
|
||||
err = c.Put().Path("pods").Path(pod.ID).Body(pod).Do().Into(&result)
|
||||
return
|
||||
}
|
||||
@@ -222,12 +220,10 @@ func (c *Client) CreateReplicationController(controller api.ReplicationControlle
|
||||
|
||||
// UpdateReplicationController updates an existing replication controller
|
||||
func (c *Client) UpdateReplicationController(controller api.ReplicationController) (result api.ReplicationController, err error) {
|
||||
var prev api.ReplicationController
|
||||
err = c.Get().Path("replicationControllers").Path(controller.ID).Do().Into(&prev)
|
||||
if err != nil {
|
||||
if controller.ResourceVersion == 0 {
|
||||
err = fmt.Errorf("invalid update object, missing resource version: %v", controller)
|
||||
return
|
||||
}
|
||||
controller.ResourceVersion = prev.ResourceVersion
|
||||
err = c.Put().Path("replicationControllers").Path(controller.ID).Body(controller).Do().Into(&result)
|
||||
return
|
||||
}
|
||||
@@ -251,12 +247,10 @@ func (c *Client) CreateService(svc api.Service) (result api.Service, err error)
|
||||
|
||||
// UpdateService updates an existing service.
|
||||
func (c *Client) UpdateService(svc api.Service) (result api.Service, err error) {
|
||||
var prev api.Service
|
||||
err = c.Get().Path("services").Path(svc.ID).Do().Into(&prev)
|
||||
if err != nil {
|
||||
if svc.ResourceVersion == 0 {
|
||||
err = fmt.Errorf("invalid update object, missing resource version: %v", svc)
|
||||
return
|
||||
}
|
||||
svc.ResourceVersion = prev.ResourceVersion
|
||||
err = c.Put().Path("services").Path(svc.ID).Body(svc).Do().Into(&result)
|
||||
return
|
||||
}
|
||||
|
@@ -154,7 +154,7 @@ func TestCreatePod(t *testing.T) {
|
||||
|
||||
func TestUpdatePod(t *testing.T) {
|
||||
requestPod := api.Pod{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: 1},
|
||||
CurrentState: api.PodState{
|
||||
Status: "Foobar",
|
||||
},
|
||||
@@ -219,7 +219,7 @@ func TestGetController(t *testing.T) {
|
||||
|
||||
func TestUpdateController(t *testing.T) {
|
||||
requestController := api.ReplicationController{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: 1},
|
||||
}
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "PUT", Path: "/replicationControllers/foo"},
|
||||
@@ -388,11 +388,12 @@ func TestCreateService(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateService(t *testing.T) {
|
||||
svc := api.Service{JSONBase: api.JSONBase{ID: "service-1", ResourceVersion: 1}}
|
||||
c := &testClient{
|
||||
Request: testRequest{Method: "PUT", Path: "/services/service-1", Body: &api.Service{JSONBase: api.JSONBase{ID: "service-1"}}},
|
||||
Response: Response{StatusCode: 200, Body: &api.Service{JSONBase: api.JSONBase{ID: "service-1"}}},
|
||||
Request: testRequest{Method: "PUT", Path: "/services/service-1", Body: &svc},
|
||||
Response: Response{StatusCode: 200, Body: &svc},
|
||||
}
|
||||
response, err := c.Setup().UpdateService(api.Service{JSONBase: api.JSONBase{ID: "service-1"}})
|
||||
response, err := c.Setup().UpdateService(svc)
|
||||
c.Validate(t, &response, err)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user