add flag 'logging-format' to kube-proxy

This commit is contained in:
cyclinder
2023-07-06 22:30:45 +08:00
committed by cyclinder
parent c550c17f7f
commit 71ef0dafa7
18 changed files with 193 additions and 9 deletions

View File

@@ -43,6 +43,7 @@ import (
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/apiserver/pkg/server/mux"
@@ -89,7 +90,7 @@ import (
func init() {
utilruntime.Must(metricsfeatures.AddFeatureGates(utilfeature.DefaultMutableFeatureGate))
logsapi.AddFeatureGates(utilfeature.DefaultMutableFeatureGate)
utilruntime.Must(logsapi.AddFeatureGates(utilfeature.DefaultMutableFeatureGate))
}
// proxyRun defines the interface to run a specified ProxyServer
@@ -207,6 +208,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.Var(&o.config.DetectLocalMode, "detect-local-mode", "Mode to use to detect local traffic. This parameter is ignored if a config file is specified by --config.")
fs.StringVar(&o.config.DetectLocal.BridgeInterface, "pod-bridge-interface", o.config.DetectLocal.BridgeInterface, "A bridge interface name in the cluster. Kube-proxy considers traffic as local if originating from an interface which matches the value. This argument should be set if DetectLocalMode is set to BridgeInterface.")
fs.StringVar(&o.config.DetectLocal.InterfaceNamePrefix, "pod-interface-name-prefix", o.config.DetectLocal.InterfaceNamePrefix, "An interface prefix in the cluster. Kube-proxy considers traffic as local if originating from interfaces that match the given prefix. This argument should be set if DetectLocalMode is set to InterfaceNamePrefix.")
logsapi.AddFlags(&o.config.Logging, fs)
}
// newKubeProxyConfiguration returns a KubeProxyConfiguration with default values
@@ -468,7 +470,6 @@ addon that provides cluster DNS for these cluster IPs. The user must create a se
with the apiserver API to configure the proxy.`,
RunE: func(cmd *cobra.Command, args []string) error {
verflag.PrintAndExitIfRequested()
cliflag.PrintFlags(cmd.Flags())
if err := initForOS(opts.WindowsService); err != nil {
return fmt.Errorf("failed os init: %w", err)
@@ -478,6 +479,13 @@ with the apiserver API to configure the proxy.`,
return fmt.Errorf("failed complete: %w", err)
}
logs.InitLogs()
if err := logsapi.ValidateAndApplyAsField(&opts.config.Logging, utilfeature.DefaultFeatureGate, field.NewPath("logging")); err != nil {
return fmt.Errorf("initialize logging: %v", err)
}
cliflag.PrintFlags(cmd.Flags())
if err := opts.Validate(); err != nil {
return fmt.Errorf("failed validate: %w", err)
}

View File

@@ -19,7 +19,6 @@ package app
import (
"errors"
"fmt"
"reflect"
"testing"
"time"
@@ -30,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientsetfake "k8s.io/client-go/kubernetes/fake"
componentbaseconfig "k8s.io/component-base/config"
logsapi "k8s.io/component-base/logs/api/v1"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
"k8s.io/utils/pointer"
)
@@ -220,6 +220,10 @@ nodePortAddresses:
BridgeInterface: string("cbr0"),
InterfaceNamePrefix: string("veth"),
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
FlushFrequency: logsapi.TimeOrMetaDuration{Duration: 5 * time.Second},
},
}
options := NewOptions()
@@ -235,8 +239,8 @@ nodePortAddresses:
assert.NoError(t, err, "unexpected error for %s: %v", tc.name, err)
if !reflect.DeepEqual(expected, config) {
t.Fatalf("unexpected config for %s, diff = %s", tc.name, cmp.Diff(config, expected))
if diff := cmp.Diff(config, expected); diff != "" {
t.Fatalf("unexpected config for %s, diff = %s", tc.name, diff)
}
}
}

View File

@@ -20,6 +20,7 @@ import (
"os"
"k8s.io/component-base/cli"
_ "k8s.io/component-base/logs/json/register"
_ "k8s.io/component-base/metrics/prometheus/clientgo" // for client metric registration
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
"k8s.io/kubernetes/cmd/kube-proxy/app"