mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	kube-proxy: fix error wrapping, error handling and deprecated methods
Signed-off-by: Daman Arora <aroradaman@gmail.com>
This commit is contained in:
		@@ -270,7 +270,7 @@ func (o *Options) Complete(fs *pflag.FlagSet) error {
 | 
				
			|||||||
		// command line flags have priority). Otherwise `--config
 | 
							// command line flags have priority). Otherwise `--config
 | 
				
			||||||
		// ... -v=5` doesn't work (config resets verbosity even
 | 
							// ... -v=5` doesn't work (config resets verbosity even
 | 
				
			||||||
		// when it contains no logging settings).
 | 
							// when it contains no logging settings).
 | 
				
			||||||
		copyLogsFromFlags(fs, &c.Logging)
 | 
							_ = copyLogsFromFlags(fs, &c.Logging)
 | 
				
			||||||
		o.config = c
 | 
							o.config = c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := o.initWatcher(); err != nil {
 | 
							if err := o.initWatcher(); err != nil {
 | 
				
			||||||
@@ -312,7 +312,7 @@ func copyLogsFromFlags(from *pflag.FlagSet, to *logsapi.LoggingConfiguration) er
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if setErr := f.Value.Set(fsFlag.Value.String()); setErr != nil {
 | 
							if setErr := f.Value.Set(fsFlag.Value.String()); setErr != nil {
 | 
				
			||||||
			err = fmt.Errorf("copying flag %s value: %v", f.Name, setErr)
 | 
								err = fmt.Errorf("copying flag %s value: %w", f.Name, setErr)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
@@ -464,10 +464,10 @@ func addressFromDeprecatedFlags(addr string, port int32) string {
 | 
				
			|||||||
func newLenientSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, error) {
 | 
					func newLenientSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, error) {
 | 
				
			||||||
	lenientScheme := runtime.NewScheme()
 | 
						lenientScheme := runtime.NewScheme()
 | 
				
			||||||
	if err := kubeproxyconfig.AddToScheme(lenientScheme); err != nil {
 | 
						if err := kubeproxyconfig.AddToScheme(lenientScheme); err != nil {
 | 
				
			||||||
		return nil, nil, fmt.Errorf("failed to add kube-proxy config API to lenient scheme: %v", err)
 | 
							return nil, nil, fmt.Errorf("failed to add kube-proxy config API to lenient scheme: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := kubeproxyconfigv1alpha1.AddToScheme(lenientScheme); err != nil {
 | 
						if err := kubeproxyconfigv1alpha1.AddToScheme(lenientScheme); err != nil {
 | 
				
			||||||
		return nil, nil, fmt.Errorf("failed to add kube-proxy config v1alpha1 API to lenient scheme: %v", err)
 | 
							return nil, nil, fmt.Errorf("failed to add kube-proxy config v1alpha1 API to lenient scheme: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	lenientCodecs := serializer.NewCodecFactory(lenientScheme, serializer.DisableStrict)
 | 
						lenientCodecs := serializer.NewCodecFactory(lenientScheme, serializer.DisableStrict)
 | 
				
			||||||
	return lenientScheme, &lenientCodecs, nil
 | 
						return lenientScheme, &lenientCodecs, nil
 | 
				
			||||||
@@ -505,7 +505,7 @@ func (o *Options) loadConfig(data []byte) (*kubeproxyconfig.KubeProxyConfigurati
 | 
				
			|||||||
		if lenientErr != nil {
 | 
							if lenientErr != nil {
 | 
				
			||||||
			// Lenient decoding failed with the current version, return the
 | 
								// Lenient decoding failed with the current version, return the
 | 
				
			||||||
			// original strict error.
 | 
								// original strict error.
 | 
				
			||||||
			return nil, fmt.Errorf("failed lenient decoding: %v", err)
 | 
								return nil, fmt.Errorf("failed lenient decoding: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Continue with the v1alpha1 object that was decoded leniently, but emit a warning.
 | 
							// Continue with the v1alpha1 object that was decoded leniently, but emit a warning.
 | 
				
			||||||
@@ -545,7 +545,7 @@ with the apiserver API to configure the proxy.`,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			logs.InitLogs()
 | 
								logs.InitLogs()
 | 
				
			||||||
			if err := logsapi.ValidateAndApplyAsField(&opts.config.Logging, utilfeature.DefaultFeatureGate, field.NewPath("logging")); err != nil {
 | 
								if err := logsapi.ValidateAndApplyAsField(&opts.config.Logging, utilfeature.DefaultFeatureGate, field.NewPath("logging")); err != nil {
 | 
				
			||||||
				return fmt.Errorf("initialize logging: %v", err)
 | 
									return fmt.Errorf("initialize logging: %w", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			cliflag.PrintFlags(cmd.Flags())
 | 
								cliflag.PrintFlags(cmd.Flags())
 | 
				
			||||||
@@ -680,7 +680,7 @@ func newProxyServer(ctx context.Context, config *kubeproxyconfig.KubeProxyConfig
 | 
				
			|||||||
	err, fatal := checkBadIPConfig(s, dualStackSupported)
 | 
						err, fatal := checkBadIPConfig(s, dualStackSupported)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		if fatal {
 | 
							if fatal {
 | 
				
			||||||
			return nil, fmt.Errorf("kube-proxy configuration is incorrect: %v", err)
 | 
								return nil, fmt.Errorf("kube-proxy configuration is incorrect: %w", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		logger.Error(err, "Kube-proxy configuration may be incomplete or incorrect")
 | 
							logger.Error(err, "Kube-proxy configuration may be incomplete or incorrect")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -859,7 +859,7 @@ func serveHealthz(ctx context.Context, hz *healthcheck.ProxierHealthServer, errC
 | 
				
			|||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			logger.Error(err, "Healthz server failed")
 | 
								logger.Error(err, "Healthz server failed")
 | 
				
			||||||
			if errCh != nil {
 | 
								if errCh != nil {
 | 
				
			||||||
				errCh <- fmt.Errorf("healthz server failed: %v", err)
 | 
									errCh <- fmt.Errorf("healthz server failed: %w", err)
 | 
				
			||||||
				// if in hardfail mode, never retry again
 | 
									// if in hardfail mode, never retry again
 | 
				
			||||||
				blockCh := make(chan error)
 | 
									blockCh := make(chan error)
 | 
				
			||||||
				<-blockCh
 | 
									<-blockCh
 | 
				
			||||||
@@ -898,7 +898,7 @@ func serveMetrics(bindAddress string, proxyMode kubeproxyconfig.ProxyMode, enabl
 | 
				
			|||||||
	fn := func() {
 | 
						fn := func() {
 | 
				
			||||||
		err := http.ListenAndServe(bindAddress, proxyMux)
 | 
							err := http.ListenAndServe(bindAddress, proxyMux)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			err = fmt.Errorf("starting metrics server failed: %v", err)
 | 
								err = fmt.Errorf("starting metrics server failed: %w", err)
 | 
				
			||||||
			utilruntime.HandleError(err)
 | 
								utilruntime.HandleError(err)
 | 
				
			||||||
			if errCh != nil {
 | 
								if errCh != nil {
 | 
				
			||||||
				errCh <- err
 | 
									errCh <- err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,15 +20,14 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
					 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/google/go-cmp/cmp"
 | 
						"github.com/google/go-cmp/cmp"
 | 
				
			||||||
	"github.com/spf13/pflag"
 | 
						"github.com/spf13/pflag"
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
					 | 
				
			||||||
	"github.com/stretchr/testify/require"
 | 
						"github.com/stretchr/testify/require"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	v1 "k8s.io/api/core/v1"
 | 
						v1 "k8s.io/api/core/v1"
 | 
				
			||||||
@@ -257,7 +256,7 @@ nodePortAddresses:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		config, err := options.loadConfig([]byte(yaml))
 | 
							config, err := options.loadConfig([]byte(yaml))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		assert.NoError(t, err, "unexpected error for %s: %v", tc.name, err)
 | 
							require.NoError(t, err, "unexpected error for %s: %v", tc.name, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if diff := cmp.Diff(config, expected); diff != "" {
 | 
							if diff := cmp.Diff(config, expected); diff != "" {
 | 
				
			||||||
			t.Fatalf("unexpected config for %s, diff = %s", tc.name, diff)
 | 
								t.Fatalf("unexpected config for %s, diff = %s", tc.name, diff)
 | 
				
			||||||
@@ -322,13 +321,11 @@ func TestLoadConfigFailures(t *testing.T) {
 | 
				
			|||||||
			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) {
 | 
								require.Error(t, err, tc.name)
 | 
				
			||||||
				if tc.expErr != "" {
 | 
								require.Contains(t, err.Error(), tc.expErr)
 | 
				
			||||||
					assert.Contains(t, err.Error(), tc.expErr)
 | 
					
 | 
				
			||||||
				}
 | 
								if tc.checkFn != nil {
 | 
				
			||||||
				if tc.checkFn != nil {
 | 
									require.True(t, tc.checkFn(err), tc.name)
 | 
				
			||||||
					assert.True(t, tc.checkFn(err), tc.name)
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -375,7 +372,7 @@ func TestProcessHostnameOverrideFlag(t *testing.T) {
 | 
				
			|||||||
					t.Fatalf("should error for this case %s", tc.name)
 | 
										t.Fatalf("should error for this case %s", tc.name)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				assert.NoError(t, err, "unexpected error %v", err)
 | 
									require.NoError(t, err, "unexpected error %v", err)
 | 
				
			||||||
				if tc.expectedHostname != options.config.HostnameOverride {
 | 
									if tc.expectedHostname != options.config.HostnameOverride {
 | 
				
			||||||
					t.Fatalf("expected hostname: %s, but got: %s", tc.expectedHostname, options.config.HostnameOverride)
 | 
										t.Fatalf("expected hostname: %s, but got: %s", tc.expectedHostname, options.config.HostnameOverride)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
@@ -500,12 +497,12 @@ kind: KubeProxyConfiguration
 | 
				
			|||||||
			if len(tc.config) > 0 {
 | 
								if len(tc.config) > 0 {
 | 
				
			||||||
				tmp := t.TempDir()
 | 
									tmp := t.TempDir()
 | 
				
			||||||
				configFile := path.Join(tmp, "kube-proxy.conf")
 | 
									configFile := path.Join(tmp, "kube-proxy.conf")
 | 
				
			||||||
				require.NoError(t, ioutil.WriteFile(configFile, []byte(tc.config), 0666))
 | 
									require.NoError(t, os.WriteFile(configFile, []byte(tc.config), 0666))
 | 
				
			||||||
				flags = append(flags, "--config", configFile)
 | 
									flags = append(flags, "--config", configFile)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			require.NoError(t, fs.Parse(flags))
 | 
								require.NoError(t, fs.Parse(flags))
 | 
				
			||||||
			require.NoError(t, options.Complete(fs))
 | 
								require.NoError(t, options.Complete(fs))
 | 
				
			||||||
			assert.Equal(t, tc.expected, options.config)
 | 
								require.Equal(t, tc.expected, options.config)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user