mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
Merge pull request #84129 from obitech/lenient_path_strict_decoding_kube_scheduler
Add lenient decoding path for v1alpha1 kube-scheduler config
This commit is contained in:
commit
3e5f6bdaa4
@ -36,6 +36,7 @@ go_library(
|
||||
"//staging/src/k8s.io/client-go/tools/leaderelection/resourcelock:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/cli/flag:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/codec:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/config:go_default_library",
|
||||
"//staging/src/k8s.io/kube-scheduler/config/v1alpha1:go_default_library",
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
|
@ -21,7 +21,10 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"k8s.io/klog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/component-base/codec"
|
||||
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
|
||||
kubeschedulerscheme "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
|
||||
kubeschedulerconfigv1alpha1 "k8s.io/kubernetes/pkg/scheduler/apis/config/v1alpha1"
|
||||
@ -38,10 +41,30 @@ func loadConfigFromFile(file string) (*kubeschedulerconfig.KubeSchedulerConfigur
|
||||
|
||||
func loadConfig(data []byte) (*kubeschedulerconfig.KubeSchedulerConfiguration, error) {
|
||||
configObj := &kubeschedulerconfig.KubeSchedulerConfiguration{}
|
||||
if err := runtime.DecodeInto(kubeschedulerscheme.Codecs.UniversalDecoder(), data, configObj); err != nil {
|
||||
// The UniversalDecoder runs defaulting and returns the internal type by default.
|
||||
err := runtime.DecodeInto(kubeschedulerscheme.Codecs.UniversalDecoder(), data, configObj)
|
||||
if err != nil {
|
||||
// Try strict decoding first. If that fails decode with a lenient
|
||||
// decoder, which has only v1alpha1 registered, and log a warning.
|
||||
// The lenient path is to be dropped when support for v1alpha1 is dropped.
|
||||
if !runtime.IsStrictDecodingError(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var lenientErr error
|
||||
_, lenientCodecs, lenientErr := codec.NewLenientSchemeAndCodecs(
|
||||
kubeschedulerconfig.AddToScheme,
|
||||
kubeschedulerconfigv1alpha1.AddToScheme,
|
||||
)
|
||||
if lenientErr != nil {
|
||||
return nil, lenientErr
|
||||
}
|
||||
if lenientErr = runtime.DecodeInto(lenientCodecs.UniversalDecoder(), data, configObj); lenientErr != nil {
|
||||
return nil, fmt.Errorf("failed lenient decoding: %v", err)
|
||||
}
|
||||
klog.Warningf("using lenient decoding as strict decoding failed: %v", err)
|
||||
}
|
||||
|
||||
return configObj, nil
|
||||
}
|
||||
|
||||
|
@ -472,16 +472,76 @@ pluginConfig:
|
||||
options: &Options{
|
||||
ConfigFile: unknownFieldConfig,
|
||||
},
|
||||
expectedError: "found unknown field: foo",
|
||||
checkErrFn: runtime.IsStrictDecodingError,
|
||||
// TODO (obitech): Remove this comment and add a new test for v1alpha2, when it's available, as this should fail then.
|
||||
// expectedError: "found unknown field: foo",
|
||||
// checkErrFn: runtime.IsStrictDecodingError,
|
||||
expectedUsername: "config",
|
||||
expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
|
||||
SchedulerName: "default-scheduler",
|
||||
AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
|
||||
HardPodAffinitySymmetricWeight: 1,
|
||||
HealthzBindAddress: "0.0.0.0:10251",
|
||||
MetricsBindAddress: "0.0.0.0:10251",
|
||||
LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
|
||||
LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
|
||||
LeaderElect: true,
|
||||
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
|
||||
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
|
||||
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
|
||||
ResourceLock: "endpointsleases",
|
||||
ResourceNamespace: "kube-system",
|
||||
ResourceName: "kube-scheduler",
|
||||
},
|
||||
},
|
||||
ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
|
||||
Kubeconfig: configKubeconfig,
|
||||
QPS: 50,
|
||||
Burst: 100,
|
||||
ContentType: "application/vnd.kubernetes.protobuf",
|
||||
},
|
||||
BindTimeoutSeconds: &defaultBindTimeoutSeconds,
|
||||
PodInitialBackoffSeconds: &defaultPodInitialBackoffSeconds,
|
||||
PodMaxBackoffSeconds: &defaultPodMaxBackoffSeconds,
|
||||
Plugins: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "duplicate fields",
|
||||
options: &Options{
|
||||
ConfigFile: duplicateFieldConfig,
|
||||
},
|
||||
expectedError: `key "leaderElect" already set`,
|
||||
checkErrFn: runtime.IsStrictDecodingError,
|
||||
// TODO (obitech): Remove this comment and add a new test for v1alpha2, when it's available, as this should fail then.
|
||||
// expectedError: `key "leaderElect" already set`,
|
||||
// checkErrFn: runtime.IsStrictDecodingError,
|
||||
expectedUsername: "config",
|
||||
expectedConfig: kubeschedulerconfig.KubeSchedulerConfiguration{
|
||||
SchedulerName: "default-scheduler",
|
||||
AlgorithmSource: kubeschedulerconfig.SchedulerAlgorithmSource{Provider: &defaultSource},
|
||||
HardPodAffinitySymmetricWeight: 1,
|
||||
HealthzBindAddress: "0.0.0.0:10251",
|
||||
MetricsBindAddress: "0.0.0.0:10251",
|
||||
LeaderElection: kubeschedulerconfig.KubeSchedulerLeaderElectionConfiguration{
|
||||
LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
|
||||
LeaderElect: false,
|
||||
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
|
||||
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
|
||||
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
|
||||
ResourceLock: "endpointsleases",
|
||||
ResourceNamespace: "kube-system",
|
||||
ResourceName: "kube-scheduler",
|
||||
},
|
||||
},
|
||||
ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
|
||||
Kubeconfig: configKubeconfig,
|
||||
QPS: 50,
|
||||
Burst: 100,
|
||||
ContentType: "application/vnd.kubernetes.protobuf",
|
||||
},
|
||||
BindTimeoutSeconds: &defaultBindTimeoutSeconds,
|
||||
PodInitialBackoffSeconds: &defaultPodInitialBackoffSeconds,
|
||||
PodMaxBackoffSeconds: &defaultPodMaxBackoffSeconds,
|
||||
Plugins: nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -523,7 +583,7 @@ pluginConfig:
|
||||
return
|
||||
}
|
||||
if username != tc.expectedUsername {
|
||||
t.Errorf("expected server call with user %s, got %s", tc.expectedUsername, username)
|
||||
t.Errorf("expected server call with user %q, got %q", tc.expectedUsername, username)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ filegroup(
|
||||
":package-srcs",
|
||||
"//staging/src/k8s.io/component-base/cli/flag:all-srcs",
|
||||
"//staging/src/k8s.io/component-base/cli/globalflag:all-srcs",
|
||||
"//staging/src/k8s.io/component-base/codec:all-srcs",
|
||||
"//staging/src/k8s.io/component-base/config:all-srcs",
|
||||
"//staging/src/k8s.io/component-base/featuregate:all-srcs",
|
||||
"//staging/src/k8s.io/component-base/logs:all-srcs",
|
||||
|
27
staging/src/k8s.io/component-base/codec/BUILD
Normal file
27
staging/src/k8s.io/component-base/codec/BUILD
Normal file
@ -0,0 +1,27 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["codec.go"],
|
||||
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/codec",
|
||||
importpath = "k8s.io/component-base/codec",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
39
staging/src/k8s.io/component-base/codec/codec.go
Normal file
39
staging/src/k8s.io/component-base/codec/codec.go
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
Copyright 2019 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 codec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
)
|
||||
|
||||
// NewLenientSchemeAndCodecs constructs a CodecFactory with strict decoding
|
||||
// disabled, that has only the Schemes registered into it which are passed
|
||||
// and added via AddToScheme functions. This can be used to skip strict decoding
|
||||
// a specific version only.
|
||||
func NewLenientSchemeAndCodecs(addToSchemeFns ...func(s *runtime.Scheme) error) (*runtime.Scheme, *serializer.CodecFactory, error) {
|
||||
lenientScheme := runtime.NewScheme()
|
||||
for _, s := range addToSchemeFns {
|
||||
if err := s(lenientScheme); err != nil {
|
||||
return nil, nil, fmt.Errorf("unable to add API to lenient scheme: %v", err)
|
||||
}
|
||||
}
|
||||
lenientCodecs := serializer.NewCodecFactory(lenientScheme, serializer.DisableStrict)
|
||||
return lenientScheme, &lenientCodecs, nil
|
||||
}
|
1
vendor/modules.txt
vendored
1
vendor/modules.txt
vendored
@ -1650,6 +1650,7 @@ k8s.io/code-generator/third_party/forked/golang/reflect
|
||||
# k8s.io/component-base v0.0.0 => ./staging/src/k8s.io/component-base
|
||||
k8s.io/component-base/cli/flag
|
||||
k8s.io/component-base/cli/globalflag
|
||||
k8s.io/component-base/codec
|
||||
k8s.io/component-base/config
|
||||
k8s.io/component-base/config/v1alpha1
|
||||
k8s.io/component-base/config/validation
|
||||
|
Loading…
Reference in New Issue
Block a user