-
Notifications
You must be signed in to change notification settings - Fork 362
/
conditions.go
81 lines (73 loc) · 2.38 KB
/
conditions.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
package pods
import (
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
)
var coreConditions = map[string]bool{
string(corev1.PodReady): true,
string(corev1.ContainersReady): true,
string(corev1.PodInitialized): true,
string(corev1.PodScheduled): true,
"PodReadyToStartContainers": true,
}
// UpdateConditions adds/updates new/old conditions in the physical Pod
func UpdateConditions(ctx *synccontext.SyncContext, physicalPod *corev1.Pod, virtualPod *corev1.Pod) (*corev1.Pod, error) {
// check if the readinessGates are added to vPod
physicalPod = physicalPod.DeepCopy()
updated := false
// check if newConditions need to be added.
for _, vCondition := range virtualPod.Status.Conditions {
if isCustomCondition(vCondition) {
found := false
for index, pCondition := range physicalPod.Status.Conditions {
// found condition in pPod with same type, updating foundCondition
if vCondition.Type == pCondition.Type {
found = true
if !equality.Semantic.DeepEqual(pCondition, vCondition) {
updated = true
physicalPod.Status.Conditions[index] = vCondition
}
break
}
}
if !found {
physicalPod.Status.Conditions = append(physicalPod.Status.Conditions, vCondition)
updated = true
}
}
}
// update physical pod
if updated {
ctx.Log.Infof("update physical pod %s/%s, because custom pod conditions have changed", physicalPod.Namespace, physicalPod.Name)
err := ctx.PhysicalClient.Status().Update(ctx.Context, physicalPod)
if err != nil {
return nil, err
}
}
// don't sync custom conditions up
newConditions := []corev1.PodCondition{}
for _, pCondition := range physicalPod.Status.Conditions {
if isCustomCondition(pCondition) {
found := false
for _, vCondition := range virtualPod.Status.Conditions {
if pCondition.Type == vCondition.Type {
found = true
break
}
}
if !found {
// don't sync custom conditions we don't have on the virtual pod
continue
}
}
newConditions = append(newConditions, pCondition)
}
physicalPod.Status.Conditions = newConditions
return physicalPod, nil
}
// Check for custom condition
func isCustomCondition(condition corev1.PodCondition) bool {
// if not a default condition, we assume it's a custom condition
return !coreConditions[string(condition.Type)]
}