Skip to content

Commit 8db49ca

Browse files
authored
feat: make a configurable poll interval (#225)
* make a configurable poll interval * add comment
1 parent 4625c2f commit 8db49ca

File tree

11 files changed

+59
-25
lines changed

11 files changed

+59
-25
lines changed

cmd/agent/agent.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/pluralsh/deployment-operator/pkg/controller/service"
2020
)
2121

22+
const pollInterval = time.Second * 30
23+
2224
func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient ctrclient.Client) (*controller.ControllerManager, *service.ServiceReconciler, *pipelinegates.GateReconciler) {
2325
r, err := time.ParseDuration(opt.refreshInterval)
2426
if err != nil {
@@ -48,7 +50,7 @@ func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient
4850
Do: sr,
4951
Queue: sr.SvcQueue,
5052
})
51-
gr, err := pipelinegates.NewGateReconciler(mgr.GetClient(), k8sClient, config, r, opt.clusterId)
53+
gr, err := pipelinegates.NewGateReconciler(mgr.GetClient(), k8sClient, config, r, pollInterval, opt.clusterId)
5254
if err != nil {
5355
setupLog.Errorw("unable to create gate reconciler", "error", err)
5456
os.Exit(1)
@@ -79,7 +81,7 @@ func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient
7981
os.Exit(1)
8082
}
8183

82-
s := stacks.NewStackReconciler(mgr.GetClient(), k8sClient, r, namespace, opt.consoleUrl, opt.deployToken)
84+
s := stacks.NewStackReconciler(mgr.GetClient(), k8sClient, r, pollInterval, namespace, opt.consoleUrl, opt.deployToken)
8385
mgr.AddController(&controller.Controller{
8486
Name: "Stack Controller",
8587
Do: s,

pkg/controller/controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Reconciler interface {
3333

3434
// ShutdownQueue containing Console resources.
3535
ShutdownQueue()
36+
37+
// GetPollInterval returns custom poll interval. If 0 then controller manager use default from the options.
38+
GetPollInterval() time.Duration
3639
}
3740

3841
type Controller struct {

pkg/controller/controller_manager.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ func (cm *ControllerManager) Start() error {
8383
go func() {
8484
defer controller.Do.ShutdownQueue()
8585
defer controller.Do.WipeCache()
86+
87+
pollInterval := cm.Refresh
88+
if controllerPollInterval := controller.Do.GetPollInterval(); controllerPollInterval > 0 {
89+
pollInterval = controllerPollInterval
90+
}
8691
//nolint:all
87-
_ = wait.PollImmediateInfinite(cm.Refresh, func() (done bool, err error) {
92+
_ = wait.PollImmediateInfinite(pollInterval, func() (done bool, err error) {
8893
return controller.Do.Poll(cm.ctx)
8994
})
9095
}()

pkg/controller/namespaces/reconciler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func NewNamespaceReconciler(consoleClient client.Client, k8sClient ctrlclient.Cl
4141
}
4242
}
4343

44+
func (n *NamespaceReconciler) GetPollInterval() time.Duration {
45+
return 0 // use default poll interval
46+
}
47+
4448
func (n *NamespaceReconciler) GetPublisher() (string, websocket.Publisher) {
4549
return "namespace.event", &socketPublisher{
4650
restoreQueue: n.NamespaceQueue,

pkg/controller/pipelinegates/reconciler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ type GateReconciler struct {
3636
discoveryClient *discovery.DiscoveryClient
3737
pinger *ping.Pinger
3838
operatorNamespace string
39+
PollInterval time.Duration
3940
}
4041

41-
func NewGateReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, config *rest.Config, refresh time.Duration, clusterId string) (*GateReconciler, error) {
42+
func NewGateReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, config *rest.Config, refresh, pollInterval time.Duration, clusterId string) (*GateReconciler, error) {
4243
utils.DisableClientLimits(config)
4344

4445
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
@@ -74,9 +75,14 @@ func NewGateReconciler(consoleClient client.Client, k8sClient ctrlclient.Client,
7475
discoveryClient: discoveryClient,
7576
pinger: ping.New(consoleClient, discoveryClient, f),
7677
operatorNamespace: namespace,
78+
PollInterval: pollInterval,
7779
}, nil
7880
}
7981

82+
func (s *GateReconciler) GetPollInterval() time.Duration {
83+
return s.PollInterval
84+
}
85+
8086
func (s *GateReconciler) WipeCache() {
8187
s.GateCache.Wipe()
8288
}

pkg/controller/restore/reconciler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func NewRestoreReconciler(consoleClient client.Client, k8sClient ctrlclient.Clie
6363
}
6464
}
6565

66+
func (s *RestoreReconciler) GetPollInterval() time.Duration {
67+
return 0 // use default poll interval
68+
}
69+
6670
func (s *RestoreReconciler) GetPublisher() (string, websocket.Publisher) {
6771
return "restore.event", &socketPublisher{
6872
restoreQueue: s.RestoreQueue,

pkg/controller/service/reconciler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ func CapabilitiesAPIVersions(discoveryClient *discovery.DiscoveryClient) error {
155155
return nil
156156
}
157157

158+
func (s *ServiceReconciler) GetPollInterval() time.Duration {
159+
return 0 // use default poll interval
160+
}
161+
158162
func (s *ServiceReconciler) GetPublisher() (string, websocket.Publisher) {
159163
return "service.event", &socketPublisher{
160164
svcQueue: s.SvcQueue,

pkg/controller/stacks/job_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
1515
var kClient client.Client
1616
fakeConsoleClient := mocks.NewClientMock(t)
1717
namespace := "default"
18-
reconciler := NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
18+
reconciler := NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
1919
run := &console.StackRunFragment{
2020
Type: console.StackTypeTerraform,
2121
Configuration: &console.StackConfigurationFragment{

pkg/controller/stacks/reconciler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,28 @@ type StackReconciler struct {
2525
Namespace string
2626
ConsoleURL string
2727
DeployToken string
28+
PollInterval time.Duration
2829
}
2930

30-
func NewStackReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, refresh time.Duration, namespace, consoleURL, deployToken string) *StackReconciler {
31+
func NewStackReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, refresh, pollInterval time.Duration, namespace, consoleURL, deployToken string) *StackReconciler {
3132
return &StackReconciler{
3233
ConsoleClient: consoleClient,
3334
K8sClient: k8sClient,
3435
StackQueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
3536
StackCache: client.NewCache[console.StackRunFragment](refresh, func(id string) (*console.StackRunFragment, error) {
3637
return consoleClient.GetStackRun(id)
3738
}),
38-
Namespace: namespace,
39-
ConsoleURL: consoleURL,
40-
DeployToken: deployToken,
39+
Namespace: namespace,
40+
ConsoleURL: consoleURL,
41+
DeployToken: deployToken,
42+
PollInterval: pollInterval,
4143
}
4244
}
4345

46+
func (r *StackReconciler) GetPollInterval() time.Duration {
47+
return r.PollInterval
48+
}
49+
4450
func (r *StackReconciler) GetPublisher() (string, websocket.Publisher) {
4551
return "stack.run.event", &socketPublisher{
4652
stackRunQueue: r.StackQueue,

pkg/controller/stacks/reconciler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var _ = Describe("Reconciler", Ordered, func() {
7373
GqlErrors: &gqlerror.List{gqlerror.Errorf(errors2.ErrorNotFound.String())},
7474
})
7575

76-
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
76+
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
7777

7878
_, err := reconciler.Reconcile(ctx, stackRunId)
7979
Expect(err).NotTo(HaveOccurred())
@@ -85,7 +85,7 @@ var _ = Describe("Reconciler", Ordered, func() {
8585
GqlErrors: &gqlerror.List{gqlerror.Errorf("unknown error")},
8686
})
8787

88-
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
88+
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
8989

9090
_, err := reconciler.Reconcile(ctx, stackRunId)
9191
Expect(err).To(HaveOccurred())
@@ -100,7 +100,7 @@ var _ = Describe("Reconciler", Ordered, func() {
100100
Status: console.StackStatusPending,
101101
}, nil)
102102

103-
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
103+
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
104104

105105
_, err := reconciler.Reconcile(ctx, stackRunId)
106106
Expect(err).NotTo(HaveOccurred())
@@ -118,7 +118,7 @@ var _ = Describe("Reconciler", Ordered, func() {
118118
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(stackRun, nil)
119119
fakeConsoleClient.On("UpdateStackRun", mock.Anything, mock.Anything).Return(nil)
120120

121-
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
121+
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
122122

123123
_, err := reconciler.Reconcile(ctx, stackRunId)
124124
Expect(err).NotTo(HaveOccurred())
@@ -157,7 +157,7 @@ var _ = Describe("Reconciler", Ordered, func() {
157157
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(stackRun, nil)
158158
fakeConsoleClient.On("UpdateStackRun", mock.Anything, mock.Anything).Return(nil)
159159

160-
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
160+
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
161161

162162
_, err := reconciler.Reconcile(ctx, stackRunId)
163163
Expect(err).NotTo(HaveOccurred())
@@ -213,7 +213,7 @@ var _ = Describe("Reconciler", Ordered, func() {
213213
fakeConsoleClient.On("GetStackRun", mock.Anything).Return(stackRun, nil)
214214
fakeConsoleClient.On("UpdateStackRun", mock.Anything, mock.Anything).Return(nil)
215215

216-
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
216+
reconciler := stacks.NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
217217

218218
_, err = reconciler.Reconcile(ctx, stackRunId)
219219
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)