Skip to content

Commit

Permalink
add nodeName check before ota update
Browse files Browse the repository at this point in the history
Signed-off-by: Xuecheng <[email protected]>
  • Loading branch information
xavier-hou committed Sep 15, 2022
1 parent 5e40ab9 commit 35b4d6d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 deletions pkg/yurthub/otaupdate/ota.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func GetPods(clientset kubernetes.Interface, nodeName string) http.Handler {
}

// UpdatePod update a specifc pod(namespace/podname) to the latest version
func UpdatePod(clientset kubernetes.Interface) http.Handler {
func UpdatePod(clientset kubernetes.Interface, nodeName string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
namespace := params["ns"]
podName := params["podname"]

err, ok := applyUpdate(clientset, namespace, podName)
err, ok := applyUpdate(clientset, namespace, podName, nodeName)
if err != nil {
returnErr(fmt.Errorf("Apply update failed"), w, http.StatusInternalServerError)
return
Expand All @@ -85,16 +85,22 @@ func UpdatePod(clientset kubernetes.Interface) http.Handler {
}

// applyUpdate execute pod update process by deleting pod under OnDelete update strategy
func applyUpdate(clientset kubernetes.Interface, namespace, podName string) (error, bool) {
func applyUpdate(clientset kubernetes.Interface, namespace, podName, nodeName string) (error, bool) {
pod, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
klog.Errorf("Get pod %v/%v failed, %v", namespace, podName, err)
return err, false
}

// Pod will not be updated while it's being deleted
// Pod will not be updated when it's being deleted
if pod.DeletionTimestamp != nil {
klog.Infof("Pod %v/%v is deleting, can not update", namespace, podName)
klog.Infof("Pod %v/%v is deleting, can not be updated", namespace, podName)
return nil, false
}

// Pod will not be updated when it's not running on the current node
if pod.Spec.NodeName != nodeName {
klog.Infof("Pod: %v/%v is running on %v, can not be updated", namespace, podName, pod.Spec.NodeName)
return nil, false
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/yurthub/otaupdate/ota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestUpdatePod(t *testing.T) {
req = mux.SetURLVars(req, vars)
rr := httptest.NewRecorder()

UpdatePod(clientset).ServeHTTP(rr, req)
UpdatePod(clientset, "").ServeHTTP(rr, req)

assert.Equal(t, test.expectedCode, rr.Code)
assert.Equal(t, test.expectedData, rr.Body.String())
Expand Down
2 changes: 1 addition & 1 deletion pkg/yurthub/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func registerHandlers(c *mux.Router, cfg *config.YurtHubConfiguration, certifica

// register handler for ota upgrade
c.Handle("/pods", ota.GetPods(clientset, nodeName)).Methods("GET")
c.Handle("/openyurt.io/v1/namespaces/{ns}/pods/{podname}/upgrade", ota.UpdatePod(clientset)).Methods("POST")
c.Handle("/openyurt.io/v1/namespaces/{ns}/pods/{podname}/upgrade", ota.UpdatePod(clientset, nodeName)).Methods("POST")
}

// healthz returns ok for healthz request
Expand Down

0 comments on commit 35b4d6d

Please sign in to comment.