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

[Feature] Allow to pass EphemeralStorage Resource to the Pods #1401

Merged
merged 1 commit into from
Sep 5, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- (Feature) Reconciliation Loop Interval option
- (Bugfix) Fix GZIP encoding in case of small responses
- (Bugfix) Fix PVC Rotation Discovery
- (Feature) Allow to pass EphemeralStorage Resource to the Pods

## [1.2.32](https://github.com/arangodb/kube-arangodb/tree/1.2.32) (2023-08-07)
- (Feature) Backup lifetime - remove Backup once its lifetime has been reached
Expand Down
34 changes: 23 additions & 11 deletions pkg/util/k8sutil/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,21 +534,33 @@ func operatorInitContainer(name, operatorImage string, command []string, securit

// ExtractPodResourceRequirement filters resource requirements for Pods.
func ExtractPodResourceRequirement(resources core.ResourceRequirements) core.ResourceRequirements {
filter := PodResourceRequirementsFilter(core.ResourceCPU, core.ResourceMemory, core.ResourceEphemeralStorage)

filterStorage := func(list core.ResourceList) core.ResourceList {
newlist := make(core.ResourceList)
if q, ok := list[core.ResourceCPU]; ok {
newlist[core.ResourceCPU] = q
return core.ResourceRequirements{
Limits: filter(resources.Limits),
Requests: filter(resources.Requests),
}
}

func PodResourceRequirementsFilter(filters ...core.ResourceName) func(in core.ResourceList) core.ResourceList {
return func(in core.ResourceList) core.ResourceList {
filtered := map[core.ResourceName]bool{}

for _, k := range filters {
filtered[k] = true
}
if q, ok := list[core.ResourceMemory]; ok {
newlist[core.ResourceMemory] = q

n := core.ResourceList{}

for k, v := range in {
if _, ok := filtered[k]; !ok {
continue
}

n[k] = v
}
return newlist
}

return core.ResourceRequirements{
Limits: filterStorage(resources.Limits),
Requests: filterStorage(resources.Requests),
return n
}
}

Expand Down
61 changes: 61 additions & 0 deletions pkg/util/k8sutil/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ package k8sutil

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
resource "k8s.io/apimachinery/pkg/api/resource"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/arangodb/kube-arangodb/pkg/handlers/utils"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
)
Expand Down Expand Up @@ -416,3 +419,61 @@ func Test_EnsureFinalizer(t *testing.T) {
require.NotContains(t, pod.Finalizers, f)
})
}

func Test_ExtractPodResourceRequirement(t *testing.T) {
v, err := resource.ParseQuantity("1Gi")
require.NoError(t, err)

t.Run("Filter Storage", func(t *testing.T) {
in := core.ResourceRequirements{
Limits: map[core.ResourceName]resource.Quantity{
core.ResourceCPU: v,
core.ResourceStorage: v,
},
}
require.Len(t, in.Limits, 2)
require.Len(t, in.Requests, 0)

out := ExtractPodResourceRequirement(in)
require.Len(t, out.Limits, 1)
require.Contains(t, out.Limits, core.ResourceCPU)
require.NotContains(t, out.Limits, core.ResourceStorage)
require.Len(t, out.Requests, 0)
})

t.Run("Ensure that all required Resources are filtered", func(t *testing.T) {
resources := map[core.ResourceName]bool{
core.ResourceCPU: true,
core.ResourceMemory: true,
core.ResourceStorage: false,
core.ResourceEphemeralStorage: true,
}

in := core.ResourceRequirements{
Limits: core.ResourceList{},
Requests: core.ResourceList{},
}

for k := range resources {
in.Limits[k] = v
in.Requests[k] = v
}

out := ExtractPodResourceRequirement(in)

for k, v := range resources {
t.Run(fmt.Sprintf("Resource %s should be %s", k, util.BoolSwitch(v, "present", "missing")), func(t *testing.T) {
require.Contains(t, in.Requests, k)
require.Contains(t, in.Limits, k)

if v {
require.Contains(t, out.Requests, k)
require.Contains(t, out.Limits, k)
} else {
require.NotContains(t, out.Requests, k)
require.NotContains(t, out.Limits, k)
}
})
}
})
}