talos/pkg/resources/network/hardrware_addr.go
Andrey Smirnov 83fdb7721f
feat: provide first NIC hardware addr as a resource
This will be used to derive e.g. KubeSpan address.

Fixes #4132

Example:

```
$ talosctl -n 172.20.0.2 get hardwareaddresses -o yaml
node: 172.20.0.2
metadata:
    namespace: network
    type: HardwareAddresses.net.talos.dev
    id: first
    version: 1
    owner: network.HardwareAddrController
    phase: running
    created: 2021-08-24T20:30:43Z
    updated: 2021-08-24T20:30:43Z
spec:
    name: eth0
    hardwareAddr: 6a:2b:bd:b2:fc:e0
```

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2021-08-25 17:11:24 +03:00

85 lines
2.2 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package network
import (
"fmt"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/talos-systems/talos/pkg/machinery/nethelpers"
)
// HardwareAddrType is type of HardwareAddr resource.
const HardwareAddrType = resource.Type("HardwareAddresses.net.talos.dev")
// FirstHardwareAddr is a resource ID for the first NIC HW addr.
const FirstHardwareAddr = resource.ID("first")
// HardwareAddr resource describes hardware address of the physical links.
type HardwareAddr struct {
md resource.Metadata
spec HardwareAddrSpec
}
// HardwareAddrSpec describes spec for the link.
type HardwareAddrSpec struct {
// Name defines link name
Name string `yaml:"name"`
// Hardware address
HardwareAddr nethelpers.HardwareAddr `yaml:"hardwareAddr"`
}
// NewHardwareAddr initializes a HardwareAddr resource.
func NewHardwareAddr(namespace resource.Namespace, id resource.ID) *HardwareAddr {
r := &HardwareAddr{
md: resource.NewMetadata(namespace, HardwareAddrType, id, resource.VersionUndefined),
spec: HardwareAddrSpec{},
}
r.md.BumpVersion()
return r
}
// Metadata implements resource.Resource.
func (r *HardwareAddr) Metadata() *resource.Metadata {
return &r.md
}
// Spec implements resource.Resource.
func (r *HardwareAddr) Spec() interface{} {
return r.spec
}
func (r *HardwareAddr) String() string {
return fmt.Sprintf("network.HardwareAddr(%q)", r.md.ID())
}
// DeepCopy implements resource.Resource.
func (r *HardwareAddr) DeepCopy() resource.Resource {
return &HardwareAddr{
md: r.md,
spec: r.spec,
}
}
// ResourceDefinition implements meta.ResourceDefinitionProvider interface.
func (r *HardwareAddr) ResourceDefinition() meta.ResourceDefinitionSpec {
return meta.ResourceDefinitionSpec{
Type: HardwareAddrType,
Aliases: []resource.Type{},
DefaultNamespace: NamespaceName,
PrintColumns: []meta.PrintColumn{},
}
}
// TypedSpec allows to access the Spec with the proper type.
func (r *HardwareAddr) TypedSpec() *HardwareAddrSpec {
return &r.spec
}