Skip to content

Commit

Permalink
rework logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tzneal committed Mar 27, 2022
1 parent 3e6b485 commit 318c70c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
22 changes: 22 additions & 0 deletions pkg/apis/provisioning/v1alpha5/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"

"go.uber.org/multierr"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -204,3 +205,24 @@ func (r *Requirements) UnmarshalJSON(b []byte) error {
*r = NewRequirements(requirements...)
return nil
}

func (r *Requirements) String() string {
var sb strings.Builder
for key, req := range r.requirements {
var values []string
if !req.IsComplement() {
values = req.Values().List()
} else {
values = req.ComplementValues().List()
}
if sb.Len() > 0 {
sb.WriteString(", ")
}
if len(values) > 5 {
values[5] = fmt.Sprintf("and %d others", len(values)-5)
values = values[0:6]
}
fmt.Fprintf(&sb, "%s %s %v", key, req.Type(), values)
}
return sb.String()
}
28 changes: 11 additions & 17 deletions pkg/controllers/provisioning/scheduling/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Node struct {
daemonResources v1.ResourceList
}

func NewNode(constraints *v1alpha5.Constraints, daemonResources v1.ResourceList, instanceTypeOptions []cloudprovider.InstanceType, pods ...*v1.Pod) (*Node, error) {
func NewNode(constraints *v1alpha5.Constraints, daemonResources v1.ResourceList, instanceTypeOptions []cloudprovider.InstanceType) *Node {
n := &Node{
Constraints: *constraints.DeepCopy(),
daemonResources: daemonResources,
Expand All @@ -56,14 +56,7 @@ func NewNode(constraints *v1alpha5.Constraints, daemonResources v1.ResourceList,
}
n.InstanceTypeOptions = append(n.InstanceTypeOptions, it)
}

for _, p := range pods {
n.Add(p)
}
if len(n.InstanceTypeOptions) == 0 {
return nil, errors.New("no instance type satisfied requirements")
}
return n, nil
return n
}

func (n Node) Compatible(pod *v1.Pod) error {
Expand Down Expand Up @@ -108,7 +101,7 @@ func (n *Node) newPodCanFit(newSize v1.ResourceList, it cloudprovider.InstanceTy

// Add adds a pod to the Node which tightens constraints, possibly reducing the available instance type options for this
// node
func (n *Node) Add(pod *v1.Pod) {
func (n *Node) Add(pod *v1.Pod) error {
n.Requirements = n.Requirements.Add(v1alpha5.NewPodRequirements(pod).Requirements...)

podRequests := resources.RequestsForPods(pod)
Expand All @@ -126,6 +119,13 @@ func (n *Node) Add(pod *v1.Pod) {
n.Pods = append(n.Pods, pod)
n.InstanceTypeOptions = instanceTypeOptions
n.podResources = resources.Merge(n.podResources, resources.RequestsForPods(pod))

if len(n.InstanceTypeOptions) == 0 {
return fmt.Errorf("no instance type satisfied resources %s and requirements %s",
resources.String(resources.RequestsForPods(pod)),
n.Requirements.String())
}
return nil
}

// hasCompatibleResources tests if a given node selector and resource request list is compatible with an instance type
Expand All @@ -144,19 +144,13 @@ func (n Node) hasCompatibleResources(resourceList v1.ResourceList, it cloudprovi
}

func (n Node) String() string {
var resSb strings.Builder

var requiredResources v1.ResourceList
if len(n.InstanceTypeOptions) == 0 {
requiredResources = resources.Merge(n.daemonResources, n.podResources)
} else {
requiredResources = resources.Merge(n.daemonResources, n.InstanceTypeOptions[0].Overhead(), n.podResources)
}

for k, v := range requiredResources {
fmt.Fprintf(&resSb, "%s: %s ", k, v.String())
}

var itSb strings.Builder
for i, it := range n.InstanceTypeOptions {
// print the first 5 instance types only (indices 0-4)
Expand All @@ -169,5 +163,5 @@ func (n Node) String() string {
fmt.Fprint(&itSb, it.Name())
}

return fmt.Sprintf("with %d pods using resources %s from types %s", len(n.Pods), resSb.String(), itSb.String())
return fmt.Sprintf("with %d pods using resources %s from types %s", len(n.Pods), resources.String(requiredResources), itSb.String())
}
1 change: 1 addition & 0 deletions pkg/controllers/provisioning/scheduling/nodeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package scheduling
import (
"context"
"fmt"

"github.com/aws/karpenter/pkg/utils/resources"

appsv1 "k8s.io/api/apps/v1"
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/provisioning/scheduling/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func (s *Scheduler) Solve(ctx context.Context, provisioner *v1alpha5.Provisioner
}
}
if !isScheduled {
n, err := NewNode(constraints, nodeSet.daemonResources, instanceTypes, pod)
if err != nil {
n := NewNode(constraints, nodeSet.daemonResources, instanceTypes)
if err := n.Add(pod); err != nil {
logging.FromContext(ctx).With("pod", client.ObjectKeyFromObject(pod)).Errorf("Scheduling pod, %s", err)
} else {
nodeSet.Add(n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestSchedulingPerformance(t *testing.T) {
totalPods := 0
totalNodes := 0
var totalTime time.Duration
//for _, instanceCount := range []int{10, 100, 200, 300, 400} {
for _, instanceCount := range []int{400} {
for _, podCount := range []int{10, 100, 500, 1000, 1500, 2000, 2500} {
start := time.Now()
Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ limitations under the License.
package resources

import (
"fmt"
"strings"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
Expand Down Expand Up @@ -73,3 +76,20 @@ func IsZero(r resource.Quantity) bool {
func Cmp(lhs resource.Quantity, rhs resource.Quantity) int {
return lhs.Cmp(rhs)
}

// String returns a string version of the resource list suitable for presenting in a log
func String(list v1.ResourceList) string {
if len(list) == 0 {
return "{none}"
}
var sb strings.Builder
sb.WriteByte('{')
for k, v := range list {
if sb.Len() > 1 {
sb.WriteByte(' ')
}
fmt.Fprintf(&sb, "%s: %s", k, v.String())
}
sb.WriteByte('}')
return sb.String()
}
9 changes: 9 additions & 0 deletions pkg/utils/sets/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ func (s Set) Values() sets.String {
return s.values
}

// ComplementValues returns the values of the complement set.
// If the set is not a complement set, it will panic
func (s Set) ComplementValues() sets.String {
if !s.complement {
panic("infinite set")
}
return s.values
}

func (s Set) String() string {
if s.complement {
return fmt.Sprintf("%v' (complement set)", s.values.UnsortedList())
Expand Down

0 comments on commit 318c70c

Please sign in to comment.