Compare commits

..

1 Commits

Author SHA1 Message Date
Kubernetes Publisher
304f9afaac Update dependencies to v0.29.0-alpha.3 tag 2023-11-03 01:17:06 +00:00
27 changed files with 134 additions and 1265 deletions

View File

@@ -1,79 +0,0 @@
/*
Copyright The Kubernetes 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.
*/
// Code generated by applyconfiguration-gen. DO NOT EDIT.
package v1
import (
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
)
// ClusterTrustBundleProjectionApplyConfiguration represents an declarative configuration of the ClusterTrustBundleProjection type for use
// with apply.
type ClusterTrustBundleProjectionApplyConfiguration struct {
Name *string `json:"name,omitempty"`
SignerName *string `json:"signerName,omitempty"`
LabelSelector *v1.LabelSelectorApplyConfiguration `json:"labelSelector,omitempty"`
Optional *bool `json:"optional,omitempty"`
Path *string `json:"path,omitempty"`
}
// ClusterTrustBundleProjectionApplyConfiguration constructs an declarative configuration of the ClusterTrustBundleProjection type for use with
// apply.
func ClusterTrustBundleProjection() *ClusterTrustBundleProjectionApplyConfiguration {
return &ClusterTrustBundleProjectionApplyConfiguration{}
}
// WithName sets the Name field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the Name field is set to the value of the last call.
func (b *ClusterTrustBundleProjectionApplyConfiguration) WithName(value string) *ClusterTrustBundleProjectionApplyConfiguration {
b.Name = &value
return b
}
// WithSignerName sets the SignerName field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the SignerName field is set to the value of the last call.
func (b *ClusterTrustBundleProjectionApplyConfiguration) WithSignerName(value string) *ClusterTrustBundleProjectionApplyConfiguration {
b.SignerName = &value
return b
}
// WithLabelSelector sets the LabelSelector field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the LabelSelector field is set to the value of the last call.
func (b *ClusterTrustBundleProjectionApplyConfiguration) WithLabelSelector(value *v1.LabelSelectorApplyConfiguration) *ClusterTrustBundleProjectionApplyConfiguration {
b.LabelSelector = value
return b
}
// WithOptional sets the Optional field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the Optional field is set to the value of the last call.
func (b *ClusterTrustBundleProjectionApplyConfiguration) WithOptional(value bool) *ClusterTrustBundleProjectionApplyConfiguration {
b.Optional = &value
return b
}
// WithPath sets the Path field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the Path field is set to the value of the last call.
func (b *ClusterTrustBundleProjectionApplyConfiguration) WithPath(value string) *ClusterTrustBundleProjectionApplyConfiguration {
b.Path = &value
return b
}

View File

@@ -25,7 +25,6 @@ type VolumeProjectionApplyConfiguration struct {
DownwardAPI *DownwardAPIProjectionApplyConfiguration `json:"downwardAPI,omitempty"`
ConfigMap *ConfigMapProjectionApplyConfiguration `json:"configMap,omitempty"`
ServiceAccountToken *ServiceAccountTokenProjectionApplyConfiguration `json:"serviceAccountToken,omitempty"`
ClusterTrustBundle *ClusterTrustBundleProjectionApplyConfiguration `json:"clusterTrustBundle,omitempty"`
}
// VolumeProjectionApplyConfiguration constructs an declarative configuration of the VolumeProjection type for use with
@@ -65,11 +64,3 @@ func (b *VolumeProjectionApplyConfiguration) WithServiceAccountToken(value *Serv
b.ServiceAccountToken = value
return b
}
// WithClusterTrustBundle sets the ClusterTrustBundle field in the declarative configuration to the given value
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
// If called multiple times, the ClusterTrustBundle field is set to the value of the last call.
func (b *VolumeProjectionApplyConfiguration) WithClusterTrustBundle(value *ClusterTrustBundleProjectionApplyConfiguration) *VolumeProjectionApplyConfiguration {
b.ClusterTrustBundle = value
return b
}

View File

@@ -4371,25 +4371,6 @@ var schemaYAML = typed.YAMLObject(`types:
- name: timeoutSeconds
type:
scalar: numeric
- name: io.k8s.api.core.v1.ClusterTrustBundleProjection
map:
fields:
- name: labelSelector
type:
namedType: io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector
- name: name
type:
scalar: string
- name: optional
type:
scalar: boolean
- name: path
type:
scalar: string
default: ""
- name: signerName
type:
scalar: string
- name: io.k8s.api.core.v1.ComponentCondition
map:
fields:
@@ -7893,9 +7874,6 @@ var schemaYAML = typed.YAMLObject(`types:
- name: io.k8s.api.core.v1.VolumeProjection
map:
fields:
- name: clusterTrustBundle
type:
namedType: io.k8s.api.core.v1.ClusterTrustBundleProjection
- name: configMap
type:
namedType: io.k8s.api.core.v1.ConfigMapProjection

View File

@@ -609,8 +609,6 @@ func ForKind(kind schema.GroupVersionKind) interface{} {
return &applyconfigurationscorev1.ClaimSourceApplyConfiguration{}
case corev1.SchemeGroupVersion.WithKind("ClientIPConfig"):
return &applyconfigurationscorev1.ClientIPConfigApplyConfiguration{}
case corev1.SchemeGroupVersion.WithKind("ClusterTrustBundleProjection"):
return &applyconfigurationscorev1.ClusterTrustBundleProjectionApplyConfiguration{}
case corev1.SchemeGroupVersion.WithKind("ComponentCondition"):
return &applyconfigurationscorev1.ComponentConditionApplyConfiguration{}
case corev1.SchemeGroupVersion.WithKind("ComponentStatus"):

View File

@@ -1,138 +0,0 @@
/*
Copyright 2024 The Kubernetes 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 features
import (
"fmt"
"os"
"strconv"
"sync"
"sync/atomic"
"k8s.io/apimachinery/pkg/util/naming"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog/v2"
)
// internalPackages are packages that ignored when creating a name for featureGates. These packages are in the common
// call chains, so they'd be unhelpful as names.
var internalPackages = []string{"k8s.io/client-go/features/envvar.go"}
var _ Gates = &envVarFeatureGates{}
// newEnvVarFeatureGates creates a feature gate that allows for registration
// of features and checking if the features are enabled.
//
// On the first call to Enabled, the effective state of all known features is loaded from
// environment variables. The environment variable read for a given feature is formed by
// concatenating the prefix "KUBE_FEATURE_" with the feature's name.
//
// For example, if you have a feature named "MyFeature"
// setting an environmental variable "KUBE_FEATURE_MyFeature"
// will allow you to configure the state of that feature.
//
// Please note that environmental variables can only be set to the boolean value.
// Incorrect values will be ignored and logged.
func newEnvVarFeatureGates(features map[Feature]FeatureSpec) *envVarFeatureGates {
known := map[Feature]FeatureSpec{}
for name, spec := range features {
known[name] = spec
}
fg := &envVarFeatureGates{
callSiteName: naming.GetNameFromCallsite(internalPackages...),
known: known,
}
fg.enabled.Store(map[Feature]bool{})
return fg
}
// envVarFeatureGates implements Gates and allows for feature registration.
type envVarFeatureGates struct {
// callSiteName holds the name of the file
// that created this instance
callSiteName string
// readEnvVarsOnce guards reading environmental variables
readEnvVarsOnce sync.Once
// known holds known feature gates
known map[Feature]FeatureSpec
// enabled holds a map[Feature]bool
// with values explicitly set via env var
enabled atomic.Value
// readEnvVars holds the boolean value which
// indicates whether readEnvVarsOnce has been called.
readEnvVars atomic.Bool
}
// Enabled returns true if the key is enabled. If the key is not known, this call will panic.
func (f *envVarFeatureGates) Enabled(key Feature) bool {
if v, ok := f.getEnabledMapFromEnvVar()[key]; ok {
return v
}
if v, ok := f.known[key]; ok {
return v.Default
}
panic(fmt.Errorf("feature %q is not registered in FeatureGates %q", key, f.callSiteName))
}
// getEnabledMapFromEnvVar will fill the enabled map on the first call.
// This is the only time a known feature can be set to a value
// read from the corresponding environmental variable.
func (f *envVarFeatureGates) getEnabledMapFromEnvVar() map[Feature]bool {
f.readEnvVarsOnce.Do(func() {
featureGatesState := map[Feature]bool{}
for feature, featureSpec := range f.known {
featureState, featureStateSet := os.LookupEnv(fmt.Sprintf("KUBE_FEATURE_%s", feature))
if !featureStateSet {
continue
}
boolVal, boolErr := strconv.ParseBool(featureState)
switch {
case boolErr != nil:
utilruntime.HandleError(fmt.Errorf("cannot set feature gate %q to %q, due to %v", feature, featureState, boolErr))
case featureSpec.LockToDefault:
if boolVal != featureSpec.Default {
utilruntime.HandleError(fmt.Errorf("cannot set feature gate %q to %q, feature is locked to %v", feature, featureState, featureSpec.Default))
break
}
featureGatesState[feature] = featureSpec.Default
default:
featureGatesState[feature] = boolVal
}
}
f.enabled.Store(featureGatesState)
f.readEnvVars.Store(true)
for feature, featureSpec := range f.known {
if featureState, ok := featureGatesState[feature]; ok {
klog.V(1).InfoS("Feature gate updated state", "feature", feature, "enabled", featureState)
continue
}
klog.V(1).InfoS("Feature gate default state", "feature", feature, "enabled", featureSpec.Default)
}
})
return f.enabled.Load().(map[Feature]bool)
}
func (f *envVarFeatureGates) hasAlreadyReadEnvVar() bool {
return f.readEnvVars.Load()
}

View File

@@ -1,156 +0,0 @@
/*
Copyright 2024 The Kubernetes 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 features
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestEnvVarFeatureGates(t *testing.T) {
defaultTestFeatures := map[Feature]FeatureSpec{
"TestAlpha": {
Default: false,
LockToDefault: false,
PreRelease: "Alpha",
},
"TestBeta": {
Default: true,
LockToDefault: false,
PreRelease: "Beta",
},
}
expectedDefaultFeaturesState := map[Feature]bool{"TestAlpha": false, "TestBeta": true}
copyExpectedStateMap := func(toCopy map[Feature]bool) map[Feature]bool {
m := map[Feature]bool{}
for k, v := range toCopy {
m[k] = v
}
return m
}
scenarios := []struct {
name string
features map[Feature]FeatureSpec
envVariables map[string]string
expectedFeaturesState map[Feature]bool
expectedInternalEnabledFeatureState map[Feature]bool
}{
{
name: "can add empty features",
},
{
name: "no env var, features get Defaults assigned",
features: defaultTestFeatures,
expectedFeaturesState: expectedDefaultFeaturesState,
},
{
name: "incorrect env var, feature gets Default assigned",
features: defaultTestFeatures,
envVariables: map[string]string{"TestAlpha": "true"},
expectedFeaturesState: expectedDefaultFeaturesState,
},
{
name: "correct env var changes the feature gets state",
features: defaultTestFeatures,
envVariables: map[string]string{"KUBE_FEATURE_TestAlpha": "true"},
expectedFeaturesState: func() map[Feature]bool {
expectedDefaultFeaturesStateCopy := copyExpectedStateMap(expectedDefaultFeaturesState)
expectedDefaultFeaturesStateCopy["TestAlpha"] = true
return expectedDefaultFeaturesStateCopy
}(),
expectedInternalEnabledFeatureState: map[Feature]bool{"TestAlpha": true},
},
{
name: "incorrect env var value gets ignored",
features: defaultTestFeatures,
envVariables: map[string]string{"KUBE_FEATURE_TestAlpha": "TrueFalse"},
expectedFeaturesState: expectedDefaultFeaturesState,
},
{
name: "empty env var value gets ignored",
features: defaultTestFeatures,
envVariables: map[string]string{"KUBE_FEATURE_TestAlpha": ""},
expectedFeaturesState: expectedDefaultFeaturesState,
},
{
name: "a feature LockToDefault wins",
features: map[Feature]FeatureSpec{
"TestAlpha": {
Default: true,
LockToDefault: true,
PreRelease: "Alpha",
},
},
envVariables: map[string]string{"KUBE_FEATURE_TestAlpha": "False"},
expectedFeaturesState: map[Feature]bool{"TestAlpha": true},
},
{
name: "setting a feature to LockToDefault changes the internal state",
features: map[Feature]FeatureSpec{
"TestAlpha": {
Default: true,
LockToDefault: true,
PreRelease: "Alpha",
},
},
envVariables: map[string]string{"KUBE_FEATURE_TestAlpha": "True"},
expectedFeaturesState: map[Feature]bool{"TestAlpha": true},
expectedInternalEnabledFeatureState: map[Feature]bool{"TestAlpha": true},
},
}
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
for k, v := range scenario.envVariables {
t.Setenv(k, v)
}
target := newEnvVarFeatureGates(scenario.features)
for expectedFeature, expectedValue := range scenario.expectedFeaturesState {
actualValue := target.Enabled(expectedFeature)
require.Equal(t, actualValue, expectedValue, "expected feature=%v, to be=%v, not=%v", expectedFeature, expectedValue, actualValue)
}
enabledInternalMap := target.enabled.Load().(map[Feature]bool)
require.Len(t, enabledInternalMap, len(scenario.expectedInternalEnabledFeatureState))
for expectedFeature, expectedInternalPresence := range scenario.expectedInternalEnabledFeatureState {
featureInternalValue, featureSet := enabledInternalMap[expectedFeature]
require.Equal(t, expectedInternalPresence, featureSet, "feature %v present = %v, expected = %v", expectedFeature, featureSet, expectedInternalPresence)
expectedFeatureInternalValue := scenario.expectedFeaturesState[expectedFeature]
require.Equal(t, expectedFeatureInternalValue, featureInternalValue)
}
})
}
}
func TestEnvVarFeatureGatesEnabledPanic(t *testing.T) {
target := newEnvVarFeatureGates(nil)
require.PanicsWithError(t, fmt.Errorf("feature %q is not registered in FeatureGates %q", "UnknownFeature", target.callSiteName).Error(), func() { target.Enabled("UnknownFeature") })
}
func TestHasAlreadyReadEnvVar(t *testing.T) {
target := newEnvVarFeatureGates(nil)
require.False(t, target.hasAlreadyReadEnvVar())
_ = target.getEnabledMapFromEnvVar()
require.True(t, target.hasAlreadyReadEnvVar())
}

View File

@@ -1,143 +0,0 @@
/*
Copyright 2024 The Kubernetes 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 features
import (
"errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sync/atomic"
)
// NOTE: types Feature, FeatureSpec, prerelease (and its values)
// were duplicated from the component-base repository
//
// for more information please refer to https://docs.google.com/document/d/1g9BGCRw-7ucUxO6OtCWbb3lfzUGA_uU9178wLdXAIfs
const (
// Values for PreRelease.
Alpha = prerelease("ALPHA")
Beta = prerelease("BETA")
GA = prerelease("")
// Deprecated
Deprecated = prerelease("DEPRECATED")
)
type prerelease string
type Feature string
type FeatureSpec struct {
// Default is the default enablement state for the feature
Default bool
// LockToDefault indicates that the feature is locked to its default and cannot be changed
LockToDefault bool
// PreRelease indicates the maturity level of the feature
PreRelease prerelease
}
// Gates indicates whether a given feature is enabled or not.
type Gates interface {
// Enabled returns true if the key is enabled.
Enabled(key Feature) bool
}
// Registry represents an external feature gates registry.
type Registry interface {
// Add adds existing feature gates to the provided registry.
//
// As of today, this method is used by AddFeaturesToExistingFeatureGates and
// ReplaceFeatureGates to take control of the features exposed by this library.
Add(map[Feature]FeatureSpec) error
}
// FeatureGates returns the feature gates exposed by this library.
//
// By default, only the default features gates will be returned.
// The default implementation allows controlling the features
// via environmental variables.
// For example, if you have a feature named "MyFeature"
// setting an environmental variable "KUBE_FEATURE_MyFeature"
// will allow you to configure the state of that feature.
//
// Please note that the actual set of the feature gates
// might be overwritten by calling ReplaceFeatureGates method.
func FeatureGates() Gates {
return featureGates.Load().(*featureGatesWrapper).Gates
}
// AddFeaturesToExistingFeatureGates adds the default feature gates to the provided registry.
// Usually this function is combined with ReplaceFeatureGates to take control of the
// features exposed by this library.
func AddFeaturesToExistingFeatureGates(registry Registry) error {
return registry.Add(defaultKubernetesFeatureGates)
}
// ReplaceFeatureGates overwrites the default implementation of the feature gates
// used by this library.
//
// Useful for binaries that would like to have full control of the features
// exposed by this library, such as allowing consumers of a binary
// to interact with the features via a command line flag.
//
// For example:
//
// // first, register client-go's features to your registry.
// clientgofeaturegate.AddFeaturesToExistingFeatureGates(utilfeature.DefaultMutableFeatureGate)
// // then replace client-go's feature gates implementation with your implementation
// clientgofeaturegate.ReplaceFeatureGates(utilfeature.DefaultMutableFeatureGate)
func ReplaceFeatureGates(newFeatureGates Gates) {
if replaceFeatureGatesWithWarningIndicator(newFeatureGates) {
utilruntime.HandleError(errors.New("the default feature gates implementation has already been used and now it's being overwritten. This might lead to unexpected behaviour. Check your initialization order"))
}
}
func replaceFeatureGatesWithWarningIndicator(newFeatureGates Gates) bool {
shouldProduceWarning := false
if defaultFeatureGates, ok := FeatureGates().(*envVarFeatureGates); ok {
if defaultFeatureGates.hasAlreadyReadEnvVar() {
shouldProduceWarning = true
}
}
wrappedFeatureGates := &featureGatesWrapper{newFeatureGates}
featureGates.Store(wrappedFeatureGates)
return shouldProduceWarning
}
func init() {
envVarGates := newEnvVarFeatureGates(defaultKubernetesFeatureGates)
wrappedFeatureGates := &featureGatesWrapper{envVarGates}
featureGates.Store(wrappedFeatureGates)
}
// featureGatesWrapper a thin wrapper to satisfy featureGates variable (atomic.Value).
// That is, all calls to Store for a given Value must use values of the same concrete type.
type featureGatesWrapper struct {
Gates
}
var (
// featureGates is a shared global FeatureGates.
//
// Top-level commands/options setup that needs to modify this feature gates
// should use AddFeaturesToExistingFeatureGates followed by ReplaceFeatureGates.
featureGates = &atomic.Value{}
)

View File

@@ -1,40 +0,0 @@
/*
Copyright 2024 The Kubernetes 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 features
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestAddFeaturesToExistingFeatureGates ensures that
// the defaultKubernetesFeatureGates are added to a test feature gates registry.
func TestAddFeaturesToExistingFeatureGates(t *testing.T) {
fakeFeatureGates := &fakeRegistry{}
require.NoError(t, AddFeaturesToExistingFeatureGates(fakeFeatureGates))
require.Equal(t, defaultKubernetesFeatureGates, fakeFeatureGates.specs)
}
type fakeRegistry struct {
specs map[Feature]FeatureSpec
}
func (f *fakeRegistry) Add(specs map[Feature]FeatureSpec) error {
f.specs = specs
return nil
}

View File

@@ -1,49 +0,0 @@
/*
Copyright 2024 The Kubernetes 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 features
const (
// Every feature gate should add method here following this template:
//
// // owner: @username
// // alpha: v1.4
// MyFeature featuregate.Feature = "MyFeature"
//
// Feature gates should be listed in alphabetical, case-sensitive
// (upper before any lower case character) order. This reduces the risk
// of code conflicts because changes are more likely to be scattered
// across the file.
// owner: @p0lyn0mial
// beta: v1.30
//
// Allow the client to get a stream of individual items instead of chunking from the server.
//
// NOTE:
// The feature is disabled in Beta by default because
// it will only be turned on for selected control plane component(s).
WatchListClient Feature = "WatchListClient"
)
// defaultKubernetesFeatureGates consists of all known Kubernetes-specific feature keys.
//
// To add a new feature, define a key for it above and add it here.
// After registering with the binary, the features are, by default, controllable using environment variables.
// For more details, please see envVarFeatureGates implementation.
var defaultKubernetesFeatureGates = map[Feature]FeatureSpec{
WatchListClient: {Default: false, PreRelease: Beta},
}

View File

@@ -1,79 +0,0 @@
/*
Copyright 2024 The Kubernetes 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 testing
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"k8s.io/client-go/features"
)
func TestDriveInitDefaultFeatureGates(t *testing.T) {
featureGates := features.FeatureGates()
assertFunctionPanicsWithMessage(t, func() { featureGates.Enabled("FakeFeatureGate") }, "features.FeatureGates().Enabled", fmt.Sprintf("feature %q is not registered in FeatureGate", "FakeFeatureGate"))
fakeFeatureGates := &alwaysEnabledFakeGates{}
require.True(t, fakeFeatureGates.Enabled("FakeFeatureGate"))
features.ReplaceFeatureGates(fakeFeatureGates)
featureGates = features.FeatureGates()
assertFeatureGatesType(t, featureGates)
require.True(t, featureGates.Enabled("FakeFeatureGate"))
}
type alwaysEnabledFakeGates struct{}
func (f *alwaysEnabledFakeGates) Enabled(features.Feature) bool {
return true
}
func assertFeatureGatesType(t *testing.T, fg features.Gates) {
_, ok := fg.(*alwaysEnabledFakeGates)
if !ok {
t.Fatalf("passed features.FeatureGates() is NOT of type *alwaysEnabledFakeGates, it is of type = %T", fg)
}
}
func assertFunctionPanicsWithMessage(t *testing.T, f func(), fName, errMessage string) {
didPanic, panicMessage := didFunctionPanic(f)
if !didPanic {
t.Fatalf("function %q did not panicked", fName)
}
panicError, ok := panicMessage.(error)
if !ok || !strings.Contains(panicError.Error(), errMessage) {
t.Fatalf("func %q should panic with error message:\t%#v\n\tPanic value:\t%#v\n", fName, errMessage, panicMessage)
}
}
func didFunctionPanic(f func()) (didPanic bool, panicMessage interface{}) {
didPanic = true
defer func() {
panicMessage = recover()
}()
f()
didPanic = false
return
}

24
go.mod
View File

@@ -2,7 +2,7 @@
module k8s.io/client-go
go 1.21
go 1.21.3
require (
github.com/evanphx/json-patch v4.12.0+incompatible
@@ -19,15 +19,15 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.19.0
golang.org/x/net v0.17.0
golang.org/x/oauth2 v0.10.0
golang.org/x/term v0.15.0
golang.org/x/term v0.13.0
golang.org/x/time v0.3.0
google.golang.org/protobuf v1.31.0
k8s.io/api v0.30.0-alpha.1
k8s.io/apimachinery v0.30.0-alpha.1
k8s.io/klog/v2 v2.120.1
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e
k8s.io/api v0.29.0-alpha.3
k8s.io/apimachinery v0.29.0-alpha.3
k8s.io/klog/v2 v2.110.1
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd
sigs.k8s.io/structured-merge-diff/v4 v4.4.1
@@ -37,7 +37,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
@@ -52,8 +52,8 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
@@ -61,6 +61,6 @@ require (
)
replace (
k8s.io/api => k8s.io/api v0.30.0-alpha.1
k8s.io/apimachinery => k8s.io/apimachinery v0.30.0-alpha.1
k8s.io/api => k8s.io/api v0.29.0-alpha.3
k8s.io/apimachinery => k8s.io/apimachinery v0.29.0-alpha.3
)

48
go.sum
View File

@@ -8,8 +8,8 @@ github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxER
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
@@ -74,10 +74,10 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE=
github.com/onsi/gomega v1.31.0/go.mod h1:DW9aCi7U6Yi40wNVAvT6kzFnEVEI5n3DloYBiKiT6zk=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -109,8 +109,8 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8=
golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -119,23 +119,23 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -157,14 +157,14 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.30.0-alpha.1 h1:8FCjW0J6yU+tZkCn2j7dRND3GM7qKXxarXmNOc3Ujt8=
k8s.io/api v0.30.0-alpha.1/go.mod h1:dmO1fSXFEGktE3nbj3Q7oCKTc5fvPBkRvn5ltvdP7jY=
k8s.io/apimachinery v0.30.0-alpha.1 h1:AAQ3jCL1jnVGUNCr0osDLaO62fJACPiR3vXyozvWKMY=
k8s.io/apimachinery v0.30.0-alpha.1/go.mod h1:Oh3ZrffM1/I8O/43oAA+aoOYgSregIXHxcWJB9ZRfQ8=
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e h1:snPmy96t93RredGRjKfMFt+gvxuVAncqSAyBveJtr4Q=
k8s.io/kube-openapi v0.0.0-20231113174909-778a5567bc1e/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/api v0.29.0-alpha.3 h1:6QllqDvVn1jBNDXtFKzJg7mNQYYHYVANNBfP4z6Fu7Q=
k8s.io/api v0.29.0-alpha.3/go.mod h1:9zVQmGyL++Ki1RnuKUQ65LVgPP7WPq6pJwoQPfz9QPk=
k8s.io/apimachinery v0.29.0-alpha.3 h1:Y/VavRd57V5fliXV8M2Zr1Xyzi+raIhkDemWdGuuw6w=
k8s.io/apimachinery v0.29.0-alpha.3/go.mod h1:yFk3nwBh/jXlkMvRKH7BKtX7saT1lRmmGV6Ru0cTSUA=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/AuzbMm96cd3YHRTU83I780=
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=

View File

@@ -191,7 +191,7 @@ func (c *client) Get(ctx context.Context, name string, opts metav1.GetOptions, s
}
obj, err := result.Get()
if runtime.IsNotRegisteredError(err) {
klog.FromContext(ctx).V(5).Info("Could not retrieve PartialObjectMetadata", "err", err)
klog.V(5).Infof("Unable to retrieve PartialObjectMetadata: %#v", err)
rawBytes, err := result.Raw()
if err != nil {
return nil, err
@@ -227,7 +227,7 @@ func (c *client) List(ctx context.Context, opts metav1.ListOptions) (*metav1.Par
}
obj, err := result.Get()
if runtime.IsNotRegisteredError(err) {
klog.FromContext(ctx).V(5).Info("Could not retrieve PartialObjectMetadataList", "err", err)
klog.V(5).Infof("Unable to retrieve PartialObjectMetadataList: %#v", err)
rawBytes, err := result.Raw()
if err != nil {
return nil, err

View File

@@ -32,7 +32,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/klog/v2/ktesting"
)
func TestClient(t *testing.T) {
@@ -56,7 +55,7 @@ func TestClient(t *testing.T) {
testCases := []struct {
name string
handler func(t *testing.T, w http.ResponseWriter, req *http.Request)
want func(ctx context.Context, t *testing.T, client *Client)
want func(t *testing.T, client *Client)
}{
{
name: "GET is able to convert a JSON object to PartialObjectMetadata",
@@ -78,8 +77,8 @@ func TestClient(t *testing.T) {
},
})
},
want: func(ctx context.Context, t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(ctx, "name", metav1.GetOptions{})
want: func(t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
@@ -126,8 +125,8 @@ func TestClient(t *testing.T) {
},
})
},
want: func(ctx context.Context, t *testing.T, client *Client) {
objs, err := client.Resource(gvr).Namespace("ns").List(ctx, metav1.ListOptions{})
want: func(t *testing.T, client *Client) {
objs, err := client.Resource(gvr).Namespace("ns").List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Fatal(err)
}
@@ -168,8 +167,8 @@ func TestClient(t *testing.T) {
},
})
},
want: func(ctx context.Context, t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(ctx, "name", metav1.GetOptions{})
want: func(t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
if err == nil || !runtime.IsMissingKind(err) {
t.Fatal(err)
}
@@ -197,8 +196,8 @@ func TestClient(t *testing.T) {
},
})
},
want: func(ctx context.Context, t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(ctx, "name", metav1.GetOptions{})
want: func(t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
if err == nil || !runtime.IsMissingVersion(err) {
t.Fatal(err)
}
@@ -225,8 +224,8 @@ func TestClient(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{},
})
},
want: func(ctx context.Context, t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(ctx, "name", metav1.GetOptions{})
want: func(t *testing.T, client *Client) {
obj, err := client.Resource(gvr).Namespace("ns").Get(context.TODO(), "name", metav1.GetOptions{})
if err == nil || !strings.Contains(err.Error(), "object does not appear to match the ObjectMeta schema") {
t.Fatal(err)
}
@@ -255,8 +254,8 @@ func TestClient(t *testing.T) {
}
writeJSON(t, w, statusOK)
},
want: func(ctx context.Context, t *testing.T, client *Client) {
err := client.Resource(gvr).Namespace("ns").Delete(ctx, "name", metav1.DeleteOptions{})
want: func(t *testing.T, client *Client) {
err := client.Resource(gvr).Namespace("ns").Delete(context.TODO(), "name", metav1.DeleteOptions{})
if err != nil {
t.Fatal(err)
}
@@ -283,8 +282,8 @@ func TestClient(t *testing.T) {
writeJSON(t, w, statusOK)
},
want: func(ctx context.Context, t *testing.T, client *Client) {
err := client.Resource(gvr).Namespace("ns").DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{})
want: func(t *testing.T, client *Client) {
err := client.Resource(gvr).Namespace("ns").DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
if err != nil {
t.Fatal(err)
}
@@ -297,10 +296,9 @@ func TestClient(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { tt.handler(t, w, req) }))
defer s.Close()
_, ctx := ktesting.NewTestContext(t)
cfg := ConfigFor(&rest.Config{Host: s.URL})
client := NewForConfigOrDie(cfg).(*Client)
tt.want(ctx, t, client)
tt.want(t, client)
})
}
}

View File

@@ -50,7 +50,8 @@ type Indexer interface {
// GetIndexers return the indexers
GetIndexers() Indexers
// AddIndexers adds more indexers to this store. This supports adding indexes after the store already has items.
// AddIndexers adds more indexers to this store. If you call this after you already have data
// in the store, the results are undefined.
AddIndexers(newIndexers Indexers) error
}

View File

@@ -43,7 +43,6 @@ import (
"k8s.io/klog/v2"
"k8s.io/utils/clock"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
"k8s.io/utils/trace"
)
@@ -108,9 +107,7 @@ type Reflector struct {
// might result in an increased memory consumption of the APIServer.
//
// See https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/3157-watch-list#design-details
//
// TODO(#115478): Consider making reflector.UseWatchList a private field. Since we implemented "api streaming" on the etcd storage layer it should work.
UseWatchList *bool
UseWatchList bool
}
// ResourceVersionUpdater is an interface that allows store implementation to
@@ -240,12 +237,8 @@ func NewReflectorWithOptions(lw ListerWatcher, expectedType interface{}, store S
r.expectedGVK = getExpectedGVKFromObject(expectedType)
}
// don't overwrite UseWatchList if already set
// because the higher layers (e.g. storage/cacher) disabled it on purpose
if r.UseWatchList == nil {
if s := os.Getenv("ENABLE_CLIENT_GO_WATCH_LIST_ALPHA"); len(s) > 0 {
r.UseWatchList = ptr.To(true)
}
if s := os.Getenv("ENABLE_CLIENT_GO_WATCH_LIST_ALPHA"); len(s) > 0 {
r.UseWatchList = true
}
return r
@@ -332,10 +325,9 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
klog.V(3).Infof("Listing and watching %v from %s", r.typeDescription, r.name)
var err error
var w watch.Interface
useWatchList := ptr.Deref(r.UseWatchList, false)
fallbackToList := !useWatchList
fallbackToList := !r.UseWatchList
if useWatchList {
if r.UseWatchList {
w, err = r.watchList(stopCh)
if w == nil && err == nil {
// stopCh was closed

View File

@@ -33,7 +33,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)
func TestWatchList(t *testing.T) {
@@ -416,7 +415,7 @@ func TestWatchList(t *testing.T) {
listWatcher.customListResponse = scenario.podList
listWatcher.closeAfterListRequests = scenario.closeAfterListRequests
if scenario.disableUseWatchList {
reflector.UseWatchList = ptr.To(false)
reflector.UseWatchList = false
}
err := reflector.ListAndWatch(stopCh)
@@ -506,7 +505,7 @@ func testData() (*fakeListWatcher, Store, *Reflector, chan struct{}) {
},
}
r := NewReflector(lw, &v1.Pod{}, s, 0)
r.UseWatchList = ptr.To(true)
r.UseWatchList = true
return lw, s, r, stopCh
}

View File

@@ -540,8 +540,8 @@ func (s *sharedIndexInformer) AddIndexers(indexers Indexers) error {
s.startedLock.Lock()
defer s.startedLock.Unlock()
if s.stopped {
return fmt.Errorf("indexer was not added because it has stopped already")
if s.started {
return fmt.Errorf("informer has already started")
}
return s.indexer.AddIndexers(indexers)

View File

@@ -26,9 +26,6 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -120,81 +117,6 @@ func isRegistered(i SharedInformer, h ResourceEventHandlerRegistration) bool {
return s.processor.getListener(h) != nil
}
func TestIndexer(t *testing.T) {
assert := assert.New(t)
// source simulates an apiserver object endpoint.
source := fcache.NewFakeControllerSource()
pod1 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Labels: map[string]string{"a": "a-val", "b": "b-val1"}}}
pod2 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod2", Labels: map[string]string{"b": "b-val2"}}}
pod3 := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod3", Labels: map[string]string{"a": "a-val2"}}}
source.Add(pod1)
source.Add(pod2)
// create the shared informer and resync every 1s
informer := NewSharedInformer(source, &v1.Pod{}, 1*time.Second).(*sharedIndexInformer)
err := informer.AddIndexers(map[string]IndexFunc{
"labels": func(obj interface{}) ([]string, error) {
res := []string{}
for k := range obj.(*v1.Pod).Labels {
res = append(res, k)
}
return res, nil
},
})
if err != nil {
t.Fatal(err)
}
stop := make(chan struct{})
defer close(stop)
go informer.Run(stop)
WaitForCacheSync(stop, informer.HasSynced)
cmpOps := cmpopts.SortSlices(func(a, b any) bool {
return a.(*v1.Pod).Name < b.(*v1.Pod).Name
})
// We should be able to lookup by index
res, err := informer.GetIndexer().ByIndex("labels", "a")
assert.NoError(err)
if diff := cmp.Diff([]any{pod1}, res); diff != "" {
t.Fatal(diff)
}
// Adding an item later is fine as well
source.Add(pod3)
// Event is async, need to poll
assert.Eventually(func() bool {
res, _ := informer.GetIndexer().ByIndex("labels", "a")
return cmp.Diff([]any{pod1, pod3}, res, cmpOps) == ""
}, time.Second*3, time.Millisecond)
// Adding an index later is also fine
err = informer.AddIndexers(map[string]IndexFunc{
"labels-again": func(obj interface{}) ([]string, error) {
res := []string{}
for k := range obj.(*v1.Pod).Labels {
res = append(res, k)
}
return res, nil
},
})
assert.NoError(err)
// Should be immediately available
res, err = informer.GetIndexer().ByIndex("labels-again", "a")
assert.NoError(err)
if diff := cmp.Diff([]any{pod1, pod3}, res, cmpOps); diff != "" {
t.Fatal(diff)
}
if got := informer.GetIndexer().ListIndexFuncValues("labels"); !sets.New(got...).Equal(sets.New("a", "b")) {
t.Fatalf("got %v", got)
}
if got := informer.GetIndexer().ListIndexFuncValues("labels-again"); !sets.New(got...).Equal(sets.New("a", "b")) {
t.Fatalf("got %v", got)
}
}
func TestListenerResyncPeriods(t *testing.T) {
// source simulates an apiserver object endpoint.
source := fcache.NewFakeControllerSource()

View File

@@ -52,7 +52,8 @@ type ThreadSafeStore interface {
ByIndex(indexName, indexedValue string) ([]interface{}, error)
GetIndexers() Indexers
// AddIndexers adds more indexers to this store. This supports adding indexes after the store already has items.
// AddIndexers adds more indexers to this store. If you call this after you already have data
// in the store, the results are undefined.
AddIndexers(newIndexers Indexers) error
// Resync is a no-op and is deprecated
Resync() error
@@ -134,66 +135,50 @@ func (i *storeIndex) addIndexers(newIndexers Indexers) error {
return nil
}
// updateSingleIndex modifies the objects location in the named index:
// - for create you must provide only the newObj
// - for update you must provide both the oldObj and the newObj
// - for delete you must provide only the oldObj
// updateSingleIndex must be called from a function that already has a lock on the cache
func (i *storeIndex) updateSingleIndex(name string, oldObj interface{}, newObj interface{}, key string) {
var oldIndexValues, indexValues []string
indexFunc, ok := i.indexers[name]
if !ok {
// Should never happen. Caller is responsible for ensuring this exists, and should call with lock
// held to avoid any races.
panic(fmt.Errorf("indexer %q does not exist", name))
}
if oldObj != nil {
var err error
oldIndexValues, err = indexFunc(oldObj)
if err != nil {
panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
}
} else {
oldIndexValues = oldIndexValues[:0]
}
if newObj != nil {
var err error
indexValues, err = indexFunc(newObj)
if err != nil {
panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
}
} else {
indexValues = indexValues[:0]
}
index := i.indices[name]
if index == nil {
index = Index{}
i.indices[name] = index
}
if len(indexValues) == 1 && len(oldIndexValues) == 1 && indexValues[0] == oldIndexValues[0] {
// We optimize for the most common case where indexFunc returns a single value which has not been changed
return
}
for _, value := range oldIndexValues {
i.deleteKeyFromIndex(key, value, index)
}
for _, value := range indexValues {
i.addKeyToIndex(key, value, index)
}
}
// updateIndices modifies the objects location in the managed indexes:
// - for create you must provide only the newObj
// - for update you must provide both the oldObj and the newObj
// - for delete you must provide only the oldObj
// updateIndices must be called from a function that already has a lock on the cache
func (i *storeIndex) updateIndices(oldObj interface{}, newObj interface{}, key string) {
for name := range i.indexers {
i.updateSingleIndex(name, oldObj, newObj, key)
var oldIndexValues, indexValues []string
var err error
for name, indexFunc := range i.indexers {
if oldObj != nil {
oldIndexValues, err = indexFunc(oldObj)
} else {
oldIndexValues = oldIndexValues[:0]
}
if err != nil {
panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
}
if newObj != nil {
indexValues, err = indexFunc(newObj)
} else {
indexValues = indexValues[:0]
}
if err != nil {
panic(fmt.Errorf("unable to calculate an index entry for key %q on index %q: %v", key, name, err))
}
index := i.indices[name]
if index == nil {
index = Index{}
i.indices[name] = index
}
if len(indexValues) == 1 && len(oldIndexValues) == 1 && indexValues[0] == oldIndexValues[0] {
// We optimize for the most common case where indexFunc returns a single value which has not been changed
continue
}
for _, value := range oldIndexValues {
i.deleteKeyFromIndex(key, value, index)
}
for _, value := range indexValues {
i.addKeyToIndex(key, value, index)
}
}
}
@@ -354,18 +339,11 @@ func (c *threadSafeMap) AddIndexers(newIndexers Indexers) error {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.index.addIndexers(newIndexers); err != nil {
return err
if len(c.items) > 0 {
return fmt.Errorf("cannot add indexers to running index")
}
// If there are already items, index them
for key, item := range c.items {
for name := range newIndexers {
c.index.updateSingleIndex(name, nil, item, key)
}
}
return nil
return c.index.addIndexers(newIndexers)
}
func (c *threadSafeMap) Resync() error {

View File

@@ -72,13 +72,6 @@ type ClientConfig interface {
ConfigAccess() ConfigAccess
}
// OverridingClientConfig is used to enable overrriding the raw KubeConfig
type OverridingClientConfig interface {
ClientConfig
// MergedRawConfig return the RawConfig merged with all overrides.
MergedRawConfig() (clientcmdapi.Config, error)
}
type PersistAuthProviderConfigForUser func(user string) restclient.AuthProviderConfigPersister
type promptedCredentials struct {
@@ -98,22 +91,22 @@ type DirectClientConfig struct {
}
// NewDefaultClientConfig creates a DirectClientConfig using the config.CurrentContext as the context name
func NewDefaultClientConfig(config clientcmdapi.Config, overrides *ConfigOverrides) OverridingClientConfig {
func NewDefaultClientConfig(config clientcmdapi.Config, overrides *ConfigOverrides) ClientConfig {
return &DirectClientConfig{config, config.CurrentContext, overrides, nil, NewDefaultClientConfigLoadingRules(), promptedCredentials{}}
}
// NewNonInteractiveClientConfig creates a DirectClientConfig using the passed context name and does not have a fallback reader for auth information
func NewNonInteractiveClientConfig(config clientcmdapi.Config, contextName string, overrides *ConfigOverrides, configAccess ConfigAccess) OverridingClientConfig {
func NewNonInteractiveClientConfig(config clientcmdapi.Config, contextName string, overrides *ConfigOverrides, configAccess ConfigAccess) ClientConfig {
return &DirectClientConfig{config, contextName, overrides, nil, configAccess, promptedCredentials{}}
}
// NewInteractiveClientConfig creates a DirectClientConfig using the passed context name and a reader in case auth information is not provided via files or flags
func NewInteractiveClientConfig(config clientcmdapi.Config, contextName string, overrides *ConfigOverrides, fallbackReader io.Reader, configAccess ConfigAccess) OverridingClientConfig {
func NewInteractiveClientConfig(config clientcmdapi.Config, contextName string, overrides *ConfigOverrides, fallbackReader io.Reader, configAccess ConfigAccess) ClientConfig {
return &DirectClientConfig{config, contextName, overrides, fallbackReader, configAccess, promptedCredentials{}}
}
// NewClientConfigFromBytes takes your kubeconfig and gives you back a ClientConfig
func NewClientConfigFromBytes(configBytes []byte) (OverridingClientConfig, error) {
func NewClientConfigFromBytes(configBytes []byte) (ClientConfig, error) {
config, err := Load(configBytes)
if err != nil {
return nil, err
@@ -136,40 +129,6 @@ func (config *DirectClientConfig) RawConfig() (clientcmdapi.Config, error) {
return config.config, nil
}
// MergedRawConfig returns the raw kube config merged with the overrides
func (config *DirectClientConfig) MergedRawConfig() (clientcmdapi.Config, error) {
if err := config.ConfirmUsable(); err != nil {
return clientcmdapi.Config{}, err
}
merged := config.config.DeepCopy()
// set the AuthInfo merged with overrides in the merged config
mergedAuthInfo, err := config.getAuthInfo()
if err != nil {
return clientcmdapi.Config{}, err
}
mergedAuthInfoName, _ := config.getAuthInfoName()
merged.AuthInfos[mergedAuthInfoName] = &mergedAuthInfo
// set the Context merged with overrides in the merged config
mergedContext, err := config.getContext()
if err != nil {
return clientcmdapi.Config{}, err
}
mergedContextName, _ := config.getContextName()
merged.Contexts[mergedContextName] = &mergedContext
merged.CurrentContext = mergedContextName
// set the Cluster merged with overrides in the merged config
configClusterInfo, err := config.getCluster()
if err != nil {
return clientcmdapi.Config{}, err
}
configClusterName, _ := config.getClusterName()
merged.Clusters[configClusterName] = &configClusterInfo
return *merged, nil
}
// ClientConfig implements ClientConfig
func (config *DirectClientConfig) ClientConfig() (*restclient.Config, error) {
// check that getAuthInfo, getContext, and getCluster do not return an error.

View File

@@ -1013,68 +1013,3 @@ func TestCleanANSIEscapeCodes(t *testing.T) {
})
}
}
func TestMergeRawConfigDoOverride(t *testing.T) {
const (
server = "https://anything.com:8080"
token = "the-token"
modifiedServer = "http://localhost:8081"
modifiedToken = "modified-token"
)
config := createValidTestConfig()
// add another context which to modify with overrides
config.Clusters["modify"] = &clientcmdapi.Cluster{
Server: server,
}
config.AuthInfos["modify"] = &clientcmdapi.AuthInfo{
Token: token,
}
config.Contexts["modify"] = &clientcmdapi.Context{
Cluster: "modify",
AuthInfo: "modify",
Namespace: "modify",
}
// create overrides for the modify context
overrides := &ConfigOverrides{
ClusterInfo: clientcmdapi.Cluster{
Server: modifiedServer,
},
Context: clientcmdapi.Context{
Namespace: "foobar",
Cluster: "modify",
AuthInfo: "modify",
},
AuthInfo: clientcmdapi.AuthInfo{
Token: modifiedToken,
},
CurrentContext: "modify",
}
cut := NewDefaultClientConfig(*config, overrides)
act, err := cut.MergedRawConfig()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// ensure overrides were applied to "modify"
actContext := act.CurrentContext
if actContext != "modify" {
t.Errorf("Expected context %v, got %v", "modify", actContext)
}
if act.Clusters[actContext].Server != "http://localhost:8081" {
t.Errorf("Expected server %v, got %v", "http://localhost:8081", act.Clusters[actContext].Server)
}
if act.Contexts[actContext].Namespace != "foobar" {
t.Errorf("Expected namespace %v, got %v", "foobar", act.Contexts[actContext].Namespace)
}
// ensure context "clean" was not touched
if act.Clusters["clean"].Server != config.Clusters["clean"].Server {
t.Errorf("Expected server %v, got %v", config.Clusters["clean"].Server, act.Clusters["clean"].Server)
}
if act.Contexts["clean"].Namespace != config.Contexts["clean"].Namespace {
t.Errorf("Expected namespace %v, got %v", config.Contexts["clean"].Namespace, act.Contexts["clean"].Namespace)
}
}

View File

@@ -404,15 +404,7 @@ type eventBroadcasterAdapterImpl struct {
// NewEventBroadcasterAdapter creates a wrapper around new and legacy broadcasters to simplify
// migration of individual components to the new Event API.
//
//logcheck:context // NewEventBroadcasterAdapterWithContext should be used instead because record.NewBroadcaster is called and works better when a context is supplied (contextual logging, cancellation).
func NewEventBroadcasterAdapter(client clientset.Interface) EventBroadcasterAdapter {
return NewEventBroadcasterAdapterWithContext(context.Background(), client)
}
// NewEventBroadcasterAdapterWithContext creates a wrapper around new and legacy broadcasters to simplify
// migration of individual components to the new Event API.
func NewEventBroadcasterAdapterWithContext(ctx context.Context, client clientset.Interface) EventBroadcasterAdapter {
eventClient := &eventBroadcasterAdapterImpl{}
if _, err := client.Discovery().ServerResourcesForGroupVersion(eventsv1.SchemeGroupVersion.String()); err == nil {
eventClient.eventsv1Client = client.EventsV1()
@@ -422,7 +414,7 @@ func NewEventBroadcasterAdapterWithContext(ctx context.Context, client clientset
// we create it unconditionally because its overhead is minor and will simplify using usage
// patterns of this library in all components.
eventClient.coreClient = client.CoreV1()
eventClient.coreBroadcaster = record.NewBroadcaster(record.WithContext(ctx))
eventClient.coreBroadcaster = record.NewBroadcaster()
return eventClient
}

View File

@@ -325,22 +325,7 @@ func (le *LeaderElector) tryAcquireOrRenew(ctx context.Context) bool {
AcquireTime: now,
}
// 1. fast path for the leader to update optimistically assuming that the record observed
// last time is the current version.
if le.IsLeader() && le.isLeaseValid(now.Time) {
oldObservedRecord := le.getObservedRecord()
leaderElectionRecord.AcquireTime = oldObservedRecord.AcquireTime
leaderElectionRecord.LeaderTransitions = oldObservedRecord.LeaderTransitions
err := le.config.Lock.Update(ctx, leaderElectionRecord)
if err == nil {
le.setObservedRecord(&leaderElectionRecord)
return true
}
klog.Errorf("Failed to update lock optimitically: %v, falling back to slow path", err)
}
// 2. obtain or create the ElectionRecord
// 1. obtain or create the ElectionRecord
oldLeaderElectionRecord, oldLeaderElectionRawRecord, err := le.config.Lock.Get(ctx)
if err != nil {
if !errors.IsNotFound(err) {
@@ -357,23 +342,24 @@ func (le *LeaderElector) tryAcquireOrRenew(ctx context.Context) bool {
return true
}
// 3. Record obtained, check the Identity & Time
// 2. Record obtained, check the Identity & Time
if !bytes.Equal(le.observedRawRecord, oldLeaderElectionRawRecord) {
le.setObservedRecord(oldLeaderElectionRecord)
le.observedRawRecord = oldLeaderElectionRawRecord
}
if len(oldLeaderElectionRecord.HolderIdentity) > 0 && le.isLeaseValid(now.Time) && !le.IsLeader() {
if len(oldLeaderElectionRecord.HolderIdentity) > 0 &&
le.observedTime.Add(time.Second*time.Duration(oldLeaderElectionRecord.LeaseDurationSeconds)).After(now.Time) &&
!le.IsLeader() {
klog.V(4).Infof("lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
return false
}
// 4. We're going to try to update. The leaderElectionRecord is set to it's default
// 3. We're going to try to update. The leaderElectionRecord is set to it's default
// here. Let's correct it before updating.
if le.IsLeader() {
leaderElectionRecord.AcquireTime = oldLeaderElectionRecord.AcquireTime
leaderElectionRecord.LeaderTransitions = oldLeaderElectionRecord.LeaderTransitions
le.metrics.slowpathExercised(le.config.Name)
} else {
leaderElectionRecord.LeaderTransitions = oldLeaderElectionRecord.LeaderTransitions + 1
}
@@ -414,10 +400,6 @@ func (le *LeaderElector) Check(maxTolerableExpiredLease time.Duration) error {
return nil
}
func (le *LeaderElector) isLeaseValid(now time.Time) bool {
return le.observedTime.Add(time.Second * time.Duration(le.getObservedRecord().LeaseDurationSeconds)).After(now)
}
// setObservedRecord will set a new observedRecord and update observedTime to the current time.
// Protect critical sections with lock.
func (le *LeaderElector) setObservedRecord(observedRecord *rl.LeaderElectionRecord) {

View File

@@ -315,7 +315,6 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) {
observedRawRecord: observedRawRecord,
observedTime: test.observedTime,
clock: clock,
metrics: globalMetricsFactory.newLeaderMetrics(),
}
if test.expectSuccess != le.tryAcquireOrRenew(context.Background()) {
if test.retryAfter != 0 {
@@ -492,7 +491,6 @@ func testReleaseLease(t *testing.T, objectType string) {
observedRawRecord: observedRawRecord,
observedTime: test.observedTime,
clock: clock.RealClock{},
metrics: globalMetricsFactory.newLeaderMetrics(),
}
if !le.tryAcquireOrRenew(context.Background()) {
t.Errorf("unexpected result of tryAcquireOrRenew: [succeeded=%v]", true)
@@ -612,18 +610,14 @@ func testReleaseOnCancellation(t *testing.T, objectType string) {
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
updates++
// Skip initial two fast path renews
if updates%2 == 1 && updates < 5 {
return true, nil, context.Canceled
}
// Second update (first renew) should return our canceled error
// FakeClient doesn't do anything with the context so we're doing this ourselves
if updates == 4 {
if updates == 2 {
close(onRenewCalled)
<-onRenewResume
return true, nil, context.Canceled
} else if updates == 5 {
} else if updates == 3 {
// We update the lock after the cancellation to release it
// This wg is to avoid the data race on lockObj
defer wg.Done()
@@ -674,12 +668,8 @@ func testReleaseOnCancellation(t *testing.T, objectType string) {
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
updates++
// Always skip fast path renew
if updates%2 == 1 {
return true, nil, context.Canceled
}
// Second update (first renew) should release the lock
if updates == 4 {
if updates == 2 {
// We update the lock after the cancellation to release it
// This wg is to avoid the data race on lockObj
defer wg.Done()
@@ -823,156 +813,3 @@ func assertEqualEvents(t *testing.T, expected []string, actual <-chan string) {
}
}
}
func TestFastPathLeaderElection(t *testing.T) {
objectType := "leases"
var (
lockObj runtime.Object
updates int
lockOps []string
cancelFunc func()
)
resetVars := func() {
lockObj = nil
updates = 0
lockOps = []string{}
cancelFunc = nil
}
lec := LeaderElectionConfig{
LeaseDuration: 15 * time.Second,
RenewDeadline: 2 * time.Second,
RetryPeriod: 1 * time.Second,
Callbacks: LeaderCallbacks{
OnNewLeader: func(identity string) {},
OnStoppedLeading: func() {},
OnStartedLeading: func(context.Context) {
},
},
}
tests := []struct {
name string
reactors []Reactor
expectedLockOps []string
}{
{
name: "Exercise fast path after lock acquired",
reactors: []Reactor{
{
verb: "get",
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
lockOps = append(lockOps, "get")
if lockObj != nil {
return true, lockObj, nil
}
return true, nil, errors.NewNotFound(action.(fakeclient.GetAction).GetResource().GroupResource(), action.(fakeclient.GetAction).GetName())
},
},
{
verb: "create",
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
lockOps = append(lockOps, "create")
lockObj = action.(fakeclient.CreateAction).GetObject()
return true, lockObj, nil
},
},
{
verb: "update",
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
updates++
lockOps = append(lockOps, "update")
if updates == 2 {
cancelFunc()
}
lockObj = action.(fakeclient.UpdateAction).GetObject()
return true, lockObj, nil
},
},
},
expectedLockOps: []string{"get", "create", "update", "update"},
},
{
name: "Fallback to slow path after fast path fails",
reactors: []Reactor{
{
verb: "get",
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
lockOps = append(lockOps, "get")
if lockObj != nil {
return true, lockObj, nil
}
return true, nil, errors.NewNotFound(action.(fakeclient.GetAction).GetResource().GroupResource(), action.(fakeclient.GetAction).GetName())
},
},
{
verb: "create",
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
lockOps = append(lockOps, "create")
lockObj = action.(fakeclient.CreateAction).GetObject()
return true, lockObj, nil
},
},
{
verb: "update",
objectType: objectType,
reaction: func(action fakeclient.Action) (handled bool, ret runtime.Object, err error) {
updates++
lockOps = append(lockOps, "update")
switch updates {
case 2:
return true, nil, errors.NewConflict(action.(fakeclient.UpdateAction).GetResource().GroupResource(), "fake conflict", nil)
case 4:
cancelFunc()
}
lockObj = action.(fakeclient.UpdateAction).GetObject()
return true, lockObj, nil
},
},
},
expectedLockOps: []string{"get", "create", "update", "update", "get", "update", "update"},
},
}
for i := range tests {
test := &tests[i]
t.Run(test.name, func(t *testing.T) {
resetVars()
recorder := record.NewFakeRecorder(100)
resourceLockConfig := rl.ResourceLockConfig{
Identity: "baz",
EventRecorder: recorder,
}
c := &fake.Clientset{}
for _, reactor := range test.reactors {
c.AddReactor(reactor.verb, objectType, reactor.reaction)
}
c.AddReactor("*", "*", func(action fakeclient.Action) (bool, runtime.Object, error) {
t.Errorf("unreachable action. testclient called too many times: %+v", action)
return true, nil, fmt.Errorf("unreachable action")
})
lock, err := rl.New("leases", "foo", "bar", c.CoreV1(), c.CoordinationV1(), resourceLockConfig)
if err != nil {
t.Fatal("resourcelock.New() = ", err)
}
lec.Lock = lock
elector, err := NewLeaderElector(lec)
if err != nil {
t.Fatal("Failed to create leader elector: ", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancelFunc = cancel
elector.Run(ctx)
assert.Equal(t, test.expectedLockOps, lockOps, "Expected lock ops %q, got %q", test.expectedLockOps, lockOps)
})
}
}

View File

@@ -26,26 +26,24 @@ import (
type leaderMetricsAdapter interface {
leaderOn(name string)
leaderOff(name string)
slowpathExercised(name string)
}
// LeaderMetric instruments metrics used in leader election.
type LeaderMetric interface {
// GaugeMetric represents a single numerical value that can arbitrarily go up
// and down.
type SwitchMetric interface {
On(name string)
Off(name string)
SlowpathExercised(name string)
}
type noopMetric struct{}
func (noopMetric) On(name string) {}
func (noopMetric) Off(name string) {}
func (noopMetric) SlowpathExercised(name string) {}
func (noopMetric) On(name string) {}
func (noopMetric) Off(name string) {}
// defaultLeaderMetrics expects the caller to lock before setting any metrics.
type defaultLeaderMetrics struct {
// leader's value indicates if the current process is the owner of name lease
leader LeaderMetric
leader SwitchMetric
}
func (m *defaultLeaderMetrics) leaderOn(name string) {
@@ -62,27 +60,19 @@ func (m *defaultLeaderMetrics) leaderOff(name string) {
m.leader.Off(name)
}
func (m *defaultLeaderMetrics) slowpathExercised(name string) {
if m == nil {
return
}
m.leader.SlowpathExercised(name)
}
type noMetrics struct{}
func (noMetrics) leaderOn(name string) {}
func (noMetrics) leaderOff(name string) {}
func (noMetrics) slowpathExercised(name string) {}
func (noMetrics) leaderOn(name string) {}
func (noMetrics) leaderOff(name string) {}
// MetricsProvider generates various metrics used by the leader election.
type MetricsProvider interface {
NewLeaderMetric() LeaderMetric
NewLeaderMetric() SwitchMetric
}
type noopMetricsProvider struct{}
func (noopMetricsProvider) NewLeaderMetric() LeaderMetric {
func (_ noopMetricsProvider) NewLeaderMetric() SwitchMetric {
return noopMetric{}
}

View File

@@ -23,6 +23,7 @@ import (
"k8s.io/utils/clock"
testingclock "k8s.io/utils/clock/testing"
"k8s.io/utils/integer"
)
type backoffEntry struct {
@@ -99,7 +100,7 @@ func (p *Backoff) Next(id string, eventTime time.Time) {
} else {
delay := entry.backoff * 2 // exponential
delay += p.jitter(entry.backoff) // add some jitter to the delay
entry.backoff = min(delay, p.maxDuration)
entry.backoff = time.Duration(integer.Int64Min(int64(delay), int64(p.maxDuration)))
}
entry.lastUpdate = p.Clock.Now()
}