This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
iterator.go
139 lines (113 loc) · 3.45 KB
/
iterator.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//go:generate goderive . -dedup -autoname
package nodeagent
import (
"errors"
"fmt"
"runtime/debug"
"gopkg.in/yaml.v3"
"github.com/caos/orbos/internal/git"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/mntr"
)
type Rebooter interface {
Reboot() error
}
type event struct {
commit string
current *common.NodeAgentCurrent
}
func Iterator(monitor mntr.Monitor, gitClient *git.Client, nodeAgentCommit string, id string, firewallEnsurer FirewallEnsurer, conv Converter, before func() error) func() {
doQuery := prepareQuery(monitor, nodeAgentCommit, firewallEnsurer, conv)
return func() {
if err := gitClient.Clone(); err != nil {
monitor.Error(err)
return
}
desired := common.NodeAgentsDesiredKind{}
if err := yaml.Unmarshal(gitClient.Read("caos-internal/orbiter/node-agents-desired.yml"), &desired); err != nil {
panic(err)
}
if desired.Spec.NodeAgents == nil {
monitor.Error(errors.New("no desired node agents found"))
return
}
naDesired, ok := desired.Spec.NodeAgents[id]
if !ok {
monitor.Error(fmt.Errorf("No desired state for node agent with id %s found", id))
return
}
if desired.Spec.Commit != nodeAgentCommit {
monitor.WithFields(map[string]interface{}{
"desired": desired.Spec.Commit,
"current": nodeAgentCommit,
}).Info("Node Agent is on the wrong commit")
return
}
curr := &common.NodeAgentCurrent{}
events := make([]*event, 0)
monitor.OnChange = mntr.Concat(func(evt string, fields map[string]string) {
clone := *curr
events = append(events, &event{
commit: mntr.CommitRecord(mntr.AggregateCommitFields(fields)),
current: &clone,
})
}, monitor.OnChange)
if err := before(); err != nil {
panic(err)
}
ensure, err := doQuery(*naDesired, curr)
if err != nil {
monitor.Error(err)
return
}
readCurrent := func() common.NodeAgentsCurrentKind {
if err := gitClient.Clone(); err != nil {
panic(err)
}
current := common.NodeAgentsCurrentKind{}
yaml.Unmarshal(gitClient.Read("caos-internal/orbiter/node-agents-current.yml"), ¤t)
current.Kind = "nodeagent.caos.ch/NodeAgents"
current.Version = "v0"
if current.Current == nil {
current.Current = make(map[string]*common.NodeAgentCurrent)
}
return current
}
current := readCurrent()
current.Current[id] = curr
reconciledCurrentStateMsg := "Current state reconciled"
reconciledCurrent, err := gitClient.StageAndCommit(mntr.CommitRecord([]*mntr.Field{{Key: "evt", Value: reconciledCurrentStateMsg}}), git.File{
Path: "caos-internal/orbiter/node-agents-current.yml",
Content: common.MarshalYAML(current),
})
if err != nil {
panic(fmt.Errorf("Commiting event \"%s\" failed: %s", reconciledCurrentStateMsg, err.Error()))
}
if reconciledCurrent {
monitor.Error(gitClient.Push())
}
events = make([]*event, 0)
if err := ensure(); err != nil {
monitor.Error(err)
return
}
current = readCurrent()
for _, event := range events {
current.Current[id] = event.current
changed, err := gitClient.StageAndCommit(event.commit, git.File{
Path: "caos-internal/orbiter/node-agents-current.yml",
Content: common.MarshalYAML(current),
})
if err != nil {
panic(fmt.Errorf("Commiting event \"%s\" failed: %s", event.commit, err.Error()))
}
if !changed {
panic(fmt.Sprint("Event has no effect:", event.commit))
}
}
if len(events) > 0 {
monitor.Error(gitClient.Push())
}
debug.FreeOSMemory()
}
}