Skip to content

Commit dbefb06

Browse files
committed
MEDIUM: client native: adjust handling of errors when calling through interface
1 parent 1caa401 commit dbefb06

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1528
-286
lines changed

adapters/adapters.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ func ConfigVersionMiddleware(client clientnative.HAProxyClient) Adapter {
130130
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
131131
qs := r.URL.Query()
132132
tID := qs.Get("transaction_id")
133-
configuration := client.Configuration()
133+
configuration, err := client.Configuration()
134+
if err != nil {
135+
http.Error(w, err.Error(), http.StatusNotImplemented)
136+
}
134137
tr, _ := configuration.GetTransaction(tID)
135138
var v int64
136-
var err error
137139
if tr != nil && tr.Status == models.TransactionStatusInProgress {
138140
v, err = configuration.GetConfigurationVersion(tr.ID)
139141
} else {

configuration/cluster_sync.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,10 @@ func (c *ClusterSync) issueJoinRequest(url, port, basePath string, registerPath
375375
return err
376376
}
377377
if c.cfg.HAProxy.NodeIDFile != "" {
378-
configuration := c.cli.Configuration()
378+
configuration, errCfg := c.cli.Configuration()
379+
if errCfg != nil {
380+
return errCfg
381+
}
379382
// write id to file
380383
errFID := ioutil.WriteFile(c.cfg.HAProxy.NodeIDFile, []byte(responseData.ID), 0644) // nolint:gosec
381384
if errFID != nil {

configuration/cluster_sync_helpers.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ func (c *ClusterSync) getNodeFacts() map[string]string {
2929
facts["dataplane_cmdline"] = c.cfg.Cmdline.String()
3030
}
3131

32-
processInfos, err := c.cli.Runtime().GetInfo()
32+
runtime, err := c.cli.Runtime()
33+
if err != nil {
34+
log.Errorf("unable to fetch processInfo: %s", err.Error())
35+
}
36+
processInfos, err := runtime.GetInfo()
3337
if err != nil || len(processInfos) < 1 {
3438
log.Error("unable to fetch processInfo")
3539
} else {

configuration/map_sync.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ func (ms *MapSync) SyncAll(client client_native.HAProxyClient) {
5555
for {
5656
select {
5757
case <-ticker.C:
58-
maps, err := client.Runtime().ShowMaps()
58+
runtime, err := client.Runtime()
59+
if err != nil {
60+
log.Warning("show maps sync error: ", err.Error())
61+
continue
62+
}
63+
maps, err := runtime.ShowMaps()
5964
if err != nil {
6065
log.Warning("show maps sync error: ", err.Error())
6166
continue
@@ -83,12 +88,16 @@ func (ms *MapSync) Sync(mp *models.Map, client client_native.HAProxyClient) (boo
8388
if err != nil {
8489
return false, fmt.Errorf("error reading map file: %s %s", mp.File, err.Error())
8590
}
86-
fileEntries := client.Runtime().ParseMapEntriesFromFile(rawFile, false)
91+
runtime, err := client.Runtime()
92+
if err != nil {
93+
return false, fmt.Errorf("getting runtime entries error: id: %s %s", mp.ID, err.Error())
94+
}
95+
fileEntries := runtime.ParseMapEntriesFromFile(rawFile, false)
8796
sort.Slice(fileEntries, func(i, j int) bool { return fileEntries[i].Key < fileEntries[j].Key })
8897

8998
// runtime map entries
9099
id := fmt.Sprintf("#%s", mp.ID)
91-
runtimeEntries, err := client.Runtime().ShowMapEntries(id)
100+
runtimeEntries, err := runtime.ShowMapEntries(id)
92101
if err != nil {
93102
return false, fmt.Errorf("getting runtime entries error: id: %s %s", id, err.Error())
94103
}

configure_data_plane.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,12 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
319319
if cfg.HAProxy.MaxOpenTransactions > 0 {
320320
// creating the threshold limit using the CLI flag as hard quota and current open transactions as starting point
321321
actualCount := func() uint64 {
322-
ts, err := client.Configuration().GetTransactions(models.TransactionStatusInProgress)
322+
configuration, err := client.Configuration()
323+
if err != nil {
324+
log.Errorf("Cannot retrieve current open transactions for rate limit, default to zero (%s)", err.Error())
325+
return 0
326+
}
327+
ts, err := configuration.GetTransactions(models.TransactionStatusInProgress)
323328
if err != nil {
324329
log.Errorf("Cannot retrieve current open transactions for rate limit, default to zero (%s)", err.Error())
325330
return 0
@@ -583,9 +588,13 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
583588
return specification.NewGetSpecificationOK().WithPayload(&m)
584589
})
585590

591+
configurationClient, err := client.Configuration()
592+
if err != nil {
593+
log.Fatal(err)
594+
}
586595
// set up service discovery handlers
587596
discovery := service_discovery.NewServiceDiscoveries(service_discovery.ServiceDiscoveriesParams{
588-
Client: client.Configuration(),
597+
Client: configurationClient,
589598
ReloadAgent: ra,
590599
Context: ctx,
591600
})
@@ -619,16 +628,16 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler {
619628

620629
// create stored AWS instances
621630
for _, data := range cfg.ServiceDiscovery.AWSRegions {
622-
var err error
631+
var errSD error
623632

624633
if data.ID == nil || len(*data.ID) == 0 {
625634
data.ID = service_discovery.NewServiceDiscoveryUUID()
626635
}
627-
if err = service_discovery.ValidateAWSData(data, true); err != nil {
628-
log.Fatalf("Error validating AWS instance: " + err.Error())
636+
if errSD = service_discovery.ValidateAWSData(data, true); errSD != nil {
637+
log.Fatalf("Error validating AWS instance: " + errSD.Error())
629638
}
630-
if err = discovery.AddNode("aws", *data.ID, data); err != nil {
631-
log.Warning("Error creating AWS instance: " + err.Error())
639+
if errSD = discovery.AddNode("aws", *data.ID, data); errSD != nil {
640+
log.Warning("Error creating AWS instance: " + errSD.Error())
632641
}
633642
}
634643
_ = cfg.SaveAWS(cfg.ServiceDiscovery.AWSRegions)
@@ -846,7 +855,7 @@ func configureNativeClient(cyx context.Context, haproxyOptions dataplaneapi_conf
846855
log.Fatalf("error trying to use empty string for SPOE configuration directory")
847856
}
848857

849-
client, err := client_native.New(context.Background(), opt...)
858+
client, err := client_native.New(cyx, opt...)
850859
if err != nil {
851860
log.Fatalf("Error initializing configuration client: %v", err)
852861
}
@@ -994,8 +1003,13 @@ func handleSignals(ctx context.Context, cancel context.CancelFunc, sigs chan os.
9941003
var clientCtx context.Context
9951004
cancel()
9961005
clientCtx, cancel = context.WithCancel(ctx)
997-
client.ReplaceRuntime(configureRuntimeClient(clientCtx, client.Configuration(), haproxyOptions))
998-
log.Info("Reloaded Data Plane API")
1006+
configuration, err := client.Configuration()
1007+
if err != nil {
1008+
log.Infof("Unable to reload Data Plane API: %s", err.Error())
1009+
} else {
1010+
client.ReplaceRuntime(configureRuntimeClient(clientCtx, configuration, haproxyOptions))
1011+
log.Info("Reloaded Data Plane API")
1012+
}
9991013
} else if sig == syscall.SIGUSR2 {
10001014
reloadConfigurationFile(client, haproxyOptions, users)
10011015
}
@@ -1022,7 +1036,12 @@ func reloadConfigurationFile(client client_native.HAProxyClient, haproxyOptions
10221036
func startWatcher(ctx context.Context, client client_native.HAProxyClient, haproxyOptions dataplaneapi_config.HAProxyConfiguration, users *dataplaneapi_config.Users) error {
10231037
cb := func() {
10241038
reloadConfigurationFile(client, haproxyOptions, users)
1025-
if err := client.Configuration().IncrementVersion(); err != nil {
1039+
configuration, err := client.Configuration()
1040+
if err != nil {
1041+
log.Warningf("Failed to increment configuration version: %v", err)
1042+
return
1043+
}
1044+
if err := configuration.IncrementVersion(); err != nil {
10261045
log.Warningf("Failed to increment configuration version: %v", err)
10271046
}
10281047
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/go-openapi/validate v0.19.8
2323
github.com/google/renameio v1.0.1
2424
github.com/google/uuid v1.2.0
25-
github.com/haproxytech/client-native/v3 v3.0.0-rc1.0.20220215222246-5a5df100599a
25+
github.com/haproxytech/client-native/v3 v3.0.0
2626
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220215134434-54d66ef01d26
2727
github.com/hashicorp/consul/api v1.6.0
2828
github.com/hashicorp/hcl v1.0.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
174174
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
175175
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
176176
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
177+
github.com/haproxytech/client-native/v3 v3.0.0 h1:D3bx4ylnkKfRqDVFk15ZLcMyKVTgdgNN25NKAjb6LYo=
178+
github.com/haproxytech/client-native/v3 v3.0.0/go.mod h1:5qDnA7M1HmgJ/CTy6ByUomvnfgAgpH6R8NYZMdcMQ9k=
177179
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220215134434-54d66ef01d26 h1:IWU3pWA4MzYzi0N5NDUgMf06V6bmXCsHok0hBMM1Zik=
178180
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20220215134434-54d66ef01d26/go.mod h1:pEuHx+aFhn0lIdvAg1OaawQfeRkpq1I8HzjtZN4/PLI=
179181
github.com/haproxytech/go-logger v1.0.1-0.20211022075555-178f1cdf4d84 h1:rSLHjJ4VGvMZcGAGQ9GaXuhvdswu1iLVXTThLX6OKN8=

handlers/acl.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ func (h *CreateACLHandlerImpl) Handle(params acl.CreateACLParams, principal inte
7474
return acl.NewCreateACLDefault(int(*e.Code)).WithPayload(e)
7575
}
7676

77-
err := h.Client.Configuration().CreateACL(params.ParentType, params.ParentName, params.Data, t, v)
77+
configuration, err := h.Client.Configuration()
78+
if err != nil {
79+
e := misc.HandleError(err)
80+
return acl.NewCreateACLDefault(int(*e.Code)).WithPayload(e)
81+
}
82+
83+
err = configuration.CreateACL(params.ParentType, params.ParentName, params.Data, t, v)
7884
if err != nil {
7985
e := misc.HandleError(err)
8086
return acl.NewCreateACLDefault(int(*e.Code)).WithPayload(e)
@@ -115,8 +121,13 @@ func (h *DeleteACLHandlerImpl) Handle(params acl.DeleteACLParams, principal inte
115121
}
116122
return acl.NewDeleteACLDefault(int(*e.Code)).WithPayload(e)
117123
}
124+
configuration, err := h.Client.Configuration()
125+
if err != nil {
126+
e := misc.HandleError(err)
127+
return acl.NewDeleteACLDefault(int(*e.Code)).WithPayload(e)
128+
}
118129

119-
err := h.Client.Configuration().DeleteACL(params.Index, params.ParentType, params.ParentName, t, v)
130+
err = configuration.DeleteACL(params.Index, params.ParentType, params.ParentName, t, v)
120131
if err != nil {
121132
e := misc.HandleError(err)
122133
return acl.NewDeleteACLDefault(int(*e.Code)).WithPayload(e)
@@ -143,7 +154,13 @@ func (h *GetACLHandlerImpl) Handle(params acl.GetACLParams, principal interface{
143154
t = *params.TransactionID
144155
}
145156

146-
v, rule, err := h.Client.Configuration().GetACL(params.Index, params.ParentType, params.ParentName, t)
157+
configuration, err := h.Client.Configuration()
158+
if err != nil {
159+
e := misc.HandleError(err)
160+
return acl.NewGetACLDefault(int(*e.Code)).WithPayload(e)
161+
}
162+
163+
v, rule, err := configuration.GetACL(params.Index, params.ParentType, params.ParentName, t)
147164
if err != nil {
148165
e := misc.HandleError(err)
149166
return acl.NewGetACLDefault(int(*e.Code)).WithPayload(e)
@@ -163,7 +180,13 @@ func (h *GetAclsHandlerImpl) Handle(params acl.GetAclsParams, principal interfac
163180
aclName = []string{*params.ACLName}
164181
}
165182

166-
v, rules, err := h.Client.Configuration().GetACLs(params.ParentType, params.ParentName, t, aclName...)
183+
configuration, err := h.Client.Configuration()
184+
if err != nil {
185+
e := misc.HandleError(err)
186+
return acl.NewGetACLDefault(int(*e.Code)).WithPayload(e)
187+
}
188+
189+
v, rules, err := configuration.GetACLs(params.ParentType, params.ParentName, t, aclName...)
167190
if err != nil {
168191
e := misc.HandleContainerGetError(err)
169192
if *e.Code == misc.ErrHTTPOk {
@@ -195,7 +218,12 @@ func (h *ReplaceACLHandlerImpl) Handle(params acl.ReplaceACLParams, principal in
195218
return acl.NewReplaceACLDefault(int(*e.Code)).WithPayload(e)
196219
}
197220

198-
err := h.Client.Configuration().EditACL(params.Index, params.ParentType, params.ParentName, params.Data, t, v)
221+
configuration, err := h.Client.Configuration()
222+
if err != nil {
223+
e := misc.HandleError(err)
224+
return acl.NewReplaceACLDefault(int(*e.Code)).WithPayload(e)
225+
}
226+
err = configuration.EditACL(params.Index, params.ParentType, params.ParentName, params.Data, t, v)
199227
if err != nil {
200228
e := misc.HandleError(err)
201229
return acl.NewReplaceACLDefault(int(*e.Code)).WithPayload(e)

handlers/acl_runtime.go

+54-15
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ type GetACLSHandlerRuntimeImpl struct {
1212
Client client_native.HAProxyClient
1313
}
1414

15-
func (g GetACLSHandlerRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeAclsParams, i interface{}) middleware.Responder {
16-
files, err := g.Client.Runtime().GetACLFiles()
15+
func (h GetACLSHandlerRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeAclsParams, i interface{}) middleware.Responder {
16+
runtime, err := h.Client.Runtime()
17+
if err != nil {
18+
e := misc.HandleError(err)
19+
return acl_runtime.NewGetServicesHaproxyRuntimeAclsDefault(int(*e.Code)).WithPayload(e)
20+
}
21+
22+
files, err := runtime.GetACLFiles()
1723
if err != nil {
1824
e := misc.HandleError(err)
1925
return acl_runtime.NewGetServicesHaproxyRuntimeAclsDefault(int(*e.Code)).WithPayload(e)
@@ -26,8 +32,14 @@ type GetACLHandlerRuntimeImpl struct {
2632
Client client_native.HAProxyClient
2733
}
2834

29-
func (g GetACLHandlerRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeAclsIDParams, i interface{}) middleware.Responder {
30-
aclFile, err := g.Client.Runtime().GetACLFile(params.ID)
35+
func (h GetACLHandlerRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeAclsIDParams, i interface{}) middleware.Responder {
36+
runtime, err := h.Client.Runtime()
37+
if err != nil {
38+
e := misc.HandleError(err)
39+
return acl_runtime.NewGetServicesHaproxyRuntimeAclsIDDefault(int(*e.Code)).WithPayload(e)
40+
}
41+
42+
aclFile, err := runtime.GetACLFile(params.ID)
3143
if err != nil {
3244
e := misc.HandleError(err)
3345
return acl_runtime.NewGetServicesHaproxyRuntimeAclsIDDefault(int(*e.Code)).WithPayload(e)
@@ -40,8 +52,13 @@ type GetACLFileEntriesHandlerRuntimeImpl struct {
4052
Client client_native.HAProxyClient
4153
}
4254

43-
func (g GetACLFileEntriesHandlerRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeACLFileEntriesParams, i interface{}) middleware.Responder {
44-
files, err := g.Client.Runtime().GetACLFilesEntries(params.ACLID)
55+
func (h GetACLFileEntriesHandlerRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeACLFileEntriesParams, i interface{}) middleware.Responder {
56+
runtime, err := h.Client.Runtime()
57+
if err != nil {
58+
e := misc.HandleError(err)
59+
return acl_runtime.NewGetServicesHaproxyRuntimeACLFileEntriesDefault(int(*e.Code)).WithPayload(e)
60+
}
61+
files, err := runtime.GetACLFilesEntries(params.ACLID)
4562
if err != nil {
4663
e := misc.HandleError(err)
4764
return acl_runtime.NewGetServicesHaproxyRuntimeACLFileEntriesDefault(int(*e.Code)).WithPayload(e)
@@ -54,16 +71,21 @@ type PostACLFileEntryHandlerRuntimeImpl struct {
5471
Client client_native.HAProxyClient
5572
}
5673

57-
func (c PostACLFileEntryHandlerRuntimeImpl) Handle(params acl_runtime.PostServicesHaproxyRuntimeACLFileEntriesParams, i interface{}) middleware.Responder {
74+
func (h PostACLFileEntryHandlerRuntimeImpl) Handle(params acl_runtime.PostServicesHaproxyRuntimeACLFileEntriesParams, i interface{}) middleware.Responder {
5875
var err error
76+
runtime, err := h.Client.Runtime()
77+
if err != nil {
78+
e := misc.HandleError(err)
79+
return acl_runtime.NewPostServicesHaproxyRuntimeACLFileEntriesDefault(int(*e.Code)).WithPayload(e)
80+
}
5981

60-
if err = c.Client.Runtime().AddACLFileEntry(params.ACLID, params.Data.Value); err != nil {
82+
if err = runtime.AddACLFileEntry(params.ACLID, params.Data.Value); err != nil {
6183
e := misc.HandleError(err)
6284
return acl_runtime.NewPostServicesHaproxyRuntimeACLFileEntriesDefault(int(*e.Code)).WithPayload(e)
6385
}
6486

6587
var fileEntry *models.ACLFileEntry
66-
fileEntry, err = c.Client.Runtime().GetACLFileEntry(params.ACLID, params.Data.Value)
88+
fileEntry, err = runtime.GetACLFileEntry(params.ACLID, params.Data.Value)
6789
if err != nil {
6890
e := misc.HandleError(err)
6991
return acl_runtime.NewPostServicesHaproxyRuntimeACLFileEntriesDefault(int(*e.Code)).WithPayload(e)
@@ -76,8 +98,14 @@ type GetACLFileEntryRuntimeImpl struct {
7698
Client client_native.HAProxyClient
7799
}
78100

79-
func (g GetACLFileEntryRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeACLFileEntriesIDParams, i interface{}) middleware.Responder {
80-
fileEntry, err := g.Client.Runtime().GetACLFileEntry(params.ACLID, params.ID)
101+
func (h GetACLFileEntryRuntimeImpl) Handle(params acl_runtime.GetServicesHaproxyRuntimeACLFileEntriesIDParams, i interface{}) middleware.Responder {
102+
runtime, err := h.Client.Runtime()
103+
if err != nil {
104+
e := misc.HandleError(err)
105+
return acl_runtime.NewGetServicesHaproxyRuntimeACLFileEntriesIDDefault(int(*e.Code)).WithPayload(e)
106+
}
107+
108+
fileEntry, err := runtime.GetACLFileEntry(params.ACLID, params.ID)
81109
if err != nil {
82110
e := misc.HandleError(err)
83111
return acl_runtime.NewGetServicesHaproxyRuntimeACLFileEntriesIDDefault(int(*e.Code)).WithPayload(e)
@@ -90,8 +118,13 @@ type DeleteACLFileEntryHandlerRuntimeImpl struct {
90118
Client client_native.HAProxyClient
91119
}
92120

93-
func (d DeleteACLFileEntryHandlerRuntimeImpl) Handle(params acl_runtime.DeleteServicesHaproxyRuntimeACLFileEntriesIDParams, i interface{}) middleware.Responder {
94-
if err := d.Client.Runtime().DeleteACLFileEntry(params.ACLID, "#"+params.ID); err != nil {
121+
func (h DeleteACLFileEntryHandlerRuntimeImpl) Handle(params acl_runtime.DeleteServicesHaproxyRuntimeACLFileEntriesIDParams, i interface{}) middleware.Responder {
122+
runtime, err := h.Client.Runtime()
123+
if err != nil {
124+
e := misc.HandleError(err)
125+
return acl_runtime.NewDeleteServicesHaproxyRuntimeACLFileEntriesIDDefault(int(*e.Code)).WithPayload(e)
126+
}
127+
if err := runtime.DeleteACLFileEntry(params.ACLID, "#"+params.ID); err != nil {
95128
e := misc.HandleError(err)
96129
return acl_runtime.NewDeleteServicesHaproxyRuntimeACLFileEntriesIDDefault(int(*e.Code)).WithPayload(e)
97130
}
@@ -103,8 +136,14 @@ type ACLRuntimeAddPayloadRuntimeACLHandlerImpl struct {
103136
Client client_native.HAProxyClient
104137
}
105138

106-
func (a ACLRuntimeAddPayloadRuntimeACLHandlerImpl) Handle(params acl_runtime.AddPayloadRuntimeACLParams, i interface{}) middleware.Responder {
107-
err := a.Client.Runtime().AddACLAtomic(params.ACLID, params.Data)
139+
func (h ACLRuntimeAddPayloadRuntimeACLHandlerImpl) Handle(params acl_runtime.AddPayloadRuntimeACLParams, i interface{}) middleware.Responder {
140+
runtime, err := h.Client.Runtime()
141+
if err != nil {
142+
e := misc.HandleError(err)
143+
return acl_runtime.NewAddPayloadRuntimeACLDefault(int(*e.Code)).WithPayload(e)
144+
}
145+
146+
err = runtime.AddACLAtomic(params.ACLID, params.Data)
108147
if err != nil {
109148
status := misc.GetHTTPStatusFromErr(err)
110149
return acl_runtime.NewAddPayloadRuntimeACLDefault(status).WithPayload(misc.SetError(status, err.Error()))

0 commit comments

Comments
 (0)