-
Notifications
You must be signed in to change notification settings - Fork 9
/
systeminfo.go
86 lines (68 loc) · 2.44 KB
/
systeminfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubernetes
import (
"context"
"fmt"
"sort"
"strings"
"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/uniquestrings"
"namespacelabs.dev/foundation/std/tasks"
)
func computeSystemInfo(ctx context.Context, cli *kubernetes.Clientset) (*kubedef.SystemInfo, error) {
var platforms uniquestrings.List
nodes, err := cli.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fnerrors.InvocationError("kubernetes", "unable to list nodes: %w", err)
}
sysInfo := &kubedef.SystemInfo{}
regions := map[string]int32{}
zones := map[string]int32{}
for _, n := range nodes.Items {
platforms.Add(fmt.Sprintf("%s/%s", n.Status.NodeInfo.OperatingSystem, n.Status.NodeInfo.Architecture))
if nodeType, ok := n.Labels["node.kubernetes.io/instance-type"]; ok && nodeType == "k3s" {
sysInfo.DetectedDistribution = "k3s"
}
if clusterName, ok := n.Labels["alpha.eksctl.io/cluster-name"]; ok {
sysInfo.DetectedDistribution = "eks"
sysInfo.EksClusterName = clusterName
}
if region, ok := n.Labels["topology.kubernetes.io/region"]; ok {
regions[region]++
}
if zone, ok := n.Labels["topology.kubernetes.io/zone"]; ok {
zones[zone]++
}
}
for region, count := range regions {
sysInfo.RegionDistribution = append(sysInfo.RegionDistribution, &kubedef.NodeDistribution{
Location: region,
Count: count,
})
sysInfo.Regions = append(sysInfo.Regions, region)
}
for zone, count := range zones {
sysInfo.ZoneDistribution = append(sysInfo.ZoneDistribution, &kubedef.NodeDistribution{
Location: zone,
Count: count,
})
sysInfo.Zones = append(sysInfo.Zones, zone)
}
sort.Strings(sysInfo.Regions)
sort.Strings(sysInfo.Zones)
less := func(a, b *kubedef.NodeDistribution) bool {
return strings.Compare(a.Location, b.Location) < 0
}
slices.SortFunc(sysInfo.RegionDistribution, less)
slices.SortFunc(sysInfo.ZoneDistribution, less)
sysInfo.NodePlatform = platforms.Strings()
sort.Strings(sysInfo.NodePlatform)
tasks.Attachments(ctx).AddResult("platforms", sysInfo.NodePlatform)
return sysInfo, nil
}