feat: add node registration verification and idle loop for joined nodes
54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"git.dws.rip/dubey/kat/internal/store"
|
|
)
|
|
|
|
// NodeRegistration represents the data stored in etcd for a node
|
|
type NodeRegistration struct {
|
|
UID string `json:"uid"`
|
|
AdvertiseAddr string `json:"advertiseAddr"`
|
|
WireguardPubKey string `json:"wireguardPubKey"`
|
|
JoinTimestamp int64 `json:"joinTimestamp"`
|
|
}
|
|
|
|
// VerifyNodeRegistration checks if a node is registered in etcd
|
|
func VerifyNodeRegistration(etcdStore store.StateStore, nodeName string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
// Construct the key for the node registration
|
|
nodeRegKey := fmt.Sprintf("/kat/nodes/registration/%s", nodeName)
|
|
|
|
// Get the node registration from etcd
|
|
kv, err := etcdStore.Get(ctx, nodeRegKey)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get node registration from etcd: %w", err)
|
|
}
|
|
|
|
// Parse the node registration
|
|
var nodeReg NodeRegistration
|
|
if err := json.Unmarshal(kv.Value, &nodeReg); err != nil {
|
|
return fmt.Errorf("failed to parse node registration: %w", err)
|
|
}
|
|
|
|
// Print the node registration details
|
|
log.Printf("Node Registration Details:")
|
|
log.Printf(" Node Name: %s", nodeName)
|
|
log.Printf(" Node UID: %s", nodeReg.UID)
|
|
log.Printf(" Advertise Address: %s", nodeReg.AdvertiseAddr)
|
|
log.Printf(" WireGuard Public Key: %s", nodeReg.WireguardPubKey)
|
|
|
|
// Convert timestamp to human-readable format
|
|
joinTime := time.Unix(nodeReg.JoinTimestamp, 0)
|
|
log.Printf(" Join Timestamp: %s (%d)", joinTime.Format(time.RFC3339), nodeReg.JoinTimestamp)
|
|
|
|
return nil
|
|
}
|