/
cloudprovider.go
210 lines (189 loc) · 7.1 KB
/
cloudprovider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright (c) Microsoft Corporation.
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.
*/
// https://github.com/kubernetes-sigs/karpenter/blob/62fe4ac537b8e381bbb11bd344bb2f05850cb619/pkg/cloudprovider/fake/cloudprovider.go
package fake
import (
"context"
"fmt"
"math"
"sort"
"sync"
"github.com/aws/karpenter-core/pkg/utils/functional"
"github.com/aws/karpenter-core/pkg/utils/resources"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
"github.com/aws/karpenter-core/pkg/cloudprovider"
"github.com/aws/karpenter-core/pkg/scheduling"
"github.com/aws/karpenter-core/pkg/test"
)
var _ cloudprovider.CloudProvider = (*CloudProvider)(nil)
type CloudProvider struct {
InstanceTypes []*cloudprovider.InstanceType
mu sync.RWMutex
// CreateCalls contains the arguments for every create call that was made since it was cleared
CreateCalls []*v1alpha5.Machine
CreatedMachines map[string]*v1alpha5.Machine
DeleteCalls []*v1alpha5.Machine
Drifted bool
AllowedCreateCalls int
}
func NewCloudProvider() *CloudProvider {
return &CloudProvider{
AllowedCreateCalls: math.MaxInt,
CreatedMachines: map[string]*v1alpha5.Machine{},
}
}
// Reset is for BeforeEach calls in testing to reset the tracking of CreateCalls
func (c *CloudProvider) Reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.CreateCalls = []*v1alpha5.Machine{}
c.CreatedMachines = map[string]*v1alpha5.Machine{}
c.DeleteCalls = []*v1alpha5.Machine{}
c.AllowedCreateCalls = math.MaxInt
c.Drifted = false
}
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 &v1alpha5.Machine{}, fmt.Errorf("erroring as number of AllowedCreateCalls has been exceeded")
}
c.mu.Unlock()
requirements := scheduling.NewNodeSelectorRequirements(machine.Spec.Requirements...)
instanceTypes := lo.Filter(lo.Must(c.GetInstanceTypes(ctx, &v1alpha5.Provisioner{})), func(i *cloudprovider.InstanceType, _ int) bool {
return requirements.Get(v1.LabelInstanceTypeStable).Has(i.Name)
})
// Order instance types so that we get the cheapest instance types of the available offerings
sort.Slice(instanceTypes, func(i, j int) bool {
iOfferings := instanceTypes[i].Offerings.Available().Requirements(requirements)
jOfferings := instanceTypes[j].Offerings.Available().Requirements(requirements)
return iOfferings.Cheapest().Price < jOfferings.Cheapest().Price
})
instanceType := instanceTypes[0]
// Labels
labels := map[string]string{}
for key, requirement := range instanceType.Requirements {
if requirement.Operator() == v1.NodeSelectorOpIn {
labels[key] = requirement.Values()[0]
}
}
// Find Offering
for _, o := range instanceType.Offerings.Available() {
if requirements.Compatible(scheduling.NewRequirements(
scheduling.NewRequirement(v1.LabelTopologyZone, v1.NodeSelectorOpIn, o.Zone),
scheduling.NewRequirement(v1alpha5.LabelCapacityType, v1.NodeSelectorOpIn, o.CapacityType),
)) == nil {
labels[v1.LabelTopologyZone] = o.Zone
labels[v1alpha5.LabelCapacityType] = o.CapacityType
break
}
}
created := &v1alpha5.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: machine.Name,
Labels: lo.Assign(labels, machine.Labels),
Annotations: machine.Annotations,
},
Spec: *machine.Spec.DeepCopy(),
Status: v1alpha5.MachineStatus{
ProviderID: test.RandomProviderID(),
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) }),
},
}
c.CreatedMachines[created.Status.ProviderID] = created
return created, nil
}
func (c *CloudProvider) Get(_ context.Context, id string) (*v1alpha5.Machine, error) {
c.mu.RLock()
defer c.mu.RUnlock()
if machine, ok := c.CreatedMachines[id]; ok {
return machine.DeepCopy(), nil
}
return nil, cloudprovider.NewMachineNotFoundError(fmt.Errorf("no machine exists with id '%s'", id))
}
func (c *CloudProvider) List(_ context.Context) ([]*v1alpha5.Machine, error) {
c.mu.RLock()
defer c.mu.RUnlock()
return lo.Map(lo.Values(c.CreatedMachines), func(m *v1alpha5.Machine, _ int) *v1alpha5.Machine {
return m.DeepCopy()
}), nil
}
func (c *CloudProvider) GetInstanceTypes(_ context.Context, _ *v1alpha5.Provisioner) ([]*cloudprovider.InstanceType, error) {
if c.InstanceTypes != nil {
return c.InstanceTypes, nil
}
return []*cloudprovider.InstanceType{
NewInstanceType(InstanceTypeOptions{
Name: "default-instance-type",
}),
NewInstanceType(InstanceTypeOptions{
Name: "small-instance-type",
Resources: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("2"),
v1.ResourceMemory: resource.MustParse("2Gi"),
},
}),
NewInstanceType(InstanceTypeOptions{
Name: "gpu-vendor-instance-type",
Resources: map[v1.ResourceName]resource.Quantity{
ResourceGPUVendorA: resource.MustParse("2"),
}}),
NewInstanceType(InstanceTypeOptions{
Name: "gpu-vendor-b-instance-type",
Resources: map[v1.ResourceName]resource.Quantity{
ResourceGPUVendorB: resource.MustParse("2"),
},
}),
NewInstanceType(InstanceTypeOptions{
Name: "arm-instance-type",
Architecture: "arm64",
OperatingSystems: sets.NewString("ios", string(v1.Linux), string(v1.Windows), "darwin"),
Resources: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: resource.MustParse("16"),
v1.ResourceMemory: resource.MustParse("128Gi"),
},
}),
NewInstanceType(InstanceTypeOptions{
Name: "single-pod-instance-type",
Resources: map[v1.ResourceName]resource.Quantity{
v1.ResourcePods: resource.MustParse("1"),
},
}),
}, nil
}
func (c *CloudProvider) Delete(_ context.Context, m *v1alpha5.Machine) error {
c.mu.Lock()
defer c.mu.Unlock()
c.DeleteCalls = append(c.DeleteCalls, m)
if _, ok := c.CreatedMachines[m.Status.ProviderID]; ok {
delete(c.CreatedMachines, m.Status.ProviderID)
return nil
}
return cloudprovider.NewMachineNotFoundError(fmt.Errorf("no machine exists with provider id '%s'", m.Status.ProviderID))
}
func (c *CloudProvider) IsMachineDrifted(context.Context, *v1alpha5.Machine) (bool, error) {
c.mu.RLock()
defer c.mu.RUnlock()
return c.Drifted, nil
}
// Name returns the CloudProvider implementation name.
func (c *CloudProvider) Name() string {
return "fake"
}