Skip to content

Commit

Permalink
feat: make a configurable poll interval (#225)
Browse files Browse the repository at this point in the history
* make a configurable poll interval

* add comment
  • Loading branch information
zreigz authored Jun 18, 2024
1 parent 4625c2f commit 8db49ca
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 25 deletions.
6 changes: 4 additions & 2 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/pluralsh/deployment-operator/pkg/controller/service"
)

const pollInterval = time.Second * 30

func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient ctrclient.Client) (*controller.ControllerManager, *service.ServiceReconciler, *pipelinegates.GateReconciler) {
r, err := time.ParseDuration(opt.refreshInterval)
if err != nil {
Expand Down Expand Up @@ -48,7 +50,7 @@ func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient
Do: sr,
Queue: sr.SvcQueue,
})
gr, err := pipelinegates.NewGateReconciler(mgr.GetClient(), k8sClient, config, r, opt.clusterId)
gr, err := pipelinegates.NewGateReconciler(mgr.GetClient(), k8sClient, config, r, pollInterval, opt.clusterId)
if err != nil {
setupLog.Errorw("unable to create gate reconciler", "error", err)
os.Exit(1)
Expand Down Expand Up @@ -79,7 +81,7 @@ func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient
os.Exit(1)
}

s := stacks.NewStackReconciler(mgr.GetClient(), k8sClient, r, namespace, opt.consoleUrl, opt.deployToken)
s := stacks.NewStackReconciler(mgr.GetClient(), k8sClient, r, pollInterval, namespace, opt.consoleUrl, opt.deployToken)
mgr.AddController(&controller.Controller{
Name: "Stack Controller",
Do: s,
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Reconciler interface {

// ShutdownQueue containing Console resources.
ShutdownQueue()

// GetPollInterval returns custom poll interval. If 0 then controller manager use default from the options.
GetPollInterval() time.Duration
}

type Controller struct {
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/controller_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ func (cm *ControllerManager) Start() error {
go func() {
defer controller.Do.ShutdownQueue()
defer controller.Do.WipeCache()

pollInterval := cm.Refresh
if controllerPollInterval := controller.Do.GetPollInterval(); controllerPollInterval > 0 {
pollInterval = controllerPollInterval
}
//nolint:all
_ = wait.PollImmediateInfinite(cm.Refresh, func() (done bool, err error) {
_ = wait.PollImmediateInfinite(pollInterval, func() (done bool, err error) {
return controller.Do.Poll(cm.ctx)
})
}()
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/namespaces/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func NewNamespaceReconciler(consoleClient client.Client, k8sClient ctrlclient.Cl
}
}

func (n *NamespaceReconciler) GetPollInterval() time.Duration {
return 0 // use default poll interval
}

func (n *NamespaceReconciler) GetPublisher() (string, websocket.Publisher) {
return "namespace.event", &socketPublisher{
restoreQueue: n.NamespaceQueue,
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/pipelinegates/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type GateReconciler struct {
discoveryClient *discovery.DiscoveryClient
pinger *ping.Pinger
operatorNamespace string
PollInterval time.Duration
}

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

discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
Expand Down Expand Up @@ -74,9 +75,14 @@ func NewGateReconciler(consoleClient client.Client, k8sClient ctrlclient.Client,
discoveryClient: discoveryClient,
pinger: ping.New(consoleClient, discoveryClient, f),
operatorNamespace: namespace,
PollInterval: pollInterval,
}, nil
}

func (s *GateReconciler) GetPollInterval() time.Duration {
return s.PollInterval
}

func (s *GateReconciler) WipeCache() {
s.GateCache.Wipe()
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/restore/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func NewRestoreReconciler(consoleClient client.Client, k8sClient ctrlclient.Clie
}
}

func (s *RestoreReconciler) GetPollInterval() time.Duration {
return 0 // use default poll interval
}

func (s *RestoreReconciler) GetPublisher() (string, websocket.Publisher) {
return "restore.event", &socketPublisher{
restoreQueue: s.RestoreQueue,
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/service/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func CapabilitiesAPIVersions(discoveryClient *discovery.DiscoveryClient) error {
return nil
}

func (s *ServiceReconciler) GetPollInterval() time.Duration {
return 0 // use default poll interval
}

func (s *ServiceReconciler) GetPublisher() (string, websocket.Publisher) {
return "service.event", &socketPublisher{
svcQueue: s.SvcQueue,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/stacks/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGetDefaultContainerImage(t *testing.T) {
var kClient client.Client
fakeConsoleClient := mocks.NewClientMock(t)
namespace := "default"
reconciler := NewStackReconciler(fakeConsoleClient, kClient, time.Minute, namespace, "", "")
reconciler := NewStackReconciler(fakeConsoleClient, kClient, time.Minute, 0, namespace, "", "")
run := &console.StackRunFragment{
Type: console.StackTypeTerraform,
Configuration: &console.StackConfigurationFragment{
Expand Down
14 changes: 10 additions & 4 deletions pkg/controller/stacks/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ type StackReconciler struct {
Namespace string
ConsoleURL string
DeployToken string
PollInterval time.Duration
}

func NewStackReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, refresh time.Duration, namespace, consoleURL, deployToken string) *StackReconciler {
func NewStackReconciler(consoleClient client.Client, k8sClient ctrlclient.Client, refresh, pollInterval time.Duration, namespace, consoleURL, deployToken string) *StackReconciler {
return &StackReconciler{
ConsoleClient: consoleClient,
K8sClient: k8sClient,
StackQueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
StackCache: client.NewCache[console.StackRunFragment](refresh, func(id string) (*console.StackRunFragment, error) {
return consoleClient.GetStackRun(id)
}),
Namespace: namespace,
ConsoleURL: consoleURL,
DeployToken: deployToken,
Namespace: namespace,
ConsoleURL: consoleURL,
DeployToken: deployToken,
PollInterval: pollInterval,
}
}

func (r *StackReconciler) GetPollInterval() time.Duration {
return r.PollInterval
}

func (r *StackReconciler) GetPublisher() (string, websocket.Publisher) {
return "stack.run.event", &socketPublisher{
stackRunQueue: r.StackQueue,
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/stacks/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("Reconciler", Ordered, func() {
GqlErrors: &gqlerror.List{gqlerror.Errorf(errors2.ErrorNotFound.String())},
})

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

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

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

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

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

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

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

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

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

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

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

_, err = reconciler.Reconcile(ctx, stackRunId)
Expect(err).NotTo(HaveOccurred())
Expand Down
20 changes: 10 additions & 10 deletions pkg/test/mocks/Client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8db49ca

Please sign in to comment.