Repository: rook/rook
Pull request #9877 (2022-03-09 17:40:30)

core: fix empty string bug for applying csi resources (backport #9868)
This is an automatic backport of pull request #9868 done by [Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>



Patch:
diff --git a/pkg/operator/ceph/csi/util.go b/pkg/operator/ceph/csi/util.go
index 6fa5c5820..44d2b42d0 100644
--- a/pkg/operator/ceph/csi/util.go
+++ b/pkg/operator/ceph/csi/util.go
@@ -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)


Test patch:
diff --git a/pkg/operator/ceph/csi/util_test.go b/pkg/operator/ceph/csi/util_test.go
index a5e94ba1e..f43648b3e 100644
--- a/pkg/operator/ceph/csi/util_test.go
+++ b/pkg/operator/ceph/csi/util_test.go
@@ -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")
+}