Skip to content

Instantly share code, notes, and snippets.

@bugbuilder
Created September 22, 2018 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugbuilder/4cfe2fda8181a1c5a2955b87f33449d4 to your computer and use it in GitHub Desktop.
Save bugbuilder/4cfe2fda8181a1c5a2955b87f33449d4 to your computer and use it in GitHub Desktop.
// plugins in Kong
currentGlobalPlugins := make(map[string]kongadminv1.Plugin)
for _, plugin := range plugins.Items {
if plugin.Route == "" && plugin.Service == "" {
currentGlobalPlugins[plugin.Name] = plugin
}
}
// sync plugins to Kong
for pluginName, pluginInk8s := range targetPluginMap {
p := &kongadminv1.Plugin{
Name: pluginName,
Config: kongadminv1.Configuration(pluginInk8s.Config),
}
// plugin exists?
if pluginInKong, ok := currentGlobalPlugins[pluginName]; !ok {
// no, create it
// check if the plugin will be used for a particular consumer only
if pluginInk8s.ConsumerRef != "" {
consumer, err := n.store.GetKongConsumer(pluginInk8s.Namespace, pluginInk8s.ConsumerRef)
if err != nil {
glog.Errorf("The plugin '%v' will not be applied. Unexpected error searching for consumer %v: %v",
pluginName, pluginInk8s.ConsumerRef, err)
continue
}
p.Consumer = fmt.Sprintf("%v", consumer.GetUID())
}
_, res := client.Plugins().CreateGlobal(p)
if res.StatusCode != http.StatusCreated {
return errors.Wrap(res.Error(), fmt.Sprintf("creating a global Kong plugin %v", p))
}
} else {
// plugin exists, is the configuration up to date
if !pluginDeepEqual(pluginInk8s.Config, &pluginInKong) {
// no, update it
_, res := client.Plugins().Patch(pluginInKong.ID, p)
if res.StatusCode != http.StatusOK {
return errors.Wrap(res.Error(), fmt.Sprintf("updating a global Kong plugin %v", p))
}
}
}
// remove from the current list, all that remain in the current list will be deleted
delete(currentGlobalPlugins, pluginName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment