Skip to content

Commit

Permalink
chore: add generic methods and use them part #2
Browse files Browse the repository at this point in the history
Use things from #5702.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Jun 9, 2022
1 parent 7114292 commit 4dbbf4a
Show file tree
Hide file tree
Showing 49 changed files with 199 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
k8sctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/k8s"
"github.com/talos-systems/talos/pkg/machinery/resources/v1alpha1"
)
Expand Down Expand Up @@ -85,11 +86,7 @@ func (suite *ControlPlaneStaticPodSuite) assertControlPlaneStaticPods(manifests
return err
}

ids := make([]string, 0, len(resources.Items))

for _, res := range resources.Items {
ids = append(ids, res.Metadata().ID())
}
ids := slices.Map(resources.Items, func(r resource.Resource) string { return r.Metadata().ID() })

if !reflect.DeepEqual(manifests, ids) {
return retry.ExpectedError(fmt.Errorf("expected %q, got %q", manifests, ids))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
k8sadapter "github.com/talos-systems/talos/internal/app/machined/pkg/adapters/k8s"
k8sctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/k8s"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
"github.com/talos-systems/talos/pkg/machinery/resources/v1alpha1"
Expand Down Expand Up @@ -78,11 +79,7 @@ func (suite *ExtraManifestSuite) assertExtraManifests(manifests []string) error
return err
}

ids := make([]string, 0, len(resources.Items))

for _, res := range resources.Items {
ids = append(ids, res.Metadata().ID())
}
ids := slices.Map(resources.Items, func(r resource.Resource) string { return r.Metadata().ID() })

if !reflect.DeepEqual(manifests, ids) {
return retry.ExpectedError(fmt.Errorf("expected %q, got %q", manifests, ids))
Expand Down
8 changes: 3 additions & 5 deletions internal/app/machined/pkg/controllers/k8s/manifest_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,9 @@ func (ctrl *ManifestApplyController) etcdLock(ctx context.Context, logger *zap.L
//nolint:gocyclo
func (ctrl *ManifestApplyController) apply(ctx context.Context, logger *zap.Logger, mapper *restmapper.DeferredDiscoveryRESTMapper, dyn dynamic.Interface, manifests resource.List) error {
// flatten list of objects to be applied
objects := make([]*unstructured.Unstructured, 0, len(manifests.Items))

for _, manifest := range manifests.Items {
objects = append(objects, k8sadapter.Manifest(manifest.(*k8s.Manifest)).Objects()...)
}
objects := slices.FlatMap(manifests.Items, func(m resource.Resource) []*unstructured.Unstructured {
return k8sadapter.Manifest(m.(*k8s.Manifest)).Objects()
})

// sort the list so that namespaces come first, followed by CRDs and everything else after that
sort.SliceStable(objects, func(i, j int) bool {
Expand Down
7 changes: 2 additions & 5 deletions internal/app/machined/pkg/controllers/k8s/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
k8sctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/k8s"
"github.com/talos-systems/talos/pkg/machinery/resources/secrets"
)
Expand Down Expand Up @@ -77,11 +78,7 @@ func (suite *ManifestSuite) assertManifests(manifests []string) error {
return err
}

ids := make([]string, 0, len(resources.Items))

for _, res := range resources.Items {
ids = append(ids, res.Metadata().ID())
}
ids := slices.Map(resources.Items, func(r resource.Resource) string { return r.Metadata().ID() })

if !reflect.DeepEqual(manifests, ids) {
return retry.ExpectedError(fmt.Errorf("expected %q, got %q", manifests, ids))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/config"
)

Expand Down Expand Up @@ -67,11 +68,7 @@ func (suite *KubeSpanSuite) assertResourceIDs(md resource.Metadata, expectedIDs
return err
}

actualIDs := make([]resource.ID, 0, len(l.Items))

for _, r := range l.Items {
actualIDs = append(actualIDs, r.Metadata().ID())
}
actualIDs := slices.Map(l.Items, func(r resource.Resource) string { return r.Metadata().ID() })

sort.Strings(expectedIDs)

Expand Down
10 changes: 2 additions & 8 deletions internal/app/machined/pkg/controllers/kubespan/peer_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.uber.org/zap"
"inet.af/netaddr"

"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/cluster"
"github.com/talos-systems/talos/pkg/machinery/resources/config"
"github.com/talos-systems/talos/pkg/machinery/resources/kubespan"
Expand Down Expand Up @@ -178,12 +179,5 @@ func (ctrl *PeerSpecController) Run(ctx context.Context, r controller.Runtime, l

// dumpSet converts IPSet to a form suitable for logging.
func dumpSet(set *netaddr.IPSet) []string {
ranges := set.Ranges()
res := make([]string, len(ranges))

for i, p := range ranges {
res[i] = p.String()
}

return res
return slices.Map(set.Ranges(), netaddr.IPRange.String)
}
9 changes: 2 additions & 7 deletions internal/app/machined/pkg/controllers/network/link_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"inet.af/netaddr"

talosconfig "github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/generic/maps"
"github.com/talos-systems/talos/pkg/machinery/nethelpers"
"github.com/talos-systems/talos/pkg/machinery/ordered"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
Expand Down Expand Up @@ -340,13 +341,7 @@ func (ctrl *LinkConfigController) processDevicesConfiguration(logger *zap.Logger
SetBondSlave(linkMap[slaveName], bondData)
}

links := make([]network.LinkSpecSpec, 0, len(linkMap))

for _, link := range linkMap {
links = append(links, *link)
}

return links
return maps.ValuesFunc(linkMap, func(link *network.LinkSpecSpec) network.LinkSpecSpec { return *link })
}

func vlanLink(linkName string, vlan talosconfig.Vlan) network.LinkSpecSpec {
Expand Down
24 changes: 9 additions & 15 deletions internal/app/machined/pkg/controllers/network/link_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
networkadapter "github.com/talos-systems/talos/internal/app/machined/pkg/adapters/network"
netctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/network"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/nethelpers"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)
Expand Down Expand Up @@ -800,25 +801,18 @@ func TestSortBonds(t *testing.T) {
}

func toResources(slice []network.LinkSpecSpec) []resource.Resource {
result := make([]resource.Resource, 0, len(slice))

for _, elem := range slice {
return slices.Map(slice, func(spec network.LinkSpecSpec) resource.Resource {
link := network.NewLinkSpec(network.NamespaceName, "bar")
*link.TypedSpec() = elem

result = append(result, link)
}
*link.TypedSpec() = spec

return result
return link
})
}

func toSpecs(slice []resource.Resource) []network.LinkSpecSpec {
result := make([]network.LinkSpecSpec, 0, len(slice))

for _, elem := range slice {
v := elem.Spec().(*network.LinkSpecSpec) //nolint:errcheck
result = append(result, *v)
}
return slices.Map(slice, func(r resource.Resource) network.LinkSpecSpec {
v := r.Spec().(*network.LinkSpecSpec) //nolint:errcheck

return result
return *v
})
}
18 changes: 9 additions & 9 deletions internal/app/machined/pkg/controllers/network/operator/dhcp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"inet.af/netaddr"

"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/nethelpers"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)
Expand Down Expand Up @@ -246,15 +247,15 @@ func (d *DHCP4) parseAck(ack *dhcpv4.DHCPv4) {
}

if len(ack.DNS()) > 0 {
dns := make([]netaddr.IP, len(ack.DNS()))
convertIP := func(ip net.IP) netaddr.IP {
result, _ := netaddr.FromStdIP(ip)

for i := range dns {
dns[i], _ = netaddr.FromStdIP(ack.DNS()[i])
return result
}

d.resolvers = []network.ResolverSpecSpec{
{
DNSServers: dns,
DNSServers: slices.Map(ack.DNS(), convertIP),
ConfigLayer: network.ConfigOperator,
},
}
Expand Down Expand Up @@ -283,16 +284,15 @@ func (d *DHCP4) parseAck(ack *dhcpv4.DHCPv4) {
}

if len(ack.NTPServers()) > 0 {
ntp := make([]string, len(ack.NTPServers()))
convertIP := func(ip net.IP) string {
result, _ := netaddr.FromStdIP(ip)

for i := range ntp {
ip, _ := netaddr.FromStdIP(ack.NTPServers()[i])
ntp[i] = ip.String()
return result.String()
}

d.timeservers = []network.TimeServerSpecSpec{
{
NTPServers: ntp,
NTPServers: slices.Map(ack.NTPServers(), convertIP),
ConfigLayer: network.ConfigOperator,
},
}
Expand Down
18 changes: 9 additions & 9 deletions internal/app/machined/pkg/controllers/network/operator/dhcp6.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"golang.org/x/sys/unix"
"inet.af/netaddr"

"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/nethelpers"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)
Expand Down Expand Up @@ -172,15 +173,15 @@ func (d *DHCP6) parseReply(reply *dhcpv6.Message) (leaseTime time.Duration) {
}

if len(reply.Options.DNS()) > 0 {
dns := make([]netaddr.IP, len(reply.Options.DNS()))
convertIP := func(ip net.IP) netaddr.IP {
result, _ := netaddr.FromStdIP(ip)

for i := range dns {
dns[i], _ = netaddr.FromStdIP(reply.Options.DNS()[i])
return result
}

d.resolvers = []network.ResolverSpecSpec{
{
DNSServers: dns,
DNSServers: slices.Map(reply.Options.DNS(), convertIP),
ConfigLayer: network.ConfigOperator,
},
}
Expand All @@ -201,16 +202,15 @@ func (d *DHCP6) parseReply(reply *dhcpv6.Message) (leaseTime time.Duration) {
}

if len(reply.Options.NTPServers()) > 0 {
ntp := make([]string, len(reply.Options.NTPServers()))
convertIP := func(ip net.IP) string {
result, _ := netaddr.FromStdIP(ip)

for i := range ntp {
ip, _ := netaddr.FromStdIP(reply.Options.NTPServers()[i])
ntp[i] = ip.String()
return result.String()
}

d.timeservers = []network.TimeServerSpecSpec{
{
NTPServers: ntp,
NTPServers: slices.Map(reply.Options.NTPServers(), convertIP),
ConfigLayer: network.ConfigOperator,
},
}
Expand Down
11 changes: 4 additions & 7 deletions internal/app/machined/pkg/controllers/network/operator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.uber.org/zap"

talosconfig "github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)

Expand Down Expand Up @@ -100,13 +101,9 @@ func (ctrl *OperatorConfigController) Run(ctx context.Context, r controller.Runt
specErrors *multierror.Error
)

devices := make([]talosconfig.Device, len(items.Items))

for i, item := range items.Items {
device := item.(*network.DeviceConfigSpec).TypedSpec().Device

devices[i] = device
}
devices := slices.Map(items.Items, func(item resource.Resource) talosconfig.Device {
return item.(*network.DeviceConfigSpec).TypedSpec().Device
})

// operators from the config
if len(devices) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/talos-systems/talos/internal/app/machined/pkg/controllers/network/operator/vip"
talosconfig "github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)

Expand Down Expand Up @@ -72,13 +73,9 @@ func (ctrl *OperatorVIPConfigController) Run(ctx context.Context, r controller.R
}
}

devices := make([]talosconfig.Device, len(items.Items))

for i, item := range items.Items {
device := item.(*network.DeviceConfigSpec).TypedSpec().Device

devices[i] = device
}
devices := slices.Map(items.Items, func(item resource.Resource) talosconfig.Device {
return item.(*network.DeviceConfigSpec).TypedSpec().Device
})

ignoredInterfaces := map[string]struct{}{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/state"
"go.uber.org/zap"
"inet.af/netaddr"

"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/machinery/resources/network"
)

Expand Down Expand Up @@ -87,12 +89,7 @@ func (ctrl *ResolverSpecController) Run(ctx context.Context, r controller.Runtim
return fmt.Errorf("error removing finalizer: %w", err)
}
case resource.PhaseRunning:
resolvers := make([]string, len(spec.TypedSpec().DNSServers))

for i := range resolvers {
resolvers[i] = spec.TypedSpec().DNSServers[i].String()
}

resolvers := slices.Map(spec.TypedSpec().DNSServers, netaddr.IP.String)
logger.Info("setting resolvers", zap.Strings("resolvers", resolvers))

if err = r.Modify(ctx, network.NewResolverStatus(network.NamespaceName, spec.Metadata().ID()), func(r resource.Resource) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/talos-systems/talos/pkg/logging"
talosconfig "github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
configresource "github.com/talos-systems/talos/pkg/machinery/resources/config"
)

Expand Down Expand Up @@ -320,10 +321,7 @@ func (ctrl *Controller) updateLoggingConfig(ctx context.Context, cfg talosconfig
var prevSenders []runtime.LogSender

if len(loggingEndpoints) > 0 {
senders := make([]runtime.LogSender, len(loggingEndpoints))
for i, u := range loggingEndpoints {
senders[i] = runtimelogging.NewJSONLines(u)
}
senders := slices.Map(loggingEndpoints, runtimelogging.NewJSONLines)

ctrl.logger.Info("enabling JSON logging")
prevSenders = ctrl.loggingManager.SetSenders(senders)
Expand Down
Loading

0 comments on commit 4dbbf4a

Please sign in to comment.