Skip to content

Commit 9abda01

Browse files
committed
Handle null pointers for cli.StringSlice
Signed-off-by: Derek Nola <[email protected]>
1 parent f4bbc0d commit 9abda01

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

pkg/agent/config/config.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
441441
}
442442
// If the supervisor and externally-facing apiserver are not on the same port, tell the proxy where to find the apiserver.
443443
if controlConfig.SupervisorPort != controlConfig.HTTPSPort {
444-
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(envInfo.NodeIP)))
444+
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(&envInfo.NodeIP)))
445445
if err := proxy.SetAPIServerPort(controlConfig.HTTPSPort, isIPv6); err != nil {
446446
return nil, errors.Wrapf(err, "failed to set apiserver port to %d", controlConfig.HTTPSPort)
447447
}
@@ -482,7 +482,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
482482
newNodePasswordFile := filepath.Join(nodeConfigPath, "password")
483483
upgradeOldNodePasswordPath(oldNodePasswordFile, newNodePasswordFile)
484484

485-
nodeName, nodeIPs, err := util.GetHostnameAndIPs(envInfo.NodeName, envInfo.NodeIP)
485+
nodeName, nodeIPs, err := util.GetHostnameAndIPs(envInfo.NodeName, &envInfo.NodeIP)
486486
if err != nil {
487487
return nil, err
488488
}
@@ -536,7 +536,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
536536
}
537537
}
538538

539-
nodeExternalIPs, err := util.ParseStringSliceToIPs(envInfo.NodeExternalIP)
539+
nodeExternalIPs, err := util.ParseStringSliceToIPs(&envInfo.NodeExternalIP)
540540
if err != nil {
541541
return nil, fmt.Errorf("invalid node-external-ip: %w", err)
542542
}

pkg/agent/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func createProxyAndValidateToken(ctx context.Context, cfg *cmds.Agent) (proxy.Pr
335335
if err := os.MkdirAll(agentDir, 0700); err != nil {
336336
return nil, err
337337
}
338-
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(cfg.NodeIP)))
338+
isIPv6 := utilsnet.IsIPv6(net.ParseIP(util.GetFirstValidIPString(&cfg.NodeIP)))
339339

340340
proxy, err := proxy.NewSupervisorProxy(ctx, !cfg.DisableLoadBalancer, agentDir, cfg.ServerURL, cfg.LBServerPort, isIPv6)
341341
if err != nil {

pkg/cli/server/server.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,13 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
252252
}
253253

254254
if serverConfig.ControlConfig.PrivateIP == "" && len(cmds.AgentConfig.NodeIP.Value()) != 0 {
255-
serverConfig.ControlConfig.PrivateIP = util.GetFirstValidIPString(cmds.AgentConfig.NodeIP)
255+
serverConfig.ControlConfig.PrivateIP = util.GetFirstValidIPString(&cmds.AgentConfig.NodeIP)
256256
}
257257

258258
// Ensure that we add the localhost name/ip and node name/ip to the SAN list. This list is shared by the
259259
// certs for the supervisor, kube-apiserver cert, and etcd. DNS entries for the in-cluster kubernetes
260260
// service endpoint are added later when the certificates are created.
261-
nodeName, nodeIPs, err := util.GetHostnameAndIPs(cmds.AgentConfig.NodeName, cmds.AgentConfig.NodeIP)
261+
nodeName, nodeIPs, err := util.GetHostnameAndIPs(cmds.AgentConfig.NodeName, &cmds.AgentConfig.NodeIP)
262262
if err != nil {
263263
return err
264264
}
@@ -303,12 +303,12 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
303303

304304
// if not set, try setting advertise-ip from agent node-external-ip
305305
if serverConfig.ControlConfig.AdvertiseIP == "" && len(cmds.AgentConfig.NodeExternalIP.Value()) != 0 {
306-
serverConfig.ControlConfig.AdvertiseIP = util.GetFirstValidIPString(cmds.AgentConfig.NodeExternalIP)
306+
serverConfig.ControlConfig.AdvertiseIP = util.GetFirstValidIPString(&cmds.AgentConfig.NodeExternalIP)
307307
}
308308

309309
// if not set, try setting advertise-ip from agent node-ip
310310
if serverConfig.ControlConfig.AdvertiseIP == "" && len(cmds.AgentConfig.NodeIP.Value()) != 0 {
311-
serverConfig.ControlConfig.AdvertiseIP = util.GetFirstValidIPString(cmds.AgentConfig.NodeIP)
311+
serverConfig.ControlConfig.AdvertiseIP = util.GetFirstValidIPString(&cmds.AgentConfig.NodeIP)
312312
}
313313
}
314314

pkg/util/net.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func JoinIP6Nets(elems []*net.IPNet) string {
137137
// GetHostnameAndIPs takes a node name and list of IPs, usually from CLI args.
138138
// If set, these are used to return the node's name and addresses. If not set,
139139
// the system hostname and primary interface addresses are returned instead.
140-
func GetHostnameAndIPs(name string, nodeIPs cli.StringSlice) (string, []net.IP, error) {
140+
func GetHostnameAndIPs(name string, nodeIPs *cli.StringSlice) (string, []net.IP, error) {
141141
ips := []net.IP{}
142142
if len(nodeIPs.Value()) == 0 {
143143
hostIP, err := apinet.ChooseHostInterface()
@@ -177,8 +177,11 @@ func GetHostnameAndIPs(name string, nodeIPs cli.StringSlice) (string, []net.IP,
177177

178178
// ParseStringSliceToIPs converts slice of strings that in turn can be lists of comma separated unparsed IP addresses
179179
// into a single slice of net.IP, it returns error if at any point parsing failed
180-
func ParseStringSliceToIPs(s cli.StringSlice) ([]net.IP, error) {
180+
func ParseStringSliceToIPs(s *cli.StringSlice) ([]net.IP, error) {
181181
var ips []net.IP
182+
if s == nil {
183+
return ips, nil
184+
}
182185
for _, unparsedIP := range s.Value() {
183186
for _, v := range strings.Split(unparsedIP, ",") {
184187
ip := net.ParseIP(v)
@@ -194,7 +197,10 @@ func ParseStringSliceToIPs(s cli.StringSlice) ([]net.IP, error) {
194197

195198
// GetFirstValidIPString returns the first valid address from a list of IP address strings,
196199
// without preference for IP family. If no address are found, an empty string is returned.
197-
func GetFirstValidIPString(s cli.StringSlice) string {
200+
func GetFirstValidIPString(s *cli.StringSlice) string {
201+
if s == nil {
202+
return ""
203+
}
198204
for _, unparsedIP := range s.Value() {
199205
for _, v := range strings.Split(unparsedIP, ",") {
200206
if ip := net.ParseIP(v); ip != nil {

pkg/util/net_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func Test_UnitParseStringSliceToIPs(t *testing.T) {
7070
for _, tt := range tests {
7171
t.Run(
7272
tt.name, func(t *testing.T) {
73-
got, err := ParseStringSliceToIPs(*tt.arg)
73+
got, err := ParseStringSliceToIPs(tt.arg)
7474
if (err != nil) != tt.wantErr {
7575
t.Errorf("ParseStringSliceToIPs() error = %v, wantErr %v", err, tt.wantErr)
7676
return

0 commit comments

Comments
 (0)