-
Notifications
You must be signed in to change notification settings - Fork 852
/
main.go
135 lines (119 loc) · 5.08 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"sort"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
corecloudprovider "github.com/aws/karpenter-core/pkg/cloudprovider"
coreoperator "github.com/aws/karpenter-core/pkg/operator"
nodepoolutil "github.com/aws/karpenter-core/pkg/utils/nodepool"
"github.com/aws/karpenter/pkg/apis/settings"
"github.com/aws/karpenter/pkg/apis/v1alpha1"
"github.com/aws/karpenter/pkg/cloudprovider"
"github.com/aws/karpenter/pkg/operator"
)
var clusterName string
var outFile string
var overheadPercent float64
func init() {
flag.StringVar(&clusterName, "cluster-name", "", "cluster name to use when passing subnets into GetInstanceTypes()")
flag.StringVar(&outFile, "out-file", "allocatable-diff.csv", "file to output the generated data")
flag.Float64Var(&overheadPercent, "overhead-percent", 0, "overhead percentage to use for calculations")
flag.Parse()
}
func main() {
if clusterName == "" {
log.Fatalf("cluster name cannot be empty")
}
restConfig := config.GetConfigOrDie()
kubeClient := lo.Must(client.New(restConfig, client.Options{}))
ctx := context.Background()
ctx = settings.ToContext(ctx, &settings.Settings{ClusterName: clusterName, IsolatedVPC: true, VMMemoryOverheadPercent: overheadPercent})
file := lo.Must(os.OpenFile(outFile, os.O_RDWR|os.O_CREATE, 0777))
defer file.Close()
w := csv.NewWriter(file)
defer w.Flush()
nodeList := &v1.NodeList{}
lo.Must0(kubeClient.List(ctx, nodeList))
ctx, op := operator.NewOperator(ctx, &coreoperator.Operator{
Manager: lo.Must(manager.New(restConfig, manager.Options{})),
KubernetesInterface: kubernetes.NewForConfigOrDie(restConfig),
})
cloudProvider := cloudprovider.New(
op.InstanceTypesProvider,
op.InstanceProvider,
op.EventRecorder,
op.GetClient(),
op.AMIProvider,
op.SecurityGroupProvider,
op.SubnetProvider,
)
raw := &runtime.RawExtension{}
lo.Must0(raw.UnmarshalJSON(lo.Must(json.Marshal(&v1alpha1.AWS{
SubnetSelector: map[string]string{
"karpenter.sh/discovery": clusterName,
},
}))))
instanceTypes := lo.Must(cloudProvider.GetInstanceTypes(ctx, nodepoolutil.New(&v1alpha5.Provisioner{
Spec: v1alpha5.ProvisionerSpec{
Provider: raw,
},
})))
// Write the header information into the CSV
lo.Must0(w.Write([]string{"Instance Type", "Expected Capacity", "", "", "Expected Allocatable", "", "", "Actual Capacity", "", "", "Actual Allocatable", ""}))
lo.Must0(w.Write([]string{"", "Memory (Mi)", "CPU (m)", "Storage (Mi)", "Memory (Mi)", "CPU (m)", "Storage (Mi)", "Memory (Mi)", "CPU (m)", "Storage (Mi)", "Memory (Mi)", "CPU (m)", "Storage (Mi)"}))
nodeList.Items = lo.Filter(nodeList.Items, func(n v1.Node, _ int) bool {
return n.Labels["karpenter.sh/provisioner-name"] != "" && n.Status.Allocatable.Memory().Value() != 0
})
sort.Slice(nodeList.Items, func(i, j int) bool {
return nodeList.Items[i].Labels[v1.LabelInstanceTypeStable] < nodeList.Items[j].Labels[v1.LabelInstanceTypeStable]
})
for _, node := range nodeList.Items {
instanceType, ok := lo.Find(instanceTypes, func(i *corecloudprovider.InstanceType) bool {
return i.Name == node.Labels[v1.LabelInstanceTypeStable]
})
if !ok {
log.Fatalf("retrieving instance type for instance %s", node.Labels[v1.LabelInstanceTypeStable])
}
allocatable := instanceType.Allocatable()
// Write the details of the expected instance and the actual instance into a CSV line format
lo.Must0(w.Write([]string{
instanceType.Name,
fmt.Sprintf("%d", instanceType.Capacity.Memory().Value()/1024/1024),
fmt.Sprintf("%d", instanceType.Capacity.Cpu().MilliValue()),
fmt.Sprintf("%d", instanceType.Capacity.StorageEphemeral().Value()/1024/1024),
fmt.Sprintf("%d", allocatable.Memory().Value()/1024/1024),
fmt.Sprintf("%d", allocatable.Cpu().MilliValue()),
fmt.Sprintf("%d", allocatable.StorageEphemeral().Value()/1024/1024),
fmt.Sprintf("%d", node.Status.Capacity.Memory().Value()/1024/1024),
fmt.Sprintf("%d", node.Status.Capacity.Cpu().MilliValue()),
fmt.Sprintf("%d", node.Status.Capacity.StorageEphemeral().Value()/1024/1024),
fmt.Sprintf("%d", node.Status.Allocatable.Memory().Value()/1024/1024),
fmt.Sprintf("%d", node.Status.Allocatable.Cpu().MilliValue()),
fmt.Sprintf("%d", node.Status.Allocatable.StorageEphemeral().Value()/1024/1024),
}))
}
}