Skip to content

Commit f39cfdc

Browse files
authored
feat: informer sync timeout configuration for principal (#603)
Signed-off-by: yeonsoo <[email protected]>
1 parent f0ae550 commit f39cfdc

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

principal/listen_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func Test_Serve(t *testing.T) {
152152
WithListenerAddress("127.0.0.1"),
153153
WithShutDownGracePeriod(2*time.Second),
154154
WithGRPC(true),
155+
WithInformerSyncTimeout(5*time.Second),
155156
)
156157
require.NoError(t, err)
157158
errch := make(chan error)

principal/options.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,21 @@ type ServerOptions struct {
7575
redisPassword string
7676
redisCompressionType cacheutil.RedisCompressionType
7777
healthzPort int
78+
informerSyncTimeout time.Duration
7879
}
7980

8081
type ServerOption func(o *Server) error
8182

8283
// defaultOptions returns a set of default options for the server
8384
func defaultOptions() *ServerOptions {
8485
return &ServerOptions{
85-
port: 443,
86-
address: "",
87-
tlsMinVersion: tls.VersionTLS13,
88-
unauthMethods: make(map[string]bool),
89-
eventProcessors: 10,
90-
rootCa: x509.NewCertPool(),
86+
port: 443,
87+
address: "",
88+
tlsMinVersion: tls.VersionTLS13,
89+
unauthMethods: make(map[string]bool),
90+
eventProcessors: 10,
91+
rootCa: x509.NewCertPool(),
92+
informerSyncTimeout: 60 * time.Second,
9193
}
9294
}
9395

@@ -415,6 +417,14 @@ func WithWebSocket(enableWebSocket bool) ServerOption {
415417
}
416418
}
417419

420+
// WithInformerSyncTimeout sets the informer sync timeout duration.
421+
func WithInformerSyncTimeout(timeout time.Duration) ServerOption {
422+
return func(o *Server) error {
423+
o.options.informerSyncTimeout = timeout
424+
return nil
425+
}
426+
}
427+
418428
func WithResourceProxyEnabled(enabled bool) ServerOption {
419429
return func(o *Server) error {
420430
o.resourceProxyEnabled = enabled

principal/options_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ package principal
1717
import (
1818
"crypto/tls"
1919
"testing"
20+
"time"
2021

2122
"github.com/stretchr/testify/assert"
2223
)
2324

25+
func Test_WithInformerSyncTimeout(t *testing.T) {
26+
s := &Server{options: &ServerOptions{}}
27+
err := WithInformerSyncTimeout(5 * time.Second)(s)
28+
assert.NoError(t, err)
29+
assert.Equal(t, 5*time.Second, s.options.informerSyncTimeout)
30+
}
31+
2432
func Test_WithPort(t *testing.T) {
2533
ports := []struct {
2634
port int

principal/server.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,22 @@ func (s *Server) Start(ctx context.Context, errch chan error) error {
508508

509509
s.events = event.NewEventSource(s.options.serverName)
510510

511-
if err := s.appManager.EnsureSynced(waitForSyncedDuration); err != nil {
511+
syncTimeout := s.options.informerSyncTimeout
512+
if syncTimeout == 0 {
513+
syncTimeout = waitForSyncedDuration
514+
}
515+
516+
if err := s.appManager.EnsureSynced(syncTimeout); err != nil {
512517
return fmt.Errorf("unable to sync Application informer: %w", err)
513518
}
514519
log().Infof("Application informer synced and ready")
515520

516-
if err := s.projectManager.EnsureSynced(waitForSyncedDuration); err != nil {
521+
if err := s.projectManager.EnsureSynced(syncTimeout); err != nil {
517522
return fmt.Errorf("unable to sync AppProject informer: %w", err)
518523
}
519524
log().Infof("AppProject informer synced and ready")
520525

521-
if err := s.repoManager.EnsureSynced(waitForSyncedDuration); err != nil {
526+
if err := s.repoManager.EnsureSynced(syncTimeout); err != nil {
522527
return fmt.Errorf("unable to sync Repository informer: %w", err)
523528
}
524529
log().Infof("Repository informer synced and ready")
@@ -534,11 +539,10 @@ func (s *Server) Start(ctx context.Context, errch chan error) error {
534539
log().Infof("Resource proxy is disabled")
535540
}
536541

537-
err = s.clusterMgr.Start()
538-
if err != nil {
539-
return err
542+
if err := s.clusterMgr.Start(); err != nil {
543+
return fmt.Errorf("unable to start cluster manager with informer sync timeout %v: %w", syncTimeout, err)
540544
}
541-
if err := s.namespaceManager.EnsureSynced(waitForSyncedDuration); err != nil {
545+
if err := s.namespaceManager.EnsureSynced(syncTimeout); err != nil {
542546
return fmt.Errorf("unable to sync Namespace informer: %w", err)
543547
}
544548
log().Infof("Namespace informer synced and ready")

principal/server_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ func Test_NewServer(t *testing.T) {
9696
assert.Error(t, err)
9797
assert.Nil(t, s)
9898
})
99+
100+
t.Run("Informer sync timeout should be configurable", func(t *testing.T) {
101+
s, err := NewServer(context.TODO(), kube.NewKubernetesFakeClientWithApps(testNamespace), testNamespace, WithGeneratedTokenSigningKey(), WithInformerSyncTimeout(10*time.Second))
102+
assert.NoError(t, err)
103+
assert.NotNil(t, s)
104+
assert.Equal(t, 10*time.Second, s.options.informerSyncTimeout)
105+
})
106+
107+
t.Run("Informer sync timeout should default to 60s when not set", func(t *testing.T) {
108+
s, err := NewServer(context.TODO(), kube.NewKubernetesFakeClientWithApps(testNamespace), testNamespace, WithGeneratedTokenSigningKey())
109+
assert.NoError(t, err)
110+
assert.NotNil(t, s)
111+
assert.Equal(t, 60*time.Second, s.options.informerSyncTimeout)
112+
})
99113
}
100114

101115
func Test_handleResyncOnConnect(t *testing.T) {

0 commit comments

Comments
 (0)