Skip to content

Commit c850835

Browse files
committed
Clarify hostname vs node name in kube-proxy
Various parts of kube-proxy passed around a "hostname", but it is actually the name of the *node* kube-proxy is running on, which is not 100% guaranteed to be exactly the same as the hostname. Rename it everywhere to make it clearer that (a) it is definitely safe to use that name to refer to the Node, (b) it is not necessarily safe to use that name with DNS, etc.
1 parent ff640c3 commit c850835

File tree

15 files changed

+378
-381
lines changed

15 files changed

+378
-381
lines changed

cmd/kube-proxy/app/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ type ProxyServer struct {
168168
Recorder events.EventRecorder
169169
NodeRef *v1.ObjectReference
170170
HealthzServer *healthcheck.ProxyHealthServer
171-
Hostname string
171+
NodeName string
172172
PrimaryIPFamily v1.IPFamily
173173
NodeIPs map[v1.IPFamily]net.IP
174174
flagz flagz.Reader
@@ -197,7 +197,7 @@ func newProxyServer(ctx context.Context, config *kubeproxyconfig.KubeProxyConfig
197197
metrics.SetShowHidden()
198198
}
199199

200-
s.Hostname, err = nodeutil.GetHostname(config.HostnameOverride)
200+
s.NodeName, err = nodeutil.GetHostname(config.HostnameOverride)
201201
if err != nil {
202202
return nil, err
203203
}
@@ -207,7 +207,7 @@ func newProxyServer(ctx context.Context, config *kubeproxyconfig.KubeProxyConfig
207207
return nil, err
208208
}
209209

210-
rawNodeIPs := getNodeIPs(ctx, s.Client, s.Hostname)
210+
rawNodeIPs := getNodeIPs(ctx, s.Client, s.NodeName)
211211
s.PrimaryIPFamily, s.NodeIPs = detectNodeIPs(ctx, rawNodeIPs, config.BindAddress)
212212

213213
if len(config.NodePortAddresses) == 1 && config.NodePortAddresses[0] == kubeproxyconfig.NodePortAddressesPrimary {
@@ -226,8 +226,8 @@ func newProxyServer(ctx context.Context, config *kubeproxyconfig.KubeProxyConfig
226226

227227
s.NodeRef = &v1.ObjectReference{
228228
Kind: "Node",
229-
Name: s.Hostname,
230-
UID: types.UID(s.Hostname),
229+
Name: s.NodeName,
230+
UID: types.UID(s.NodeName),
231231
Namespace: "",
232232
}
233233

cmd/kube-proxy/app/server_linux.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfigur
8181
func (s *ProxyServer) platformSetup(ctx context.Context) error {
8282
logger := klog.FromContext(ctx)
8383
if s.Config.DetectLocalMode == proxyconfigapi.LocalModeNodeCIDR {
84-
logger.Info("Watching for node, awaiting podCIDR allocation", "hostname", s.Hostname)
85-
node, err := waitForPodCIDR(ctx, s.Client, s.Hostname)
84+
logger.Info("Watching for node, awaiting podCIDR allocation", "node", s.NodeName)
85+
node, err := waitForPodCIDR(ctx, s.Client, s.NodeName)
8686
if err != nil {
8787
return err
8888
}
@@ -157,7 +157,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
157157
*config.IPTables.LocalhostNodePorts,
158158
int(*config.IPTables.MasqueradeBit),
159159
localDetectors,
160-
s.Hostname,
160+
s.NodeName,
161161
s.NodeIPs,
162162
s.Recorder,
163163
s.HealthzServer,
@@ -179,7 +179,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
179179
*config.IPTables.LocalhostNodePorts,
180180
int(*config.IPTables.MasqueradeBit),
181181
localDetectors[s.PrimaryIPFamily],
182-
s.Hostname,
182+
s.NodeName,
183183
s.NodeIPs[s.PrimaryIPFamily],
184184
s.Recorder,
185185
s.HealthzServer,
@@ -217,7 +217,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
217217
config.Linux.MasqueradeAll,
218218
int(*config.IPTables.MasqueradeBit),
219219
localDetectors,
220-
s.Hostname,
220+
s.NodeName,
221221
s.NodeIPs,
222222
s.Recorder,
223223
s.HealthzServer,
@@ -243,7 +243,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
243243
config.Linux.MasqueradeAll,
244244
int(*config.IPTables.MasqueradeBit),
245245
localDetectors[s.PrimaryIPFamily],
246-
s.Hostname,
246+
s.NodeName,
247247
s.NodeIPs[s.PrimaryIPFamily],
248248
s.Recorder,
249249
s.HealthzServer,
@@ -267,7 +267,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
267267
config.Linux.MasqueradeAll,
268268
int(*config.NFTables.MasqueradeBit),
269269
localDetectors,
270-
s.Hostname,
270+
s.NodeName,
271271
s.NodeIPs,
272272
s.Recorder,
273273
s.HealthzServer,
@@ -285,7 +285,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
285285
config.Linux.MasqueradeAll,
286286
int(*config.NFTables.MasqueradeBit),
287287
localDetectors[s.PrimaryIPFamily],
288-
s.Hostname,
288+
s.NodeName,
289289
s.NodeIPs[s.PrimaryIPFamily],
290290
s.Recorder,
291291
s.HealthzServer,

cmd/kube-proxy/app/server_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ func TestProxyServer_platformSetup(t *testing.T) {
703703
s := &ProxyServer{
704704
Config: tt.config,
705705
Client: client,
706-
Hostname: "nodename",
706+
NodeName: "nodename",
707707
NodeIPs: map[v1.IPFamily]net.IP{
708708
v1.IPv4Protocol: netutils.ParseIPSloppy("127.0.0.1"),
709709
v1.IPv6Protocol: net.IPv6zero,

cmd/kube-proxy/app/server_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
9393
proxier, err = winkernel.NewDualStackProxier(
9494
config.SyncPeriod.Duration,
9595
config.MinSyncPeriod.Duration,
96-
s.Hostname,
96+
s.NodeName,
9797
s.NodeIPs,
9898
s.Recorder,
9999
s.HealthzServer,
@@ -105,7 +105,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
105105
s.PrimaryIPFamily,
106106
config.SyncPeriod.Duration,
107107
config.MinSyncPeriod.Duration,
108-
s.Hostname,
108+
s.NodeName,
109109
s.NodeIPs[s.PrimaryIPFamily],
110110
s.Recorder,
111111
s.HealthzServer,

pkg/proxy/endpointschangetracker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type makeEndpointFunc func(info *BaseEndpointInfo, svcPortName *ServicePortName)
5959
type processEndpointsMapChangeFunc func(oldEndpointsMap, newEndpointsMap EndpointsMap)
6060

6161
// NewEndpointsChangeTracker initializes an EndpointsChangeTracker
62-
func NewEndpointsChangeTracker(ipFamily v1.IPFamily, hostname string, makeEndpointInfo makeEndpointFunc, processEndpointsMapChange processEndpointsMapChangeFunc) *EndpointsChangeTracker {
62+
func NewEndpointsChangeTracker(ipFamily v1.IPFamily, nodeName string, makeEndpointInfo makeEndpointFunc, processEndpointsMapChange processEndpointsMapChangeFunc) *EndpointsChangeTracker {
6363
addressType := discovery.AddressTypeIPv4
6464
if ipFamily == v1.IPv6Protocol {
6565
addressType = discovery.AddressTypeIPv6
@@ -70,7 +70,7 @@ func NewEndpointsChangeTracker(ipFamily v1.IPFamily, hostname string, makeEndpoi
7070
lastChangeTriggerTimes: make(map[types.NamespacedName][]time.Time),
7171
trackerStartTime: time.Now(),
7272
processEndpointsMapChange: processEndpointsMapChange,
73-
endpointSliceCache: NewEndpointSliceCache(hostname, makeEndpointInfo),
73+
endpointSliceCache: NewEndpointSliceCache(nodeName, makeEndpointInfo),
7474
}
7575
}
7676

pkg/proxy/endpointslicecache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type EndpointSliceCache struct {
4343
trackerByServiceMap map[types.NamespacedName]*endpointSliceTracker
4444

4545
makeEndpointInfo makeEndpointFunc
46-
hostname string
46+
nodeName string
4747
}
4848

4949
// endpointSliceTracker keeps track of EndpointSlices as they have been applied
@@ -65,13 +65,13 @@ type endpointSliceData struct {
6565
}
6666

6767
// NewEndpointSliceCache initializes an EndpointSliceCache.
68-
func NewEndpointSliceCache(hostname string, makeEndpointInfo makeEndpointFunc) *EndpointSliceCache {
68+
func NewEndpointSliceCache(nodeName string, makeEndpointInfo makeEndpointFunc) *EndpointSliceCache {
6969
if makeEndpointInfo == nil {
7070
makeEndpointInfo = standardEndpointInfo
7171
}
7272
return &EndpointSliceCache{
7373
trackerByServiceMap: map[types.NamespacedName]*endpointSliceTracker{},
74-
hostname: hostname,
74+
nodeName: nodeName,
7575
makeEndpointInfo: makeEndpointInfo,
7676
}
7777
}
@@ -233,8 +233,8 @@ func (cache *EndpointSliceCache) addEndpoints(svcPortName *ServicePortName, port
233233
return endpointSet
234234
}
235235

236-
func (cache *EndpointSliceCache) isLocal(hostname string) bool {
237-
return len(cache.hostname) > 0 && hostname == cache.hostname
236+
func (cache *EndpointSliceCache) isLocal(nodeName string) bool {
237+
return len(cache.nodeName) > 0 && nodeName == cache.nodeName
238238
}
239239

240240
// esDataChanged returns true if the esData parameter should be set as a new

pkg/proxy/healthcheck/service_health.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type proxyHealthChecker interface {
5757
Health() ProxyHealth
5858
}
5959

60-
func newServiceHealthServer(hostname string, recorder events.EventRecorder, listener listener, factory httpServerFactory, nodePortAddresses *proxyutil.NodePortAddresses, healthzServer proxyHealthChecker) ServiceHealthServer {
60+
func newServiceHealthServer(nodeName string, recorder events.EventRecorder, listener listener, factory httpServerFactory, nodePortAddresses *proxyutil.NodePortAddresses, healthzServer proxyHealthChecker) ServiceHealthServer {
6161
// It doesn't matter whether we listen on "0.0.0.0", "::", or ""; go
6262
// treats them all the same.
6363
nodeIPs := []net.IP{net.IPv4zero}
@@ -72,7 +72,7 @@ func newServiceHealthServer(hostname string, recorder events.EventRecorder, list
7272
}
7373

7474
return &server{
75-
hostname: hostname,
75+
nodeName: nodeName,
7676
recorder: recorder,
7777
listener: listener,
7878
httpFactory: factory,
@@ -83,12 +83,12 @@ func newServiceHealthServer(hostname string, recorder events.EventRecorder, list
8383
}
8484

8585
// NewServiceHealthServer allocates a new service healthcheck server manager
86-
func NewServiceHealthServer(hostname string, recorder events.EventRecorder, nodePortAddresses *proxyutil.NodePortAddresses, healthzServer proxyHealthChecker) ServiceHealthServer {
87-
return newServiceHealthServer(hostname, recorder, stdNetListener{}, stdHTTPServerFactory{}, nodePortAddresses, healthzServer)
86+
func NewServiceHealthServer(nodeName string, recorder events.EventRecorder, nodePortAddresses *proxyutil.NodePortAddresses, healthzServer proxyHealthChecker) ServiceHealthServer {
87+
return newServiceHealthServer(nodeName, recorder, stdNetListener{}, stdHTTPServerFactory{}, nodePortAddresses, healthzServer)
8888
}
8989

9090
type server struct {
91-
hostname string
91+
nodeName string
9292
// node addresses where health check port will listen on
9393
nodeIPs []net.IP
9494
recorder events.EventRecorder // can be nil
@@ -131,7 +131,7 @@ func (hcs *server) SyncServices(newServices map[types.NamespacedName]uint16) err
131131
err := svc.listenAndServeAll(hcs)
132132

133133
if err != nil {
134-
msg := fmt.Sprintf("node %s failed to start healthcheck %q on port %d: %v", hcs.hostname, nsn.String(), port, err)
134+
msg := fmt.Sprintf("node %s failed to start healthcheck %q on port %d: %v", hcs.nodeName, nsn.String(), port, err)
135135

136136
if hcs.recorder != nil {
137137
hcs.recorder.Eventf(
@@ -142,7 +142,7 @@ func (hcs *server) SyncServices(newServices map[types.NamespacedName]uint16) err
142142
UID: types.UID(nsn.String()),
143143
}, nil, api.EventTypeWarning, "FailedToStartServiceHealthcheck", "Listen", msg)
144144
}
145-
klog.ErrorS(err, "Failed to start healthcheck", "node", hcs.hostname, "service", nsn, "port", port)
145+
klog.ErrorS(err, "Failed to start healthcheck", "node", hcs.nodeName, "service", nsn, "port", port)
146146
continue
147147
}
148148
hcs.services[nsn] = svc

pkg/proxy/iptables/proxier.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func NewDualStackProxier(
102102
localhostNodePorts bool,
103103
masqueradeBit int,
104104
localDetectors map[v1.IPFamily]proxyutil.LocalTrafficDetector,
105-
hostname string,
105+
nodeName string,
106106
nodeIPs map[v1.IPFamily]net.IP,
107107
recorder events.EventRecorder,
108108
healthzServer *healthcheck.ProxyHealthServer,
@@ -112,15 +112,15 @@ func NewDualStackProxier(
112112
// Create an ipv4 instance of the single-stack proxier
113113
ipv4Proxier, err := NewProxier(ctx, v1.IPv4Protocol, ipts[v1.IPv4Protocol], sysctl,
114114
syncPeriod, minSyncPeriod, masqueradeAll, localhostNodePorts, masqueradeBit,
115-
localDetectors[v1.IPv4Protocol], hostname, nodeIPs[v1.IPv4Protocol],
115+
localDetectors[v1.IPv4Protocol], nodeName, nodeIPs[v1.IPv4Protocol],
116116
recorder, healthzServer, nodePortAddresses, initOnly)
117117
if err != nil {
118118
return nil, fmt.Errorf("unable to create ipv4 proxier: %v", err)
119119
}
120120

121121
ipv6Proxier, err := NewProxier(ctx, v1.IPv6Protocol, ipts[v1.IPv6Protocol], sysctl,
122122
syncPeriod, minSyncPeriod, masqueradeAll, false, masqueradeBit,
123-
localDetectors[v1.IPv6Protocol], hostname, nodeIPs[v1.IPv6Protocol],
123+
localDetectors[v1.IPv6Protocol], nodeName, nodeIPs[v1.IPv6Protocol],
124124
recorder, healthzServer, nodePortAddresses, initOnly)
125125
if err != nil {
126126
return nil, fmt.Errorf("unable to create ipv6 proxier: %v", err)
@@ -166,7 +166,7 @@ type Proxier struct {
166166
conntrack conntrack.Interface
167167
nfacct nfacct.Interface
168168
localDetector proxyutil.LocalTrafficDetector
169-
hostname string
169+
nodeName string
170170
nodeIP net.IP
171171

172172
serviceHealthServer healthcheck.ServiceHealthServer
@@ -224,7 +224,7 @@ func NewProxier(ctx context.Context,
224224
localhostNodePorts bool,
225225
masqueradeBit int,
226226
localDetector proxyutil.LocalTrafficDetector,
227-
hostname string,
227+
nodeName string,
228228
nodeIP net.IP,
229229
recorder events.EventRecorder,
230230
healthzServer *healthcheck.ProxyHealthServer,
@@ -265,7 +265,7 @@ func NewProxier(ctx context.Context,
265265
masqueradeMark := fmt.Sprintf("%#08x", masqueradeValue)
266266
logger.V(2).Info("Using iptables mark for masquerade", "mark", masqueradeMark)
267267

268-
serviceHealthServer := healthcheck.NewServiceHealthServer(hostname, recorder, nodePortAddresses, healthzServer)
268+
serviceHealthServer := healthcheck.NewServiceHealthServer(nodeName, recorder, nodePortAddresses, healthzServer)
269269
nfacctRunner, err := nfacct.New()
270270
if err != nil {
271271
logger.Error(err, "Failed to create nfacct runner, nfacct based metrics won't be available")
@@ -276,7 +276,7 @@ func NewProxier(ctx context.Context,
276276
svcPortMap: make(proxy.ServicePortMap),
277277
serviceChanges: proxy.NewServiceChangeTracker(ipFamily, newServiceInfo, nil),
278278
endpointsMap: make(proxy.EndpointsMap),
279-
endpointsChanges: proxy.NewEndpointsChangeTracker(ipFamily, hostname, newEndpointInfo, nil),
279+
endpointsChanges: proxy.NewEndpointsChangeTracker(ipFamily, nodeName, newEndpointInfo, nil),
280280
needFullSync: true,
281281
syncPeriod: syncPeriod,
282282
iptables: ipt,
@@ -285,7 +285,7 @@ func NewProxier(ctx context.Context,
285285
conntrack: conntrack.New(),
286286
nfacct: nfacctRunner,
287287
localDetector: localDetector,
288-
hostname: hostname,
288+
nodeName: nodeName,
289289
nodeIP: nodeIP,
290290
serviceHealthServer: serviceHealthServer,
291291
healthzServer: healthzServer,
@@ -615,9 +615,9 @@ func (proxier *Proxier) OnEndpointSlicesSynced() {
615615
// OnNodeAdd is called whenever creation of new node object
616616
// is observed.
617617
func (proxier *Proxier) OnNodeAdd(node *v1.Node) {
618-
if node.Name != proxier.hostname {
618+
if node.Name != proxier.nodeName {
619619
proxier.logger.Error(nil, "Received a watch event for a node that doesn't match the current node",
620-
"eventNode", node.Name, "currentNode", proxier.hostname)
620+
"eventNode", node.Name, "currentNode", proxier.nodeName)
621621
return
622622
}
623623

@@ -640,9 +640,9 @@ func (proxier *Proxier) OnNodeAdd(node *v1.Node) {
640640
// OnNodeUpdate is called whenever modification of an existing
641641
// node object is observed.
642642
func (proxier *Proxier) OnNodeUpdate(oldNode, node *v1.Node) {
643-
if node.Name != proxier.hostname {
643+
if node.Name != proxier.nodeName {
644644
proxier.logger.Error(nil, "Received a watch event for a node that doesn't match the current node",
645-
"eventNode", node.Name, "currentNode", proxier.hostname)
645+
"eventNode", node.Name, "currentNode", proxier.nodeName)
646646
return
647647
}
648648

@@ -665,9 +665,9 @@ func (proxier *Proxier) OnNodeUpdate(oldNode, node *v1.Node) {
665665
// OnNodeDelete is called whenever deletion of an existing node
666666
// object is observed.
667667
func (proxier *Proxier) OnNodeDelete(node *v1.Node) {
668-
if node.Name != proxier.hostname {
668+
if node.Name != proxier.nodeName {
669669
proxier.logger.Error(nil, "Received a watch event for a node that doesn't match the current node",
670-
"eventNode", node.Name, "currentNode", proxier.hostname)
670+
"eventNode", node.Name, "currentNode", proxier.nodeName)
671671
return
672672
}
673673

0 commit comments

Comments
 (0)