Skip to content

Commit

Permalink
use context.Context as input params
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
  • Loading branch information
fatsheep9146 committed Feb 8, 2024
1 parent 90fe9f0 commit 239ea97
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
10 changes: 5 additions & 5 deletions cmd/kube-proxy/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,16 +924,16 @@ func (s *ProxyServer) Run() error {
// Note: RegisterHandler() calls need to happen before creation of Sources because sources
// only notify on changes, and the initial update (on process start) may be lost if no handlers
// are registered yet.
serviceConfig := config.NewServiceConfig(s.logger, informerFactory.Core().V1().Services(), s.Config.ConfigSyncPeriod.Duration)
serviceConfig := config.NewServiceConfig(context.TODO(), informerFactory.Core().V1().Services(), s.Config.ConfigSyncPeriod.Duration)
serviceConfig.RegisterEventHandler(s.Proxier)
go serviceConfig.Run(wait.NeverStop)

endpointSliceConfig := config.NewEndpointSliceConfig(s.logger, informerFactory.Discovery().V1().EndpointSlices(), s.Config.ConfigSyncPeriod.Duration)
endpointSliceConfig := config.NewEndpointSliceConfig(context.TODO(), informerFactory.Discovery().V1().EndpointSlices(), s.Config.ConfigSyncPeriod.Duration)
endpointSliceConfig.RegisterEventHandler(s.Proxier)
go endpointSliceConfig.Run(wait.NeverStop)

if utilfeature.DefaultFeatureGate.Enabled(features.MultiCIDRServiceAllocator) {
serviceCIDRConfig := config.NewServiceCIDRConfig(s.logger, informerFactory.Networking().V1alpha1().ServiceCIDRs(), s.Config.ConfigSyncPeriod.Duration)
serviceCIDRConfig := config.NewServiceCIDRConfig(context.TODO(), informerFactory.Networking().V1alpha1().ServiceCIDRs(), s.Config.ConfigSyncPeriod.Duration)
serviceCIDRConfig.RegisterEventHandler(s.Proxier)
go serviceCIDRConfig.Run(wait.NeverStop)
}
Expand All @@ -946,10 +946,10 @@ func (s *ProxyServer) Run() error {
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.FieldSelector = fields.OneTermEqualSelector("metadata.name", s.NodeRef.Name).String()
}))
nodeConfig := config.NewNodeConfig(s.logger, currentNodeInformerFactory.Core().V1().Nodes(), s.Config.ConfigSyncPeriod.Duration)
nodeConfig := config.NewNodeConfig(context.TODO(), currentNodeInformerFactory.Core().V1().Nodes(), s.Config.ConfigSyncPeriod.Duration)
// https://issues.k8s.io/111321
if s.Config.DetectLocalMode == kubeproxyconfig.LocalModeNodeCIDR {
nodeConfig.RegisterEventHandler(proxy.NewNodePodCIDRHandler(s.podCIDRs))
nodeConfig.RegisterEventHandler(proxy.NewNodePodCIDRHandler(context.TODO(), s.podCIDRs))
}
if utilfeature.DefaultFeatureGate.Enabled(features.KubeProxyDrainingTerminatingNodes) {
nodeConfig.RegisterEventHandler(&proxy.NodeEligibleHandler{
Expand Down
4 changes: 2 additions & 2 deletions cmd/kube-proxy/app/server_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguratio

// TODO this has side effects that should only happen when Run() is invoked.
proxier, err = iptables.NewDualStackProxier(
context.Background(),
context.TODO(),
ipt,
utilsysctl.New(),
exec.New(),
Expand All @@ -201,7 +201,7 @@ func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguratio

// TODO this has side effects that should only happen when Run() is invoked.
proxier, err = iptables.NewProxier(
s.logger,
context.TODO(),
s.PrimaryIPFamily,
iptInterface,
utilsysctl.New(),
Expand Down
14 changes: 7 additions & 7 deletions pkg/proxy/config/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func TestNewServicesSourceApi_UpdatesAndMultipleServices(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
service1v1 := &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "s1"},
Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Protocol: "TCP", Port: 10}}}}
Expand All @@ -58,7 +58,7 @@ func TestNewServicesSourceApi_UpdatesAndMultipleServices(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

serviceConfig := NewServiceConfig(logger, sharedInformers.Core().V1().Services(), time.Minute)
serviceConfig := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
serviceConfig.RegisterEventHandler(handler)
go sharedInformers.Start(stopCh)
go serviceConfig.Run(stopCh)
Expand All @@ -85,7 +85,7 @@ func TestNewServicesSourceApi_UpdatesAndMultipleServices(t *testing.T) {
}

func TestNewEndpointsSourceApi_UpdatesAndMultipleEndpoints(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
endpoints1v1 := &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "e1"},
AddressType: discoveryv1.AddressTypeIPv4,
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestNewEndpointsSourceApi_UpdatesAndMultipleEndpoints(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

endpointsliceConfig := NewEndpointSliceConfig(logger, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
endpointsliceConfig := NewEndpointSliceConfig(ctx, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
endpointsliceConfig.RegisterEventHandler(handler)
go sharedInformers.Start(stopCh)
go endpointsliceConfig.Run(stopCh)
Expand All @@ -166,7 +166,7 @@ func TestNewEndpointsSourceApi_UpdatesAndMultipleEndpoints(t *testing.T) {
}

func TestInitialSync(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
svc1 := &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "testnamespace", Name: "foo"},
Spec: v1.ServiceSpec{Ports: []v1.ServicePort{{Protocol: "TCP", Port: 10}}},
Expand Down Expand Up @@ -195,11 +195,11 @@ func TestInitialSync(t *testing.T) {
client := fake.NewSimpleClientset(svc1, svc2, eps2, eps1)
sharedInformers := informers.NewSharedInformerFactory(client, 0)

svcConfig := NewServiceConfig(logger, sharedInformers.Core().V1().Services(), 0)
svcConfig := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), 0)
svcHandler := NewServiceHandlerMock()
svcConfig.RegisterEventHandler(svcHandler)

epsConfig := NewEndpointSliceConfig(logger, sharedInformers.Discovery().V1().EndpointSlices(), 0)
epsConfig := NewEndpointSliceConfig(ctx, sharedInformers.Discovery().V1().EndpointSlices(), 0)
epsHandler := NewEndpointSliceHandlerMock()
epsConfig.RegisterEventHandler(epsHandler)

Expand Down
17 changes: 9 additions & 8 deletions pkg/proxy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"context"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -75,10 +76,10 @@ type EndpointSliceConfig struct {
}

// NewEndpointSliceConfig creates a new EndpointSliceConfig.
func NewEndpointSliceConfig(logger klog.Logger, endpointSliceInformer discoveryv1informers.EndpointSliceInformer, resyncPeriod time.Duration) *EndpointSliceConfig {
func NewEndpointSliceConfig(ctx context.Context, endpointSliceInformer discoveryv1informers.EndpointSliceInformer, resyncPeriod time.Duration) *EndpointSliceConfig {
result := &EndpointSliceConfig{
listerSynced: endpointSliceInformer.Informer().HasSynced,
logger: logger,
logger: klog.FromContext(ctx),
}

_, _ = endpointSliceInformer.Informer().AddEventHandlerWithResyncPeriod(
Expand Down Expand Up @@ -168,10 +169,10 @@ type ServiceConfig struct {
}

// NewServiceConfig creates a new ServiceConfig.
func NewServiceConfig(logger klog.Logger, serviceInformer v1informers.ServiceInformer, resyncPeriod time.Duration) *ServiceConfig {
func NewServiceConfig(ctx context.Context, serviceInformer v1informers.ServiceInformer, resyncPeriod time.Duration) *ServiceConfig {
result := &ServiceConfig{
listerSynced: serviceInformer.Informer().HasSynced,
logger: logger,
logger: klog.FromContext(ctx),
}

_, _ = serviceInformer.Informer().AddEventHandlerWithResyncPeriod(
Expand Down Expand Up @@ -297,10 +298,10 @@ type NodeConfig struct {
}

// NewNodeConfig creates a new NodeConfig.
func NewNodeConfig(logger klog.Logger, nodeInformer v1informers.NodeInformer, resyncPeriod time.Duration) *NodeConfig {
func NewNodeConfig(ctx context.Context, nodeInformer v1informers.NodeInformer, resyncPeriod time.Duration) *NodeConfig {
result := &NodeConfig{
listerSynced: nodeInformer.Informer().HasSynced,
logger: logger,
logger: klog.FromContext(ctx),
}

_, _ = nodeInformer.Informer().AddEventHandlerWithResyncPeriod(
Expand Down Expand Up @@ -400,11 +401,11 @@ type ServiceCIDRConfig struct {
}

// NewServiceCIDRConfig creates a new ServiceCIDRConfig.
func NewServiceCIDRConfig(logger klog.Logger, serviceCIDRInformer networkingv1alpha1informers.ServiceCIDRInformer, resyncPeriod time.Duration) *ServiceCIDRConfig {
func NewServiceCIDRConfig(ctx context.Context, serviceCIDRInformer networkingv1alpha1informers.ServiceCIDRInformer, resyncPeriod time.Duration) *ServiceCIDRConfig {
result := &ServiceCIDRConfig{
listerSynced: serviceCIDRInformer.Informer().HasSynced,
cidrs: sets.New[string](),
logger: logger,
logger: klog.FromContext(ctx),
}

_, _ = serviceCIDRInformer.Informer().AddEventHandlerWithResyncPeriod(
Expand Down
20 changes: 10 additions & 10 deletions pkg/proxy/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (h *EndpointSliceHandlerMock) ValidateEndpointSlices(t *testing.T, expected
}

func TestNewServiceAddedAndNotified(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
client := fake.NewSimpleClientset()
fakeWatch := watch.NewFake()
client.PrependWatchReactor("services", ktesting.DefaultWatchReactor(fakeWatch, nil))
Expand All @@ -237,7 +237,7 @@ func TestNewServiceAddedAndNotified(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

config := NewServiceConfig(logger, sharedInformers.Core().V1().Services(), time.Minute)
config := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
handler := NewServiceHandlerMock()
config.RegisterEventHandler(handler)
go sharedInformers.Start(stopCh)
Expand All @@ -252,7 +252,7 @@ func TestNewServiceAddedAndNotified(t *testing.T) {
}

func TestServiceAddedRemovedSetAndNotified(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
client := fake.NewSimpleClientset()
fakeWatch := watch.NewFake()
client.PrependWatchReactor("services", ktesting.DefaultWatchReactor(fakeWatch, nil))
Expand All @@ -262,7 +262,7 @@ func TestServiceAddedRemovedSetAndNotified(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

config := NewServiceConfig(logger, sharedInformers.Core().V1().Services(), time.Minute)
config := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
handler := NewServiceHandlerMock()
config.RegisterEventHandler(handler)
go sharedInformers.Start(stopCh)
Expand All @@ -289,7 +289,7 @@ func TestServiceAddedRemovedSetAndNotified(t *testing.T) {
}

func TestNewServicesMultipleHandlersAddedAndNotified(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
client := fake.NewSimpleClientset()
fakeWatch := watch.NewFake()
client.PrependWatchReactor("services", ktesting.DefaultWatchReactor(fakeWatch, nil))
Expand All @@ -299,7 +299,7 @@ func TestNewServicesMultipleHandlersAddedAndNotified(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

config := NewServiceConfig(logger, sharedInformers.Core().V1().Services(), time.Minute)
config := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
handler := NewServiceHandlerMock()
handler2 := NewServiceHandlerMock()
config.RegisterEventHandler(handler)
Expand All @@ -324,7 +324,7 @@ func TestNewServicesMultipleHandlersAddedAndNotified(t *testing.T) {
}

func TestNewEndpointsMultipleHandlersAddedAndNotified(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
client := fake.NewSimpleClientset()
fakeWatch := watch.NewFake()
client.PrependWatchReactor("endpointslices", ktesting.DefaultWatchReactor(fakeWatch, nil))
Expand All @@ -334,7 +334,7 @@ func TestNewEndpointsMultipleHandlersAddedAndNotified(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

config := NewEndpointSliceConfig(logger, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
config := NewEndpointSliceConfig(ctx, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
handler := NewEndpointSliceHandlerMock()
handler2 := NewEndpointSliceHandlerMock()
config.RegisterEventHandler(handler)
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestNewEndpointsMultipleHandlersAddedAndNotified(t *testing.T) {
}

func TestNewEndpointsMultipleHandlersAddRemoveSetAndNotified(t *testing.T) {
logger, _ := klogtesting.NewTestContext(t)
_, ctx := klogtesting.NewTestContext(t)
client := fake.NewSimpleClientset()
fakeWatch := watch.NewFake()
client.PrependWatchReactor("endpointslices", ktesting.DefaultWatchReactor(fakeWatch, nil))
Expand All @@ -381,7 +381,7 @@ func TestNewEndpointsMultipleHandlersAddRemoveSetAndNotified(t *testing.T) {

sharedInformers := informers.NewSharedInformerFactory(client, time.Minute)

config := NewEndpointSliceConfig(logger, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
config := NewEndpointSliceConfig(ctx, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
handler := NewEndpointSliceHandlerMock()
handler2 := NewEndpointSliceHandlerMock()
config.RegisterEventHandler(handler)
Expand Down
5 changes: 3 additions & 2 deletions pkg/proxy/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package proxy

import (
"context"
"reflect"
"sync"

Expand All @@ -35,10 +36,10 @@ type NodePodCIDRHandler struct {
logger klog.Logger
}

func NewNodePodCIDRHandler(logger klog.Logger, podCIDRs []string) *NodePodCIDRHandler {
func NewNodePodCIDRHandler(ctx context.Context, podCIDRs []string) *NodePodCIDRHandler {
return &NodePodCIDRHandler{
podCIDRs: podCIDRs,
logger: logger,
logger: klog.FromContext(ctx),
}
}

Expand Down

0 comments on commit 239ea97

Please sign in to comment.