forked from secretflow/kuscia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
151 lines (131 loc) · 4.41 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2023 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resources
import (
"context"
"encoding/json"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)
// PatchPod is used to patch pod.
func PatchPod(ctx context.Context, kubeClient kubernetes.Interface, oldPod, newPod *corev1.Pod) error {
oldData, err := json.Marshal(oldPod)
if err != nil {
return err
}
newData, err := json.Marshal(newPod)
if err != nil {
return err
}
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, &corev1.Pod{})
if err != nil {
return fmt.Errorf("failed to create merge patch for pod %v/%v, %v", newPod.Namespace, newPod.Name, err)
}
if "{}" == string(patchBytes) {
return nil
}
patchFn := func() error {
_, err = kubeClient.CoreV1().Pods(newPod.Namespace).Patch(ctx, newPod.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{})
return err
}
return retry.OnError(retry.DefaultBackoff, net.IsConnectionRefused, patchFn)
}
// PatchPodStatus is used to patch pod status.
func PatchPodStatus(ctx context.Context, kubeClient kubernetes.Interface, oldPod, newPod *corev1.Pod) error {
oldData, err := json.Marshal(oldPod)
if err != nil {
return err
}
newData, err := json.Marshal(newPod)
if err != nil {
return err
}
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, &corev1.Pod{})
if err != nil {
return fmt.Errorf("failed to create merge patch for pod %v/%v, %v", newPod.Namespace, newPod.Name, err)
}
if string(patchBytes) == "{}" {
return nil
}
patchFn := func() error {
_, err = kubeClient.CoreV1().Pods(newPod.Namespace).Patch(ctx, newPod.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "status")
return err
}
return retry.OnError(retry.DefaultBackoff, net.IsConnectionRefused, patchFn)
}
// ExtractPodAnnotations is used to extract pod annotations.
func ExtractPodAnnotations(p *corev1.Pod) *corev1.Pod {
pp := &corev1.Pod{}
pp.Namespace = p.Namespace
pp.Name = p.Name
pp.Annotations = p.Annotations
return pp
}
// ExtractPodLabels is used to extract pod labels.
func ExtractPodLabels(p *corev1.Pod) *corev1.Pod {
pp := &corev1.Pod{}
pp.Namespace = p.Namespace
pp.Name = p.Name
pp.Labels = p.Labels
return pp
}
// ExtractPodAnnotationsAndLabels is used to extract pod annotations and labels.
func ExtractPodAnnotationsAndLabels(p *corev1.Pod) *corev1.Pod {
pp := &corev1.Pod{}
pp.Namespace = p.Namespace
pp.Name = p.Name
pp.Labels = p.Labels
pp.Annotations = p.Annotations
return pp
}
// ExtractPodSpec is used to extract pod spec.
func ExtractPodSpec(p *corev1.Pod) *corev1.Pod {
automountServiceAccountToken := false
pp := &corev1.Pod{}
pp.Namespace = p.Namespace
pp.Name = p.Name
pp.Labels = p.Labels
pp.Annotations = p.Annotations
pp.Spec.Containers = p.Spec.Containers
pp.Spec.AutomountServiceAccountToken = &automountServiceAccountToken
pp.Spec.SchedulerName = p.Spec.SchedulerName
pp.Spec.Affinity = p.Spec.Affinity
pp.Spec.InitContainers = p.Spec.InitContainers
pp.Spec.NodeSelector = p.Spec.NodeSelector
pp.Spec.ImagePullSecrets = p.Spec.ImagePullSecrets
pp.Spec.RestartPolicy = p.Spec.RestartPolicy
pp.Spec.Volumes = p.Spec.Volumes
pp.Spec.Tolerations = p.Spec.Tolerations
return pp
}
// ExtractPodStatus is used to extract pod status.
func ExtractPodStatus(p *corev1.Pod) *corev1.Pod {
pp := &corev1.Pod{}
pp.Namespace = p.Namespace
pp.Name = p.Name
pp.Status.Conditions = p.Status.Conditions
pp.Status.Message = p.Status.Message
pp.Status.Phase = p.Status.Phase
pp.Status.Reason = p.Status.Reason
pp.Status.ContainerStatuses = p.Status.ContainerStatuses
pp.Status.InitContainerStatuses = p.Status.InitContainerStatuses
pp.Status.StartTime = p.Status.StartTime
return pp
}