mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +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/apimachinery/pkg/util/rand:go_default_library",
|
||||||
"//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library",
|
||||||
"//staging/src/k8s.io/component-base/config: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"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
@ -112,7 +113,7 @@ leaderElection:
|
|||||||
t.Fatal(err)
|
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(`
|
if err := ioutil.WriteFile(invalidconfigFile, []byte(fmt.Sprintf(`
|
||||||
apiVersion: componentconfig/v1alpha2
|
apiVersion: componentconfig/v1alpha2
|
||||||
kind: KubeSchedulerConfiguration
|
kind: KubeSchedulerConfiguration
|
||||||
@ -123,6 +124,30 @@ leaderElection:
|
|||||||
t.Fatal(err)
|
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
|
// flag-specified kubeconfig
|
||||||
flagKubeconfig := filepath.Join(tmpDir, "flag.kubeconfig")
|
flagKubeconfig := filepath.Join(tmpDir, "flag.kubeconfig")
|
||||||
if err := ioutil.WriteFile(flagKubeconfig, []byte(fmt.Sprintf(`
|
if err := ioutil.WriteFile(flagKubeconfig, []byte(fmt.Sprintf(`
|
||||||
@ -189,6 +214,7 @@ pluginConfig:
|
|||||||
expectedUsername string
|
expectedUsername string
|
||||||
expectedError string
|
expectedError string
|
||||||
expectedConfig kubeschedulerconfig.KubeSchedulerConfiguration
|
expectedConfig kubeschedulerconfig.KubeSchedulerConfiguration
|
||||||
|
checkErrFn func(err error) bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "config file",
|
name: "config file",
|
||||||
@ -433,6 +459,22 @@ pluginConfig:
|
|||||||
options: &Options{},
|
options: &Options{},
|
||||||
expectedError: "no configuration has been provided",
|
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 {
|
for _, tc := range testcases {
|
||||||
@ -442,11 +484,16 @@ pluginConfig:
|
|||||||
|
|
||||||
// handle errors
|
// handle errors
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if tc.expectedError == "" {
|
if tc.expectedError != "" || tc.checkErrFn != nil {
|
||||||
t.Error(err)
|
if tc.expectedError != "" {
|
||||||
} else if !strings.Contains(err.Error(), tc.expectedError) {
|
assert.Contains(t, err.Error(), tc.expectedError, tc.name)
|
||||||
t.Errorf("expected %q, got %q", tc.expectedError, err.Error())
|
}
|
||||||
|
if tc.checkErrFn != nil {
|
||||||
|
assert.True(t, tc.checkErrFn(err), "got error: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
assert.NoError(t, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ var (
|
|||||||
Scheme = runtime.NewScheme()
|
Scheme = runtime.NewScheme()
|
||||||
|
|
||||||
// Codecs provides access to encoding and decoding for the scheme.
|
// Codecs provides access to encoding and decoding for the scheme.
|
||||||
Codecs = serializer.NewCodecFactory(Scheme)
|
Codecs = serializer.NewCodecFactory(Scheme, serializer.EnableStrict)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
Loading…
Reference in New Issue
Block a user