Skip to content

Commit

Permalink
Update resources to allocatable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Aug 31, 2022
1 parent 4283b53 commit 835a11c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 30 deletions.
6 changes: 3 additions & 3 deletions charts/karpenter/crds/karpenter.sh_instancetypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ spec:
for specifying custom values on a per-instance type basis for scheduling
and launching of nodes
properties:
resources:
allocatable:
additionalProperties:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
description: Resources contains a map of allocatable resources for
description: Allocatable contains a map of allocatable resources for
the instance type used by the scheduler. This resource list can
contain known resources (cpu, memory, etc.) or it may also contain
unknown custom device resources for custom device plugins
unknown custom device resources for custom device plugins.
type: object
type: object
type: object
Expand Down
2 changes: 1 addition & 1 deletion examples/instancetypes/custom-requests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ kind: InstanceType
metadata:
name: c5.large
spec:
resources:
allocatable:
hardware.vendor.com/resource: 2
hardware.vendor.com/other-resource: 10
11 changes: 11 additions & 0 deletions examples/instancetypes/memory-overhead.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This example instance type override will
# override allocatable capacity for c5.large instance types
# provisioned by Karpenter

apiVersion: karpenter.sh/v1alpha1
kind: InstanceType
metadata:
name: c5.large
spec:
allocatable:
memory: 3876Mi
6 changes: 3 additions & 3 deletions pkg/apis/instancetype/v1alpha1/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
// for specifying custom values on a per-instance type basis for scheduling and
// launching of nodes
type InstanceTypeSpec struct {
// Resources contains a map of allocatable resources for the instance type
// Allocatable contains a map of allocatable resources for the instance type
// used by the scheduler. This resource list can contain known resources (cpu, memory, etc.)
// or it may also contain unknown custom device resources for custom device plugins
// or it may also contain unknown custom device resources for custom device plugins.
// +optional
Resources v1.ResourceList `json:"resources,omitempty"`
Allocatable v1.ResourceList `json:"allocatable,omitempty"`
}

// InstanceType is the Schema for the InstanceType API
Expand Down
9 changes: 1 addition & 8 deletions pkg/apis/instancetype/v1alpha1/instancetype_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ package v1alpha1
import (
"context"
"fmt"
"strings"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/validation"
"knative.dev/pkg/apis"

"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5"
"github.com/aws/karpenter/pkg/utils/resources"
)

func (in *InstanceType) Validate(_ context.Context) (errs *apis.FieldError) {
Expand Down Expand Up @@ -55,13 +52,9 @@ func (in *InstanceTypeSpec) validate() (errs *apis.FieldError) {
// are part of the well-known requirements set
func (in *InstanceTypeSpec) validateResources() (errs *apis.FieldError) {
fmt.Println(v1alpha5.WellKnownLabels)
for k := range in.Resources {
for k := range in.Allocatable {
if v1alpha5.WellKnownLabels.Has(k.String()) {
errs = errs.Also(apis.ErrInvalidValue("cannot be from the set of well-known requirements", fmt.Sprintf("resources[%s]", k)))
} else if resources.WellKnownResourceNames.Has(k.String()) {
errs = errs.Also(apis.ErrInvalidValue("cannot be from the set of well-known resource names", fmt.Sprintf("resources[%s]", k)))
} else if strings.HasPrefix(k.String(), v1.ResourceHugePagesPrefix) || strings.HasPrefix(k.String(), v1.ResourceAttachableVolumesPrefix) {
errs = errs.Also(apis.ErrInvalidValue("cannot use a resource name from a set of well-known prefixes", fmt.Sprintf("resources[%s]", k)))
}
}
return errs
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/instancetype/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pkg/cloudprovider/aws/instancetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
instancetypev1alpha1 "github.com/aws/karpenter/pkg/apis/instancetype/v1alpha1"
"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5"
"github.com/aws/karpenter/pkg/scheduling"
"github.com/aws/karpenter/pkg/utils/resources"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -290,9 +291,11 @@ func UnavailableOfferingsCacheKey(instanceType string, zone string, capacityType
// pulled from the EC2 APIs. Resources should already be validated when they get here, so we will automatically
// add them to the instance type resources and its requirements
func mergeInstanceTypeOverrides(it *InstanceType, instanceType instancetypev1alpha1.InstanceType) *InstanceType {
for name, quantity := range instanceType.Spec.Resources {
for name, quantity := range instanceType.Spec.Allocatable {
it.resources[name] = quantity
it.requirements.Upsert(scheduling.NewRequirement(name.String(), v1.NodeSelectorOpIn, fmt.Sprint(quantity.Value())))
if !resources.WellKnownResourceNames.Has(name.String()) && !v1alpha5.WellKnownLabels.Has(name.String()) {
it.requirements.Add(scheduling.NewRequirement(name.String(), v1.NodeSelectorOpIn, fmt.Sprint(quantity.Value())))
}
}
return it
}
5 changes: 2 additions & 3 deletions pkg/cloudprovider/fake/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"github.com/samber/lo"

"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5"
"github.com/aws/karpenter/pkg/cloudprovider/aws/apis/v1alpha1"
"github.com/aws/karpenter/pkg/scheduling"
"github.com/aws/karpenter/pkg/utils/resources"

"github.com/aws/karpenter/pkg/cloudprovider/aws/apis/v1alpha1"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
utilsets "k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -218,7 +217,7 @@ func (i *InstanceType) Requirements() scheduling.Requirements {
// Add custom resources as requirements values on the node
for k, v := range i.options.Resources {
if !resources.WellKnownResourceNames.Has(k.String()) && !v1alpha5.WellKnownLabels.Has(k.String()) {
requirements.Upsert(scheduling.NewRequirement(k.String(), v1.NodeSelectorOpIn, fmt.Sprint(v.Value())))
requirements.Add(scheduling.NewRequirement(k.String(), v1.NodeSelectorOpIn, fmt.Sprint(v.Value())))
}
}
return requirements
Expand Down
7 changes: 0 additions & 7 deletions pkg/scheduling/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ func (r Requirements) Add(requirements ...*Requirement) {
}
}

// Upsert replaces the requirements set with all existing requirements
func (r Requirements) Upsert(requirements ...*Requirement) {
for _, requirement := range requirements {
r[requirement.Key] = requirement
}
}

// Keys returns unique set of the label keys from the requirements
func (r Requirements) Keys() sets.String {
keys := sets.NewString()
Expand Down
2 changes: 1 addition & 1 deletion pkg/test/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func InstanceType(name string, overrides ...InstanceTypeOptions) *v1alpha1.Insta
instanceType := &v1alpha1.InstanceType{
ObjectMeta: ObjectMeta(options.ObjectMeta),
Spec: v1alpha1.InstanceTypeSpec{
Resources: options.Resources,
Allocatable: options.Resources,
},
}
instanceType.SetDefaults(context.Background())
Expand Down

0 comments on commit 835a11c

Please sign in to comment.