Merge pull request #9877 from rook/mergify/bp/release-1.8/pr-9868

core: fix empty string bug for applying csi resources (backport #9868)
This commit is contained in:
mergify[bot] authored and GitHub committed 2022-03-09 18:20:00 +00:00
commit a2a2dc3651
2 files changed
+37 -2

No files matched your search

+1 -2
View File
@@ -99,10 +99,9 @@ func applyResourcesToContainers(opConfig map[string]string, key string, podspec
func getComputeResource(opConfig map[string]string, key string) []k8sutil.ContainerResource {
// Add Resource list if any
resource := []k8sutil.ContainerResource{}
resourceRaw := ""
var err error
if k8sutil.GetValue(opConfig, key, "") != "" {
if resourceRaw := k8sutil.GetValue(opConfig, key, ""); resourceRaw != "" {
resource, err = k8sutil.YamlToContainerResource(resourceRaw)
if err != nil {
logger.Warningf("failed to parse %q. %v", resourceRaw, err)
+36
View File
@@ -21,6 +21,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
func TestDaemonSetTemplate(t *testing.T) {
@@ -80,3 +81,38 @@ func TestGetPortFromConfig(t *testing.T) {
err = os.Unsetenv(key)
assert.Nil(t, err)
}
func TestApplyingResourcesToRBDPlugin(t *testing.T) {
tp := templateParam{}
rbdPlugin, err := templateToDaemonSet("rbdplugin", RBDPluginTemplatePath, tp)
assert.Nil(t, err)
params := make(map[string]string)
// need to build using map[string]interface{} because the following resource
// doesn't serialise nicely
// https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity
resource := []map[string]interface{}{
{
"name": "driver-registrar",
"resource": map[string]interface{}{
"limits": map[string]interface{}{
"cpu": "200m",
"memory": "256Mi",
},
"requests": map[string]interface{}{
"cpu": "100m",
"memory": "128Mi",
},
},
},
}
resourceRaw, err := yaml.Marshal(resource)
assert.Nil(t, err)
params[rbdPluginResource] = string(resourceRaw)
applyResourcesToContainers(params, rbdPluginResource, &rbdPlugin.Spec.Template.Spec)
assert.Equal(t, rbdPlugin.Spec.Template.Spec.Containers[0].Resources.Requests.Memory().String(), "128Mi")
assert.Equal(t, rbdPlugin.Spec.Template.Spec.Containers[0].Resources.Limits.Memory().String(), "256Mi")
assert.Equal(t, rbdPlugin.Spec.Template.Spec.Containers[0].Resources.Requests.Cpu().String(), "100m")
assert.Equal(t, rbdPlugin.Spec.Template.Spec.Containers[0].Resources.Limits.Cpu().String(), "200m")
}