Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit d3db048

Browse files
fix: don't wait indefinitely for k8s secrets (#947)
1 parent 83e3bdd commit d3db048

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ NOTE: all releases may include dependency updates, not specifically mentioned
99
- feat: integrate try-as library [#912](https://github.com/hypermodeinc/modus/pull/912)
1010
- fix: sentry source context [#940](https://github.com/hypermodeinc/modus/pull/940)
1111
- fix: connect kubernetes logger [#945](https://github.com/hypermodeinc/modus/pull/945)
12+
- fix: don't wait indefinitely for k8s secrets [#947](https://github.com/hypermodeinc/modus/pull/947)
1213

1314
## 2025-07-09 - Runtime v0.18.4
1415

runtime/secrets/kubernetes.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"strings"
12+
"time"
1213

1314
"github.com/go-logr/zerologr"
1415
"github.com/hypermodeinc/modus/lib/manifest"
@@ -27,6 +28,8 @@ import (
2728
"sigs.k8s.io/controller-runtime/pkg/cluster"
2829
)
2930

31+
var cacheSyncTimeout = 10 * time.Second
32+
3033
type kubernetesSecretsProvider struct {
3134
k8sClient client.Client
3235
secretName string
@@ -53,7 +56,10 @@ func (sp *kubernetesSecretsProvider) initialize(ctx context.Context) {
5356
sp.secretNamespace = parts[0]
5457
sp.secretName = parts[1]
5558

56-
cli, cache, err := newK8sClientForSecret(ctx, sp.secretNamespace, sp.secretName)
59+
// Important to hook up the logger so we see any logs from the controller-runtime package.
60+
ctrl.SetLogger(zerologr.New(logger.Get(ctx)))
61+
62+
cli, cache, err := newK8sClientForSecret(sp.secretNamespace, sp.secretName)
5763
if err != nil {
5864
const msg = "Failed to initialize Kubernetes client."
5965
sentryutils.CaptureError(ctx, err, msg,
@@ -69,7 +75,7 @@ func (sp *kubernetesSecretsProvider) initialize(ctx context.Context) {
6975
// See: https://kubernetes.io/docs/reference/using-api/api-concepts/#efficient-detection-of-changes
7076
//
7177
// This cache is then used by k8sClient.Get(), so that it does not need
72-
// to call the API server everytime.
78+
// to call the API server every time.
7379
go func() {
7480
if err := cache.Start(ctx); err != nil {
7581
const msg = "Failed to start Kubernetes client cache."
@@ -81,8 +87,10 @@ func (sp *kubernetesSecretsProvider) initialize(ctx context.Context) {
8187
logger.Info(ctx).Msg("Kubernetes client cache stopped.")
8288
}()
8389

84-
// Wait for initial cache sync
85-
if !cache.WaitForCacheSync(ctx) {
90+
// Wait for initial cache sync - but not indefinitely.
91+
waitCtx, cancel := context.WithTimeout(ctx, cacheSyncTimeout)
92+
defer cancel()
93+
if !cache.WaitForCacheSync(waitCtx) {
8694
const msg = "Failed to sync Kubernetes client cache."
8795
sentryutils.CaptureError(ctx, nil, msg,
8896
sentryutils.WithData("namespace", sp.secretNamespace),
@@ -158,20 +166,25 @@ func (sp *kubernetesSecretsProvider) hasSecret(ctx context.Context, name string)
158166

159167
// newK8sClientForSecret creates a new Kubernetes client that watches
160168
// only a single secret `name` in namespace `ns`.
161-
func newK8sClientForSecret(ctx context.Context, ns, name string) (client.Client, pkgcache.Cache, error) {
169+
func newK8sClientForSecret(ns, name string) (client.Client, pkgcache.Cache, error) {
162170
scheme := runtime.NewScheme()
163171
if err := clientgoscheme.AddToScheme(scheme); err != nil {
164172
return nil, nil, err
165173
}
166174

167-
ctrl.SetLogger(zerologr.New(logger.Get(ctx)))
175+
cfg, err := ctrl.GetConfig()
176+
if err != nil {
177+
return nil, nil, err
178+
}
168179

169-
cl, err := cluster.New(ctrl.GetConfigOrDie(), func(clusterOptions *cluster.Options) {
180+
cl, err := cluster.New(cfg, func(clusterOptions *cluster.Options) {
170181
clusterOptions.Scheme = scheme
171-
clusterOptions.Cache.DefaultNamespaces = make(map[string]pkgcache.Config)
182+
172183
// Only cache the one secret we need, so we don't waste memory space
173-
clusterOptions.Cache.DefaultNamespaces[ns] = pkgcache.Config{
174-
FieldSelector: fields.OneTermEqualSelector(metav1.ObjectNameField, name),
184+
clusterOptions.Cache.DefaultNamespaces = map[string]pkgcache.Config{
185+
ns: {
186+
FieldSelector: fields.OneTermEqualSelector(metav1.ObjectNameField, name),
187+
},
175188
}
176189
})
177190
if err != nil {

0 commit comments

Comments
 (0)