-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get Admin API services dynamically in admission webhook
- Loading branch information
Showing
8 changed files
with
277 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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) 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package admission_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v2/internal/adminapi" | ||
"github.com/kong/kubernetes-ingress-controller/v2/internal/admission" | ||
) | ||
|
||
type fakeGatewayClientsProvider struct { | ||
clients []*adminapi.Client | ||
} | ||
|
||
func (f fakeGatewayClientsProvider) GatewayClients() []*adminapi.Client { | ||
return f.clients | ||
} | ||
|
||
func TestDefaultAdminAPIServicesProvider(t *testing.T) { | ||
t.Run("no clients available should return false from methods", func(t *testing.T) { | ||
p := admission.NewDefaultAdminAPIServicesProvider(fakeGatewayClientsProvider{}) | ||
|
||
_, ok := p.GetConsumersService() | ||
require.False(t, ok) | ||
|
||
_, ok = p.GetPluginsService() | ||
require.False(t, ok) | ||
}) | ||
|
||
t.Run("when clients available should return first one", func(t *testing.T) { | ||
firstClient := lo.Must(adminapi.NewTestClient("localhost:8080")) | ||
p := admission.NewDefaultAdminAPIServicesProvider(fakeGatewayClientsProvider{ | ||
clients: []*adminapi.Client{ | ||
firstClient, | ||
lo.Must(adminapi.NewTestClient("localhost:8081")), | ||
}, | ||
}) | ||
|
||
consumersSvc, ok := p.GetConsumersService() | ||
require.True(t, ok) | ||
require.Equal(t, firstClient.AdminAPIClient().Consumers, consumersSvc) | ||
|
||
pluginsSvc, ok := p.GetPluginsService() | ||
require.True(t, ok) | ||
require.Equal(t, firstClient.AdminAPIClient().Plugins, pluginsSvc) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.