Skip to content

Commit

Permalink
Changed method names containing mac and ip to match conventions; adde…
Browse files Browse the repository at this point in the history
…d some comments for clarification
  • Loading branch information
kqdeng committed Jun 22, 2020
1 parent 8a42884 commit b88dc4e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
10 changes: 5 additions & 5 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func CreateFromIP(ip net.IP) (Job, error) {
if err != nil {
return Job{}, errors.WithMessage(err, "discovering from ip address")
}
mac := (*d).GetMac(ip)
mac := (*d).GetMAC(ip)
if mac.String() == packet.ZeroMAC.String() {
joblog.With("ip", ip).Fatal(errors.New("somehow got a zero mac"))
}
Expand Down Expand Up @@ -107,20 +107,20 @@ func (j *Job) setup(dp *packet.Discovery) error {
j.Logger = joblog.With("mac", j.mac, "hardware.id", dh.HardwareID())

// mac is needed to find the hostname for DiscoveryCacher
d.SetMac(j.mac)
d.SetMAC(j.mac)

// is this necessary?
// (kdeng3849) is this necessary?
j.hardware = d.Hardware()

//how can we remove this?
// (kdeng3849) how can we remove this?
j.instance = d.Instance()
if j.instance == nil {
j.instance = &packet.Instance{}
} else {
j.Logger = j.Logger.With("instance.id", j.InstanceID())
}

ip := d.GetIp(j.mac)
ip := d.GetIP(j.mac)
if ip.Address == nil {
return errors.New("could not find IP address")
}
Expand Down
2 changes: 1 addition & 1 deletion job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestSetupInstance(t *testing.T) {
t.Fatalf("incorect mode, want: %v, got: %v\n", wantMode, mode)
}

netConfig := d.GetIp(macs[1].HardwareAddr())
netConfig := d.GetIP(macs[1].HardwareAddr())
if !netConfig.Address.Equal(j.dhcp.Address()) {
t.Fatalf("incorrect Address, want: %v, got: %v\n", netConfig.Address, j.dhcp.Address())
}
Expand Down
11 changes: 7 additions & 4 deletions packet/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ import (
"github.com/tinkerbell/boots/files/ignition"
)

// models.go contains the Hardware structures matching the data models defined by tink and cacher

// BondingMode is the hardware bonding mode
type BondingMode int

// Discovery interface is the base for cacher and tinkerbell hardware discovery
type Discovery interface {
Instance() *Instance
Mac() net.HardwareAddr
MAC() net.HardwareAddr
Mode() string
GetIp(addr net.HardwareAddr) IP
GetMac(ip net.IP) net.HardwareAddr
GetIP(addr net.HardwareAddr) IP
GetMAC(ip net.IP) net.HardwareAddr
DnsServers() []net.IP
LeaseTime(mac net.HardwareAddr) time.Duration
Hostname() (string, error)
Hardware() *Hardware
SetMac(mac net.HardwareAddr)
SetMAC(mac net.HardwareAddr)
}

// DiscoveryCacher presents the structure for old data model
Expand All @@ -39,6 +41,7 @@ type DiscoveryTinkerbellV1 struct {
mac net.HardwareAddr
}

// Interface is the base for cacher and tinkerbell hardware (network) interface
type Interface interface {
}

Expand Down
10 changes: 6 additions & 4 deletions packet/models_cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/tinkerbell/boots/conf"
)

// models_cacher.go contains the interface methods specific to DiscoveryCacher and HardwareCacher structs

func (d DiscoveryCacher) Hardware() *Hardware {
var h Hardware = d.HardwareCacher
return &h
Expand All @@ -25,7 +27,7 @@ func (d DiscoveryCacher) LeaseTime(mac net.HardwareAddr) time.Duration {
return conf.DHCPLeaseTime
}

func (d DiscoveryCacher) Mac() net.HardwareAddr {
func (d DiscoveryCacher) MAC() net.HardwareAddr {
if d.mac == nil {
mac := d.PrimaryDataMAC()
return mac.HardwareAddr()
Expand Down Expand Up @@ -71,7 +73,7 @@ func (d DiscoveryCacher) Mode() string {
}

// NetConfig returns the network configuration that corresponds to the interface whose MAC address is mac.
func (d DiscoveryCacher) GetIp(mac net.HardwareAddr) IP {
func (d DiscoveryCacher) GetIP(mac net.HardwareAddr) IP {
ip := d.InstanceIP(mac.String())
if ip != nil {
return *ip
Expand All @@ -92,7 +94,7 @@ func (d DiscoveryCacher) GetIp(mac net.HardwareAddr) IP {
}

// dummy method for tink data model transition
func (d DiscoveryCacher) GetMac(ip net.IP) net.HardwareAddr {
func (d DiscoveryCacher) GetMAC(ip net.IP) net.HardwareAddr {
return d.PrimaryDataMAC().HardwareAddr()
}

Expand Down Expand Up @@ -213,7 +215,7 @@ func (d DiscoveryCacher) Hostname() (string, error) {
return hostname, nil
}

func (d *DiscoveryCacher) SetMac(mac net.HardwareAddr) {
func (d *DiscoveryCacher) SetMAC(mac net.HardwareAddr) {
d.mac = mac
}

Expand Down
24 changes: 12 additions & 12 deletions packet/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestInterfaces(t *testing.T) {
}

func TestNewDiscoveryCacher(t *testing.T) {
t.Log("HARDWARE_DATA_MODEL (should be empty to use cacher):", os.Getenv("HARDWARE_DATA_MODEL"))
t.Log("DATA_MODEL_VERSION (should be empty to use cacher):", os.Getenv("DATA_MODEL_VERSION"))

for name, test := range tests {
t.Log(name)
Expand All @@ -31,8 +31,8 @@ func TestNewDiscoveryCacher(t *testing.T) {
}

func TestNewDiscoveryTinkerbell(t *testing.T) {
os.Setenv("HARDWARE_DATA_MODEL", "tinkerbell")
t.Log("HARDWARE_DATA_MODEL:", os.Getenv("HARDWARE_DATA_MODEL"))
os.Setenv("DATA_MODEL_VERSION", "1")
t.Log("DATA_MODEL_VERSION:", os.Getenv("DATA_MODEL_VERSION"))

for name, test := range tinkerbellTests {
t.Log(name)
Expand Down Expand Up @@ -61,8 +61,8 @@ func TestNewDiscoveryMismatch(t *testing.T) {
}
}()

os.Setenv("HARDWARE_DATA_MODEL", "tinkerbell")
t.Log("HARDWARE_DATA_MODEL:", os.Getenv("HARDWARE_DATA_MODEL"))
os.Setenv("DATA_MODEL_VERSION", "1")
t.Log("DATA_MODEL_VERSION:", os.Getenv("DATA_MODEL_VERSION"))

for name, test := range tinkerbellTests {
t.Log(name)
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestDiscoveryCacher(t *testing.T) {
t.Logf("MacType: %s\n", d.MacType(mac.String()))
t.Logf("MacIsType=data: %v\n", d.MacIsType(mac.String(), "data"))
t.Logf("primaryDataMac: %s\n", d.PrimaryDataMAC().HardwareAddr().String())
t.Logf("Mac: %v\n", d.Mac())
t.Logf("MAC: %v\n", d.MAC())
t.Logf("d.mac: %v\n", d.mac)
t.Logf("mac: %s\n", mac.String())
if d.Instance() != nil {
Expand All @@ -110,7 +110,7 @@ func TestDiscoveryCacher(t *testing.T) {
t.Log("\n")
}

d.SetMac(mac)
d.SetMAC(mac)
mode := d.Mode()
if mode != test.mode {
t.Fatalf("unexpected mode, want: %s, got: %s\n", test.mode, mode)
Expand All @@ -123,11 +123,11 @@ func TestDiscoveryCacher(t *testing.T) {
if d.PrimaryDataMAC().String() != test.primaryDataMac {
t.Fatalf("unexpected address, want: %s, got: %s\n", test.primaryDataMac, d.PrimaryDataMAC().String())
}
if d.Mac().String() != test.mac {
t.Fatalf("unexpected address, want: %s, got: %s\n", test.mac, d.Mac().String())
if d.MAC().String() != test.mac {
t.Fatalf("unexpected address, want: %s, got: %s\n", test.mac, d.MAC().String())
}

conf := d.GetIp(mac)
conf := d.GetIP(mac)
if conf.Address.String() != test.conf.Address.String() {
t.Fatalf("unexpected address, want: %s, got: %s\n", test.conf.Address, conf.Address)
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestDiscoveryTinkerbell(t *testing.T) {
t.Log("\n")
}

d.SetMac(mac)
d.SetMAC(mac)
mode := d.Mode()
if mode != test.mode {
t.Fatalf("unexpected mode, want: %s, got: %s\n", test.mode, mode)
Expand All @@ -233,7 +233,7 @@ func TestDiscoveryTinkerbell(t *testing.T) {
continue
}

conf := d.GetIp(mac)
conf := d.GetIP(mac)
if conf.Address.String() != test.ip.Address.String() {
t.Fatalf("unexpected address, want: %s, got: %s\n", test.ip.Address, conf.Address)
}
Expand Down
14 changes: 10 additions & 4 deletions packet/models_tinkerbell.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/tinkerbell/boots/conf"
)

// models_tinkerbell.go contains the interface methods specific to DiscoveryTinkerbell and HardwareTinkerbell structs

func (i InterfaceTinkerbell) Name() string {
return i.DHCP.IfaceName
}
Expand All @@ -31,25 +33,27 @@ func (d DiscoveryTinkerbellV1) Instance() *Instance {
return d.Metadata.Instance
}

func (d DiscoveryTinkerbellV1) Mac() net.HardwareAddr {
func (d DiscoveryTinkerbellV1) MAC() net.HardwareAddr {
return d.mac
}

func (d DiscoveryTinkerbellV1) Mode() string {
return "hardware"
}

func (d DiscoveryTinkerbellV1) GetIp(mac net.HardwareAddr) IP {
func (d DiscoveryTinkerbellV1) GetIP(mac net.HardwareAddr) IP {
//if i := d.Network.Interface(mac); i.DHCP.IP.Address != nil {
// return i.DHCP.IP
//}
return d.Network.InterfaceByMac(mac).DHCP.IP
}

func (d DiscoveryTinkerbellV1) GetMac(ip net.IP) net.HardwareAddr {
func (d DiscoveryTinkerbellV1) GetMAC(ip net.IP) net.HardwareAddr {
return d.Network.InterfaceByIp(ip).DHCP.MAC.HardwareAddr()
}

// InterfacesByMac returns the NetworkInterface that contains the matching mac address
// returns an empty NetworkInterface if not found
func (n Network) InterfaceByMac(mac net.HardwareAddr) NetworkInterface {
for _, i := range n.Interfaces {
if i.DHCP.MAC.String() == mac.String() {
Expand All @@ -59,6 +63,8 @@ func (n Network) InterfaceByMac(mac net.HardwareAddr) NetworkInterface {
return NetworkInterface{}
}

// InterfacesByIp returns the NetworkInterface that contains the matching ip address
// returns an empty NetworkInterface if not found
func (n Network) InterfaceByIp(ip net.IP) NetworkInterface {
for _, i := range n.Interfaces {
if i.DHCP.IP.Address.String() == ip.String() {
Expand All @@ -78,7 +84,7 @@ func (d *DiscoveryTinkerbellV1) Hostname() (string, error) {
return d.Instance().Hostname, nil // temp
}

func (d *DiscoveryTinkerbellV1) SetMac(mac net.HardwareAddr) {
func (d *DiscoveryTinkerbellV1) SetMAC(mac net.HardwareAddr) {
d.mac = mac
}

Expand Down

0 comments on commit b88dc4e

Please sign in to comment.