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
package infra
import "github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
type Action string
const (
ActionInSync Action = "InSync"
ActionCreate Action = "Create"
ActionPush Action = "Push"
ActionPull Action = "Pull"
ActionConflict Action = "Conflict"
ActionDelete Action = "Delete"
ActionInvalid Action = "Invalid"
ActionEnded Action = "Ended"
ActionReserved Action = "Reserved"
)
type Record struct {
Present bool
CID string
Hash string
Err error
Ended bool
Reserved bool
}
type SyncInput struct {
Follow bool
Publish bool
Stored *v1alpha1.InfraRecordStatus
URI string
SpecHash string
Record Record
}
func Decide(in SyncInput) Action {
var stored v1alpha1.InfraRecordStatus
if in.Stored != nil && in.Stored.URI == in.URI {
stored = *in.Stored
}
switch {
case in.Record.Reserved:
return ActionReserved
case in.Record.Present && in.Record.Ended:
return ActionEnded
}
if in.Publish {
switch {
case !in.Record.Present:
return ActionCreate
case in.Record.Err == nil && in.Record.Hash == in.SpecHash:
return ActionInSync
}
return ActionPush
}
if !in.Record.Present {
if in.Follow || stored.CID != "" {
return ActionDelete
}
return ActionCreate
}
if in.Record.Err != nil {
return ActionInvalid
}
if in.Record.Hash == in.SpecHash {
return ActionInSync
}
if stored.DeclarationHash == "" {
if in.Follow {
return ActionPull
}
return ActionConflict
}
kcpChanged := in.SpecHash != stored.DeclarationHash
recordChanged := in.Record.Hash != stored.DeclarationHash
switch {
case kcpChanged && recordChanged:
return ActionConflict
case recordChanged:
return ActionPull
}
return ActionPush
}