Skip to content

Commit

Permalink
fix falky test pkg/registry/core/persistentvolume/storage TestUpdateS…
Browse files Browse the repository at this point in the history
…tatus
  • Loading branch information
carlory authored and WillardHu committed Dec 27, 2023
1 parent e94a712 commit 6e1cf85
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 115 deletions.
16 changes: 10 additions & 6 deletions pkg/registry/core/persistentvolume/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ limitations under the License.
package storage

import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/features"
"context"
"testing"
"time"

"context"
"github.com/google/go-cmp/cmp"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -35,7 +33,10 @@ import (
genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing"
"k8s.io/apiserver/pkg/registry/rest"
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/registry/core/persistentvolume"
"k8s.io/kubernetes/pkg/registry/registrytest"
)
Expand Down Expand Up @@ -190,13 +191,16 @@ func TestUpdateStatus(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}

// We need to set custom timestamp which is not the same as the one on existing PV
// - doing so will prevent timestamp update on phase change and custom one is used instead.
pvStartTimestamp = &metav1.Time{Time: pvStartTimestamp.Time.Add(time.Second)}

pvIn := &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumeBound,
// Set the same timestamp as original PV so this won't get updated on phase change breaking DeepEqual() later in test.
Phase: api.VolumeBound,
LastPhaseTransitionTime: pvStartTimestamp,
},
}
Expand Down
20 changes: 10 additions & 10 deletions staging/src/k8s.io/client-go/rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,36 +335,36 @@ func TestHTTPProxy(t *testing.T) {
}

func TestCreateBackoffManager(t *testing.T) {

ctx := context.Background()
theUrl, _ := url.Parse("http://localhost")

// 1 second base backoff + duration of 2 seconds -> exponential backoff for requests.
t.Setenv(envBackoffBase, "1")
t.Setenv(envBackoffDuration, "2")
backoff := readExpBackoffConfig()
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(theUrl, nil, 500)
if backoff.CalculateBackoff(theUrl)/time.Second != 2 {
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
if backoff.CalculateBackoff(ctx, theUrl)/time.Second != 2 {
t.Errorf("Backoff env not working.")
}

// 0 duration -> no backoff.
t.Setenv(envBackoffBase, "1")
t.Setenv(envBackoffDuration, "0")
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff = readExpBackoffConfig()
if backoff.CalculateBackoff(theUrl)/time.Second != 0 {
if backoff.CalculateBackoff(ctx, theUrl)/time.Second != 0 {
t.Errorf("Zero backoff duration, but backoff still occurring.")
}

// No env -> No backoff.
t.Setenv(envBackoffBase, "")
t.Setenv(envBackoffDuration, "")
backoff = readExpBackoffConfig()
backoff.UpdateBackoff(theUrl, nil, 500)
backoff.UpdateBackoff(theUrl, nil, 500)
if backoff.CalculateBackoff(theUrl)/time.Second != 0 {
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
backoff.UpdateBackoff(ctx, theUrl, nil, 500)
if backoff.CalculateBackoff(ctx, theUrl)/time.Second != 0 {
t.Errorf("Backoff should have been 0.")
}

Expand Down
7 changes: 6 additions & 1 deletion staging/src/k8s.io/client-go/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import (
const (
DefaultQPS float32 = 5.0
DefaultBurst int = 10

loggerNameConfig = "rest_config"
)

var ErrNotInCluster = errors.New("unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined")
Expand Down Expand Up @@ -508,11 +510,14 @@ func DefaultKubernetesUserAgent() string {
// kubernetes gives to pods. It's intended for clients that expect to be
// running inside a pod running on kubernetes. It will return ErrNotInCluster
// if called from a process not running in a kubernetes environment.
// logcheck:context InClusterConfig should not be used in code which supports contextual logging.
func InClusterConfig() (*Config, error) {
const (
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
)
logger := klog.Background().WithName(loggerNameConfig)

host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, ErrNotInCluster
Expand All @@ -526,7 +531,7 @@ func InClusterConfig() (*Config, error) {
tlsClientConfig := TLSClientConfig{}

if _, err := certutil.NewPool(rootCAFile); err != nil {
klog.Errorf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
logger.Error(err, "Expected to load root CA config", "rootCAFile", rootCAFile)
} else {
tlsClientConfig.CAFile = rootCAFile
}
Expand Down
4 changes: 3 additions & 1 deletion staging/src/k8s.io/client-go/rest/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"k8s.io/client-go/transport"
"k8s.io/client-go/util/flowcontrol"

"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -264,7 +265,8 @@ var fakeWrapperFunc = func(http.RoundTripper) http.RoundTripper {

type fakeWarningHandler struct{}

func (f fakeWarningHandler) HandleWarningHeader(code int, agent string, message string) {}
func (f fakeWarningHandler) HandleWarningHeader(logger logr.Logger, code int, agent string, message string) {
}

type fakeNegotiatedSerializer struct{}

Expand Down
5 changes: 4 additions & 1 deletion staging/src/k8s.io/client-go/rest/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ func (n *noopPersister) Persist(_ map[string]string) error {
var pluginsLock sync.Mutex
var plugins = make(map[string]Factory)

// RegisterAuthProviderPlugin ...
// logcheck:context this function RegisterAuthProviderPlugin(..) is called only when auth provider plugins are init().
func RegisterAuthProviderPlugin(name string, plugin Factory) error {
logger := klog.Background()
pluginsLock.Lock()
defer pluginsLock.Unlock()
if _, found := plugins[name]; found {
return fmt.Errorf("auth Provider Plugin %q was registered twice", name)
}
klog.V(4).Infof("Registered Auth Provider Plugin %q", name)
logger.V(4).Info("registered auth provider plugin", "name", name)
plugins[name] = plugin
return nil
}
Expand Down

0 comments on commit 6e1cf85

Please sign in to comment.