github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/common/nodeagent-model.go (about) 1 //go:generate goderive -autoname -dedup . 2 3 package common 4 5 import ( 6 "regexp" 7 "sort" 8 "sync" 9 "time" 10 ) 11 12 type NodeAgentSpec struct { 13 ChangesAllowed bool 14 LeaveOSRepositories bool 15 // RebootEnabled bool 16 Software *Software 17 Networking *Networking 18 Firewall *Firewall 19 RebootRequired time.Time 20 } 21 22 type NodeAgentCurrent struct { 23 NodeIsReady bool `mapstructure:"ready" yaml:"ready"` 24 Software Software 25 Open FirewallCurrent 26 Networking NetworkingCurrent 27 Commit string 28 Booted time.Time 29 } 30 31 var prune = regexp.MustCompile("[^a-zA-Z0-9]+") 32 33 func configEquals(this, that map[string]string) bool { 34 if this == nil || that == nil { 35 return this == nil && that == nil 36 } 37 if len(this) != len(that) { 38 return false 39 } 40 for k, v := range this { 41 thatv, ok := that[k] 42 if !ok { 43 return false 44 } 45 46 if prune.ReplaceAllString(v, "") != prune.ReplaceAllString(thatv, "") { 47 return false 48 } 49 } 50 return true 51 } 52 53 func (p Package) Equals(other Package) bool { 54 return PackageEquals(p, other) 55 } 56 func PackageEquals(this, that Package) bool { 57 equals := this.Version == that.Version && 58 configEquals(this.Config, that.Config) 59 return equals 60 } 61 62 type MarshallableSlice []string 63 64 func (m MarshallableSlice) MarshalYAML() (interface{}, error) { 65 sort.Strings(m) 66 type s []string 67 return s(m), nil 68 } 69 70 type NodeAgentsCurrentKind struct { 71 Kind string 72 Version string 73 Current CurrentNodeAgents 74 } 75 76 type CurrentNodeAgents struct { 77 // NA is exported for yaml (de)serialization and not intended to be accessed by any other code outside this package 78 NA map[string]*NodeAgentCurrent `yaml:",inline"` 79 mux sync.Mutex `yaml:"-"` 80 } 81 82 func (n *CurrentNodeAgents) Set(id string, na *NodeAgentCurrent) { 83 n.mux.Lock() 84 defer n.mux.Unlock() 85 if n.NA == nil { 86 n.NA = make(map[string]*NodeAgentCurrent) 87 } 88 89 if _, ok := n.NA[id]; ok { 90 n.NA[id] = nil 91 } 92 93 n.NA[id] = na 94 } 95 96 func (n *CurrentNodeAgents) Get(id string) (*NodeAgentCurrent, bool) { 97 n.mux.Lock() 98 defer n.mux.Unlock() 99 100 if n.NA == nil { 101 n.NA = make(map[string]*NodeAgentCurrent) 102 } 103 104 na, ok := n.NA[id] 105 if !ok { 106 na = &NodeAgentCurrent{ 107 Open: make(FirewallCurrent, 0), 108 Networking: make(NetworkingCurrent, 0), 109 } 110 n.NA[id] = na 111 } 112 return na, ok 113 114 } 115 116 type NodeAgentsSpec struct { 117 Commit string 118 NodeAgents DesiredNodeAgents 119 } 120 121 type DesiredNodeAgents struct { 122 // NA is exported for yaml (de)serialization and not intended to be accessed by any other code outside this package 123 NA map[string]*NodeAgentSpec `yaml:",inline"` 124 mux sync.Mutex `yaml:"-"` 125 } 126 127 func (n *DesiredNodeAgents) Delete(id string) { 128 n.mux.Lock() 129 defer n.mux.Unlock() 130 131 if _, ok := n.NA[id]; ok { 132 n.NA[id] = nil 133 } 134 delete(n.NA, id) 135 } 136 137 func (n *DesiredNodeAgents) List() []string { 138 n.mux.Lock() 139 defer n.mux.Unlock() 140 var ids []string 141 for id := range n.NA { 142 ids = append(ids, id) 143 } 144 return ids 145 } 146 147 func (n *DesiredNodeAgents) Get(id string) (*NodeAgentSpec, bool) { 148 n.mux.Lock() 149 defer n.mux.Unlock() 150 151 if n.NA == nil { 152 n.NA = make(map[string]*NodeAgentSpec) 153 } 154 155 na, ok := n.NA[id] 156 if !ok { 157 na = &NodeAgentSpec{ 158 Software: &Software{ 159 Sysctl: Package{ 160 Config: map[string]string{ 161 string(IpForward): "0", 162 string(NonLocalBind): "0", 163 string(BridgeNfCallIptables): "0", 164 string(BridgeNfCallIp6tables): "0", 165 }, 166 }, 167 }, 168 Firewall: &Firewall{ 169 Zones: map[string]*Zone{ 170 "internal": { 171 Interfaces: []string{}, 172 FW: map[string]*Allowed{}, 173 Services: map[string]*Service{}, 174 }, "external": { 175 Interfaces: []string{}, 176 FW: map[string]*Allowed{}, 177 Services: map[string]*Service{}, 178 }, 179 }, 180 }, 181 Networking: &Networking{ 182 Interfaces: map[string]*NetworkingInterface{}, 183 }, 184 } 185 n.NA[id] = na 186 } 187 return na, ok 188 } 189 190 type NodeAgentsDesiredKind struct { 191 Kind string 192 Version string 193 Spec NodeAgentsSpec `yaml:",omitempty"` 194 } 195 196 type KernelModule string 197 198 const ( 199 IpForward KernelModule = "net.ipv4.ip_forward" 200 NonLocalBind KernelModule = "net.ipv4.ip_nonlocal_bind" 201 BridgeNfCallIptables KernelModule = "net.bridge.bridge-nf-call-iptables" 202 BridgeNfCallIp6tables KernelModule = "net.bridge.bridge-nf-call-ip6tables" 203 )