cmd/kube-proxy: refactor options test

Signed-off-by: Daman Arora <aroradaman@gmail.com>
This commit is contained in:
Daman Arora 2024-07-10 23:48:50 +05:30
parent 458b56811a
commit 0d14f18f8f

View File

@ -179,81 +179,83 @@ nodePortAddresses:
} }
for _, tc := range testCases { for _, tc := range testCases {
expBindAddr := tc.bindAddress t.Run(tc.name, func(t *testing.T) {
if tc.bindAddress[0] == '"' { expBindAddr := tc.bindAddress
// Surrounding double quotes will get stripped by the yaml parser. if tc.bindAddress[0] == '"' {
expBindAddr = expBindAddr[1 : len(tc.bindAddress)-1] // Surrounding double quotes will get stripped by the yaml parser.
} expBindAddr = expBindAddr[1 : len(tc.bindAddress)-1]
expected := &kubeproxyconfig.KubeProxyConfiguration{ }
BindAddress: expBindAddr, expected := &kubeproxyconfig.KubeProxyConfiguration{
ClientConnection: componentbaseconfig.ClientConnectionConfiguration{ BindAddress: expBindAddr,
AcceptContentTypes: "abc", ClientConnection: componentbaseconfig.ClientConnectionConfiguration{
Burst: 100, AcceptContentTypes: "abc",
ContentType: "content-type", Burst: 100,
Kubeconfig: "/path/to/kubeconfig", ContentType: "content-type",
QPS: 7, Kubeconfig: "/path/to/kubeconfig",
}, QPS: 7,
ClusterCIDR: tc.clusterCIDR, },
ConfigSyncPeriod: metav1.Duration{Duration: 15 * time.Second}, ClusterCIDR: tc.clusterCIDR,
Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{ ConfigSyncPeriod: metav1.Duration{Duration: 15 * time.Second},
MaxPerCore: ptr.To[int32](2), Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
Min: ptr.To[int32](1), MaxPerCore: ptr.To[int32](2),
TCPCloseWaitTimeout: &metav1.Duration{Duration: 10 * time.Second}, Min: ptr.To[int32](1),
TCPEstablishedTimeout: &metav1.Duration{Duration: 20 * time.Second}, TCPCloseWaitTimeout: &metav1.Duration{Duration: 10 * time.Second},
}, TCPEstablishedTimeout: &metav1.Duration{Duration: 20 * time.Second},
FeatureGates: map[string]bool{}, },
HealthzBindAddress: tc.healthzBindAddress, FeatureGates: map[string]bool{},
HostnameOverride: "foo", HealthzBindAddress: tc.healthzBindAddress,
IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{ HostnameOverride: "foo",
MasqueradeAll: true, IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
MasqueradeBit: ptr.To[int32](17), MasqueradeAll: true,
LocalhostNodePorts: ptr.To(true), MasqueradeBit: ptr.To[int32](17),
MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second}, LocalhostNodePorts: ptr.To(true),
SyncPeriod: metav1.Duration{Duration: 60 * time.Second}, MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second},
}, SyncPeriod: metav1.Duration{Duration: 60 * time.Second},
IPVS: kubeproxyconfig.KubeProxyIPVSConfiguration{ },
MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second}, IPVS: kubeproxyconfig.KubeProxyIPVSConfiguration{
SyncPeriod: metav1.Duration{Duration: 60 * time.Second}, MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second},
ExcludeCIDRs: []string{"10.20.30.40/16", "fd00:1::0/64"}, SyncPeriod: metav1.Duration{Duration: 60 * time.Second},
}, ExcludeCIDRs: []string{"10.20.30.40/16", "fd00:1::0/64"},
NFTables: kubeproxyconfig.KubeProxyNFTablesConfiguration{ },
MasqueradeAll: true, NFTables: kubeproxyconfig.KubeProxyNFTablesConfiguration{
MasqueradeBit: ptr.To[int32](18), MasqueradeAll: true,
MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second}, MasqueradeBit: ptr.To[int32](18),
SyncPeriod: metav1.Duration{Duration: 60 * time.Second}, MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second},
}, SyncPeriod: metav1.Duration{Duration: 60 * time.Second},
MetricsBindAddress: tc.metricsBindAddress, },
Mode: kubeproxyconfig.ProxyMode(tc.mode), MetricsBindAddress: tc.metricsBindAddress,
OOMScoreAdj: ptr.To[int32](17), Mode: kubeproxyconfig.ProxyMode(tc.mode),
PortRange: "2-7", OOMScoreAdj: ptr.To[int32](17),
NodePortAddresses: []string{"10.20.30.40/16", "fd00:1::0/64"}, PortRange: "2-7",
DetectLocalMode: kubeproxyconfig.LocalModeClusterCIDR, NodePortAddresses: []string{"10.20.30.40/16", "fd00:1::0/64"},
DetectLocal: kubeproxyconfig.DetectLocalConfiguration{ DetectLocalMode: kubeproxyconfig.LocalModeClusterCIDR,
BridgeInterface: string("cbr0"), DetectLocal: kubeproxyconfig.DetectLocalConfiguration{
InterfaceNamePrefix: string("veth"), BridgeInterface: "cbr0",
}, InterfaceNamePrefix: "veth",
Logging: logsapi.LoggingConfiguration{ },
Format: "text", Logging: logsapi.LoggingConfiguration{
FlushFrequency: logsapi.TimeOrMetaDuration{Duration: metav1.Duration{Duration: 5 * time.Second}, SerializeAsString: true}, Format: "text",
}, FlushFrequency: logsapi.TimeOrMetaDuration{Duration: metav1.Duration{Duration: 5 * time.Second}, SerializeAsString: true},
} },
}
options := NewOptions() options := NewOptions()
baseYAML := fmt.Sprintf( baseYAML := fmt.Sprintf(
yamlTemplate, tc.bindAddress, tc.clusterCIDR, yamlTemplate, tc.bindAddress, tc.clusterCIDR,
tc.healthzBindAddress, tc.metricsBindAddress, tc.mode) tc.healthzBindAddress, tc.metricsBindAddress, tc.mode)
// Append additional configuration to the base yaml template // Append additional configuration to the base yaml template
yaml := fmt.Sprintf("%s\n%s", baseYAML, tc.extraConfig) yaml := fmt.Sprintf("%s\n%s", baseYAML, tc.extraConfig)
config, err := options.loadConfig([]byte(yaml)) config, err := options.loadConfig([]byte(yaml))
require.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, diff = %s", diff)
} }
})
} }
} }
@ -557,21 +559,13 @@ func TestAddressFromDeprecatedFlags(t *testing.T) {
}, },
} }
for i := range testCases { for _, tc := range testCases {
gotHealthz := addressFromDeprecatedFlags(testCases[i].healthzBindAddress, testCases[i].healthzPort) t.Run(tc.name, func(t *testing.T) {
gotMetrics := addressFromDeprecatedFlags(testCases[i].metricsBindAddress, testCases[i].metricsPort) gotHealthz := addressFromDeprecatedFlags(tc.healthzBindAddress, tc.healthzPort)
gotMetrics := addressFromDeprecatedFlags(tc.metricsBindAddress, tc.metricsPort)
errFn := func(name, except, got string) {
t.Errorf("case %s: expected %v, got %v", name, except, got)
}
if gotHealthz != testCases[i].expHealthz {
errFn(testCases[i].name, testCases[i].expHealthz, gotHealthz)
}
if gotMetrics != testCases[i].expMetrics {
errFn(testCases[i].name, testCases[i].expMetrics, gotMetrics)
}
require.Equal(t, tc.expHealthz, gotHealthz)
require.Equal(t, tc.expMetrics, gotMetrics)
})
} }
} }