Skip to content

Commit

Permalink
chore: add generic methods and use them
Browse files Browse the repository at this point in the history
Things like ToSet, Keys etc...

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Jun 6, 2022
1 parent 27f8e50 commit 4257b51
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 251 deletions.
17 changes: 5 additions & 12 deletions cmd/talosctl/cmd/mgmt/cluster/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cluster
import (
"context"
"fmt"
stdnet "net"
"os"
"sort"
"strings"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/talos-systems/net"

"github.com/talos-systems/talos/pkg/cli"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/provision"
"github.com/talos-systems/talos/pkg/provision/providers"
)
Expand Down Expand Up @@ -54,17 +56,11 @@ func showCluster(cluster provision.Cluster) error {
fmt.Fprintf(w, "NAME\t%s\n", cluster.Info().ClusterName)
fmt.Fprintf(w, "NETWORK NAME\t%s\n", cluster.Info().Network.Name)

cidrs := make([]string, len(cluster.Info().Network.CIDRs))
for i := range cidrs {
cidrs[i] = net.FormatCIDR(cluster.Info().Network.CIDRs[i].IP, cluster.Info().Network.CIDRs[i])
}
cidrs := slices.Map(cluster.Info().Network.CIDRs, func(v stdnet.IPNet) string { return net.FormatCIDR(v.IP, v) })

fmt.Fprintf(w, "NETWORK CIDR\t%s\n", strings.Join(cidrs, ","))

gateways := make([]string, len(cluster.Info().Network.GatewayAddrs))
for i := range gateways {
gateways[i] = cluster.Info().Network.GatewayAddrs[i].String()
}
gateways := slices.Map(cluster.Info().Network.GatewayAddrs, stdnet.IP.String)

fmt.Fprintf(w, "NETWORK GATEWAY\t%s\n", strings.Join(gateways, ","))
fmt.Fprintf(w, "NETWORK MTU\t%d\n", cluster.Info().Network.MTU)
Expand Down Expand Up @@ -98,10 +94,7 @@ func showCluster(cluster provision.Cluster) error {
disk = humanize.Bytes(node.DiskSize)
}

ips := make([]string, len(node.IPs))
for i := range ips {
ips[i] = node.IPs[i].String()
}
ips := slices.Map(node.IPs, stdnet.IP.String)

fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
node.Name,
Expand Down
8 changes: 4 additions & 4 deletions cmd/talosctl/cmd/mgmt/loadbalancer_launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/talos-systems/go-loadbalancer/loadbalancer"

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

var loadbalancerLaunchCmdFlags struct {
Expand All @@ -30,10 +31,9 @@ var loadbalancerLaunchCmd = &cobra.Command{
var lb loadbalancer.TCP

for _, port := range []int{constants.DefaultControlPlanePort} {
upstreams := make([]string, len(loadbalancerLaunchCmdFlags.upstreams))
for i := range upstreams {
upstreams[i] = fmt.Sprintf("%s:%d", loadbalancerLaunchCmdFlags.upstreams[i], port)
}
upstreams := slices.Map(loadbalancerLaunchCmdFlags.upstreams, func(upstream string) string {
return fmt.Sprintf("%s:%d", upstream, port)
})

if err := lb.AddRoute(fmt.Sprintf("%s:%d", loadbalancerLaunchCmdFlags.addr, port), upstreams); err != nil {
return err
Expand Down
14 changes: 3 additions & 11 deletions cmd/talosctl/cmd/talos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
machineapi "github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/client"
clientconfig "github.com/talos-systems/talos/pkg/machinery/client/config"
"github.com/talos-systems/talos/pkg/machinery/generic/maps"
"github.com/talos-systems/talos/pkg/machinery/role"
)

Expand Down Expand Up @@ -203,12 +204,7 @@ var configGetContextsCmd = &cobra.Command{
return fmt.Errorf("error reading config: %w", err)
}

keys := make([]string, len(c.Contexts))
i := 0
for key := range c.Contexts {
keys[i] = key
i++
}
keys := maps.Keys(c.Contexts)
sort.Strings(keys)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
Expand Down Expand Up @@ -418,11 +414,7 @@ func CompleteConfigContext(cmd *cobra.Command, args []string, toComplete string)
return nil, cobra.ShellCompDirectiveError
}

contextnames := make([]string, 0, len(c.Contexts))
for contextname := range c.Contexts {
contextnames = append(contextnames, contextname)
}

contextnames := maps.Keys(c.Contexts)
sort.Strings(contextnames)

return contextnames, cobra.ShellCompDirectiveNoFileComp
Expand Down
7 changes: 2 additions & 5 deletions cmd/talosctl/cmd/talos/dashboard/components/graphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gizak/termui/v3/widgets"

"github.com/talos-systems/talos/cmd/talosctl/cmd/talos/dashboard/data"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
)

// BaseGraph represents the widget with some usage graph.
Expand All @@ -25,12 +26,8 @@ func NewBaseGraph(title string, labels []string) *BaseGraph {
widget.Title = title
widget.DataLabels = labels
widget.ShowAxes = false
widget.Data = make([][]float64, len(labels))

// TODO: looks to be a bug as it requires at least 2 points
for i := range widget.Data {
widget.Data[i] = []float64{0, 0}
}
widget.Data = slices.Map(labels, func(label string) []float64 { return []float64{0, 0} })

return widget
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/talosctl/cmd/talos/dashboard/components/nodetabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gizak/termui/v3/widgets"

"github.com/talos-systems/talos/cmd/talosctl/cmd/talos/dashboard/data"
"github.com/talos-systems/talos/pkg/machinery/generic/maps"
)

// NodeTabs represents the bottom bar with node list.
Expand All @@ -28,12 +29,7 @@ func NewNodeTabs() *NodeTabs {

// Update implements the DataWidget interface.
func (widget *NodeTabs) Update(node string, data *data.Data) {
nodes := make([]string, 0, len(data.Nodes))

for node := range data.Nodes {
nodes = append(nodes, node)
}

nodes := maps.Keys(data.Nodes)
sort.Strings(nodes)

widget.TabNames = nodes
Expand Down
21 changes: 10 additions & 11 deletions cmd/talosctl/cmd/talos/dashboard/data/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package data

import "github.com/talos-systems/talos/pkg/machinery/api/machine"
import (
"github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
)

// Node represents data gathered from a single node.
type Node struct {
Expand Down Expand Up @@ -186,15 +189,11 @@ func (node *Node) UpdateDiff(old *Node) {
Total: diskStatDiff(old.DiskStats.GetTotal(), node.DiskStats.GetTotal()),
}

node.ProcsDiff = make(map[int32]*machine.ProcessInfo)
index := slices.ToMap(old.Processes.GetProcesses(), func(proc *machine.ProcessInfo) (int32, *machine.ProcessInfo) {
return proc.Pid, proc
})

index := make(map[int32]*machine.ProcessInfo)

for _, proc := range old.Processes.GetProcesses() {
index[proc.Pid] = proc
}

for _, proc := range node.Processes.GetProcesses() {
node.ProcsDiff[proc.Pid] = procDiff(index[proc.Pid], proc)
}
node.ProcsDiff = slices.ToMap(node.Processes.GetProcesses(), func(proc *machine.ProcessInfo) (int32, *machine.ProcessInfo) {
return proc.Pid, procDiff(index[proc.Pid], proc)
})
}
9 changes: 2 additions & 7 deletions cmd/talosctl/cmd/talos/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/talos-systems/talos/pkg/machinery/client"
clientconfig "github.com/talos-systems/talos/pkg/machinery/client/config"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/machinery/generic/maps"
)

var kubernetes bool
Expand Down Expand Up @@ -163,13 +164,7 @@ func completePathFromNode(inputPath string) []string {

paths = getPathFromNode(pathToSearch, inputPath)

result := make([]string, 0, len(paths))

for k := range paths {
result = append(result, k)
}

return result
return maps.Keys(paths)
}

//nolint:gocyclo
Expand Down
7 changes: 2 additions & 5 deletions cmd/talosctl/pkg/talos/helpers/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/talos-systems/talos/pkg/cli"
"github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/generic/maps"
)

// InteractiveMode fake mode value for the interactive config mode.
Expand Down Expand Up @@ -71,11 +72,7 @@ func (m *Mode) Set(value string) error {

// Type implements Flag interface.
func (m *Mode) Type() string {
options := make([]string, 0, len(m.options))
for s := range m.options {
options = append(options, s)
}

options := maps.Keys(m.options)
sort.Strings(options)

return strings.Join(options, ", ")
Expand Down
10 changes: 3 additions & 7 deletions internal/app/apid/pkg/director/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

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

// Router wraps grpc-proxy StreamDirector.
Expand Down Expand Up @@ -79,13 +81,7 @@ func (r *Router) aggregateDirector(targets []string) (proxy.Mode, []proxy.Backen

// StreamedDetector implements proxy.StreamedDetector.
func (r *Router) StreamedDetector(fullMethodName string) bool {
for _, re := range r.streamedMatchers {
if re.MatchString(fullMethodName) {
return true
}
}

return false
return slices.Contains(r.streamedMatchers, func(regex *regexp.Regexp) bool { return regex.MatchString(fullMethodName) })
}

// RegisterStreamedRegex register regex for streamed method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"google.golang.org/protobuf/types/known/emptypb"

"github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/generic/maps"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
)

// Hostname implements the machine.MachineServer interface.
Expand Down Expand Up @@ -88,13 +90,7 @@ func (s *Server) SystemStat(ctx context.Context, in *emptypb.Empty) (*machine.Sy
}

translateListOfCPUStat := func(in []procfs.CPUStat) []*machine.CPUStat {
res := make([]*machine.CPUStat, len(in))

for i := range in {
res[i] = translateCPUStat(in[i])
}

return res
return slices.Map(in, translateCPUStat)
}

translateSoftIRQ := func(in procfs.SoftIRQStat) *machine.SoftIRQStat {
Expand Down Expand Up @@ -175,17 +171,11 @@ func (s *Server) CPUInfo(ctx context.Context, in *emptypb.Empty) (*machine.CPUIn
}
}

resp := machine.CPUsInfo{
CpuInfo: make([]*machine.CPUInfo, len(info)),
}

for i := range info {
resp.CpuInfo[i] = translateCPUInfo(info[i])
}

reply := &machine.CPUInfoResponse{
Messages: []*machine.CPUsInfo{
&resp,
{
CpuInfo: slices.Map(info, translateCPUInfo),
},
},
}

Expand Down Expand Up @@ -226,21 +216,12 @@ func (s *Server) NetworkDeviceStats(ctx context.Context, in *emptypb.Empty) (*ma
}
}

resp := machine.NetworkDeviceStats{
Devices: make([]*machine.NetDev, len(info)),
Total: translateNetDevLine(info.Total()),
}

i := 0

for _, line := range info {
resp.Devices[i] = translateNetDevLine(line)
i++
}

reply := &machine.NetworkDeviceStatsResponse{
Messages: []*machine.NetworkDeviceStats{
&resp,
{
Devices: maps.ValuesFunc(info, translateNetDevLine),
Total: translateNetDevLine(info.Total()),
},
},
}

Expand Down
Loading

0 comments on commit 4257b51

Please sign in to comment.