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(extensions): Automatically apply extension configs without restarting API-Server #15574

Merged
merged 9 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 6 additions & 4 deletions docs/developer-guide/extensions/proxy-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data:
Once the proxy extension is enabled, it can be configured in the main
Argo CD configmap ([argocd-cm][2]).

The example below demonstrate all possible configurations available
The example below demonstrates all possible configurations available
for proxy extensions:

```yaml
Expand Down Expand Up @@ -60,9 +60,11 @@ data:
server: https://some-cluster
```

If a the configuration is changed, Argo CD Server will need to be
restarted as the proxy handlers are only registered once during the
initialization of the server.
Note: There is no need to restart Argo CD Server after modifiying the
`extension.config` entry in Argo CD configmap. Changes will be
automatically applied. A new proxy registry will be built making
all new incoming extensions requests (`<argocd-host>/extensions/*`) to
respect the new configuration.

Every configuration entry is explained below:

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/gosimple/slug v1.13.1
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,6 @@ github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down
87 changes: 64 additions & 23 deletions server/extension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"time"

"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -300,6 +299,7 @@ type Manager struct {
application ApplicationGetter
project ProjectGetter
rbac RbacEnforcer
registry ExtensionRegistry
}

// NewManager will initialize a new manager.
Expand All @@ -313,6 +313,11 @@ func NewManager(log *log.Entry, sg SettingsGetter, ag ApplicationGetter, pg Proj
}
}

// ExtensionRegistry is an in memory registry that contains contains all
// proxies for all extensions. The key is the extension name defined in
// the Argo CD configmap.
type ExtensionRegistry map[string]ProxyRegistry

// ProxyRegistry is an in memory registry that contains all proxies for a
// given extension. Different extensions will have independent proxy registries.
// This is required to address the use case when one extension is configured with
Expand Down Expand Up @@ -344,6 +349,10 @@ func proxyKey(extName, cName, cServer string) ProxyKey {
}

func parseAndValidateConfig(s *settings.ArgoCDSettings) (*ExtensionConfigs, error) {
leoluz marked this conversation as resolved.
Show resolved Hide resolved
if s.ExtensionConfig == "" {
return nil, fmt.Errorf("no extensions configurations found")
}

extConfigMap := map[string]interface{}{}
err := yaml.Unmarshal([]byte(s.ExtensionConfig), &extConfigMap)
if err != nil {
Expand Down Expand Up @@ -383,6 +392,9 @@ func validateConfigs(configs *ExtensionConfigs) error {
}
exts[ext.Name] = struct{}{}
svcTotal := len(ext.Backend.Services)
if svcTotal == 0 {
return fmt.Errorf("no backend service configured for extension %s", ext.Name)
}
for _, svc := range ext.Backend.Services {
if svc.URL == "" {
return fmt.Errorf("extensions.backend.services.url must be configured")
Expand Down Expand Up @@ -465,25 +477,30 @@ func applyProxyConfigDefaults(c *ProxyConfig) {
}
}

// RegisterHandlers will retrieve all configured extensions
// and register the respective http handlers in the given
// router.
func (m *Manager) RegisterHandlers(r *mux.Router) error {
m.log.Info("Registering extension handlers...")
// RegisterExtensions will retrieve all extensions configurations
// and update the extension registry.
func (m *Manager) RegisterExtensions() error {
settings, err := m.settings.Get()
if err != nil {
return fmt.Errorf("error getting settings: %s", err)
}

if settings.ExtensionConfig == "" {
return fmt.Errorf("No extensions configurations found")
err = m.UpdateExtensionRegistry(settings)
if err != nil {
return fmt.Errorf("error updating extension registry: %s", err)
}
return nil
}

extConfigs, err := parseAndValidateConfig(settings)
// UpdateExtensionRegistry will first parse and validate the extensions
// configurations from the given settings. If no errors are found, it will
// iterate over the given configurations building a new extension registry.
// At the end, it will update the manager with the newly created registry.
func (m *Manager) UpdateExtensionRegistry(s *settings.ArgoCDSettings) error {
extConfigs, err := parseAndValidateConfig(s)
if err != nil {
return fmt.Errorf("error parsing extension config: %s", err)
}
return m.registerExtensions(r, extConfigs)
return m.updateExtensionRegistry(extConfigs)
crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
}

// appendProxy will append the given proxy in the given registry. Will use
Expand Down Expand Up @@ -525,28 +542,27 @@ func appendProxy(registry ProxyRegistry,
return nil
}

// registerExtensions will iterate over the given extConfigs and register
// http handlers for every extension. It also registers a list extensions
// handler under the "/extensions/" endpoint.
func (m *Manager) registerExtensions(r *mux.Router, extConfigs *ExtensionConfigs) error {
extRouter := r.PathPrefix(fmt.Sprintf("%s/", URLPrefix)).Subrouter()
// updateExtensionRegistry will iterate over the given extConfigs building
// a new extension registry. At the end, if no errors are returned in
// this process it updates the manager with the new created registry.
func (m *Manager) updateExtensionRegistry(extConfigs *ExtensionConfigs) error {
extReg := make(map[string]ProxyRegistry)
for _, ext := range extConfigs.Extensions {
registry := NewProxyRegistry()
proxyReg := NewProxyRegistry()
singleBackend := len(ext.Backend.Services) == 1
for _, service := range ext.Backend.Services {
proxy, err := NewProxy(service.URL, service.Headers, ext.Backend.ProxyConfig)
if err != nil {
return fmt.Errorf("error creating proxy: %s", err)
}
err = appendProxy(registry, ext.Name, service, proxy, singleBackend)
err = appendProxy(proxyReg, ext.Name, service, proxy, singleBackend)
if err != nil {
return fmt.Errorf("error appending proxy: %s", err)
}
}
m.log.Infof("Registering handler for %s/%s...", URLPrefix, ext.Name)
extRouter.PathPrefix(fmt.Sprintf("/%s/", ext.Name)).
HandlerFunc(m.CallExtension(ext.Name, registry))
extReg[ext.Name] = proxyReg
}
m.registry = extReg
return nil
}

Expand Down Expand Up @@ -624,10 +640,29 @@ func findProxy(registry ProxyRegistry, extName string, dest v1alpha1.Application
return nil, fmt.Errorf("no proxy found for extension %q", extName)
}

// ProxyRegistry returns the proxy registry associated for the given
// extension name.
func (m *Manager) ProxyRegistry(name string) (ProxyRegistry, bool) {
pReg, found := m.registry[name]
return pReg, found
}

crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
// CallExtension returns a handler func responsible for forwarding requests to the
// extension service. The request will be sanitized by removing sensitive headers.
func (m *Manager) CallExtension(extName string, registry ProxyRegistry) func(http.ResponseWriter, *http.Request) {
func (m *Manager) CallExtension() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
segments := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
if segments[0] != "extensions" {
http.Error(w, fmt.Sprintf("Invalid URL: first segment must be %s", URLPrefix), http.StatusBadRequest)
return
}
extName := segments[1]
crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
if extName == "" {
http.Error(w, "Invalid URL: extension name must be provided", http.StatusBadRequest)
return
}
extName = strings.ReplaceAll(extName, "\n", "")
extName = strings.ReplaceAll(extName, "\r", "")
reqResources, err := ValidateHeaders(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid headers: %s", err), http.StatusBadRequest)
Expand All @@ -640,7 +675,13 @@ func (m *Manager) CallExtension(extName string, registry ProxyRegistry) func(htt
return
}

proxy, err := findProxy(registry, extName, app.Spec.Destination)
proxyRegistry, ok := m.ProxyRegistry(extName)
if !ok {
m.log.Warnf("proxy extension warning: attempt to call unregistered extension: %s", extName)
crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
http.Error(w, "Extension not found", http.StatusNotFound)
return
}
proxy, err := findProxy(proxyRegistry, extName, app.Spec.Destination)
if err != nil {
m.log.Errorf("findProxy error: %s", err)
http.Error(w, "invalid extension", http.StatusBadRequest)
Expand Down
Loading