Skip to content

Commit

Permalink
gh-681: use cloudCIDR block as command-line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
TrekkieCoder committed May 28, 2024
1 parent 4d6c640 commit 9d8cc1f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ var Opts struct {
FallBack bool `long:"fallback" description:"Fallback to system default networking(experimental)"`
LocalVIP bool `long:"localvip" description:"support vip availability from lb node(experimental)"`
Cloud string `long:"cloud" description:"cloud type if any e.g aws,ncloud" default:"on-prem"`
CloudCIDRBlock string `long:"cloudcidrblock" description:"cloud implementations need VIP cidr blocks(experimental)"`
}
10 changes: 8 additions & 2 deletions pkg/loxinet/loxinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,15 @@ func loxiNetInit() {
signal.Notify(mh.sigCh, os.Interrupt, syscall.SIGCHLD, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)

if mh.cloudLabel == "aws" {
AWSApiInit()
err := AWSApiInit(opts.Opts.CloudCIDRBlock)
if err != nil {
os.Exit(1)
}
} else if mh.cloudLabel == "ncloud" {
NcloudApiInit()
err := NcloudApiInit()
if err != nil {
os.Exit(1)
}
}

// Check if profiling is enabled
Expand Down
35 changes: 29 additions & 6 deletions pkg/loxinet/utils_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
vpcID string
instanceID string
azName string
awsCIDRnet *net.IPNet
loxiEniID string
)

func AWSGetInstanceIDInfo() (string, error) {
Expand Down Expand Up @@ -105,6 +107,10 @@ func AWSGetInstanceAvailabilityZone() (string, error) {
}

func AWSPrepVIPNetwork() error {
if awsCIDRnet == nil {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*2))
defer cancel()

Expand Down Expand Up @@ -158,7 +164,7 @@ func AWSPrepVIPNetwork() error {
}
}

cidrBlock := "123.123.123.0/28"
cidrBlock := awsCIDRnet.String()
loxilbSubNetKey := "loxiType"
loxilbSubNetKeyVal := "loxilb-subnet"
subnetTag := types.Tag{Key: &loxilbSubNetKey, Value: &loxilbSubNetKeyVal}
Expand Down Expand Up @@ -193,6 +199,8 @@ func AWSPrepVIPNetwork() error {
return nil
}

loxiEniID = *intfOutput.NetworkInterface.NetworkInterfaceId

tk.LogIt(tk.LogInfo, "Created interface (%s) for loxilb instance %v\n", *intfOutput.NetworkInterface.NetworkInterfaceId, vpcID)

devIdx := int32(1)
Expand Down Expand Up @@ -294,10 +302,16 @@ func AWSUpdatePrivateIP(vIP net.IP, add bool) error {
return err
}

niID, err := AWSGetNetworkInterface(instanceID, vIP)
if err != nil {
tk.LogIt(tk.LogError, "AWS get network interface failed: %v\n", err)
return err
niID := ""

if awsCIDRnet == nil || loxiEniID == "" {
niID, err = AWSGetNetworkInterface(instanceID, vIP)
if err != nil {
tk.LogIt(tk.LogError, "AWS get network interface failed: %v\n", err)
return err
}
} else {
niID = loxiEniID
}

if !add {
Expand All @@ -307,7 +321,8 @@ func AWSUpdatePrivateIP(vIP net.IP, add bool) error {
return AWSCreatePrivateIp(niID, vIP)
}

func AWSApiInit() error {
func AWSApiInit(cloudCIDRBlock string) error {

// Using the SDK's default configuration, loading additional config
// and credentials values from the environment variables, shared
// credentials, and shared configuration files
Expand All @@ -316,6 +331,14 @@ func AWSApiInit() error {
return err
}

if cloudCIDRBlock != "" {
_, awsCIDRnet, err = net.ParseCIDR(cloudCIDRBlock)
if err != nil {
tk.LogIt(tk.LogError, "failed to parse cloud cidr block %s\n", cloudCIDRBlock)
return err
}
}

// Using the Config value, create the DynamoDB client
imdsClient = imds.NewFromConfig(cfg)
ec2Client = ec2.NewFromConfig(cfg)
Expand Down

0 comments on commit 9d8cc1f

Please sign in to comment.