Skip to content

Commit e1499a9

Browse files
author
Ravi Sankar Penta
committed
Refactor: Move sdn node Hostname/SelfIP initialization to setNodeIP()
Check proxy mode in node.New() before computing hostname/selfIP so that we can return early in case of incompatible proxy mode error.
1 parent e92d5c5 commit e1499a9

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

pkg/network/node/node.go

+36-26
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,18 @@ func New(c *OsdnNodeConfig) (network.NodeInterface, error) {
143143
// Not an OpenShift plugin
144144
return nil, nil
145145
}
146+
glog.Infof("Initializing SDN node of type %q with configured hostname %q (IP %q), iptables sync period %q", c.PluginName, c.Hostname, c.SelfIP, c.IPTablesSyncPeriod.String())
147+
148+
if useConnTrack && c.ProxyMode != componentconfig.ProxyModeIPTables {
149+
return nil, fmt.Errorf("%q plugin is not compatible with proxy-mode %q", c.PluginName, c.ProxyMode)
150+
}
146151

147152
// If our CNI config file exists, remove it so that kubelet doesn't think
148153
// we're ready yet
149154
os.Remove(filepath.Join(cniDirPath, openshiftCNIFile))
150155

151-
glog.Infof("Initializing SDN node of type %q with configured hostname %q (IP %q), iptables sync period %q", c.PluginName, c.Hostname, c.SelfIP, c.IPTablesSyncPeriod.String())
152-
if c.Hostname == "" {
153-
output, err := kexec.New().Command("uname", "-n").CombinedOutput()
154-
if err != nil {
155-
return nil, err
156-
}
157-
c.Hostname = strings.TrimSpace(string(output))
158-
glog.Infof("Resolved hostname to %q", c.Hostname)
159-
}
160-
if c.SelfIP == "" {
161-
var err error
162-
c.SelfIP, err = netutils.GetNodeIP(c.Hostname)
163-
if err != nil {
164-
glog.V(5).Infof("Failed to determine node address from hostname %s; using default interface (%v)", c.Hostname, err)
165-
var defaultIP net.IP
166-
defaultIP, err = kubeutilnet.ChooseHostInterface()
167-
if err != nil {
168-
return nil, err
169-
}
170-
c.SelfIP = defaultIP.String()
171-
glog.Infof("Resolved IP address to %q", c.SelfIP)
172-
}
173-
}
174-
175-
if useConnTrack && c.ProxyMode != componentconfig.ProxyModeIPTables {
176-
return nil, fmt.Errorf("%q plugin is not compatible with proxy-mode %q", c.PluginName, c.ProxyMode)
156+
if err := c.setNodeIP(); err != nil {
157+
return nil, err
177158
}
178159

179160
ovsif, err := ovs.New(kexec.New(), Br0, minOvsVersion)
@@ -215,6 +196,35 @@ func New(c *OsdnNodeConfig) (network.NodeInterface, error) {
215196
return plugin, nil
216197
}
217198

199+
// Set node IP if required
200+
func (c *OsdnNodeConfig) setNodeIP() error {
201+
if len(c.Hostname) == 0 {
202+
output, err := kexec.New().Command("uname", "-n").CombinedOutput()
203+
if err != nil {
204+
return err
205+
}
206+
c.Hostname = strings.TrimSpace(string(output))
207+
glog.Infof("Resolved hostname to %q", c.Hostname)
208+
}
209+
210+
if len(c.SelfIP) == 0 {
211+
var err error
212+
c.SelfIP, err = netutils.GetNodeIP(c.Hostname)
213+
if err != nil {
214+
glog.V(5).Infof("Failed to determine node address from hostname %s; using default interface (%v)", c.Hostname, err)
215+
var defaultIP net.IP
216+
defaultIP, err = kubeutilnet.ChooseHostInterface()
217+
if err != nil {
218+
return err
219+
}
220+
c.SelfIP = defaultIP.String()
221+
glog.Infof("Resolved IP address to %q", c.SelfIP)
222+
}
223+
}
224+
225+
return nil
226+
}
227+
218228
// Detect whether we are upgrading from a pre-CNI openshift and clean up
219229
// interfaces and iptables rules that are no longer required
220230
func (node *OsdnNode) dockerPreCNICleanup() error {

0 commit comments

Comments
 (0)