mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #83030 from obitech/kube_scheduler_strict_serializer
Enable strict serializer in kube-scheduler
This commit is contained in:
commit
6dde8daa76
@ -75,5 +75,6 @@ go_test(
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
|
||||
"//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library",
|
||||
"//staging/src/k8s.io/component-base/config:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@ -24,10 +24,11 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
@ -112,7 +113,7 @@ leaderElection:
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
invalidconfigFile := filepath.Join(tmpDir, "scheduler_invalid.yaml")
|
||||
invalidconfigFile := filepath.Join(tmpDir, "scheduler_invalid_wrong_api_version.yaml")
|
||||
if err := ioutil.WriteFile(invalidconfigFile, []byte(fmt.Sprintf(`
|
||||
apiVersion: componentconfig/v1alpha2
|
||||
kind: KubeSchedulerConfiguration
|
||||
@ -123,6 +124,30 @@ leaderElection:
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
unknownFieldConfig := filepath.Join(tmpDir, "scheduler_invalid_unknown_field.yaml")
|
||||
if err := ioutil.WriteFile(unknownFieldConfig, []byte(fmt.Sprintf(`
|
||||
apiVersion: kubescheduler.config.k8s.io/v1alpha1
|
||||
kind: KubeSchedulerConfiguration
|
||||
clientConnection:
|
||||
kubeconfig: "%s"
|
||||
leaderElection:
|
||||
leaderElect: true
|
||||
foo: bar`, configKubeconfig)), os.FileMode(0600)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
duplicateFieldConfig := filepath.Join(tmpDir, "scheduler_invalid_duplicate_fields.yaml")
|
||||
if err := ioutil.WriteFile(duplicateFieldConfig, []byte(fmt.Sprintf(`
|
||||
apiVersion: kubescheduler.config.k8s.io/v1alpha1
|
||||
kind: KubeSchedulerConfiguration
|
||||
clientConnection:
|
||||
kubeconfig: "%s"
|
||||
leaderElection:
|
||||
leaderElect: true
|
||||
leaderElect: false`, configKubeconfig)), os.FileMode(0600)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// flag-specified kubeconfig
|
||||
flagKubeconfig := filepath.Join(tmpDir, "flag.kubeconfig")
|
||||
if err := ioutil.WriteFile(flagKubeconfig, []byte(fmt.Sprintf(`
|
||||
@ -189,6 +214,7 @@ pluginConfig:
|
||||
expectedUsername string
|
||||
expectedError string
|
||||
expectedConfig kubeschedulerconfig.KubeSchedulerConfiguration
|
||||
checkErrFn func(err error) bool
|
||||
}{
|
||||
{
|
||||
name: "config file",
|
||||
@ -433,6 +459,22 @@ pluginConfig:
|
||||
options: &Options{},
|
||||
expectedError: "no configuration has been provided",
|
||||
},
|
||||
{
|
||||
name: "unknown field",
|
||||
options: &Options{
|
||||
ConfigFile: unknownFieldConfig,
|
||||
},
|
||||
expectedError: "found unknown field: foo",
|
||||
checkErrFn: runtime.IsStrictDecodingError,
|
||||
},
|
||||
{
|
||||
name: "duplicate fields",
|
||||
options: &Options{
|
||||
ConfigFile: duplicateFieldConfig,
|
||||
},
|
||||
expectedError: `key "leaderElect" already set`,
|
||||
checkErrFn: runtime.IsStrictDecodingError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
@ -442,11 +484,16 @@ pluginConfig:
|
||||
|
||||
// handle errors
|
||||
if err != nil {
|
||||
if tc.expectedError == "" {
|
||||
t.Error(err)
|
||||
} else if !strings.Contains(err.Error(), tc.expectedError) {
|
||||
t.Errorf("expected %q, got %q", tc.expectedError, err.Error())
|
||||
if tc.expectedError != "" || tc.checkErrFn != nil {
|
||||
if tc.expectedError != "" {
|
||||
assert.Contains(t, err.Error(), tc.expectedError, tc.name)
|
||||
}
|
||||
if tc.checkErrFn != nil {
|
||||
assert.True(t, tc.checkErrFn(err), "got error: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ var (
|
||||
Scheme = runtime.NewScheme()
|
||||
|
||||
// Codecs provides access to encoding and decoding for the scheme.
|
||||
Codecs = serializer.NewCodecFactory(Scheme)
|
||||
Codecs = serializer.NewCodecFactory(Scheme, serializer.EnableStrict)
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
Loading…
Reference in New Issue
Block a user