Skip to content
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

Moved a few libraries to be more generically accessible #1564

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pkg/cloudprovider/requirements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 cloudprovider

import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"

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

func Requirements(instanceTypes []InstanceType) v1alpha5.Requirements {
supported := map[string]sets.String{
v1.LabelInstanceTypeStable: sets.NewString(),
v1.LabelTopologyZone: sets.NewString(),
v1.LabelArchStable: sets.NewString(),
v1.LabelOSStable: sets.NewString(),
v1alpha5.LabelCapacityType: sets.NewString(),
}
for _, instanceType := range instanceTypes {
for _, offering := range instanceType.Offerings() {
supported[v1.LabelTopologyZone].Insert(offering.Zone)
supported[v1alpha5.LabelCapacityType].Insert(offering.CapacityType)
}
supported[v1.LabelInstanceTypeStable].Insert(instanceType.Name())
supported[v1.LabelArchStable].Insert(instanceType.Architecture())
supported[v1.LabelOSStable].Insert(instanceType.OperatingSystems().List()...)
}
requirements := v1alpha5.NewRequirements()
for key, values := range supported {
requirements = requirements.Add(v1.NodeSelectorRequirement{Key: key, Operator: v1.NodeSelectorOpIn, Values: values.UnsortedList()})
}
return requirements
}

func Compatible(it InstanceType, requirements v1alpha5.Requirements) bool {
if !requirements.Get(v1.LabelInstanceTypeStable).Has(it.Name()) {
return false
}
if !requirements.Get(v1.LabelArchStable).Has(it.Architecture()) {
return false
}
if !requirements.Get(v1.LabelOSStable).HasAny(it.OperatingSystems().List()...) {
return false
}
// acceptable if we have any offering that is valid
for _, offering := range it.Offerings() {
if requirements.Get(v1.LabelTopologyZone).Has(offering.Zone) && requirements.Get(v1alpha5.LabelCapacityType).Has(offering.CapacityType) {
return true
}
}
return false
}
28 changes: 1 addition & 27 deletions pkg/controllers/provisioning/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
"time"

"github.com/mitchellh/hashstructure/v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/sets"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"knative.dev/pkg/logging"
controllerruntime "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -104,7 +102,7 @@ func (c *Controller) Apply(ctx context.Context, provisioner *v1alpha5.Provisione
}
provisioner.Spec.Labels = functional.UnionStringMaps(provisioner.Spec.Labels, map[string]string{v1alpha5.ProvisionerNameLabelKey: provisioner.Name})
provisioner.Spec.Requirements = provisioner.Spec.Requirements.
Add(requirements(instanceTypes)...).
Add(cloudprovider.Requirements(instanceTypes).Requirements...).
Add(v1alpha5.NewLabelRequirements(provisioner.Spec.Labels).Requirements...)
if err := provisioner.Spec.Requirements.Validate(); err != nil {
return fmt.Errorf("requirements are not compatible with cloud provider, %w", err)
Expand Down Expand Up @@ -145,30 +143,6 @@ func (c *Controller) List(ctx context.Context) []*Provisioner {
return provisioners
}

func requirements(instanceTypes []cloudprovider.InstanceType) []v1.NodeSelectorRequirement {
supported := map[string]sets.String{
v1.LabelInstanceTypeStable: sets.NewString(),
v1.LabelTopologyZone: sets.NewString(),
v1.LabelArchStable: sets.NewString(),
v1.LabelOSStable: sets.NewString(),
v1alpha5.LabelCapacityType: sets.NewString(),
}
for _, instanceType := range instanceTypes {
for _, offering := range instanceType.Offerings() {
supported[v1.LabelTopologyZone].Insert(offering.Zone)
supported[v1alpha5.LabelCapacityType].Insert(offering.CapacityType)
}
supported[v1.LabelInstanceTypeStable].Insert(instanceType.Name())
supported[v1.LabelArchStable].Insert(instanceType.Architecture())
supported[v1.LabelOSStable].Insert(instanceType.OperatingSystems().List()...)
}
requirements := []v1.NodeSelectorRequirement{}
for key, values := range supported {
requirements = append(requirements, v1.NodeSelectorRequirement{Key: key, Operator: v1.NodeSelectorOpIn, Values: values.UnsortedList()})
}
return requirements
}

// Register the controller to the manager
func (c *Controller) Register(_ context.Context, m manager.Manager) error {
return controllerruntime.
Expand Down
25 changes: 3 additions & 22 deletions pkg/controllers/provisioning/scheduling/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewNode(constraints *v1alpha5.Constraints, instanceTypeOptions []cloudprovi

for _, it := range instanceTypeOptions {
// pre-filter our list of all possible instance types by what the provisioner allows
if !compatibleInstanceType(constraints.Requirements, it) {
if !cloudprovider.Compatible(it, constraints.Requirements) {
continue
}
n.InstanceTypeOptions = append(n.InstanceTypeOptions, it)
Expand Down Expand Up @@ -66,7 +66,7 @@ func (n Node) Compatible(pod *v1.Pod) error {
// Ensure that at least one instance type of the instance types that we are already narrowed down to based on the
// existing pods can support the pod resources and combined pod + provider requirements
for _, it := range n.InstanceTypeOptions {
if compatibleInstanceType(tightened, it) && n.hasCompatibleResources(resources.RequestsForPods(pod), it) {
if cloudprovider.Compatible(it, tightened) && n.hasCompatibleResources(resources.RequestsForPods(pod), it) {
return nil
}
}
Expand All @@ -80,7 +80,7 @@ func (n *Node) Add(pod *v1.Pod) {
n.Constraints = n.Constraints.Tighten(pod)
var instanceTypeOptions []cloudprovider.InstanceType
for _, it := range n.InstanceTypeOptions {
if compatibleInstanceType(n.Constraints.Requirements, it) &&
if cloudprovider.Compatible(it, n.Constraints.Requirements) &&
n.hasCompatibleResources(resources.RequestsForPods(pod), it) {
instanceTypeOptions = append(instanceTypeOptions, it)
}
Expand All @@ -102,22 +102,3 @@ func (n Node) hasCompatibleResources(resourceList v1.ResourceList, it cloudprovi
}
return true
}

func compatibleInstanceType(requirements v1alpha5.Requirements, it cloudprovider.InstanceType) bool {
if !requirements.Get(v1.LabelInstanceTypeStable).Has(it.Name()) {
return false
}
if !requirements.Get(v1.LabelArchStable).Has(it.Architecture()) {
return false
}
if !requirements.Get(v1.LabelOSStable).HasAny(it.OperatingSystems().List()...) {
return false
}
// acceptable if we have any offering that is valid
for _, offering := range it.Offerings() {
if requirements.Get(v1.LabelTopologyZone).Has(offering.Zone) && requirements.Get(v1alpha5.LabelCapacityType).Has(offering.CapacityType) {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion pkg/controllers/provisioning/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package provisioning_test

import (
"context"
"github.com/aws/karpenter/pkg/cloudprovider/aws/apis/v1alpha1"
"strings"
"testing"

"github.com/Pallinder/go-randomdata"
"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5"
"github.com/aws/karpenter/pkg/cloudprovider/aws/apis/v1alpha1"
"github.com/aws/karpenter/pkg/cloudprovider/fake"
"github.com/aws/karpenter/pkg/cloudprovider/registry"
"github.com/aws/karpenter/pkg/controllers/provisioning"
Expand Down