From 275e5c38b8e3fe1424de5c898b6edde3ec3231b2 Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Thu, 29 Jun 2023 17:51:19 +0900 Subject: [PATCH] Introduced a managed flag for LB rules for saving --- cmd/create/create_endpoint.go | 4 ++-- cmd/create/create_loadbalancer.go | 8 ++++---- cmd/delete/delete_endpoint.go | 34 +++++++++++++++---------------- cmd/get/get_loadbalancer.go | 22 +++++++++++++++++++- pkg/api/loadBalancer.go | 7 ++++--- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/cmd/create/create_endpoint.go b/cmd/create/create_endpoint.go index 33a2893..441f317 100644 --- a/cmd/create/create_endpoint.go +++ b/cmd/create/create_endpoint.go @@ -29,7 +29,7 @@ import ( type CreateEndPointOptions struct { Host string - Name string + Name string ProbeType string ProbeReq string ProbeResp string @@ -84,7 +84,7 @@ ex) loxicmd create endpoint 32.32.32.1 --name=32.32.32.1_http_8080 --probetype=h return } } - + if o.ProbeType == "ping" && o.ProbePort != 0 { fmt.Printf("probeport should be 0 for '%s' probes\n", o.ProbeType) return diff --git a/cmd/create/create_loadbalancer.go b/cmd/create/create_loadbalancer.go index 5359241..2b0af32 100644 --- a/cmd/create/create_loadbalancer.go +++ b/cmd/create/create_loadbalancer.go @@ -44,7 +44,7 @@ type CreateLoadBalancerOptions struct { Mark uint16 SCTP []string Endpoints []string - SecIPs []string + SecIPs []string Select string } @@ -202,13 +202,13 @@ ex) loxicmd create lb 192.168.0.200 --tcp=80:32015 --endpoints=10.212.0.1:1,10.2 lbModel.Endpoints = append(lbModel.Endpoints, ep) } - for _, sip:= range o.SecIPs { + for _, sip := range o.SecIPs { sp := api.LoadBalancerSecIp{ SecondaryIP: sip, } lbModel.SecondaryIPs = append(lbModel.SecondaryIPs, sp) } - + resp, err := LoadbalancerAPICall(restOptions, lbModel) if err != nil { fmt.Printf("Error: %s\n", err.Error()) @@ -320,4 +320,4 @@ func LoadbalancerAPICall(restOptions *api.RESTOptions, lbModel api.LoadBalancerM } return client.LoadBalancer().Create(ctx, lbModel) -} \ No newline at end of file +} diff --git a/cmd/delete/delete_endpoint.go b/cmd/delete/delete_endpoint.go index 24bec94..953cb05 100644 --- a/cmd/delete/delete_endpoint.go +++ b/cmd/delete/delete_endpoint.go @@ -18,26 +18,26 @@ package delete import ( "context" "fmt" + "loxicmd/pkg/api" "net" "net/http" "os" - "time" "strconv" - "loxicmd/pkg/api" + "time" "github.com/spf13/cobra" ) type DeleteEndPoint struct { - Host string - Name string - ProbeType string - ProbePort int + Host string + Name string + ProbeType string + ProbePort int } func NewDeleteEndPointCmd(restOptions *api.RESTOptions) *cobra.Command { o := DeleteEndPoint{} - + var deleteEndPointCmd = &cobra.Command{ Use: "endpoint IP [--name=] [--probetype=] [--probeport=]", Short: "Delete a LB EndPoint from monitoring", @@ -88,14 +88,14 @@ ex) loxicmd delete endpoint 31.31.31.31 --name=31.31.31.31_http_8080 --probetype return } - /* subResources := []string{ - "epipaddress", o.Host, - "name", o.Name, - "probetype", o.ProbeType, - "probeport", strconv.Itoa(o.ProbePort), - } - resp, err := client.EndPoint().SubResources(subResources).Delete(ctx) - */ + /* subResources := []string{ + "epipaddress", o.Host, + "name", o.Name, + "probetype", o.ProbeType, + "probeport", strconv.Itoa(o.ProbePort), + } + resp, err := client.EndPoint().SubResources(subResources).Delete(ctx) + */ subResources := []string{ "epipaddress", o.Host, } @@ -103,7 +103,7 @@ ex) loxicmd delete endpoint 31.31.31.31 --name=31.31.31.31_http_8080 --probetype qmap := map[string]string{} qmap["name"] = o.Name qmap["probe_type"] = o.ProbeType - qmap["probe_port"] = strconv.Itoa(o.ProbePort) + qmap["probe_port"] = strconv.Itoa(o.ProbePort) resp, err := client.EndPoint().SubResources(subResources).Query(qmap).Delete(ctx) @@ -123,4 +123,4 @@ ex) loxicmd delete endpoint 31.31.31.31 --name=31.31.31.31_http_8080 --probetype deleteEndPointCmd.Flags().StringVar(&o.ProbeType, "probetype", "ping", "Probe-type:ping,http,https,udp,tcp,sctp,none") deleteEndPointCmd.Flags().IntVar(&o.ProbePort, "probeport", 0, "If probe is http,https,tcp,udp,sctp one can specify custom l4port to use") return deleteEndPointCmd -} \ No newline at end of file +} diff --git a/cmd/get/get_loadbalancer.go b/cmd/get/get_loadbalancer.go index e2644fb..e2bbde4 100644 --- a/cmd/get/get_loadbalancer.go +++ b/cmd/get/get_loadbalancer.go @@ -189,6 +189,8 @@ func LoadbalancerAPICall(restOptions *api.RESTOptions) (*http.Response, error) { } func Lbdump(restOptions *api.RESTOptions, path string) (string, error) { + lbresp := api.LbRuleModGet{} + dresp := api.LbRuleModGet{} // File Open fileP := []string{"lbconfig_", ".txt"} t := time.Now() @@ -218,8 +220,26 @@ func Lbdump(restOptions *api.RESTOptions, path string) (string, error) { if err != nil { fmt.Printf("Error: Failed to read HTTP response: (%s)\n", err.Error()) } + + if err := json.Unmarshal(resultByte, &lbresp); err != nil { + fmt.Printf("Error: Failed to unmarshal HTTP response: (%s)\n", err.Error()) + return "", err + } + + for _, lbrule := range lbresp.LbRules { + if !lbrule.Service.Managed { + dresp.LbRules = append(dresp.LbRules, lbrule) + } + } + + dumpBytes, err := json.Marshal(dresp) + if err != nil { + fmt.Printf("Error: Failed to marshal dump LB rules (%s)\n", err.Error()) + return "", err + } + // Write - _, err = f.Write(resultByte) + _, err = f.Write(dumpBytes) if err != nil { fmt.Println("File write error") } diff --git a/pkg/api/loadBalancer.go b/pkg/api/loadBalancer.go index b8501e2..b3e7a04 100644 --- a/pkg/api/loadBalancer.go +++ b/pkg/api/loadBalancer.go @@ -31,9 +31,9 @@ type LbRuleModGet struct { } type LoadBalancerModel struct { - Service LoadBalancerService `json:"serviceArguments" yaml:"serviceArguments"` - SecondaryIPs []LoadBalancerSecIp `json:"secondaryIPs" yaml:"secondaryIPs"` - Endpoints []LoadBalancerEndpoint `json:"endpoints" yaml:"endpoints"` + Service LoadBalancerService `json:"serviceArguments" yaml:"serviceArguments"` + SecondaryIPs []LoadBalancerSecIp `json:"secondaryIPs" yaml:"secondaryIPs"` + Endpoints []LoadBalancerEndpoint `json:"endpoints" yaml:"endpoints"` } type LoadBalancerService struct { @@ -46,6 +46,7 @@ type LoadBalancerService struct { Monitor bool `json:"Monitor" yaml:"Monitor"` Timeout uint32 `json:"inactiveTimeOut" yaml:"inactiveTimeOut"` Block uint16 `json:"block" yaml:"block"` + Managed bool `json:"managed,omitempty" yaml:"managed"` } type LoadBalancerEndpoint struct {