Skip to content

Commit

Permalink
loxilb-io/loxilb#675 Initial support to enable proxy protocol v2
Browse files Browse the repository at this point in the history
  • Loading branch information
TrekkieCoder committed Nov 26, 2024
1 parent f0aa16d commit 13c8d12
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/create/create_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type CreateLoadBalancerOptions struct {
Name string
Host string
AllowedSources []string
PPv2En bool
}

type CreateLoadBalancerResult struct {
Expand Down Expand Up @@ -139,7 +140,7 @@ func NewCreateLoadBalancerCmd(restOptions *api.RESTOptions) *cobra.Command {
o := CreateLoadBalancerOptions{}

var createLbCmd = &cobra.Command{
Use: "lb IP [--select=<rr|hash|priority|persist>] [--tcp=<port>:<targetPort>] [--udp=<port>:<targetPort>] [--sctp=<port>:<targetPort>] [--icmp] [--mark=<val>] [--secips=<ip>,] [--sources=<ip>,] [--endpoints=<ip>:<weight>,] [--mode=<onearm|fullnat>] [--bgp] [--monitor] [--inatimeout=<to>] [--name=<service-name>] [--attachEP] [--detachEP] [--security=<https|e2ehttps|none>] [--host=<url>]",
Use: "lb IP [--select=<rr|hash|priority|persist>] [--tcp=<port>:<targetPort>] [--udp=<port>:<targetPort>] [--sctp=<port>:<targetPort>] [--icmp] [--mark=<val>] [--secips=<ip>,] [--sources=<ip>,] [--endpoints=<ip>:<weight>,] [--mode=<onearm|fullnat>] [--bgp] [--monitor] [--inatimeout=<to>] [--name=<service-name>] [--attachEP] [--detachEP] [--security=<https|e2ehttps|none>] [--host=<url>] [--ppv2en]",
Short: "Create a LoadBalancer",
Long: `Create a LoadBalancer
Expand Down Expand Up @@ -170,6 +171,7 @@ ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.2
loxicmd create lb 2001::1 --tcp=2020:8080 --endpoints=4ffe::1:1,5ffe::1:1,6ffe::1:1
loxicmd create lb 2001::1 --tcp=2020:8080 --endpoints=31.31.31.1:1,32.32.32.1:1,33.33.33.1:1
loxicmd create lb 10.10.10.254 --sctp=2020:8080 --endpoints=33.33.33.1:1 --attachEP
loxicmd create lb 100.100.100.1 --tcp=8080:80 --endpoints=10.10.10.1:1 --ppv2en
`,
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Expand Down Expand Up @@ -240,6 +242,7 @@ ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.2
Oper: api.LbOP(oper),
Security: api.LbSec(SecStringToNum(o.Security)),
Host: o.Host,
PpV2: o.PPv2En,
}

if o.Mode == "dsr" && targetPort != port {
Expand Down Expand Up @@ -308,6 +311,7 @@ ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.2
createLbCmd.Flags().StringVarP(&o.Security, "security", "", o.Security, "Security mode for load balancer rule")
createLbCmd.Flags().StringVarP(&o.Host, "host", "", o.Host, "Ingress Host URL Path")
createLbCmd.Flags().StringSliceVar(&o.AllowedSources, "sources", o.AllowedSources, "Allowed sources for this rule as '<allowedSources>'")
createLbCmd.Flags().BoolVarP(&o.PPv2En, "ppv2en", "", false, "Enable proxy procotol v2")

return createLbCmd
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/get/get_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NumToSecurty(sec int) string {
return ret
}

func NumToMode(mode int) string {
func NumToMode(mode int, ppv2 bool) string {
var ret string
switch mode {
case 1:
Expand All @@ -117,6 +117,9 @@ func NumToMode(mode int) string {
default:
ret = "default"
}
if ppv2 {
ret += ":ppv2"
}
return ret
}

Expand Down Expand Up @@ -188,7 +191,7 @@ func PrintGetLbResult(resp *http.Response, o api.RESTOptions) {
for i, eps := range lbrule.Endpoints {
if i == 0 {

data = append(data, []string{lbrule.Service.ExternalIP, secIPs, sources, lbrule.Service.Host, fmt.Sprintf("%d", lbrule.Service.Port), protocolStr, lbrule.Service.Name, fmt.Sprintf("%d", lbrule.Service.Block), NumToSelect(int(lbrule.Service.Sel)), NumToMode(int(lbrule.Service.Mode)),
data = append(data, []string{lbrule.Service.ExternalIP, secIPs, sources, lbrule.Service.Host, fmt.Sprintf("%d", lbrule.Service.Port), protocolStr, lbrule.Service.Name, fmt.Sprintf("%d", lbrule.Service.Block), NumToSelect(int(lbrule.Service.Sel)), NumToMode(int(lbrule.Service.Mode), lbrule.Service.PpV2),
eps.EndpointIP, fmt.Sprintf("%d", eps.TargetPort), fmt.Sprintf("%d", eps.Weight), eps.State, eps.Counter})
} else {
data = append(data, []string{"", "", "", "", "", "", "", "", "", "", eps.EndpointIP, fmt.Sprintf("%d", eps.TargetPort), fmt.Sprintf("%d", eps.Weight), eps.State, eps.Counter})
Expand All @@ -197,7 +200,7 @@ func PrintGetLbResult(resp *http.Response, o api.RESTOptions) {
} else {
for i, eps := range lbrule.Endpoints {
if i == 0 {
data = append(data, []string{lbrule.Service.ExternalIP, secIPs, sources, lbrule.Service.Host, fmt.Sprintf("%d", lbrule.Service.Port), protocolStr, lbrule.Service.Name, fmt.Sprintf("%d", lbrule.Service.Block), NumToSelect(int(lbrule.Service.Sel)), NumToMode(int(lbrule.Service.Mode)),
data = append(data, []string{lbrule.Service.ExternalIP, secIPs, sources, lbrule.Service.Host, fmt.Sprintf("%d", lbrule.Service.Port), protocolStr, lbrule.Service.Name, fmt.Sprintf("%d", lbrule.Service.Block), NumToSelect(int(lbrule.Service.Sel)), NumToMode(int(lbrule.Service.Mode), lbrule.Service.PpV2),
eps.EndpointIP, fmt.Sprintf("%d", eps.TargetPort), fmt.Sprintf("%d", eps.Weight), "-", eps.Counter})
} else {
data = append(data, []string{"", "", "", "", "", "", "", "", "", "", eps.EndpointIP, fmt.Sprintf("%d", eps.TargetPort), fmt.Sprintf("%d", eps.Weight), "-", eps.Counter})
Expand All @@ -206,7 +209,7 @@ func PrintGetLbResult(resp *http.Response, o api.RESTOptions) {
}
} else {
table.SetHeader(LOADBALANCER_TITLE)
data = append(data, []string{lbrule.Service.ExternalIP, fmt.Sprintf("%d", lbrule.Service.Port), protocolStr, lbrule.Service.Name, fmt.Sprintf("%d", lbrule.Service.Block), NumToSelect(int(lbrule.Service.Sel)), NumToMode(int(lbrule.Service.Mode)), fmt.Sprintf("%d", len(lbrule.Endpoints)), BoolToMon(lbrule.Service.Monitor)})
data = append(data, []string{lbrule.Service.ExternalIP, fmt.Sprintf("%d", lbrule.Service.Port), protocolStr, lbrule.Service.Name, fmt.Sprintf("%d", lbrule.Service.Block), NumToSelect(int(lbrule.Service.Sel)), NumToMode(int(lbrule.Service.Mode), lbrule.Service.PpV2), fmt.Sprintf("%d", len(lbrule.Endpoints)), BoolToMon(lbrule.Service.Monitor)})
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/api/loadBalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type LoadBalancerService struct {
Oper LbOP `json:"oper,omitempty"`
Security LbSec `json:"security,omitempty" yaml:"security"`
Host string `json:"host,omitempty" yaml:"path"`
PpV2 bool `json:"proxyprotocolv2" yaml:"proxyprotocolv2"`
}

type LoadBalancerEndpoint struct {
Expand Down

0 comments on commit 13c8d12

Please sign in to comment.