...
1
16
17 package types
18
19 import (
20 "fmt"
21
22 v1 "k8s.io/api/core/v1"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/kubernetes/pkg/apis/scheduling"
25 )
26
27
28 const (
29 ConfigSourceAnnotationKey = "kubernetes.io/config.source"
30 ConfigMirrorAnnotationKey = v1.MirrorPodAnnotationKey
31 ConfigFirstSeenAnnotationKey = "kubernetes.io/config.seen"
32 ConfigHashAnnotationKey = "kubernetes.io/config.hash"
33 )
34
35
36 type PodOperation int
37
38
39 const (
40
41 SET PodOperation = iota
42
43 ADD
44
45 DELETE
46
47 REMOVE
48
49 UPDATE
50
51
52 RECONCILE
53 )
54
55
56 const (
57
58 FileSource = "file"
59
60 HTTPSource = "http"
61
62 ApiserverSource = "api"
63
64 AllSource = "*"
65 )
66
67
68 const NamespaceDefault = metav1.NamespaceDefault
69
70
71
72
73
74
75
76
77
78
79 type PodUpdate struct {
80 Pods []*v1.Pod
81 Op PodOperation
82 Source string
83 }
84
85
86 func GetValidatedSources(sources []string) ([]string, error) {
87 validated := make([]string, 0, len(sources))
88 for _, source := range sources {
89 switch source {
90 case AllSource:
91 return []string{FileSource, HTTPSource, ApiserverSource}, nil
92 case FileSource, HTTPSource, ApiserverSource:
93 validated = append(validated, source)
94 case "":
95
96 default:
97 return []string{}, fmt.Errorf("unknown pod source %q", source)
98 }
99 }
100 return validated, nil
101 }
102
103
104 func GetPodSource(pod *v1.Pod) (string, error) {
105 if pod.Annotations != nil {
106 if source, ok := pod.Annotations[ConfigSourceAnnotationKey]; ok {
107 return source, nil
108 }
109 }
110 return "", fmt.Errorf("cannot get source of pod %q", pod.UID)
111 }
112
113
114 type SyncPodType int
115
116 const (
117
118 SyncPodSync SyncPodType = iota
119
120 SyncPodUpdate
121
122 SyncPodCreate
123
124
125 SyncPodKill
126 )
127
128 func (sp SyncPodType) String() string {
129 switch sp {
130 case SyncPodCreate:
131 return "create"
132 case SyncPodUpdate:
133 return "update"
134 case SyncPodSync:
135 return "sync"
136 case SyncPodKill:
137 return "kill"
138 default:
139 return "unknown"
140 }
141 }
142
143
144 func IsMirrorPod(pod *v1.Pod) bool {
145 if pod.Annotations == nil {
146 return false
147 }
148 _, ok := pod.Annotations[ConfigMirrorAnnotationKey]
149 return ok
150 }
151
152
153 func IsStaticPod(pod *v1.Pod) bool {
154 source, err := GetPodSource(pod)
155 return err == nil && source != ApiserverSource
156 }
157
158
159 func IsCriticalPod(pod *v1.Pod) bool {
160 if IsStaticPod(pod) {
161 return true
162 }
163 if IsMirrorPod(pod) {
164 return true
165 }
166 if pod.Spec.Priority != nil && IsCriticalPodBasedOnPriority(*pod.Spec.Priority) {
167 return true
168 }
169 return false
170 }
171
172
173
174 func Preemptable(preemptor, preemptee *v1.Pod) bool {
175 if IsCriticalPod(preemptor) && !IsCriticalPod(preemptee) {
176 return true
177 }
178 if (preemptor != nil && preemptor.Spec.Priority != nil) &&
179 (preemptee != nil && preemptee.Spec.Priority != nil) {
180 return *(preemptor.Spec.Priority) > *(preemptee.Spec.Priority)
181 }
182
183 return false
184 }
185
186
187 func IsCriticalPodBasedOnPriority(priority int32) bool {
188 return priority >= scheduling.SystemCriticalPriority
189 }
190
191
192 func IsNodeCriticalPod(pod *v1.Pod) bool {
193 return IsCriticalPod(pod) && (pod.Spec.PriorityClassName == scheduling.SystemNodeCritical)
194 }
195
196
197
198 func IsRestartableInitContainer(initContainer *v1.Container) bool {
199 if initContainer.RestartPolicy == nil {
200 return false
201 }
202
203 return *initContainer.RestartPolicy == v1.ContainerRestartPolicyAlways
204 }
205
View as plain text