Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/new plugins anns #124

Merged
merged 2 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion internal/ingress/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import (
// pluginAnnotationSuffix sufix of kong annotations to configure plugins
const pluginAnnotationSuffix = "plugin.konghq.com"

const pluginListAnnotation = "plugins.konghq.com"

const configurationAnnotation = "configuration.konghq.com"

// ExtractKongPluginAnnotations extracts information about kong plugins
// configured using annotations.
// configured using plugin.konghq.com annotation.
// DEPRECATED, please use ExtractKongPluginsFromAnnotations instead.
func ExtractKongPluginAnnotations(anns map[string]string) map[string][]string {
ka := make(map[string][]string, 0)
for k, v := range anns {
Expand All @@ -47,6 +50,24 @@ func ExtractKongPluginAnnotations(anns map[string]string) map[string][]string {
return ka
}

// ExtractKongPluginsFromAnnotations extracts information about Kong
// Plugins configured using plugins.konghq.com annotation.
// This returns a list of KongPlugin resource names that should be applied.
func ExtractKongPluginsFromAnnotations(anns map[string]string) []string {
var kongPluginCRs []string
v, ok := anns[pluginListAnnotation]
if !ok {
return kongPluginCRs
}
for _, kongPlugin := range strings.Split(v, ",") {
s := strings.TrimSpace(kongPlugin)
if s != "" {
kongPluginCRs = append(kongPluginCRs, s)
}
}
return kongPluginCRs
}

// ExtractConfigurationName extracts the name of the KongIngress object that holds
// information about the configuration to use in Routes, Services and Upstreams
func ExtractConfigurationName(anns map[string]string) string {
Expand Down
14 changes: 14 additions & 0 deletions internal/ingress/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ func TestExtractKongPluginAnnotations(t *testing.T) {
}
}

func TestExtractKongPluginsFromAnnotations(t *testing.T) {
data := map[string]string{
"plugins.konghq.com": "kp-rl, kp-cors",
}

ka := ExtractKongPluginsFromAnnotations(data)
if len(ka) != 2 {
t.Errorf("expected two keys but %v returned", len(ka))
}
if ka[0] != "kp-rl" {
t.Errorf("expected first element to be 'kp-rl'")
}
}

func TestExtractConfigurationName(t *testing.T) {
data := map[string]string{
"configuration.konghq.com": "demo",
Expand Down
59 changes: 35 additions & 24 deletions internal/ingress/controller/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@ func syncTargets(upstream string, ingressEndpopint *ingress.Backend, client *kon
return nil
}

// getPluginsFromAnnotations extracts plugins to be applied on an ingress/service from annotations
func (n *NGINXController) getPluginsFromAnnotations(namespace string, anns map[string]string) (map[string]*pluginv1.KongPlugin, error) {
pluginAnnotations := annotations.ExtractKongPluginAnnotations(anns)
pluginsInk8s := make(map[string]*pluginv1.KongPlugin)
for plugin, crdNames := range pluginAnnotations {
for _, crdName := range crdNames {
// search configured plugin CRD in k8s
k8sPlugin, err := n.store.GetKongPlugin(namespace, crdName)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("searching plugin KongPlugin %v", crdName))
}
pluginsInk8s[plugin] = k8sPlugin
}
}
pluginList := annotations.ExtractKongPluginsFromAnnotations(anns)
// override plugins configured by new annotation
for _, plugin := range pluginList {
k8sPlugin, err := n.store.GetKongPlugin(namespace, plugin)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("searching plugin KongPlugin %v", plugin))
}
// ignore plugins with no name
if k8sPlugin.PluginName == "" {
hbagdi marked this conversation as resolved.
Show resolved Hide resolved
glog.Errorf("KongPlugin Custom resource '%v' has no `plugin` property, the plugin will not be configured", k8sPlugin.Name)
continue
}
pluginsInk8s[k8sPlugin.PluginName] = k8sPlugin
}
return pluginsInk8s, nil
}

// syncServices reconciles the state between the ingress controller and
// kong services. After the synchronization of services we also check
// if there was any changes to the kong plugins applied to the service
Expand Down Expand Up @@ -409,18 +440,8 @@ func (n *NGINXController) syncServices(ingressCfg *ingress.Configuration) (bool,
}

// Get plugin annotations from k8s, these plugins should be configured for this service
pluginAnnotations := annotations.ExtractKongPluginAnnotations(location.Service.GetAnnotations())
pluginsInk8s := make(map[string]*pluginv1.KongPlugin)
for plugin, crdNames := range pluginAnnotations {
for _, crdName := range crdNames {
// search configured plugin CRD in k8s
k8sPlugin, err := n.store.GetKongPlugin(location.Ingress.Namespace, crdName)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("searching plugin KongPlugin %v", crdName))
}
pluginsInk8s[plugin] = k8sPlugin
}
}
anns := location.Service.GetAnnotations()
pluginsInk8s, err := n.getPluginsFromAnnotations(location.Ingress.Namespace, anns)

// Get plugins configured in Kong currently
plugins, err := client.Plugins().GetAllByService(svc.ID)
Expand Down Expand Up @@ -830,18 +851,8 @@ func (n *NGINXController) syncRoutes(ingressCfg *ingress.Configuration) (bool, e
continue
}

pluginAnnotations := annotations.ExtractKongPluginAnnotations(location.Ingress.GetAnnotations())
pluginsInk8s := make(map[string]*pluginv1.KongPlugin)
for plugin, crdNames := range pluginAnnotations {
for _, crdName := range crdNames {
// search configured plugin CRD in k8s
k8sPlugin, err := n.store.GetKongPlugin(location.Ingress.Namespace, crdName)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("searching plugin KongPlugin %v", crdName))
}
pluginsInk8s[plugin] = k8sPlugin
}
}
anns := location.Ingress.GetAnnotations()
pluginsInk8s, err := n.getPluginsFromAnnotations(location.Ingress.Namespace, anns)

// Get plugins configured in Kong currently
plugins, err := client.Plugins().GetAllByRoute(route.ID)
Expand Down