Skip to content

Commit

Permalink
wip: backoff konnect strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed May 9, 2023
1 parent ef5a0e3 commit 2e743f1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
52 changes: 52 additions & 0 deletions internal/dataplane/sendconfig/backoff_strategy_decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package sendconfig

import (
"context"
"time"

"github.com/kong/deck/file"

"github.com/kong/kubernetes-ingress-controller/v2/internal/metrics"
)

type LastRetryProvider interface {
LastRetry() time.Time
}

type UpdateStrategyBackoff struct {
decorated UpdateStrategy
lastRetryProvider LastRetryProvider
}

func NewUpdateStrategyBackoff(decorated UpdateStrategy, lastRetryProvider LastRetryProvider) UpdateStrategyBackoff {
return UpdateStrategyBackoff{
decorated: decorated,
lastRetryProvider: lastRetryProvider,
}
}

func (s UpdateStrategyBackoff) Update(ctx context.Context, targetContent *file.Content) (
err error,
resourceErrors []ResourceError,
resourceErrorsParseErr error,
) {
if !s.shouldUpdate() {
return ErrUpdateSkippedDueToBackoffStrategy, nil, nil
}

return s.decorated.Update(ctx, targetContent)
}

// TODO: implement this method properly
// We can take into account the last returned error from Update() and decide whether to retry or not, e.g. if
// - the HTTP error code was 429, we should wait the time returned in HTTP headers
// - the config was invalid, we should retry only after the config changes
// - ...
func (s UpdateStrategyBackoff) shouldUpdate() bool {
const backoffDuration = time.Minute
return time.Now().After(s.lastRetryProvider.LastRetry().Add(backoffDuration))
}

func (s UpdateStrategyBackoff) MetricsProtocol() metrics.Protocol {
return s.decorated.MetricsProtocol()
}
25 changes: 17 additions & 8 deletions internal/dataplane/sendconfig/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sendconfig

import (
"context"
"errors"

"github.com/kong/deck/dump"
"github.com/kong/deck/file"
Expand All @@ -11,6 +12,10 @@ import (
"github.com/kong/kubernetes-ingress-controller/v2/internal/metrics"
)

var (
ErrUpdateSkippedDueToBackoffStrategy = errors.New("update skipped due to backoff strategy")
)

// UpdateStrategy is the way we approach updating data-plane's configuration, depending on its type.
type UpdateStrategy interface {
// Update applies targetConfig to the data-plane.
Expand Down Expand Up @@ -63,14 +68,18 @@ func (r DefaultUpdateStrategyResolver) ResolveUpdateStrategy(
// In case the client communicates with Konnect Admin API, we know it has to use DB-mode. There's no need to check
// config.InMemory that is meant for regular Kong Gateway clients.
if client.IsKonnect() {
return NewUpdateStrategyDBMode(
adminAPIClient,
dump.Config{
SkipCACerts: true,
KonnectRuntimeGroup: client.KonnectRuntimeGroup(),
},
r.config.Version,
r.config.Concurrency,
return NewUpdateStrategyBackoff(
NewUpdateStrategyDBMode(
adminAPIClient,
dump.Config{
SkipCACerts: true,
KonnectRuntimeGroup: client.KonnectRuntimeGroup(),
},
r.config.Version,
r.config.Concurrency,
),
// TODO: pass a proper LastRetryProvider here that keeps track of the last retry time between ResolveUpdateStrategy calls.
nil,
)
}

Expand Down

0 comments on commit 2e743f1

Please sign in to comment.