mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
NewOptions doesn't need to return error in signature.
This commit is contained in:
parent
1d589600bc
commit
91ab8fe1e7
@ -163,15 +163,13 @@ func AddFlags(options *Options, fs *pflag.FlagSet) {
|
|||||||
utilfeature.DefaultFeatureGate.AddFlag(fs)
|
utilfeature.DefaultFeatureGate.AddFlag(fs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOptions() (*Options, error) {
|
func NewOptions() *Options {
|
||||||
o := &Options{
|
return &Options{
|
||||||
config: new(proxyconfig.KubeProxyConfiguration),
|
config: new(proxyconfig.KubeProxyConfiguration),
|
||||||
healthzPort: ports.ProxyHealthzPort,
|
healthzPort: ports.ProxyHealthzPort,
|
||||||
scheme: scheme.Scheme,
|
scheme: scheme.Scheme,
|
||||||
codecs: scheme.Codecs,
|
codecs: scheme.Codecs,
|
||||||
}
|
}
|
||||||
|
|
||||||
return o, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete completes all the required options.
|
// Complete completes all the required options.
|
||||||
@ -316,10 +314,7 @@ func (o *Options) ApplyDefaults(in *proxyconfig.KubeProxyConfiguration) (*proxyc
|
|||||||
|
|
||||||
// NewProxyCommand creates a *cobra.Command object with default parameters
|
// NewProxyCommand creates a *cobra.Command object with default parameters
|
||||||
func NewProxyCommand() *cobra.Command {
|
func NewProxyCommand() *cobra.Command {
|
||||||
opts, err := NewOptions()
|
opts := NewOptions()
|
||||||
if err != nil {
|
|
||||||
glog.Fatalf("Unable to initialize command options: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "kube-proxy",
|
Use: "kube-proxy",
|
||||||
@ -338,6 +333,7 @@ with the apiserver API to configure the proxy.`,
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
opts.config, err = opts.ApplyDefaults(opts.config)
|
opts.config, err = opts.ApplyDefaults(opts.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Fatalf("unable to create flag defaults: %v", err)
|
glog.Fatalf("unable to create flag defaults: %v", err)
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -28,11 +27,9 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
|
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
"k8s.io/kubernetes/pkg/proxy/apis/proxyconfig"
|
"k8s.io/kubernetes/pkg/proxy/apis/proxyconfig"
|
||||||
"k8s.io/kubernetes/pkg/proxy/apis/proxyconfig/v1alpha1"
|
|
||||||
"k8s.io/kubernetes/pkg/util/configz"
|
"k8s.io/kubernetes/pkg/util/configz"
|
||||||
"k8s.io/kubernetes/pkg/util/iptables"
|
"k8s.io/kubernetes/pkg/util/iptables"
|
||||||
utilpointer "k8s.io/kubernetes/pkg/util/pointer"
|
utilpointer "k8s.io/kubernetes/pkg/util/pointer"
|
||||||
@ -138,39 +135,6 @@ func Test_getProxyMode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestNewOptionsFailures tests failure modes for NewOptions()
|
|
||||||
func TestNewOptionsFailures(t *testing.T) {
|
|
||||||
// Create a fake scheme builder that generates an error
|
|
||||||
errString := fmt.Sprintf("Simulated error")
|
|
||||||
genError := func(scheme *k8sRuntime.Scheme) error {
|
|
||||||
return errors.New(errString)
|
|
||||||
}
|
|
||||||
fakeSchemeBuilder := k8sRuntime.NewSchemeBuilder(genError)
|
|
||||||
|
|
||||||
simulatedErrorTest := func(target string) {
|
|
||||||
var addToScheme *func(s *k8sRuntime.Scheme) error
|
|
||||||
if target == proxyconfig.GroupName {
|
|
||||||
addToScheme = &proxyconfig.AddToScheme
|
|
||||||
} else {
|
|
||||||
addToScheme = &v1alpha1.AddToScheme
|
|
||||||
}
|
|
||||||
restoreValue := *addToScheme
|
|
||||||
restore := func() {
|
|
||||||
*addToScheme = restoreValue
|
|
||||||
}
|
|
||||||
defer restore()
|
|
||||||
*addToScheme = fakeSchemeBuilder.AddToScheme
|
|
||||||
_, err := NewOptions()
|
|
||||||
assert.Error(t, err, fmt.Sprintf("Simulated error in component %s", target))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simulate errors in calls to AddToScheme()
|
|
||||||
faultTargets := []string{proxyconfig.GroupName, "v1alpha1"}
|
|
||||||
for _, target := range faultTargets {
|
|
||||||
simulatedErrorTest(target)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This test verifies that NewProxyServer does not crash when CleanupAndExit is true.
|
// This test verifies that NewProxyServer does not crash when CleanupAndExit is true.
|
||||||
func TestProxyServerWithCleanupAndExit(t *testing.T) {
|
func TestProxyServerWithCleanupAndExit(t *testing.T) {
|
||||||
// Each bind address below is a separate test case
|
// Each bind address below is a separate test case
|
||||||
@ -179,10 +143,7 @@ func TestProxyServerWithCleanupAndExit(t *testing.T) {
|
|||||||
"::",
|
"::",
|
||||||
}
|
}
|
||||||
for _, addr := range bindAddresses {
|
for _, addr := range bindAddresses {
|
||||||
options, err := NewOptions()
|
options := NewOptions()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected error with address %s: %v", addr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
options.config = &proxyconfig.KubeProxyConfiguration{
|
options.config = &proxyconfig.KubeProxyConfiguration{
|
||||||
BindAddress: addr,
|
BindAddress: addr,
|
||||||
@ -410,8 +371,7 @@ udpTimeoutMilliseconds: 123ms
|
|||||||
UDPIdleTimeout: metav1.Duration{Duration: 123 * time.Millisecond},
|
UDPIdleTimeout: metav1.Duration{Duration: 123 * time.Millisecond},
|
||||||
}
|
}
|
||||||
|
|
||||||
options, err := NewOptions()
|
options := NewOptions()
|
||||||
assert.NoError(t, err, "unexpected error for %s: %v", tc.name, err)
|
|
||||||
|
|
||||||
yaml := fmt.Sprintf(
|
yaml := fmt.Sprintf(
|
||||||
yamlTemplate, tc.bindAddress, tc.clusterCIDR,
|
yamlTemplate, tc.bindAddress, tc.clusterCIDR,
|
||||||
@ -449,7 +409,7 @@ func TestLoadConfigFailures(t *testing.T) {
|
|||||||
}
|
}
|
||||||
version := "apiVersion: kubeproxy.k8s.io/v1alpha1"
|
version := "apiVersion: kubeproxy.k8s.io/v1alpha1"
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
options, _ := NewOptions()
|
options := NewOptions()
|
||||||
config := fmt.Sprintf("%s\n%s", version, tc.config)
|
config := fmt.Sprintf("%s\n%s", version, tc.config)
|
||||||
_, err := options.loadConfig([]byte(config))
|
_, err := options.loadConfig([]byte(config))
|
||||||
if assert.Error(t, err, tc.name) {
|
if assert.Error(t, err, tc.name) {
|
||||||
|
Loading…
Reference in New Issue
Block a user