/
adminapi_provider.go
79 lines (67 loc) · 2.48 KB
/
adminapi_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package admission
import (
"github.com/kong/go-kong/kong"
"github.com/kong/kubernetes-ingress-controller/v2/internal/adminapi"
)
// GatewayClientsProvider returns the most recent set of Gateway Admin API clients.
type GatewayClientsProvider interface {
GatewayClients() []*adminapi.Client
}
// DefaultAdminAPIServicesProvider allows getting Admin API services that require having at least one Gateway discovered.
// In the case there's no Gateways, it will return `false` from every method, signalling there's no Gateway available.
type DefaultAdminAPIServicesProvider struct {
gatewayClientsProvider GatewayClientsProvider
}
func NewDefaultAdminAPIServicesProvider(gatewaysProvider GatewayClientsProvider) *DefaultAdminAPIServicesProvider {
return &DefaultAdminAPIServicesProvider{gatewayClientsProvider: gatewaysProvider}
}
func (p DefaultAdminAPIServicesProvider) GetConsumersService() (kong.AbstractConsumerService, bool) {
c, ok := p.designatedAdminAPIClient()
if !ok {
return nil, ok
}
return c.Consumers, true
}
func (p DefaultAdminAPIServicesProvider) GetPluginsService() (kong.AbstractPluginService, bool) {
c, ok := p.designatedAdminAPIClient()
if !ok {
return nil, ok
}
return c.Plugins, true
}
func (p DefaultAdminAPIServicesProvider) GetConsumerGroupsService() (kong.AbstractConsumerGroupService, bool) {
c, ok := p.designatedAdminAPIClient()
if !ok {
return nil, ok
}
return c.ConsumerGroups, true
}
func (p DefaultAdminAPIServicesProvider) GetInfoService() (kong.AbstractInfoService, bool) {
c, ok := p.designatedAdminAPIClient()
if !ok {
return nil, ok
}
return c.Info, true
}
func (p DefaultAdminAPIServicesProvider) GetRoutesService() (kong.AbstractRouteService, bool) {
c, ok := p.designatedAdminAPIClient()
if !ok {
return nil, ok
}
return c.Routes, true
}
func (p DefaultAdminAPIServicesProvider) designatedAdminAPIClient() (*kong.Client, bool) {
gwClients := p.gatewayClientsProvider.GatewayClients()
if len(gwClients) == 0 {
return nil, false
}
// For now using first client is kind of OK. Using Consumer and Plugin
// services from first kong client should theoretically return the same
// results as for all other clients. There might be instances where
// configurations in different Kong Gateways are ever so slightly
// different but that shouldn't cause a fatal failure.
//
// TODO: We should take a look at this sooner rather than later.
// https://github.com/Kong/kubernetes-ingress-controller/issues/3363
return gwClients[0].AdminAPIClient(), true
}