Skip to content
This repository was archived by the owner on Apr 2, 2022. It is now read-only.

Commit 04e2b1a

Browse files
committed
Remove generating of random MAC address
MAC address is optional parameter for port. It is better to let the provider to take care of MAC address allocation. Signed-off-by: Ales Musil <amusil@redhat.com>
1 parent 1830409 commit 04e2b1a

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

pkg/admission/admission.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,10 @@ func (ah *AdmissionHook) handleAdmissionRequestToCreate(requestName string, req
162162
// Create port per each network request, such ports are only in OVN NB database, no actual interfaces are created at this point
163163
// TODO: cleanup if fails
164164
for _, network := range networks {
165-
macAddress := generateRandomMac()
166165
portName := generatePortName(network)
167166

168167
// Create the port on OVN NB
169-
portID, hasFixedIPs, err := ah.providerClient.CreateNetworkPort(providerNetworkByName[network].ID, portName, macAddress)
168+
portParams, err := ah.providerClient.CreateNetworkPort(providerNetworkByName[network].ID, portName)
170169
if err != nil {
171170
setResponseError(resp, requestName, "Error creating port: %v", err)
172171
return
@@ -187,16 +186,16 @@ func (ah *AdmissionHook) handleAdmissionRequestToCreate(requestName string, req
187186
requestedResources[ah.ReservedOverlayResourceName] = 1
188187
}
189188
// If selected overlay network has a subnet assigned, fixed IPs will be assigned to the port, add such interfaces to dhclientInterfaces list so we later call DHCP client on them
190-
if hasFixedIPs {
189+
if portParams.HasFixedIps {
191190
dhclientInterfaces = append(dhclientInterfaces, portName)
192191
}
193192
}
194193

195194
// NetworkSpec is later used by Device Plugin to create an interface with PortName and MacAddress and mark it with PortID on OVS so it will be mapped to already created OVN port
196195
networksSpec[network] = spec.NetworkSpec{
197-
MacAddress: macAddress,
196+
MacAddress: portParams.MacAddress,
198197
PortName: portName,
199-
PortID: portID,
198+
PortID: portParams.PortId,
200199
}
201200
}
202201

@@ -361,17 +360,6 @@ func setResponseError(resp *admissionv1beta1.AdmissionResponse, requestName stri
361360
}
362361
}
363362

364-
// generateRandomMac returns random local unicast MAC address
365-
func generateRandomMac() string {
366-
buf := make([]byte, 6)
367-
_, err := rand.Read(buf)
368-
if err != nil {
369-
panic(err)
370-
}
371-
buf[0] = (buf[0] | 2) & 0xfe // make the address local and unicast
372-
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
373-
}
374-
375363
// generatePortName builds name of a port in format ${NETWORK_NAME}-${RANDOM_SUFFIX}, length of the name is set to 15 characters so it fits max length of an interface name
376364
// TODO: keep prefix and suffix length in a constant
377365
func generatePortName(networkName string) string {

pkg/admission/provider.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ type Network struct {
1515
Physnet string `json:"provider:physical_network"`
1616
}
1717

18+
type PortParams struct {
19+
PortId string
20+
MacAddress string
21+
HasFixedIps bool
22+
}
23+
1824
type providerClient struct {
1925
client *gophercloud.ServiceClient
2026
}
@@ -63,15 +69,19 @@ func (c *providerClient) ListNetworkByName() (map[string]*Network, error) {
6369
}
6470

6571
// CreateNetworkPort creates new Neutron Port on Neutron network with given ID
66-
func (c *providerClient) CreateNetworkPort(network, port, macAddress string) (string, bool, error) {
67-
createOpts := ports.CreateOpts{NetworkID: network, Name: port, MACAddress: macAddress, AdminStateUp: newTrue()}
72+
func (c *providerClient) CreateNetworkPort(network, port string) (*PortParams, error) {
73+
createOpts := ports.CreateOpts{NetworkID: network, Name: port, AdminStateUp: newTrue()}
6874
responsePort, err := ports.Create(c.client, createOpts).Extract()
6975

7076
if err != nil {
71-
return "", false, err
77+
return nil, err
7278
}
73-
74-
return responsePort.ID, len(responsePort.FixedIPs) != 0, err
79+
portParams := PortParams{
80+
PortId: responsePort.ID,
81+
MacAddress: responsePort.MACAddress,
82+
HasFixedIps: len(responsePort.FixedIPs) != 0,
83+
}
84+
return &portParams, nil
7585
}
7686

7787
// DeleteNetworkPort removes Neutron Port with given ID

0 commit comments

Comments
 (0)