Skip to content

Commit

Permalink
Create port for node when node exists and has no port allocated.
Browse files Browse the repository at this point in the history
Signed-off-by: s3rj1k <evasive.gyron@gmail.com>
  • Loading branch information
s3rj1k committed Mar 17, 2021
1 parent 3e860bf commit cf66abd
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/provisioner/ironic/ironic.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,52 @@ func (p *ironicProvisioner) listAllPorts(address string) ([]ports.Port, error) {

}

// Verifies that node has port assigned by Ironic.
func (p *ironicProvisioner) nodeHasAssignedPort(ironicNode *nodes.Node) bool {
opts := ports.ListOpts{
Fields: []string{"node_uuid"},
}

opts.NodeUUID = ironicNode.UUID

pager := ports.List(p.client, opts)
if pager.Err != nil {
p.log.Info("node has no assigned port", "error", pager.Err)
return false
}

allPages, err := pager.AllPages()
if err != nil {
p.log.Info("node has no assigned port", "error", err)
return false
}

ok, err := allPages.IsEmpty()
if err != nil {
p.log.Info("node has no assigned port", "error", err)
return false
}

if ok {
p.log.Info("node has no assigned port")
return false
}

p.log.Info("node has assigned port")
return true
}

// Verify that MAC is already allocated to some node port.
func (p *ironicProvisioner) isPortAllocatedToAddress(address string) bool {
if allPorts, err := p.listAllPorts(address); err == nil && len(allPorts) > 0 {
p.log.Info("address is allocated to port", "address", address)
return true
}

p.log.Info("address is not allocated to port", "address", address)
return false
}

// Look for an existing registration for the host in Ironic.
func (p *ironicProvisioner) findExistingHost() (ironicNode *nodes.Node, err error) {
// Try to load the node by UUID
Expand Down Expand Up @@ -513,6 +559,25 @@ func (p *ironicProvisioner) ValidateManagementAccess(credentialsChanged, force b

}

// When node exists but has no assigned port to it by Ironic and actuall address (MAC) is present
// in host config and is not allocated to different node lets try to create port for this node.
if p.host.Spec.BootMACAddress != "" && !p.nodeHasAssignedPort(ironicNode) && !p.isPortAllocatedToAddress(p.host.Spec.BootMACAddress) {
enable := true
p.log.Info("creating port for node in ironic", "MAC",
p.host.Spec.BootMACAddress)
_, err = ports.Create(
p.client,
ports.CreateOpts{
NodeUUID: ironicNode.UUID,
Address: p.host.Spec.BootMACAddress,
PXEEnabled: &enable,
}).Extract()
if err != nil {
result, err = transientError(errors.Wrap(err, "failed to create port in ironic"))
return
}
}

// Look for the case where we previously enrolled this node
// and now the credentials have changed.
if credentialsChanged {
Expand Down

0 comments on commit cf66abd

Please sign in to comment.