Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/loxilb-io/loxicmd into main
Browse files Browse the repository at this point in the history
  • Loading branch information
inhogog2 committed Jun 28, 2024
2 parents a81cabb + 5d4bac0 commit 4783d89
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
19 changes: 18 additions & 1 deletion cmd/create/create_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type CreateLoadBalancerOptions struct {
ICMP bool
Mode string
BGP bool
Security string
Monitor bool
Attach bool
Detach bool
Expand Down Expand Up @@ -113,11 +114,24 @@ func ModeToNum(sel string) int {
return ret
}

func SecStringToNum(sec string) int {
var ret int
switch sec {
case "https":
ret = 1
case "tls":
ret = 1
default:
ret = 0
}
return ret
}

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>,][--endpoints=<ip>:<weight>,] [--mode=<onearm|fullnat>] [--bgp] [--monitor] [--inatimeout=<to>] [--name=<service-name>] [--attachEP] [--detachEP]",
Use: "lb IP [--select=<rr|hash|priority|persist>] [--tcp=<port>:<targetPort>] [--udp=<port>:<targetPort>] [--sctp=<port>:<targetPort>] [--icmp] [--mark=<val>] [--secips=<ip>,][--endpoints=<ip>:<weight>,] [--mode=<onearm|fullnat>] [--bgp] [--monitor] [--inatimeout=<to>] [--name=<service-name>] [--attachEP] [--detachEP] [--security=<https|none>]",
Short: "Create a LoadBalancer",
Long: `Create a LoadBalancer
Expand All @@ -134,6 +148,7 @@ func NewCreateLoadBalancerCmd(restOptions *api.RESTOptions) *cobra.Command {
hostonearm - LB operating in host one-arm
ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.212.0.2:1,10.212.0.3:1
loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.212.0.2:1,10.212.0.3:1 --security=https
loxicmd create lb 192.168.0.200 --tcp=80:32015 --name="http-service" --endpoints=10.212.0.1:1,10.212.0.2:1,10.212.0.3:1
loxicmd create lb 192.168.0.200 --udp=80:32015 --endpoints=10.212.0.1:1,10.212.0.2:1,10.212.0.3:1 --mark=10
loxicmd create lb 192.168.0.200 --tcp=80:32015 --udp=80:32015 --endpoints=10.212.0.1:1,10.212.0.2:1,10.212.0.3:1
Expand Down Expand Up @@ -212,6 +227,7 @@ ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.2
Block: o.Mark,
Name: o.Name,
Oper: api.LbOP(oper),
Security: api.LbSec(SecStringToNum(o.Security)),
}

if o.Mode == "dsr" && targetPort != port {
Expand Down Expand Up @@ -270,6 +286,7 @@ ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.2
createLbCmd.Flags().StringVarP(&o.Name, "name", "", o.Name, "Name for load balancer rule")
createLbCmd.Flags().BoolVarP(&o.Attach, "attachEP", "", false, "Attach endpoints to the load balancer rule")
createLbCmd.Flags().BoolVarP(&o.Detach, "detachEP", "", false, "Detach endpoints from the load balancer rule")
createLbCmd.Flags().StringVarP(&o.Security, "security", "", o.Security, "Security mode for load balancer rule")

return createLbCmd
}
Expand Down
24 changes: 21 additions & 3 deletions cmd/get/get_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ func NumToSelect(sel int) string {
return ret
}

func NumToSecurty(sec int) string {
var ret string
switch sec {
case 0:
ret = ""
case 1:
ret = "https"
default:
ret = ""
}
return ret
}

func NumToMode(mode int) string {
var ret string
switch mode {
Expand Down Expand Up @@ -146,6 +159,10 @@ func PrintGetLbResult(resp *http.Response, o api.RESTOptions) {
if o.ServiceName != "" && o.ServiceName != lbrule.Service.Name {
continue
}
protocolStr := lbrule.Service.Protocol
if lbrule.Service.Security != 0 {
protocolStr += fmt.Sprintf(":%s", NumToSecurty(int(lbrule.Service.Security)))
}
if o.PrintOption == "wide" {
table.SetHeader(LOADBALANCER_WIDE_TITLE)
secIPs = ""
Expand All @@ -159,7 +176,8 @@ func PrintGetLbResult(resp *http.Response, o api.RESTOptions) {
if lbrule.Service.Monitor {
for i, eps := range lbrule.Endpoints {
if i == 0 {
data = append(data, []string{lbrule.Service.ExternalIP, secIPs, fmt.Sprintf("%d", lbrule.Service.Port), lbrule.Service.Protocol, 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, 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)),
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 @@ -168,7 +186,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, fmt.Sprintf("%d", lbrule.Service.Port), lbrule.Service.Protocol, 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, 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)),
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 @@ -177,7 +195,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), lbrule.Service.Protocol, 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)), fmt.Sprintf("%d", len(lbrule.Endpoints)), BoolToMon(lbrule.Service.Monitor)})
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/api/loadBalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type LoadBalancer struct {
type EpSelect uint
type LbMode int32
type LbOP int32
type LbSec int32

type LbRuleModGet struct {
LbRules []LoadBalancerModel `json:"lbAttr"`
Expand All @@ -51,6 +52,7 @@ type LoadBalancerService struct {
Managed bool `json:"managed,omitempty" yaml:"managed"`
Name string `json:"name,omitempty" yaml:"name"`
Oper LbOP `json:"oper,omitempty"`
Security LbSec `json:"security,omitempty" yaml:"security"`
}

type LoadBalancerEndpoint struct {
Expand Down

0 comments on commit 4783d89

Please # to comment.