Skip to content

Commit 7755b4a

Browse files
committed
fix(node.go): improve logic for GetNodeObject
Before the logic ran like the following in terms of preference: 1. Prefer environment var NODE_NAME 2. `Use os.Hostname()` 3. Fallback to `--hostname-override` passed by user This didn't make a whole lot of sense, as `--hostname-override` is directly, and supposedly intentionally set by the user, therefore it should be the MOST preferred, not the least preferred. Additionally, none of the errors encountered were passed back to the user so that future conditions could be considered, so if there was an error at the API level, that error was swallowed. Now the logic looks like: 1. Prefer `--hostname-override` if it is set. If it is set and we weren't able to resolve to a node object, return the error 2. Use environment var NODE_NAME if it is set. If it is set and we weren't able to resolve to a node object, return the error 3. Fallback to `os.Hostname()`. If we weren't able to resolve to a node object then return the error and give the user options
1 parent d12f422 commit 7755b4a

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

pkg/controllers/netpol/network_policy_controller_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"net"
8+
"os"
89
"strings"
910
"sync"
1011
"testing"
@@ -754,6 +755,7 @@ func (ips *fakeIPSet) Name(name string) string {
754755
}
755756

756757
func TestNetworkPolicyController(t *testing.T) {
758+
curHostname, _ := os.Hostname()
757759
testCases := []tNetPolConfigTestCase{
758760
{
759761
"Default options are successful",
@@ -765,7 +767,8 @@ func TestNetworkPolicyController(t *testing.T) {
765767
"Missing nodename fails appropriately",
766768
newMinimalKubeRouterConfig([]string{""}, "", "", nil, nil, false),
767769
true,
768-
"failed to identify the node by NODE_NAME, hostname or --hostname-override",
770+
fmt.Sprintf("failed to identify the node by NODE_NAME, %s or --hostname-override: nodes \"%s\""+
771+
" not found", curHostname, curHostname),
769772
},
770773
{
771774
"Test bad cluster CIDR (not properly formatting ip address)",

pkg/utils/node.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,33 @@ import (
1717

1818
// GetNodeObject returns the node API object for the node
1919
func GetNodeObject(clientset kubernetes.Interface, hostnameOverride string) (*apiv1.Node, error) {
20+
// if env NODE_NAME is not set and node is not registered with hostname, then use host name override
21+
if hostnameOverride != "" {
22+
node, err := clientset.CoreV1().Nodes().Get(context.Background(), hostnameOverride, metav1.GetOptions{})
23+
if err != nil {
24+
return nil, fmt.Errorf("unable to get node %s, due to: %v", hostnameOverride, err)
25+
}
26+
return node, nil
27+
}
28+
2029
// assuming kube-router is running as pod, first check env NODE_NAME
2130
nodeName := os.Getenv("NODE_NAME")
2231
if nodeName != "" {
2332
node, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{})
24-
if err == nil {
25-
return node, nil
33+
if err != nil {
34+
return nil, fmt.Errorf("unable to get node %s, due to: %v", nodeName, err)
2635
}
36+
return node, nil
2737
}
2838

2939
// if env NODE_NAME is not set then check if node is register with hostname
3040
hostName, _ := os.Hostname()
3141
node, err := clientset.CoreV1().Nodes().Get(context.Background(), hostName, metav1.GetOptions{})
32-
if err == nil {
33-
return node, nil
34-
}
35-
36-
// if env NODE_NAME is not set and node is not registered with hostname, then use host name override
37-
if hostnameOverride != "" {
38-
node, err = clientset.CoreV1().Nodes().Get(context.Background(), hostnameOverride, metav1.GetOptions{})
39-
if err == nil {
40-
return node, nil
41-
}
42+
if err != nil {
43+
return nil, fmt.Errorf("failed to identify the node by NODE_NAME, %s or --hostname-override: %v", hostName, err)
4244
}
4345

44-
return nil, fmt.Errorf("failed to identify the node by NODE_NAME, hostname or --hostname-override")
46+
return node, nil
4547
}
4648

4749
// GetPrimaryNodeIP returns the most valid external facing IP address for a node.

pkg/utils/node_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Test_GetNodeObject(t *testing.T) {
3838
nil,
3939
},
4040
{
41-
"node with hostname overrie exists",
41+
"node with hostname override exists",
4242
"something-else",
4343
"test-node",
4444
&apiv1.Node{
@@ -50,8 +50,8 @@ func Test_GetNodeObject(t *testing.T) {
5050
},
5151
{
5252
"node with current hostname exists",
53-
"something-else",
54-
"something-else",
53+
"",
54+
"",
5555
&apiv1.Node{
5656
ObjectMeta: metav1.ObjectMeta{
5757
Name: curHostname,
@@ -68,7 +68,7 @@ func Test_GetNodeObject(t *testing.T) {
6868
Name: "another-node",
6969
},
7070
},
71-
errors.New("failed to identify the node by NODE_NAME, hostname or --hostname-override"),
71+
errors.New("unable to get node test-node, due to: nodes \"test-node\" not found"),
7272
},
7373
}
7474

0 commit comments

Comments
 (0)