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

use patch to replace update pod operator #2392

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion installer/helm/chart/volcano/templates/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rules:
verbs: ["create", "list", "watch", "update", "patch"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["create", "get", "list", "watch", "update", "bind", "delete"]
verbs: ["create", "get", "list", "watch", "update", "bind", "delete", "patch"]
- apiGroups: [""]
resources: ["pods/finalizers"]
verbs: ["update", "patch"]
Expand Down
2 changes: 1 addition & 1 deletion installer/volcano-development-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8433,7 +8433,7 @@ rules:
verbs: ["create", "list", "watch", "update", "patch"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["create", "get", "list", "watch", "update", "bind", "delete"]
verbs: ["create", "get", "list", "watch", "update", "bind", "delete", "patch"]
- apiGroups: [""]
resources: ["pods/finalizers"]
verbs: ["update", "patch"]
Expand Down
2 changes: 1 addition & 1 deletion installer/volcano-development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8433,7 +8433,7 @@ rules:
verbs: ["create", "list", "watch", "update", "patch"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["create", "get", "list", "watch", "update", "bind", "delete"]
verbs: ["create", "get", "list", "watch", "update", "bind", "delete", "patch"]
- apiGroups: [""]
resources: ["pods/finalizers"]
verbs: ["update", "patch"]
Expand Down
36 changes: 28 additions & 8 deletions pkg/controllers/podgroup/pg_controller_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package podgroup

import (
"context"
"encoding/json"
"strings"

v1 "k8s.io/api/core/v1"
Expand All @@ -28,6 +29,7 @@ import (
quotacore "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
"k8s.io/utils/clock"

"k8s.io/apimachinery/pkg/types"
"volcano.sh/apis/pkg/apis/helpers"
scheduling "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
)
Expand All @@ -37,6 +39,14 @@ type podRequest struct {
podNamespace string
}

type metadataForMergePatch struct {
Metadata annotationForMergePatch `json:"metadata"`
}

type annotationForMergePatch struct {
Annotations map[string]string `json:"annotations"`
}

func (pg *pgcontroller) addPod(obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
Expand All @@ -57,20 +67,30 @@ func (pg *pgcontroller) updatePodAnnotations(pod *v1.Pod, pgName string) error {
pod.Annotations = make(map[string]string)
}
if pod.Annotations[scheduling.KubeGroupNameAnnotationKey] == "" {
pod.Annotations[scheduling.KubeGroupNameAnnotationKey] = pgName
patch := metadataForMergePatch{
Metadata: annotationForMergePatch{
Annotations: map[string]string{
scheduling.KubeGroupNameAnnotationKey: pgName,
},
},
}

patchBytes, err := json.Marshal(&patch)
if err != nil {
klog.Errorf("Failed to json.Marshal pod annotation: %v", err)
return err
}

if _, err := pg.kubeClient.CoreV1().Pods(pod.Namespace).Patch(context.TODO(), pod.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}); err != nil {
klog.Errorf("Failed to update pod <%s/%s>: %v", pod.Namespace, pod.Name, err)
return err
}
} else {
if pod.Annotations[scheduling.KubeGroupNameAnnotationKey] != pgName {
klog.Errorf("normal pod %s/%s annotations %s value is not %s, but %s", pod.Namespace, pod.Name,
scheduling.KubeGroupNameAnnotationKey, pgName, pod.Annotations[scheduling.KubeGroupNameAnnotationKey])
}
return nil
}

if _, err := pg.kubeClient.CoreV1().Pods(pod.Namespace).Update(context.TODO(), pod, metav1.UpdateOptions{}); err != nil {
klog.Errorf("Failed to update pod <%s/%s>: %v", pod.Namespace, pod.Name, err)
return err
}

return nil
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/controllers/podgroup/pg_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ func TestAddPodGroup(t *testing.T) {
t.Errorf("Case %s failed, expect %v, got %v", testCase.name, testCase.expectedPodGroup, pg)
}

podAnnotation := pod.Annotations[scheduling.KubeGroupNameAnnotationKey]
newpod, err := c.kubeClient.CoreV1().Pods(testCase.pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
if err != nil {
t.Errorf("Case %s failed when creating pod for %v", testCase.name, err)
}

podAnnotation := newpod.Annotations[scheduling.KubeGroupNameAnnotationKey]
if testCase.expectedPodGroup.Name != podAnnotation {
t.Errorf("Case %s failed, expect %v, got %v", testCase.name,
testCase.expectedPodGroup.Name, podAnnotation)
Expand Down