Skip to content

Commit

Permalink
feat(vol_provision): distribute cvrs randomly on its pools (#895) (#910)
Browse files Browse the repository at this point in the history
Signed-off-by: Vishnu Itta <vitta@mayadata.io>
  • Loading branch information
vishnuitta committed Jan 28, 2019
1 parent 329b8ab commit 8e2acf8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/install/v1alpha1/cstor_volume.go
Expand Up @@ -543,7 +543,7 @@ spec:
Calculate the replica count
Add as many poolUid to resources as there is replica count
*/}}
{{- $poolUids := keys .ListItems.cvolPoolList.pools }}
{{- $poolUids := keys .ListItems.cvolPoolList.pools | randomize }}
{{- $replicaCount := .Config.ReplicaCount.value | int64 -}}
repeatWith:
resources:
Expand Down
25 changes: 22 additions & 3 deletions pkg/template/template.go
Expand Up @@ -21,14 +21,17 @@ package template
import (
"bytes"
"fmt"
"math/rand"
"reflect"
"strings"
"text/template"
"time"

"github.com/Masterminds/sprig"
"github.com/ghodss/yaml"
v1alpha1 "github.com/openebs/maya/pkg/task/v1alpha1"
"github.com/openebs/maya/pkg/util"
kubever "github.com/openebs/maya/pkg/version/kubernetes"
"reflect"
"strings"
"text/template"
)

// empty returns true if the given value has the zero value for its type.
Expand Down Expand Up @@ -780,6 +783,7 @@ func runtaskFuncs() (f template.FuncMap) {
"keyMap": keyMap,
"splitKeyMap": splitKeyMap,
"splitListTrim": splitListTrim,
"randomize": randomize,
}
}

Expand Down Expand Up @@ -879,3 +883,18 @@ func splitListTrim(sep, orig string) []string {
processedStr := strings.TrimRight(strings.TrimLeft(orig, sep), sep)
return strings.Split(processedStr, sep)
}

// For given []string as input, randomize API returns its randomized list
// Example:
// {{- $poolUids := keys .ListItems.cvolPoolList.pools | randomize }}
func randomize(list []string) []string {
res := []string{}

r := rand.New(rand.NewSource(time.Now().Unix()))
perm := r.Perm(len(list))
for _, randomIdx := range perm {
res = append(res, list[randomIdx])
}

return res
}
37 changes: 34 additions & 3 deletions pkg/template/template_test.go
Expand Up @@ -36,10 +36,11 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/ghodss/yaml"
"reflect"
"testing"
"text/template"

"github.com/ghodss/yaml"
)

func TestAddTo(t *testing.T) {
Expand Down Expand Up @@ -1260,7 +1261,7 @@ kindTwo: Deployment`,
},
expectedYaml: `
default:
mydeploy:
mydeploy:
kind: Deployment
openebs:
mypod:
Expand Down Expand Up @@ -1314,7 +1315,7 @@ kind: MyList
apiVersion: v1alpha1
items:
# NOTE:
# Below range and if blocks should end with }}
# Below range and if blocks should end with }}
# If they end with -}} a new line is not formed and makes the yaml invalid
{{- range $pkey, $val := .Values.scenario }}
- label: {{ $pkey }}
Expand Down Expand Up @@ -2125,6 +2126,36 @@ all:
}
}

func TestRandomize(t *testing.T) {
tests := map[string]struct {
list []string
want []string
}{
"SingleKey": {
list: []string{
"key1",
},
want: []string{"key1"},
},
"TwoKeys": {
list: []string{
"key1", "key2",
},
want: []string{"key1", "key2"},
},
}

for name, mock := range tests {
t.Run(name, func(t *testing.T) {
list := randomize(mock.list)
if len(list) != len(mock.want) {
t.Fatalf("failed to test randomize: expected '%v' %d: actual '%v' %d",
mock.want, len(mock.want), list, len(list))
}
})
}
}

func TestSplitListTrim(t *testing.T) {
tests := map[string]struct {
sep string
Expand Down

0 comments on commit 8e2acf8

Please sign in to comment.