...
1
2
3
4 package taskrunner
5
6 import (
7 "sigs.k8s.io/cli-utils/pkg/kstatus/status"
8 "sigs.k8s.io/cli-utils/pkg/object"
9 )
10
11
12
13 type Condition string
14
15 const (
16
17
18 AllCurrent Condition = "AllCurrent"
19
20
21
22
23 AllNotFound Condition = "AllNotFound"
24 )
25
26
27
28 func (c Condition) Meets(s status.Status) bool {
29 switch c {
30 case AllCurrent:
31 return s == status.CurrentStatus
32 case AllNotFound:
33 return s == status.NotFoundStatus
34 default:
35 return false
36 }
37 }
38
39
40
41
42 func conditionMet(taskContext *TaskContext, ids object.ObjMetadataSet, c Condition) bool {
43 switch c {
44 case AllCurrent:
45 return allMatchStatus(taskContext, ids, status.CurrentStatus)
46 case AllNotFound:
47 return allMatchStatus(taskContext, ids, status.NotFoundStatus)
48 default:
49 return noneMatchStatus(taskContext, ids, status.UnknownStatus)
50 }
51 }
52
53
54
55 func allMatchStatus(taskContext *TaskContext, ids object.ObjMetadataSet, s status.Status) bool {
56 for _, id := range ids {
57 cached := taskContext.ResourceCache().Get(id)
58 if cached.Status != s {
59 return false
60 }
61
62 applyGen, _ := taskContext.InventoryManager().AppliedGeneration(id)
63 cachedGen := int64(0)
64 if cached.Resource != nil {
65 cachedGen = cached.Resource.GetGeneration()
66 }
67 if cachedGen < applyGen {
68
69 return false
70 }
71 }
72 return true
73 }
74
75
76
77 func noneMatchStatus(taskContext *TaskContext, ids object.ObjMetadataSet, s status.Status) bool {
78 for _, id := range ids {
79 cached := taskContext.ResourceCache().Get(id)
80 if cached.Status == s {
81 return false
82 }
83
84 applyGen, _ := taskContext.InventoryManager().AppliedGeneration(id)
85 cachedGen := int64(0)
86 if cached.Resource != nil {
87 cachedGen = cached.Resource.GetGeneration()
88 }
89 if cachedGen < applyGen {
90
91 return false
92 }
93 }
94 return true
95 }
96
View as plain text