Skip to content

Commit

Permalink
chore: refactor LinkRefresh and LinkStatus into typed.Resource
Browse files Browse the repository at this point in the history
From #5472 Andrey comments, this commit changes LinkRefresh and LinkStatus into typed.Resource by moving Bump and Physical methods to *Spec types.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed May 4, 2022
1 parent b52e0b9 commit d2935f9
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (ctrl *HardwareAddrController) Run(ctx context.Context, r controller.Runtim
for _, res := range links.Items {
link := res.(*network.LinkStatus) //nolint:errcheck,forcetypeassert

if !link.Physical() {
if !link.TypedSpec().Physical() {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (ctrl *LinkConfigController) Run(ctx context.Context, r controller.Runtime,
linkStatus := item.(*network.LinkStatus) //nolint:errcheck,forcetypeassert

if _, configured := configuredLinks[linkStatus.Metadata().ID()]; !configured {
if linkStatus.Physical() {
if linkStatus.TypedSpec().Physical() {
var ids []string

ids, err = ctrl.apply(ctx, r, []network.LinkSpecSpec{
Expand Down
2 changes: 1 addition & 1 deletion internal/app/machined/pkg/controllers/network/link_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (ctrl *LinkSpecController) syncLink(ctx context.Context, r controller.Runti

// notify link status controller, as wireguard updates can't be watched via netlink API
if err = r.Modify(ctx, network.NewLinkRefresh(network.NamespaceName, network.LinkKindWireguard), func(r resource.Resource) error {
r.(*network.LinkRefresh).Bump()
r.(*network.LinkRefresh).TypedSpec().Bump()

return nil
}); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (ctrl *NodeAddressController) Run(ctx context.Context, r controller.Runtime

if link.TypedSpec().OperationalState == nethelpers.OperStateUp || link.TypedSpec().OperationalState == nethelpers.OperStateUnknown {
// skip physical interfaces without carrier
if !link.Physical() || link.TypedSpec().LinkState {
if !link.TypedSpec().Physical() || link.TypedSpec().LinkState {
linksUp[link.TypedSpec().Index] = struct{}{}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (ctrl *OperatorConfigController) Run(ctx context.Context, r controller.Runt
for _, item := range list.Items {
linkStatus := item.(*network.LinkStatus) //nolint:errcheck,forcetypeassert

if linkStatus.Physical() {
if linkStatus.TypedSpec().Physical() {
if _, configured := configuredInterfaces[linkStatus.Metadata().ID()]; !configured {
if _, ignored := ignoredInterfaces[linkStatus.Metadata().ID()]; !ignored {
// enable DHCPv4 operator on physical interfaces which don't have any explicit configuration and are not ignored
Expand Down
52 changes: 17 additions & 35 deletions pkg/machinery/resources/network/link_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package network
import (
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/cosi-project/runtime/pkg/resource/typed"
)

// LinkRefreshType is type of LinkRefresh resource.
Expand All @@ -19,57 +20,38 @@ const LinkRefreshType = resource.Type("LinkRefreshes.net.talos.dev")
//
// Whenever Wireguard interface is updated, LinkRefresh resource is modified to trigger a reconcile
// loop in the LinkStatusController.
type LinkRefresh struct {
md resource.Metadata
spec LinkRefreshSpec
}
type LinkRefresh = typed.Resource[LinkRefreshSpec, LinkRefreshRD]

// LinkRefreshSpec describes status of rendered secrets.
type LinkRefreshSpec struct {
Generation int `yaml:"generation"`
}

// NewLinkRefresh initializes a LinkRefresh resource.
func NewLinkRefresh(namespace resource.Namespace, id resource.ID) *LinkRefresh {
r := &LinkRefresh{
md: resource.NewMetadata(namespace, LinkRefreshType, id, resource.VersionUndefined),
spec: LinkRefreshSpec{},
}

r.md.BumpVersion()

return r
}
// DeepCopy implements typed.DeepCopyable interface.
func (s LinkRefreshSpec) DeepCopy() LinkRefreshSpec { return s }

// Metadata implements resource.Resource.
func (r *LinkRefresh) Metadata() *resource.Metadata {
return &r.md
// Bump performs an update.
func (s *LinkRefreshSpec) Bump() {
s.Generation++
}

// Spec implements resource.Resource.
func (r *LinkRefresh) Spec() interface{} {
return r.spec
// NewLinkRefresh initializes a LinkRefresh resource.
func NewLinkRefresh(namespace resource.Namespace, id resource.ID) *LinkRefresh {
return typed.NewResource[LinkRefreshSpec, LinkRefreshRD](
resource.NewMetadata(namespace, LinkRefreshType, id, resource.VersionUndefined),
LinkRefreshSpec{},
)
}

// DeepCopy implements resource.Resource.
func (r *LinkRefresh) DeepCopy() resource.Resource {
return &LinkRefresh{
md: r.md,
spec: r.spec,
}
}
// LinkRefreshRD provides auxiliary methods for LinkRefresh.
type LinkRefreshRD struct{}

// ResourceDefinition implements meta.ResourceDefinitionProvider interface.
func (r *LinkRefresh) ResourceDefinition() meta.ResourceDefinitionSpec {
// ResourceDefinition implements typed.ResourceDefinition interface.
func (LinkRefreshRD) ResourceDefinition(resource.Metadata, LinkRefreshSpec) meta.ResourceDefinitionSpec {
return meta.ResourceDefinitionSpec{
Type: LinkRefreshType,
Aliases: []resource.Type{},
DefaultNamespace: NamespaceName,
PrintColumns: []meta.PrintColumn{},
}
}

// Bump performs an update.
func (r *LinkRefresh) Bump() {
r.spec.Generation++
}
75 changes: 39 additions & 36 deletions pkg/machinery/resources/network/link_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package network
import (
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/cosi-project/runtime/pkg/resource/typed"
"inet.af/netaddr"

"github.com/talos-systems/talos/pkg/machinery/nethelpers"
)
Expand All @@ -15,10 +17,7 @@ import (
const LinkStatusType = resource.Type("LinkStatuses.net.talos.dev")

// LinkStatus resource holds physical network link status.
type LinkStatus struct {
md resource.Metadata
spec LinkStatusSpec
}
type LinkStatus = typed.Resource[LinkStatusSpec, LinkStatusRD]

// LinkStatusSpec describes status of rendered secrets.
type LinkStatusSpec struct {
Expand Down Expand Up @@ -46,38 +45,52 @@ type LinkStatusSpec struct {
Wireguard WireguardSpec `yaml:"wireguard,omitempty"`
}

// NewLinkStatus initializes a LinkStatus resource.
func NewLinkStatus(namespace resource.Namespace, id resource.ID) *LinkStatus {
r := &LinkStatus{
md: resource.NewMetadata(namespace, LinkStatusType, id, resource.VersionUndefined),
spec: LinkStatusSpec{},
// DeepCopy implements typed.DeepCopyable interface.
func (s LinkStatusSpec) DeepCopy() LinkStatusSpec {
cp := s

if s.HardwareAddr != nil {
cp.HardwareAddr = make([]byte, len(s.HardwareAddr))
copy(cp.HardwareAddr, s.HardwareAddr)
}

r.md.BumpVersion()
if s.BroadcastAddr != nil {
cp.BroadcastAddr = make([]byte, len(s.BroadcastAddr))
copy(cp.BroadcastAddr, s.BroadcastAddr)
}

return r
}
if s.Wireguard.Peers != nil {
cp.Wireguard.Peers = append([]WireguardPeer(nil), s.Wireguard.Peers...)

for i3 := range s.Wireguard.Peers {
if s.Wireguard.Peers[i3].AllowedIPs != nil {
cp.Wireguard.Peers[i3].AllowedIPs = make([]netaddr.IPPrefix, len(s.Wireguard.Peers[i3].AllowedIPs))
copy(cp.Wireguard.Peers[i3].AllowedIPs, s.Wireguard.Peers[i3].AllowedIPs)
}
}
}

// Metadata implements resource.Resource.
func (r *LinkStatus) Metadata() *resource.Metadata {
return &r.md
return cp
}

// Spec implements resource.Resource.
func (r *LinkStatus) Spec() interface{} {
return r.spec
// Physical checks if the link is physical ethernet.
func (s LinkStatusSpec) Physical() bool {
return s.Type == nethelpers.LinkEther && s.Kind == ""
}

// DeepCopy implements resource.Resource.
func (r *LinkStatus) DeepCopy() resource.Resource {
return &LinkStatus{
md: r.md,
spec: r.spec,
}
// NewLinkStatus initializes a LinkStatus resource.
func NewLinkStatus(namespace resource.Namespace, id resource.ID) *LinkStatus {
return typed.NewResource[LinkStatusSpec, LinkStatusRD](
resource.NewMetadata(namespace, LinkStatusType, id, resource.VersionUndefined),
LinkStatusSpec{},
)
}

// ResourceDefinition implements meta.ResourceDefinitionProvider interface.
func (r *LinkStatus) ResourceDefinition() meta.ResourceDefinitionSpec {
// LinkStatusRD provides auxiliary methods for LinkStatus.
type LinkStatusRD struct{}

// ResourceDefinition implements typed.ResourceDefinition interface.
func (LinkStatusRD) ResourceDefinition(resource.Metadata, LinkStatusSpec) meta.ResourceDefinitionSpec {
return meta.ResourceDefinitionSpec{
Type: LinkStatusType,
Aliases: []resource.Type{"link", "links"},
Expand Down Expand Up @@ -107,13 +120,3 @@ func (r *LinkStatus) ResourceDefinition() meta.ResourceDefinitionSpec {
Sensitivity: meta.NonSensitive,
}
}

// TypedSpec allows to access the Spec with the proper type.
func (r *LinkStatus) TypedSpec() *LinkStatusSpec {
return &r.spec
}

// Physical checks if the link is physical ethernet.
func (r *LinkStatus) Physical() bool {
return r.TypedSpec().Type == nethelpers.LinkEther && r.TypedSpec().Kind == ""
}

0 comments on commit d2935f9

Please sign in to comment.