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

@@ -47,6 +47,9 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
obj.OOMScoreAdj = pointer.Int32(c.Int31())
obj.ClientConnection.ContentType = "bar"
obj.NodePortAddresses = []string{"1.2.3.0/24"}
if obj.Logging.Format == "" {
obj.Logging.Format = "text"
}
},
}
}

View File

@@ -22,6 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
componentconfigtesting "k8s.io/component-base/config/testing"
logsapi "k8s.io/component-base/logs/api/v1"
)
func TestComponentConfigSetup(t *testing.T) {
@@ -31,8 +32,9 @@ func TestComponentConfigSetup(t *testing.T) {
SchemeGroupVersion: SchemeGroupVersion,
AddToScheme: AddToScheme,
AllowedTags: map[reflect.Type]bool{
reflect.TypeOf(metav1.TypeMeta{}): true,
reflect.TypeOf(metav1.Duration{}): true,
reflect.TypeOf(metav1.TypeMeta{}): true,
reflect.TypeOf(metav1.Duration{}): true,
reflect.TypeOf(logsapi.LoggingConfiguration{}): true,
},
}

View File

@@ -37,6 +37,13 @@ ipvs:
tcpTimeout: 0s
udpTimeout: 0s
kind: KubeProxyConfiguration
logging:
flushFrequency: 5s
format: text
options:
json:
infoBufferSize: "0"
verbosity: 0
metricsBindAddress: 127.0.0.1:10249
mode: ""
nodePortAddresses: null

View File

@@ -37,6 +37,13 @@ ipvs:
tcpTimeout: 0s
udpTimeout: 0s
kind: KubeProxyConfiguration
logging:
flushFrequency: 5s
format: text
options:
json:
infoBufferSize: "0"
verbosity: 0
metricsBindAddress: 127.0.0.1:10249
mode: ""
nodePortAddresses: null

View File

@@ -23,6 +23,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
componentbaseconfig "k8s.io/component-base/config"
logsapi "k8s.io/component-base/logs/api/v1"
)
// KubeProxyIPTablesConfiguration contains iptables-related configuration
@@ -188,6 +189,9 @@ type KubeProxyConfiguration struct {
DetectLocalMode LocalMode
// DetectLocal contains optional configuration settings related to DetectLocalMode.
DetectLocal DetectLocalConfiguration
// Logging specifies the options of logging.
// Refer to [Logs Options](https://github.com/kubernetes/component-base/blob/master/logs/options.go) for more information.
Logging logsapi.LoggingConfiguration
}
// ProxyMode represents modes used by the Kubernetes proxy server.

View File

@@ -24,6 +24,7 @@ import (
kruntime "k8s.io/apimachinery/pkg/runtime"
kubeproxyconfigv1alpha1 "k8s.io/kube-proxy/config/v1alpha1"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/kubernetes/pkg/cluster/ports"
"k8s.io/kubernetes/pkg/kubelet/qos"
proxyutil "k8s.io/kubernetes/pkg/proxy/util"
@@ -124,6 +125,8 @@ func SetDefaults_KubeProxyConfiguration(obj *kubeproxyconfigv1alpha1.KubeProxyCo
if obj.FeatureGates == nil {
obj.FeatureGates = make(map[string]bool)
}
// Use the Default LoggingConfiguration option
logsapi.SetRecommendedLoggingConfiguration(&obj.Logging)
}
// getDefaultAddresses returns default address of healthz and metrics server

View File

@@ -24,6 +24,7 @@ import (
"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
componentbaseconfig "k8s.io/component-base/config/v1alpha1"
logsapi "k8s.io/component-base/logs/api/v1"
kubeproxyconfigv1alpha1 "k8s.io/kube-proxy/config/v1alpha1"
)
@@ -67,6 +68,10 @@ func TestDefaultsKubeProxyConfiguration(t *testing.T) {
TCPCloseWaitTimeout: &metav1.Duration{Duration: 1 * time.Hour},
},
ConfigSyncPeriod: metav1.Duration{Duration: 15 * time.Minute},
Logging: logsapi.LoggingConfiguration{
Format: "text",
FlushFrequency: logsapi.TimeOrMetaDuration{Duration: 5 * time.Second},
},
},
},
{
@@ -103,6 +108,10 @@ func TestDefaultsKubeProxyConfiguration(t *testing.T) {
TCPCloseWaitTimeout: &metav1.Duration{Duration: 1 * time.Hour},
},
ConfigSyncPeriod: metav1.Duration{Duration: 15 * time.Minute},
Logging: logsapi.LoggingConfiguration{
Format: "text",
FlushFrequency: logsapi.TimeOrMetaDuration{Duration: 5 * time.Second},
},
},
},
}

View File

@@ -158,6 +158,7 @@ func autoConvert_v1alpha1_KubeProxyConfiguration_To_config_KubeProxyConfiguratio
if err := Convert_v1alpha1_DetectLocalConfiguration_To_config_DetectLocalConfiguration(&in.DetectLocal, &out.DetectLocal, s); err != nil {
return err
}
out.Logging = in.Logging
return nil
}
@@ -200,6 +201,7 @@ func autoConvert_config_KubeProxyConfiguration_To_v1alpha1_KubeProxyConfiguratio
if err := Convert_config_DetectLocalConfiguration_To_v1alpha1_DetectLocalConfiguration(&in.DetectLocal, &out.DetectLocal, s); err != nil {
return err
}
out.Logging = in.Logging
return nil
}

View File

@@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
utilfeature "k8s.io/apiserver/pkg/util/feature"
componentbaseconfig "k8s.io/component-base/config"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/component-base/metrics"
apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
@@ -103,6 +104,7 @@ func Validate(config *kubeproxyconfig.KubeProxyConfiguration) field.ErrorList {
if config.DetectLocalMode == kubeproxyconfig.LocalModeInterfaceNamePrefix {
allErrs = append(allErrs, validateInterface(config.DetectLocal.InterfaceNamePrefix, newPath.Child("InterfacePrefix"))...)
}
allErrs = append(allErrs, logsapi.Validate(&config.Logging, effectiveFeatures, newPath.Child("logging"))...)
return allErrs
}

View File

@@ -24,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
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"
@@ -58,6 +59,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "192.168.59.103",
HealthzBindAddress: "0.0.0.0:10256",
@@ -75,6 +79,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "192.168.59.103",
HealthzBindAddress: "",
@@ -92,6 +99,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "fd00:192:168:59::103",
HealthzBindAddress: "",
@@ -109,6 +119,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "10.10.12.11",
HealthzBindAddress: "0.0.0.0:12345",
@@ -126,6 +139,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "10.10.12.11",
HealthzBindAddress: "0.0.0.0:12345",
@@ -143,6 +159,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "10.10.12.11",
HealthzBindAddress: "0.0.0.0:12345",
@@ -160,6 +179,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "10.10.12.11",
HealthzBindAddress: "0.0.0.0:12345",
@@ -181,6 +203,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
DetectLocal: kubeproxyconfig.DetectLocalConfiguration{
InterfaceNamePrefix: "vethabcde",
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}, {
BindAddress: "10.10.12.11",
HealthzBindAddress: "0.0.0.0:12345",
@@ -202,6 +227,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
DetectLocal: kubeproxyconfig.DetectLocalConfiguration{
BridgeInterface: "avz",
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
}}
for _, successCase := range successCases {
@@ -233,6 +261,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("BindAddress"), "10.10.12.11:2000", "not a valid textual representation of an IP address")},
},
@@ -254,6 +285,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("HealthzBindAddress"), "0.0.0.0", "must be IP:port")},
},
@@ -275,6 +309,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("MetricsBindAddress"), "127.0.0.1", "must be IP:port")},
},
@@ -296,6 +333,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ClusterCIDR"), "192.168.59.0", "must be a valid CIDR block (e.g. 10.100.0.0/16 or fde4:8dba:82e1::/48)")},
},
@@ -317,6 +357,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ClusterCIDR"), "192.168.59.0/24,fd00:192:168::/64,10.0.0.0/16", "only one CIDR allowed or a valid DualStack CIDR (e.g. 10.100.0.0/16,fde4:8dba:82e1::/48)")},
},
@@ -338,6 +381,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ConfigSyncPeriod"), metav1.Duration{Duration: -1 * time.Second}, "must be greater than 0")},
},
@@ -361,6 +407,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("KubeProxyIPVSConfiguration.SyncPeriod"), metav1.Duration{Duration: 0}, "must be greater than 0")},
},
@@ -386,6 +435,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
DetectLocal: kubeproxyconfig.DetectLocalConfiguration{
InterfaceNamePrefix: "",
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("InterfacePrefix"), "", "must not be empty")},
},
@@ -411,6 +463,9 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
DetectLocal: kubeproxyconfig.DetectLocalConfiguration{
InterfaceNamePrefix: "eth0", // we won't care about prefix since mode is not prefix
},
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("InterfaceName"), "", "must not be empty")},
},
@@ -433,9 +488,36 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
DetectLocalMode: "Guess",
Logging: logsapi.LoggingConfiguration{
Format: "text",
},
},
expectedErrs: field.ErrorList{field.NotSupported(newPath.Child("DetectLocalMode"), "Guess", []string{"ClusterCIDR", "NodeCIDR", "BridgeInterface", "InterfaceNamePrefix", ""})},
},
"invalid logging format": {
config: kubeproxyconfig.KubeProxyConfiguration{
BindAddress: "10.10.12.11",
HealthzBindAddress: "0.0.0.0:12345",
MetricsBindAddress: "127.0.0.1:10249",
ClusterCIDR: "192.168.59.0/24",
ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
MasqueradeAll: true,
SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
},
Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
MaxPerCore: pointer.Int32(1),
Min: pointer.Int32(1),
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
},
Logging: logsapi.LoggingConfiguration{
Format: "unsupported format",
},
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("logging.format"), "unsupported format", "Unsupported log format")},
},
}
for name, testCase := range testCases {

View File

@@ -92,6 +92,7 @@ func (in *KubeProxyConfiguration) DeepCopyInto(out *KubeProxyConfiguration) {
}
out.Winkernel = in.Winkernel
out.DetectLocal = in.DetectLocal
in.Logging.DeepCopyInto(&out.Logging)
return
}