Skip to content

Commit 57a8861

Browse files
committed
profile handling
1 parent 31c808e commit 57a8861

18 files changed

Lines changed: 493 additions & 211 deletions

File tree

internal/kube/adaptor/config_init.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,20 @@ func InitialiseConfig(cli internalclient.Clients, namespace string, path string,
4848
if routerConfiguration == nil {
4949
return fmt.Errorf("empty router configuration in ConfigMap %q", routerConfigMap)
5050
}
51-
delta := secretsSync.Expect(routerConfiguration.SslProfiles)
51+
delta := secretsSync.ExpectSslProfiles(routerConfiguration.SslProfiles)
5252
if len(delta.Missing) > 0 {
5353
slog.Info("Waiting for Secrets to be created for SslProfiles", slog.Any("sslProfiles", delta.Missing))
5454
}
5555
for name, diff := range delta.PendingOrdinals {
5656
slog.Info("Secret has outdated ordinal", slog.String("secret", diff.SecretName), slog.Uint64("ordinal", diff.Current), slog.String("profile", name), slog.Uint64("expected", diff.Expect))
5757
}
58+
deltaProxy := secretsSync.ExpectProxyProfiles(namespace+"/"+routerConfigMap, routerConfiguration.ProxyProfiles)
59+
if len(deltaProxy.Missing) > 0 {
60+
slog.Info("Waiting for Secrets to be created for ProxyProfiles", slog.Any("proxProfiles", deltaProxy.Missing))
61+
}
62+
for _, err := range deltaProxy.Errors {
63+
delta.Errors = append(delta.Errors, err)
64+
}
5865
return delta.Error()
5966
}, backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(time.Second*60)))
6067
if retryErr != nil {

internal/kube/adaptor/config_sync.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package adaptor
22

33
import (
4+
"context"
45
"fmt"
56
"log/slog"
67
"os"
78

89
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/util/retry"
913

1014
internalclient "github.com/skupperproject/skupper/internal/kube/client"
1115
"github.com/skupperproject/skupper/internal/kube/secrets"
@@ -14,8 +18,10 @@ import (
1418
)
1519

1620
// Syncs the live router config with the configmap (bridge configuration,
17-
// secrets for services with TLS enabled, and secrets and connectors for links)
21+
// secrets for services with TLS enabled, and secrets and connectors for links
22+
// as well as proxy profiles)
1823
type ConfigSync struct {
24+
cli internalclient.Clients
1925
agentPool *qdr.AgentPool
2026
controller *watchers.EventProcessor
2127
namespace string
@@ -37,6 +43,7 @@ func sslSecretsWatcher(namespace string, eventProcessor *watchers.EventProcessor
3743
func NewConfigSync(cli internalclient.Clients, namespace string, path string, routerConfigMap string, metrics watchers.MetricsProvider) *ConfigSync {
3844
controller := watchers.NewEventProcessor("config-sync", cli, watchers.WithMetricsProvider(metrics))
3945
configSync := &ConfigSync{
46+
cli: cli,
4047
agentPool: qdr.NewAgentPool("amqp://localhost:5672", nil),
4148
controller: controller,
4249
namespace: namespace,
@@ -88,6 +95,26 @@ func (c *ConfigSync) key(name string) string {
8895
return fmt.Sprintf("%s/%s", c.namespace, name)
8996
}
9097

98+
func UpdateRouterConfig(client kubernetes.Interface, ctxt context.Context, update *qdr.RouterConfig, configmap *corev1.ConfigMap) error {
99+
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
100+
return updateRouterConfig(client, ctxt, update, configmap)
101+
})
102+
}
103+
104+
func updateRouterConfig(client kubernetes.Interface, ctxt context.Context, update *qdr.RouterConfig, configmap *corev1.ConfigMap) error {
105+
106+
err := update.WriteToConfigMap(configmap)
107+
if err != nil {
108+
return err
109+
}
110+
111+
_, err = client.CoreV1().ConfigMaps(configmap.Namespace).Update(ctxt, configmap, metav1.UpdateOptions{})
112+
if err != nil {
113+
return err
114+
}
115+
return nil
116+
}
117+
91118
func (c *ConfigSync) configEvent(key string, configmap *corev1.ConfigMap) error {
92119
if configmap == nil {
93120
return nil
@@ -102,6 +129,18 @@ func (c *ConfigSync) configEvent(key string, configmap *corev1.ConfigMap) error
102129
if err := c.syncSslProfilesToRouter(desired.SslProfiles); err != nil {
103130
return err
104131
}
132+
// Note: change to a proxy secret is one case where a non-skupper resource
133+
// requires update to the router config and config map
134+
proxyUpdates, err := c.syncProxyProfileCredentialsToDisk(key, desired.ProxyProfiles)
135+
if err != nil {
136+
return err
137+
} else {
138+
for _, update := range proxyUpdates {
139+
if _, ok := desired.ProxyProfiles[update.Name]; ok {
140+
desired.ProxyProfiles[update.Name] = update
141+
}
142+
}
143+
}
105144
if err := c.syncProxyProfilesToRouter(desired.ProxyProfiles); err != nil {
106145
return err
107146
}
@@ -113,6 +152,13 @@ func (c *ConfigSync) configEvent(key string, configmap *corev1.ConfigMap) error
113152
c.logger.Error("sync failed", slog.Any("error", err))
114153
return err
115154
}
155+
if len(proxyUpdates) > 0 {
156+
err := UpdateRouterConfig(c.cli.GetKubeClient(), context.TODO(), desired, configmap)
157+
if err != nil {
158+
return err
159+
}
160+
}
161+
116162
return nil
117163
}
118164

@@ -222,6 +268,7 @@ func (c *ConfigSync) syncSslProfilesToRouter(desired map[string]qdr.SslProfile)
222268
if err := agent.CreateSslProfile(profile); err != nil {
223269
return err
224270
}
271+
continue
225272
}
226273
if current != profile {
227274
if err := agent.UpdateSslProfile(profile); err != nil {
@@ -240,7 +287,7 @@ func (c *ConfigSync) syncSslProfilesToRouter(desired map[string]qdr.SslProfile)
240287
}
241288

242289
func (c *ConfigSync) syncSslProfileCredentialsToDisk(profiles map[string]qdr.SslProfile) error {
243-
delta := c.profileSyncer.Expect(profiles)
290+
delta := c.profileSyncer.ExpectSslProfiles(profiles)
244291
return delta.Error()
245292
}
246293

@@ -261,6 +308,7 @@ func (c *ConfigSync) syncProxyProfilesToRouter(desired map[string]qdr.ProxyProfi
261308
if err := agent.CreateProxyProfile(profile); err != nil {
262309
return err
263310
}
311+
continue
264312
}
265313
if current != profile {
266314
if err := agent.UpdateProxyProfile(profile); err != nil {
@@ -278,6 +326,11 @@ func (c *ConfigSync) syncProxyProfilesToRouter(desired map[string]qdr.ProxyProfi
278326
return nil
279327
}
280328

329+
func (c *ConfigSync) syncProxyProfileCredentialsToDisk(key string, profiles map[string]qdr.ProxyProfile) (map[string]qdr.ProxyProfile, error) {
330+
delta := c.profileSyncer.ExpectProxyProfiles(key, profiles)
331+
return delta.ProxyUpdates, delta.Error()
332+
}
333+
281334
func (c *ConfigSync) recoverTracking() error {
282335
configmap, err := c.config.Get(c.key(c.routerConfigMap))
283336
if err != nil {

internal/kube/secrets/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type profileWatcherContext struct {
3636

3737
type ProfilesWatcher struct {
3838
logger *slog.Logger
39-
cache SecretsCache
39+
Cache SecretsCache
4040
client typedv1.SecretInterface
4141
update UpdateRouterConfigFn
4242
pvProvider PriorValidityProvider
@@ -58,7 +58,7 @@ func NewProfilesWatcher(factory SecretsCacheFactory, client kubernetes.Interface
5858
state: make(map[string]*profileWatcherContext),
5959
cleanup: sync.OnceFunc(func() { close(stopCh) }),
6060
}
61-
w.cache = factory(stopCh, w.handleSecret)
61+
w.Cache = factory(stopCh, w.handleSecret)
6262
return w
6363
}
6464

@@ -160,7 +160,7 @@ func (w *ProfilesWatcher) UseProfiles(profiles map[string]qdr.SslProfile) {
160160
}
161161
for _, secretName := range profileSecrets(profileName) {
162162
key := w.keyfunc(secretName)
163-
secret, err := w.cache.Get(key)
163+
secret, err := w.Cache.Get(key)
164164
if err != nil || secret == nil {
165165
continue
166166
}
@@ -171,7 +171,7 @@ func (w *ProfilesWatcher) UseProfiles(profiles map[string]qdr.SslProfile) {
171171
state := w.state[profileName]
172172
delete(w.state, profileName)
173173
if state != nil && state.SecretKey != "" {
174-
secret, err := w.cache.Get(state.SecretKey)
174+
secret, err := w.Cache.Get(state.SecretKey)
175175
if err != nil || secret == nil {
176176
continue
177177
}

0 commit comments

Comments
 (0)