Skip to content

Commit

Permalink
Add ephemeral storage price to PodPrice
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslava-serdiuk committed May 25, 2022
1 parent e930b1a commit 42ad8d9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cluster-autoscaler/cloudprovider/gce/gce_price_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ func (model *GcePriceModel) getBasePrice(resources apiv1.ResourceList, instanceT
}
price += float64(mem.Value()) / float64(units.GiB) * memPrice * hours

if model.EphemeralStorageSupport {
ephemeralStorage := resources[apiv1.ResourceEphemeralStorage]
ephemeralStoragePrice := model.PriceInfo.LocalSsdPricePerHour()
price += float64(ephemeralStorage.Value()) / float64(units.GiB) * ephemeralStoragePrice * hours
}

return price
}

Expand Down
10 changes: 7 additions & 3 deletions cluster-autoscaler/cloudprovider/gce/gce_price_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,20 @@ func TestGetNodePrice(t *testing.T) {
}

func TestGetPodPrice(t *testing.T) {
pod1 := BuildTestPod("a1", 100, 500*units.MiB)
pod2 := BuildTestPod("a2", 2*100, 2*500*units.MiB)
pod1 := BuildTestPodWithEphemeralStorage("a1", 100, 500*units.MiB, 100*units.GiB)
pod2 := BuildTestPodWithEphemeralStorage("a2", 2*100, 2*500*units.MiB, 2*100*units.GiB)
pod3 := BuildTestPodWithEphemeralStorage("a2", 2*100, 2*500*units.MiB, 100*units.GiB)

model := NewGcePriceModel(NewGcePriceInfo(), false)
model := NewGcePriceModel(NewGcePriceInfo(), true)
now := time.Now()

price1, err := model.PodPrice(pod1, now, now.Add(time.Hour))
assert.NoError(t, err)
price2, err := model.PodPrice(pod2, now, now.Add(time.Hour))
assert.NoError(t, err)
price3, err := model.PodPrice(pod3, now, now.Add(time.Hour))
assert.NoError(t, err)
// 2 times bigger pod should cost twice as much.
assert.True(t, math.Abs(price1*2-price2) < 0.001)
assert.True(t, price2 > price3)
}
38 changes: 38 additions & 0 deletions cluster-autoscaler/utils/test/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,44 @@ func BuildTestPod(name string, cpu int64, mem int64) *apiv1.Pod {
return pod
}

// BuildTestPodWithEphemeralStorage creates a pod with cpu, memory and ephemeral storage resources.
func BuildTestPodWithEphemeralStorage(name string, cpu, mem, ephemeralStorage int64) *apiv1.Pod {
startTime := metav1.Unix(0, 0)
pod := &apiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: types.UID(name),
Namespace: "default",
Name: name,
SelfLink: fmt.Sprintf("/api/v1/namespaces/default/pods/%s", name),
Annotations: map[string]string{},
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{},
},
},
},
},
Status: apiv1.PodStatus{
StartTime: &startTime,
},
}

if cpu >= 0 {
pod.Spec.Containers[0].Resources.Requests[apiv1.ResourceCPU] = *resource.NewMilliQuantity(cpu, resource.DecimalSI)
}
if mem >= 0 {
pod.Spec.Containers[0].Resources.Requests[apiv1.ResourceMemory] = *resource.NewQuantity(mem, resource.DecimalSI)
}
if ephemeralStorage >= 0 {
pod.Spec.Containers[0].Resources.Requests[apiv1.ResourceEphemeralStorage] = *resource.NewQuantity(ephemeralStorage, resource.DecimalSI)
}

return pod
}

// BuildServiceTokenProjectedVolumeSource returns a ProjectedVolumeSource with SA token
// projection
func BuildServiceTokenProjectedVolumeSource(path string) *apiv1.ProjectedVolumeSource {
Expand Down

0 comments on commit 42ad8d9

Please sign in to comment.