Skip to content

Commit

Permalink
Return a machine struct from the cloudprovider create call (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Jan 18, 2023
1 parent a42dc9c commit 12c3355
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
22 changes: 15 additions & 7 deletions pkg/cloudprovider/fake/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/aws/karpenter-core/pkg/cloudprovider"
"github.com/aws/karpenter-core/pkg/scheduling"
"github.com/aws/karpenter-core/pkg/test"
"github.com/aws/karpenter-core/pkg/utils/functional"
"github.com/aws/karpenter-core/pkg/utils/resources"
)

var _ cloudprovider.CloudProvider = (*CloudProvider)(nil)
Expand Down Expand Up @@ -62,12 +64,12 @@ func (c *CloudProvider) Reset() {
c.AllowedCreateCalls = math.MaxInt
}

func (c *CloudProvider) Create(ctx context.Context, machine *v1alpha5.Machine) (*v1.Node, error) {
func (c *CloudProvider) Create(ctx context.Context, machine *v1alpha5.Machine) (*v1alpha5.Machine, error) {
c.mu.Lock()
c.CreateCalls = append(c.CreateCalls, machine)
if len(c.CreateCalls) > c.AllowedCreateCalls {
c.mu.Unlock()
return &v1.Node{}, fmt.Errorf("erroring as number of AllowedCreateCalls has been exceeded")
return &v1alpha5.Machine{}, fmt.Errorf("erroring as number of AllowedCreateCalls has been exceeded")
}
c.mu.Unlock()

Expand Down Expand Up @@ -101,16 +103,22 @@ func (c *CloudProvider) Create(ctx context.Context, machine *v1alpha5.Machine) (
}
}
name := test.RandomName()
n := &v1.Node{
return &v1alpha5.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: v1.NodeSpec{
ProviderID: fmt.Sprintf("fake://%s", name),
Spec: *machine.Spec.DeepCopy(),
Status: v1alpha5.MachineStatus{
ProviderID: fmt.Sprintf("fake://%s", name),
Capacity: functional.FilterMap(instanceType.Capacity, func(_ v1.ResourceName, v resource.Quantity) bool { return !resources.IsZero(v) }),
Allocatable: functional.FilterMap(instanceType.Allocatable(), func(_ v1.ResourceName, v resource.Quantity) bool { return !resources.IsZero(v) }),
},
}
return n, nil
}, nil
}

func (c *CloudProvider) Get(context.Context, string, string) (*v1alpha5.Machine, error) {
return nil, nil
}

func (c *CloudProvider) GetInstanceTypes(_ context.Context, _ *v1alpha5.Provisioner) ([]*cloudprovider.InstanceType, error) {
Expand Down
8 changes: 6 additions & 2 deletions pkg/cloudprovider/metrics/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"

"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/core/v1"
crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
Expand Down Expand Up @@ -67,7 +66,7 @@ func Decorate(cloudProvider cloudprovider.CloudProvider) cloudprovider.CloudProv
return &decorator{cloudProvider}
}

func (d *decorator) Create(ctx context.Context, machine *v1alpha5.Machine) (*v1.Node, error) {
func (d *decorator) Create(ctx context.Context, machine *v1alpha5.Machine) (*v1alpha5.Machine, error) {
defer metrics.Measure(methodDurationHistogramVec.WithLabelValues(injection.GetControllerName(ctx), "Create", d.Name()))()
return d.CloudProvider.Create(ctx, machine)
}
Expand All @@ -77,6 +76,11 @@ func (d *decorator) Delete(ctx context.Context, machine *v1alpha5.Machine) error
return d.CloudProvider.Delete(ctx, machine)
}

func (d *decorator) Get(ctx context.Context, machineName, provisionerName string) (*v1alpha5.Machine, error) {
defer metrics.Measure(methodDurationHistogramVec.WithLabelValues(injection.GetControllerName(ctx), "Get", d.Name()))()
return d.CloudProvider.Get(ctx, machineName, provisionerName)
}

func (d *decorator) GetInstanceTypes(ctx context.Context, provisioner *v1alpha5.Provisioner) ([]*cloudprovider.InstanceType, error) {
defer metrics.Measure(methodDurationHistogramVec.WithLabelValues(injection.GetControllerName(ctx), "GetInstanceTypes", d.Name()))()
return d.CloudProvider.GetInstanceTypes(ctx, provisioner)
Expand Down
12 changes: 6 additions & 6 deletions pkg/cloudprovider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ type Context struct {

// CloudProvider interface is implemented by cloud providers to support provisioning.
type CloudProvider interface {
// Create a node given constraints and instance type options. This API uses a
// callback pattern to enable cloudproviders to batch capacity creation
// requests. The callback must be called with a theoretical node object that
// is fulfilled by the cloud providers capacity creation request.
Create(context.Context, *v1alpha5.Machine) (*v1.Node, error)
// Delete node in cloudprovider
// Create launches a machine with the given resource requests and requirements and returns a hydrated
// machine back with resolved machine labels for the launched machine
Create(context.Context, *v1alpha5.Machine) (*v1alpha5.Machine, error)
// Delete removes a machine from the cloudprovider by its machine name
Delete(context.Context, *v1alpha5.Machine) error
// Get retrieves a machine from the cloudprovider by its machine name
Get(context.Context, string, string) (*v1alpha5.Machine, error)
// GetInstanceTypes returns instance types supported by the cloudprovider.
// Availability of types or zone may vary by provisioner or over time. Regardless of
// availability, the GetInstanceTypes method should always return all instance types,
Expand Down
11 changes: 10 additions & 1 deletion pkg/controllers/provisioning/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,22 @@ func (p *Provisioner) launch(ctx context.Context, machine *scheduler.Node, opts
}

logging.FromContext(ctx).Infof("launching %s", machine)
k8sNode, err := p.cloudProvider.Create(
created, err := p.cloudProvider.Create(
logging.WithLogger(ctx, logging.FromContext(ctx).Named("cloudprovider")),
machine.ToMachine(latest),
)
if err != nil {
return "", fmt.Errorf("creating cloud provider instance, %w", err)
}
k8sNode := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: created.Name,
Labels: created.Labels,
},
Spec: v1.NodeSpec{
ProviderID: created.Status.ProviderID,
},
}
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).With("node", k8sNode.Name))

if err := mergo.Merge(k8sNode, machine.ToNode()); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/functional/functional.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ func Unmarshal[T any](raw []byte) (*T, error) {
}
return &t, nil
}

func FilterMap[K comparable, V any](m map[K]V, f func(K, V) bool) map[K]V {
ret := map[K]V{}
for k, v := range m {
if f(k, v) {
ret[k] = v
}
}
return ret
}

0 comments on commit 12c3355

Please sign in to comment.