-
Notifications
You must be signed in to change notification settings - Fork 362
/
conditions.go
57 lines (52 loc) · 1.71 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
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"
)
// UpdateConditions adds/updates new/old conditions in the physical Pod
func UpdateConditions(ctx *synccontext.SyncContext, physicalPod *corev1.Pod, virtualPod *corev1.Pod) (bool, error) {
// check if the readinessGates are added to vPod
updated := false
if len(virtualPod.Spec.ReadinessGates) > 0 {
// check if newConditions need to be added.
for _, vCondition := range virtualPod.Status.Conditions {
if isCustomCondition(virtualPod, 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 readiness gate condition status has changed", physicalPod.Namespace, physicalPod.Name)
err := ctx.PhysicalClient.Status().Update(ctx.Context, physicalPod)
if err != nil {
return false, err
}
}
}
return updated, nil
}
// Check for custom condition
func isCustomCondition(pod *corev1.Pod, condition corev1.PodCondition) bool {
for _, readinessGate := range pod.Spec.ReadinessGates {
if readinessGate.ConditionType == condition.Type {
return true
}
}
return false
}