Skip to content

Commit

Permalink
Merge pull request #3638 from telepresenceio/thallgren/amend-client-c…
Browse files Browse the repository at this point in the history
…onfig-extension

Add extension point enable client config mods made by traffic-manager.
  • Loading branch information
thallgren authored Jul 1, 2024
2 parents c31d323 + f957de9 commit a185219
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
35 changes: 21 additions & 14 deletions cmd/traffic/cmd/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,32 @@ func (c *config) configMapEventHandler(ctx context.Context, evCh <-chan watch.Ev
}
}

var AmendClientConfigFunc = AmendClientConfig //nolint:gochecknoglobals // extension point

func AmendClientConfig(ctx context.Context, cfg client.Config) bool {
env := managerutil.GetEnv(ctx)
if len(env.ManagedNamespaces) > 0 {
dlog.Debugf(ctx, "Checking if Augment mapped namespaces with %d managed namespaces", len(env.ManagedNamespaces))
if len(cfg.Cluster().MappedNamespaces) == 0 {
dlog.Debugf(ctx, "Augment mapped namespaces with %d managed namespaces", len(env.ManagedNamespaces))
cfg.Cluster().MappedNamespaces = env.ManagedNamespaces
}
return true
}
return false
}

func (c *config) refreshFile(ctx context.Context, data map[string]string) {
c.Lock()
if yml, ok := data[clientConfigFileName]; ok {
c.clientYAML = []byte(yml)
env := managerutil.GetEnv(ctx)
if len(env.ManagedNamespaces) > 0 {
dlog.Debugf(ctx, "Checking if Augment mapped namespaces with %d managed namespaces", len(env.ManagedNamespaces))
cfg, err := client.ParseConfigYAML(c.clientYAML)
if err != nil {
dlog.Errorf(ctx, "failed to unmarshal YAML from %s", clientConfigFileName)
}
if len(cfg.Cluster().MappedNamespaces) == 0 {
dlog.Debugf(ctx, "Augment mapped namespaces with %d managed namespaces", len(env.ManagedNamespaces))
cfg.Cluster().MappedNamespaces = env.ManagedNamespaces
yml = cfg.String()
c.clientYAML = []byte(yml)
}
cfg, err := client.ParseConfigYAML(c.clientYAML)
if err != nil {
dlog.Errorf(ctx, "failed to unmarshal YAML from %s", clientConfigFileName)
} else if AmendClientConfigFunc(ctx, cfg) {
c.clientYAML = []byte(cfg.String())
dlog.Debugf(ctx, "Refreshed client config: %s", yml)
}
dlog.Debugf(ctx, "Refreshed client config: %s", yml)
} else {
c.clientYAML = nil
dlog.Debugf(ctx, "Cleared client config")
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/userd/trafficmgr/session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package trafficmgr

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -209,6 +211,13 @@ func NewSession(
dlog.Warnf(ctx, "Failed to get remote config from traffic manager: %v", err)
}
} else {
if dlog.MaxLogLevel(ctx) >= dlog.LogLevelDebug {
dlog.Debug(ctx, "Applying client configuration from cluster")
sc := bufio.NewScanner(bytes.NewReader(cliCfg.ConfigYaml))
for sc.Scan() {
dlog.Debug(ctx, sc.Text())
}
}
if err := yaml.Unmarshal(cliCfg.ConfigYaml, tmgr.sessionConfig); err != nil {
dlog.Warnf(ctx, "Failed to deserialize remote config: %v", err)
}
Expand Down

0 comments on commit a185219

Please sign in to comment.