Skip to content

Commit

Permalink
use context poll interval/timeout in DeleteResources (knative-extensi…
Browse files Browse the repository at this point in the history
…ons#626)

* use context poll interval/timeout in DeleteResources

* move timings to state to break dep cycle

* comments
  • Loading branch information
maschmid authored and mgencur committed Nov 21, 2023
1 parent 5fb93a7 commit c0729ef
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
27 changes: 8 additions & 19 deletions pkg/environment/timings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,26 @@ package environment
import (
"context"
"time"

"knative.dev/reconciler-test/pkg/state"
)

// this has been moved to state pkg to break cycle between environment and feature package,
// keeping the consts here for backwards API compatibility
const (
DefaultPollInterval = 3 * time.Second
DefaultPollTimeout = 2 * time.Minute
)

type timingsKey struct{}
type timingsType struct {
interval time.Duration
timeout time.Duration
}

// PollTimingsFromContext will get the previously set poll timing from context,
// or return the defaults if not found.
// - values from from context.
// - defaults.
// ContextWithPollTimings returns a context with poll timings set
func ContextWithPollTimings(ctx context.Context, interval, timeout time.Duration) context.Context {
return context.WithValue(ctx, timingsKey{}, timingsType{
interval: interval,
timeout: timeout,
})
return state.ContextWithPollTimings(ctx, interval, timeout)
}

// PollTimingsFromContext will get the previously set poll timing from context,
// or return the defaults if not found.
// - values from from context.
// - values from context.
// - defaults.
func PollTimingsFromContext(ctx context.Context) (time.Duration, time.Duration) {
if t, ok := ctx.Value(timingsKey{}).(timingsType); ok {
return t.interval, t.timeout
}
return DefaultPollInterval, DefaultPollTimeout
return state.PollTimingsFromContext(ctx)
}
5 changes: 2 additions & 3 deletions pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"runtime"
"strings"
"sync"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -32,7 +31,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/pkg/apis"
"knative.dev/pkg/injection/clients/dynamicclient"

"knative.dev/reconciler-test/pkg/state"
)

Expand Down Expand Up @@ -229,7 +227,8 @@ func DeleteResources(ctx context.Context, t T, refs []corev1.ObjectReference) er

var lastResource corev1.ObjectReference // One still present resource

err := wait.Poll(time.Second, 4*time.Minute, func() (bool, error) {
interval, timeout := state.PollTimingsFromContext(ctx)
err := wait.Poll(interval, timeout, func() (bool, error) {
for _, ref := range refs {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions pkg/state/timings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2021 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package state

import (
"context"
"time"
)

const (
DefaultPollInterval = 3 * time.Second
DefaultPollTimeout = 2 * time.Minute
)

type timingsKey struct{}
type timingsType struct {
interval time.Duration
timeout time.Duration
}

// ContextWithPollTimings returns a context with poll timings set
func ContextWithPollTimings(ctx context.Context, interval, timeout time.Duration) context.Context {
return context.WithValue(ctx, timingsKey{}, timingsType{
interval: interval,
timeout: timeout,
})
}

// PollTimingsFromContext will get the previously set poll timing from context,
// or return the defaults if not found.
// - values from context.
// - defaults.
func PollTimingsFromContext(ctx context.Context) (time.Duration, time.Duration) {
if t, ok := ctx.Value(timingsKey{}).(timingsType); ok {
return t.interval, t.timeout
}
return DefaultPollInterval, DefaultPollTimeout
}

0 comments on commit c0729ef

Please sign in to comment.