Skip to content

Commit

Permalink
feat(agent): haproxy and dataplaneapi on agent setup added
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt committed Feb 8, 2025
1 parent b2289a0 commit 9f3ab91
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 5 deletions.
63 changes: 60 additions & 3 deletions agent/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func init() {
setupCmd.Flags().String("wireguard-address", "", "Wireguard address")
setupCmd.Flags().String("docker-network-gateway-address", "", "Docker network gateway address")
setupCmd.Flags().String("docker-network-subnet", "", "Docker network subnet")
setupCmd.Flags().String("swiftwave-service-address", "", "Swiftwave service address ip:port")
setupCmd.Flags().Bool("enable-haproxy", false, "Enable haproxy")

setupCmd.Flags().Bool("master-node", false, "Setup as a master node")
setupCmd.Flags().String("master-node-endpoint", "", "Master server endpoint")
Expand All @@ -35,6 +37,7 @@ func init() {
setupCmd.MarkFlagRequired("wireguard-address")
setupCmd.MarkFlagRequired("docker-network-gateway-address")
setupCmd.MarkFlagRequired("docker-network-subnet")
setupCmd.MarkFlagRequired("swiftwave-service-address")
}

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -68,7 +71,14 @@ var startCmd = &cobra.Command{
var setupCmd = &cobra.Command{
Use: "setup",
Run: func(cmd *cobra.Command, args []string) {
_, err := GetConfig()
// Migrate the database
err := MigrateDatabase()
if err != nil {
cmd.PrintErr("Failed to migrate database")
return
}
// Try to get the config
_, err = GetConfig()
if err == nil {
cmd.Println("Sorry, you can't change any config")
return
Expand All @@ -80,13 +90,27 @@ var setupCmd = &cobra.Command{
return
}

isEnableHaproxy, err := cmd.Flags().GetBool("enable-haproxy")
if err != nil {
cmd.PrintErr("Invalid enable haproxy flag")
return
}

// validate swiftwave service address
_, _, err = net.SplitHostPort(cmd.Flag("swiftwave-service-address").Value.String())
if err != nil {
cmd.PrintErr("Invalid swiftwave service address")
return
}

nodeType := WorkerNode
if isMasterNode {
nodeType = MasterNode
}

config := AgentConfig{
NodeType: nodeType,
NodeType: nodeType,
SwiftwaveServiceAddress: cmd.Flag("swiftwave-service-address").Value.String(),
WireguardConfig: WireguardConfig{
PrivateKey: cmd.Flag("wireguard-private-key").Value.String(),
Address: cmd.Flag("wireguard-address").Value.String(),
Expand All @@ -100,6 +124,11 @@ var setupCmd = &cobra.Command{
PublicKey: cmd.Flag("master-node-public-key").Value.String(),
AllowedIPs: cmd.Flag("master-node-allowed-ips").Value.String(),
},
HaproxyConfig: HAProxyConfig{
Enabled: isEnableHaproxy,
Username: GenerateRandomString(10),
Password: GenerateRandomString(30),
},
}

if !isMasterNode {
Expand Down Expand Up @@ -137,6 +166,20 @@ var setupCmd = &cobra.Command{
}
}()

// Get ip from wireguard address
ip, _, err := net.ParseCIDR(config.WireguardConfig.Address)
if err != nil {
cmd.PrintErr("Failed to parse wireguard address")
return
}

// Install haproxy
err = installHAProxy(config.SwiftwaveServiceAddress, fmt.Sprintf("%s:53", ip), config.HaproxyConfig.Username, config.HaproxyConfig.Password)
if err != nil {
cmd.PrintErr(err.Error())
return
}

err = SetConfig(&config)
if err != nil {
cmd.PrintErr(err.Error())
Expand All @@ -162,6 +205,12 @@ var setupCmd = &cobra.Command{
cmd.PrintErr(err.Error())
return
}
// Enable haproxy and data plane api
if config.HaproxyConfig.Enabled {
enableHAProxy()
}
cmd.Println("Haproxy and data plane api enabled")

isSuccess = true
},
}
Expand Down Expand Up @@ -208,6 +257,11 @@ var getConfig = &cobra.Command{
cmd.Printf(" • Bridge ID ---------- %s\n", config.DockerNetwork.BridgeId)
cmd.Printf(" • Gateway Address ---- %s\n", config.DockerNetwork.GatewayAddress)
cmd.Printf(" • Subnet ------------- %s\n", config.DockerNetwork.Subnet)
cmd.Println()
cmd.Println("Haproxy Configuration:")
cmd.Printf(" • Enabled ------------ %t\n", config.HaproxyConfig.Enabled)
cmd.Printf(" • Username ---------- %s\n", config.HaproxyConfig.Username)
cmd.Printf(" • Password ---------- %s\n", config.HaproxyConfig.Password)
},
}

Expand All @@ -216,7 +270,7 @@ var cleanup = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// Ask for confirmation
fmt.Println("This will delete all containers and remove docker and wireguard networks")
fmt.Println("Are you sure you want to continue? (y/n)")
fmt.Print("Are you sure you want to continue? (y/n) : ")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
Expand Down Expand Up @@ -274,6 +328,9 @@ var cleanup = &cobra.Command{
_ = IPTablesClient.ClearChain("nat", NatPostroutingChainName)
_ = IPTablesClient.ClearChain("nat", NatInputChainName)
_ = IPTablesClient.ClearChain("nat", NatOutputChainName)
// Disable haproxy and data plane api
disableHAProxy()
cmd.Println("Haproxy and data plane api disabled")
// Backup and remove the database file
moveDBFilesToBackup()
// Done
Expand Down
8 changes: 8 additions & 0 deletions agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const (
type AgentConfig struct {
ID uint `gorm:"primaryKey"`
NodeType NodeType `json:"node_type" gorm:"column:node_type"`
SwiftwaveServiceAddress string `json:"swiftwave_service_address" gorm:"column:swiftwave_service_address"`
WireguardConfig WireguardConfig `json:"wireguard_config" gorm:"embedded;embeddedPrefix:wireguard_"`
MasterNodeConnectConfig MasterNodeConnectConfig `json:"master_node_connect_config" gorm:"embedded;embeddedPrefix:master_node_connect_config_"`
DockerNetwork DockerNetworkConfig `json:"docker_network" gorm:"embedded;embeddedPrefix:docker_network_"`
HaproxyConfig HAProxyConfig `json:"haproxy_config" gorm:"embedded;embeddedPrefix:haproxy_"`
}

type WireguardConfig struct {
Expand All @@ -37,6 +39,12 @@ type DockerNetworkConfig struct {
Subnet string `json:"subnet" gorm:"column:subnet"`
}

type HAProxyConfig struct {
Enabled bool `json:"enabled" gorm:"column:enabled"`
Username string `json:"username" gorm:"column:username"`
Password string `json:"password" gorm:"column:password"`
}

func GetConfig() (*AgentConfig, error) {
var config AgentConfig
if err := rDB.First(&config).Error; err != nil {
Expand Down
6 changes: 4 additions & 2 deletions agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.22.0 // indirect
golang.org/x/tools v0.29.0 // indirect
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
Expand All @@ -58,6 +59,7 @@ require (
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/cobra v1.8.1
github.com/tredoe/osutil v1.5.0
github.com/vishvananda/netlink v1.3.0
github.com/vishvananda/netns v0.0.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions agent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tredoe/osutil v1.5.0 h1:UGVxbbHRoZi8xXVmbNZ2vgG6XoJ15ndE4LniiQ3rJKg=
github.com/tredoe/osutil v1.5.0/go.mod h1:TEzphzUUunysbdDRfdOgqkg10POQbnfIPV50ynqOfIg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
Expand Down Expand Up @@ -149,10 +151,13 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 h1:qNgPs5exUA+G0C96DrPwNrvLSj7GT/9D+3WMWUcUg34=
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down Expand Up @@ -188,6 +193,7 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
Loading

0 comments on commit 9f3ab91

Please sign in to comment.