-
Notifications
You must be signed in to change notification settings - Fork 828
/
instancetypes_gen_docs.go
196 lines (175 loc) · 5.82 KB
/
instancetypes_gen_docs.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
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 (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/aws/karpenter-core/pkg/apis/provisioning/v1alpha5"
"github.com/aws/karpenter/pkg/cloudproviders/aws/apis/v1alpha1"
awscloudprovider "github.com/aws/karpenter/pkg/cloudproviders/aws/cloudprovider"
"github.com/aws/karpenter/pkg/cloudproviders/common/cloudprovider"
"github.com/aws/karpenter/pkg/operator"
"github.com/aws/karpenter/pkg/utils/resources"
)
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatalf("Usage: %s path/to/markdown.md", os.Args[0])
}
os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
os.Setenv("AWS_REGION", "us-east-1")
os.Setenv("CLUSTER_NAME", "docs-gen")
os.Setenv("CLUSTER_ENDPOINT", "https://docs-gen.aws")
os.Setenv("AWS_ISOLATED_VPC", "true") // disable pricing lookup
opts := operator.NewOptions()
opts = opts.MustParse()
ctx := operator.WithOptions(context.Background(), *opts)
cp := awscloudprovider.NewCloudProvider(ctx, cloudprovider.Options{})
provider := v1alpha1.AWS{SubnetSelector: map[string]string{
"*": "*",
}}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(provider); err != nil {
log.Fatalf("encoding provider, %s", err)
}
prov := &v1alpha5.Provisioner{
Spec: v1alpha5.ProvisionerSpec{
Requirements: []v1.NodeSelectorRequirement{
{
Key: v1.LabelInstanceTypeStable,
Operator: v1.NodeSelectorOpExists,
},
},
Provider: &v1alpha5.Provider{
Raw: buf.Bytes(),
},
},
}
instanceTypes, err := cp.GetInstanceTypes(ctx, prov)
if err != nil {
log.Fatalf("listing instance types, %s", err)
}
outputFileName := flag.Arg(0)
f, err := os.Create(outputFileName)
if err != nil {
log.Fatalf("error creating output file %s, %s", outputFileName, err)
}
log.Println("writing output to", outputFileName)
fmt.Fprintf(f, `---
title: "Instance Types"
linkTitle: "Instance Types"
weight: 100
description: >
Evaluate Instance Type Resources
---
`)
fmt.Fprintln(f, "<!-- this document is generated from hack/docs/instancetypes_gen_docs.go -->")
fmt.Fprintln(f, `AWS instance types offer varying resources and can be selected by labels. The values provided
below are the resources available with some assumptions and after the instance overhead has been subtracted:
- `+"`blockDeviceMappings` are not configured"+`
- `+"`aws-eni-limited-pod-density` is assumed to be `true`"+`
- `+"`amiFamily` is set to the default of `AL2`")
// generate a map of family -> instance types along with some other sorted lists. The sorted lists ensure we
// generate consistent docs every run.
families := map[string][]cloudprovider.InstanceType{}
labelNameMap := sets.String{}
resourceNameMap := sets.String{}
for _, it := range instanceTypes {
familyName := strings.Split(it.Name(), ".")[0]
families[familyName] = append(families[familyName], it)
for labelName := range it.Requirements() {
labelNameMap.Insert(labelName)
}
for resourceName := range it.Resources() {
resourceNameMap.Insert(string(resourceName))
}
}
familyNames := lo.Keys(families)
sort.Strings(familyNames)
// we don't want to show a few labels that will vary amongst regions
delete(labelNameMap, v1.LabelTopologyZone)
delete(labelNameMap, v1alpha5.LabelCapacityType)
labelNames := lo.Keys(labelNameMap)
sort.Strings(labelNames)
resourceNames := lo.Keys(resourceNameMap)
sort.Strings(resourceNames)
for _, familyName := range familyNames {
fmt.Fprintf(f, "## %s Family\n", familyName)
// sort the instance types within the family, we sort by CPU and memory which should be a pretty good ordering
sort.Slice(families[familyName], func(a, b int) bool {
lhs := families[familyName][a]
rhs := families[familyName][b]
lhsResources := lhs.Resources()
rhsResources := rhs.Resources()
if cpuCmp := resources.Cmp(*lhsResources.Cpu(), *rhsResources.Cpu()); cpuCmp != 0 {
return cpuCmp < 0
}
if memCmp := resources.Cmp(*lhsResources.Memory(), *rhsResources.Memory()); memCmp != 0 {
return memCmp < 0
}
return lhs.Name() < rhs.Name()
})
for _, it := range families[familyName] {
fmt.Fprintf(f, "### `%s`\n", it.Name())
minusOverhead := v1.ResourceList{}
for k, v := range it.Resources() {
if v.IsZero() {
continue
}
cp := v.DeepCopy()
cp.Sub(it.Overhead()[k])
minusOverhead[k] = cp
}
fmt.Fprintln(f, "#### Labels")
fmt.Fprintln(f, " | Label | Value |")
fmt.Fprintln(f, " |--|--|")
for _, label := range labelNames {
req, ok := it.Requirements()[label]
if !ok {
continue
}
if req.Key == v1.LabelTopologyRegion {
continue
}
if len(req.Values()) == 1 {
fmt.Fprintf(f, " |%s|%s|\n", label, req.Values()[0])
}
}
fmt.Fprintln(f, "#### Resources")
fmt.Fprintln(f, " | Resource | Quantity |")
fmt.Fprintln(f, " |--|--|")
for _, resourceName := range resourceNames {
quantity := minusOverhead[v1.ResourceName(resourceName)]
if quantity.IsZero() {
continue
}
if v1.ResourceName(resourceName) == v1.ResourceEphemeralStorage {
i64, _ := quantity.AsInt64()
quantity = *resource.NewQuantity(i64, resource.BinarySI)
}
fmt.Fprintf(f, " |%s|%s|\n", resourceName, quantity.String())
}
}
}
}