Refactor architecture: extract reconciler, add interfaces, reduce complexity, improve test coverage
Architecture: - Extract reconcileNetworking into internal/reconcile package with 7 consumer-side interfaces - Add locked modifyState helper to fix state.yaml read-modify-write race condition - Extract CrowdSec and VPN handler groups with interfaces documenting dependency surface - Replace raw map[string]any YAML manipulation with typed AddDHCPStaticLease/RemoveDHCPStaticLease - Extract Cloudflare API functions into cfClient struct, eliminating repeated auth boilerplate Complexity reduction: - haproxy.GenerateWithOpts: 50 → 6 (extracted 8 focused helpers) - reconcile.Reconcile: 42 → 11 (extracted buildRoutes, buildDNSEntries, writeHAProxyConfig) - AutheliaUpdateConfig: 25 → 10 (extracted enableAuthelia/disableAuthelia) Test coverage improvements: - reconcile: 4.5% → 56.8% (stub-based tests for route building, DNS entries, orchestration) - dnsfilter: 10.7% → 45.6% (Manager.Compile, AddList, ToggleList, custom entries) - config: 67.3% → 89.8% (DHCP static lease mutation tests)
This commit is contained in:
@@ -11,10 +11,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/config"
|
||||
"github.com/wild-cloud/wild-central/internal/storage"
|
||||
)
|
||||
|
||||
// DnsmasqStatus returns the status of the dnsmasq service
|
||||
@@ -259,8 +257,10 @@ func (api *API) DnsmasqDHCPAddStatic(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
globalConfigPath := api.statePath()
|
||||
if err := api.addDHCPStaticLease(globalConfigPath, req); err != nil {
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
state.AddDHCPStaticLease(req)
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to add static lease: %v", err))
|
||||
return
|
||||
}
|
||||
@@ -280,8 +280,10 @@ func (api *API) DnsmasqDHCPDeleteStatic(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
globalConfigPath := api.statePath()
|
||||
if err := api.removeDHCPStaticLease(globalConfigPath, mac); err != nil {
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
state.RemoveDHCPStaticLease(mac)
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to remove static lease: %v", err))
|
||||
return
|
||||
}
|
||||
@@ -292,113 +294,3 @@ func (api *API) DnsmasqDHCPDeleteStatic(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]string{"message": "Static lease removed successfully"})
|
||||
}
|
||||
|
||||
// addDHCPStaticLease adds or replaces a static lease entry in the global config file
|
||||
func (api *API) addDHCPStaticLease(configPath string, lease config.DHCPStaticLease) error {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading config: %w", err)
|
||||
}
|
||||
|
||||
var cfg map[string]any
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return fmt.Errorf("parsing config: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = make(map[string]any)
|
||||
}
|
||||
|
||||
// Navigate/create the path cloud.dnsmasq.dhcp.staticLeases
|
||||
cloud := getOrCreateMap(cfg, "cloud")
|
||||
dnsmasqMap := getOrCreateMap(cloud, "dnsmasq")
|
||||
dhcpMap := getOrCreateMap(dnsmasqMap, "dhcp")
|
||||
|
||||
leases, _ := dhcpMap["staticLeases"].([]any)
|
||||
|
||||
// Replace existing entry with same MAC, or append
|
||||
newEntry := map[string]any{"mac": lease.MAC, "ip": lease.IP}
|
||||
if lease.Hostname != "" {
|
||||
newEntry["hostname"] = lease.Hostname
|
||||
}
|
||||
replaced := false
|
||||
for i, l := range leases {
|
||||
if m, ok := l.(map[string]any); ok {
|
||||
if m["mac"] == lease.MAC {
|
||||
leases[i] = newEntry
|
||||
replaced = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
leases = append(leases, newEntry)
|
||||
}
|
||||
dhcpMap["staticLeases"] = leases
|
||||
|
||||
out, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling config: %w", err)
|
||||
}
|
||||
|
||||
lockPath := configPath + ".lock"
|
||||
return storage.WithLock(lockPath, func() error {
|
||||
return storage.WriteFile(configPath, out, 0644)
|
||||
})
|
||||
}
|
||||
|
||||
// removeDHCPStaticLease removes a static lease entry by MAC from the global config file
|
||||
func (api *API) removeDHCPStaticLease(configPath string, mac string) error {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading config: %w", err)
|
||||
}
|
||||
|
||||
var cfg map[string]any
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return fmt.Errorf("parsing config: %w", err)
|
||||
}
|
||||
|
||||
cloud, _ := cfg["cloud"].(map[string]any)
|
||||
if cloud == nil {
|
||||
return nil
|
||||
}
|
||||
dnsmasqMap, _ := cloud["dnsmasq"].(map[string]any)
|
||||
if dnsmasqMap == nil {
|
||||
return nil
|
||||
}
|
||||
dhcpMap, _ := dnsmasqMap["dhcp"].(map[string]any)
|
||||
if dhcpMap == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
leases, _ := dhcpMap["staticLeases"].([]any)
|
||||
var filtered []any
|
||||
for _, l := range leases {
|
||||
if m, ok := l.(map[string]any); ok {
|
||||
if m["mac"] != mac {
|
||||
filtered = append(filtered, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
dhcpMap["staticLeases"] = filtered
|
||||
|
||||
out, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling config: %w", err)
|
||||
}
|
||||
|
||||
lockPath := configPath + ".lock"
|
||||
return storage.WithLock(lockPath, func() error {
|
||||
return storage.WriteFile(configPath, out, 0644)
|
||||
})
|
||||
}
|
||||
|
||||
// getOrCreateMap returns the map at key in parent, creating it if absent
|
||||
func getOrCreateMap(parent map[string]any, key string) map[string]any {
|
||||
if v, ok := parent[key].(map[string]any); ok {
|
||||
return v
|
||||
}
|
||||
m := make(map[string]any)
|
||||
parent[key] = m
|
||||
return m
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user