diff --git a/job/job.go b/job/job.go index 688e5801..92b83289 100644 --- a/job/job.go +++ b/job/job.go @@ -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")) } @@ -107,12 +107,12 @@ 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{} @@ -120,7 +120,7 @@ func (j *Job) setup(dp *packet.Discovery) error { 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") } diff --git a/job/job_test.go b/job/job_test.go index 6c348e1c..79f7fef1 100644 --- a/job/job_test.go +++ b/job/job_test.go @@ -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()) } diff --git a/packet/models.go b/packet/models.go index 7afe8330..d50ae462 100644 --- a/packet/models.go +++ b/packet/models.go @@ -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 @@ -39,6 +41,7 @@ type DiscoveryTinkerbellV1 struct { mac net.HardwareAddr } +// Interface is the base for cacher and tinkerbell hardware (network) interface type Interface interface { } diff --git a/packet/models_cacher.go b/packet/models_cacher.go index bcd0b218..7c8507f8 100644 --- a/packet/models_cacher.go +++ b/packet/models_cacher.go @@ -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 @@ -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() @@ -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 @@ -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() } @@ -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 } diff --git a/packet/models_test.go b/packet/models_test.go index 00059786..c77a25f8 100644 --- a/packet/models_test.go +++ b/packet/models_test.go @@ -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) @@ -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) @@ -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) @@ -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 { @@ -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) @@ -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) } @@ -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) @@ -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) } diff --git a/packet/models_tinkerbell.go b/packet/models_tinkerbell.go index 39d0bc91..250df277 100644 --- a/packet/models_tinkerbell.go +++ b/packet/models_tinkerbell.go @@ -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 } @@ -31,7 +33,7 @@ func (d DiscoveryTinkerbellV1) Instance() *Instance { return d.Metadata.Instance } -func (d DiscoveryTinkerbellV1) Mac() net.HardwareAddr { +func (d DiscoveryTinkerbellV1) MAC() net.HardwareAddr { return d.mac } @@ -39,17 +41,19 @@ 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() { @@ -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() { @@ -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 }