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
164 lines (132 loc) · 3.83 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//go:generate goderive . -dedup -autoname
package nodeagent
import (
"fmt"
"io/ioutil"
"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 RepoKey() ([]byte, error) {
path := "/var/orbiter/repo-key"
key, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("repokey not found at %s: %w", path, err)
}
return key, err
}
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() {
repoKey, err := RepoKey()
if err != nil {
monitor.Error(err)
return
}
repoURL, err := ioutil.ReadFile("/var/orbiter/repo-url")
if err != nil {
monitor.Error(err)
return
}
if err := gitClient.Configure(string(repoURL), repoKey); err != nil {
monitor.Error(err)
return
}
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 {
monitor.Error(err)
return
}
naDesired, ok := desired.Spec.NodeAgents.Get(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"
return current
}
current := readCurrent()
current.Current.Set(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 {
monitor.Error(fmt.Errorf("commiting event \"%s\" failed: %s", reconciledCurrentStateMsg, err.Error()))
return
}
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.Set(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 {
monitor.Error(fmt.Errorf("commiting event \"%s\" failed: %s", event.commit, err.Error()))
return
}
if !changed {
monitor.Error(fmt.Errorf("event has no effect:", event.commit))
return
}
}
if len(events) > 0 {
monitor.Error(gitClient.Push())
}
debug.FreeOSMemory()
}
}