-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add tool to generate instance type docs #1946
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5" | ||
"github.com/aws/karpenter/pkg/cloudprovider" | ||
"github.com/aws/karpenter/pkg/cloudprovider/aws" | ||
"github.com/aws/karpenter/pkg/cloudprovider/aws/apis/v1alpha1" | ||
"github.com/aws/karpenter/pkg/utils/resources" | ||
"github.com/samber/lo" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"log" | ||
"os" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if flag.NArg() != 2 { | ||
log.Printf("Usage: %s subnet-discovery-tag-value path/to/markdown.md", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
|
||
os.Setenv("AWS_SDK_LOAD_CONFIG", "true") | ||
ctx := context.Background() | ||
|
||
cp := aws.NewCloudProvider(ctx, cloudprovider.Options{ | ||
ClientSet: nil, | ||
KubeClient: nil, | ||
}) | ||
provider := v1alpha1.AWS{SubnetSelector: map[string]string{ | ||
"karpenter.sh/discovery": flag.Arg(0), | ||
}} | ||
var buf bytes.Buffer | ||
enc := json.NewEncoder(&buf) | ||
if err := enc.Encode(provider); err != nil { | ||
log.Fatalf("encoding provider, %s", err) | ||
} | ||
instanceTypes, err := cp.GetInstanceTypes(ctx, &v1alpha5.Provider{ | ||
Raw: buf.Bytes(), | ||
Object: nil, | ||
}) | ||
if err != nil { | ||
log.Fatalf("listing instance types, %s", err) | ||
} | ||
|
||
outputFileName := flag.Arg(1) | ||
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 the zone label that was applied based on our credentials | ||
delete(labelNameMap, v1.LabelTopologyZone) | ||
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.Values().Len() == 1 { | ||
fmt.Fprintf(f, " |%s|%s|\n", label, req.Values().List()[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()) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth calling out the the resources have overhead taken out. Although it looks like ephemeral storage does not and maybe not CPU either? https://github.com/aws/karpenter/blob/205c4db5743f1f6824253e92304a5df2f875c90c/pkg/cloudprovider/aws/amifamily/al2.go#L94
Might also be worth calling out that pods are assuming the
aws-eni-limited-pod-density = true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM. Good catch on overhead! I calculated the value with overhead subtracted and then ignored it 😆