Skip to content

Commit

Permalink
Merge pull request #609 from cgalibern/dev
Browse files Browse the repository at this point in the history
misc
  • Loading branch information
cgalibern authored Oct 25, 2024
2 parents 9fdeb31 + 8e19482 commit d0eb85c
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 84 deletions.
18 changes: 12 additions & 6 deletions core/keyop/keyop.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const (
Toggle
// Insert adds an element at the position specified by Index
Insert
// Exist tests the existance of a key
// Exist tests the existence of a key
Exist
// Equal tests if the current value of the key is equal to the keyop.T value
Equal
Expand Down Expand Up @@ -92,7 +92,7 @@ var (

// ":" is a suffixer, not a spliter
splitOps = []string{"+=", "-=", "|=", "^=", "!=", ">=", "<=", ">", "<", "="}
regexpIndex = regexp.MustCompile(`(.+)\[(\d+)\]`)
regexpIndex = regexp.MustCompile(`(.+)\[(\d+)]`)
)

func (t Op) String() string {
Expand All @@ -108,11 +108,17 @@ func New(key key.T, op Op, value string, idx int) *T {
}
}

// ParseOps function processes a list of strings, parses them into operations,
// filters out any invalid operations (based on the IsZero check),
// and returns a list of valid operations.
func ParseOps(kws []string) L {
l := make(L, len(kws))
for i, kw := range kws {
l := make(L, 0, len(kws))
for _, kw := range kws {
op := Parse(kw)
l[i] = *op
if op.IsZero() {
continue
}
l = append(l, *op)
}
return l
}
Expand All @@ -138,7 +144,7 @@ func (t Op) MarshalJSON() ([]byte, error) {
return buffer.Bytes(), nil
}

// UnmarshalJSON unmashals a quoted json string to the enum value
// UnmarshalJSON unmarshals a quoted json string to the enum value
func (t *Op) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
Expand Down
49 changes: 46 additions & 3 deletions core/keyop/keyop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package keyop
import (
"testing"

"github.com/opensvc/om3/util/key"
"github.com/stretchr/testify/assert"

"github.com/opensvc/om3/util/key"
)

func TestKeyopsDrop(t *testing.T) {
func TestDrop(t *testing.T) {
op1 := T{
Key: key.Parse("topology"),
Op: Set,
Expand All @@ -28,7 +29,7 @@ func TestKeyopsDrop(t *testing.T) {
assert.Len(t, ops, 1)
}

func TestKeyopParse(t *testing.T) {
func TestParse(t *testing.T) {
tests := []struct {
expr string
key key.T
Expand Down Expand Up @@ -125,3 +126,45 @@ func TestKeyopParse(t *testing.T) {
})
}
}

func TestParseOps(t *testing.T) {
cases := []struct {
l []string
expected L
}{
{
l: []string{"foo=bar"},
expected: L{
T{Key: key.T{Section: "DEFAULT", Option: "foo"}, Op: 1, Value: "bar", Index: 0},
},
},
{
l: []string{"foo1=bar1", "must_be_dropped", "foo2=bar2"},
expected: L{
T{Key: key.T{Section: "DEFAULT", Option: "foo1"}, Op: 1, Value: "bar1", Index: 0},
T{Key: key.T{Section: "DEFAULT", Option: "foo2"}, Op: 1, Value: "bar2", Index: 0},
},
},
{
l: []string{"must_be_dropped"},
expected: L{},
},
{
l: []string{""},
expected: L{},
},
{
l: []string{},
expected: L{},
},
{
l: nil,
expected: L{},
},
}
for _, tc := range cases {
t.Logf("ParseOps(%q)", tc.l)
ops := ParseOps(tc.l)
assert.Equal(t, tc.expected, ops)
}
}
2 changes: 1 addition & 1 deletion core/object/core_keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ var keywordStore = keywords.Store{
{
Aliases: []string{"flex_max_nodes"},
Converter: converters.Int,
Default: "{flex_target}",
Default: "{#nodes}",
DefaultText: keywords.NewText(fs, "text/kw/core/flex_max.default"),
Depends: keyop.ParseList("topology=flex"),
Inherit: keywords.InheritHead,
Expand Down
4 changes: 4 additions & 0 deletions core/xconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,15 @@ func (t *T) GetDurationStrict(k key.T) (*time.Duration, error) {
}
}

// GetInt returns the evaluated integer value associated with a key k.
// On errors returns 0.
func (t *T) GetInt(k key.T) int {
val, _ := t.GetIntStrict(k)
return val
}

// GetIntStrict returns the evaluated integer value associated with a key k.
// On errors returns 0 and an appropriate error.
func (t *T) GetIntStrict(k key.T) (int, error) {
if v, err := t.Eval(k); err != nil {
return 0, err
Expand Down
9 changes: 8 additions & 1 deletion daemon/daemonapi/post_object_config_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ func (a *DaemonAPI) PostObjectConfigUpdate(ctx echo.Context, namespace string, k
unsets = key.ParseStrings(*params.Unset)
}
if params.Delete != nil {
deletes = *params.Delete
for _, section := range *params.Delete {
if section == "" {
// Prevents from accidental remove DEFAULT section. SectionsByName("")
// return "DEFAULT". Use explicit section="DEFAULT" to remove DEFAULT section.
continue
}
deletes = append(deletes, section)
}
}
if len(sets)+len(unsets)+len(deletes) == 0 {
return JSONProblemf(ctx, http.StatusBadRequest, "No valid update requested", "")
Expand Down
53 changes: 32 additions & 21 deletions daemon/icfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ func (t *Manager) configFileCheck() error {
cfg.Pool = &pool
}
if cfg.Topology == topology.Flex {
cfg.FlexMin = t.getFlexMin(cf)
cfg.FlexMax = t.getFlexMax(cf)
cfg.FlexTarget = t.getFlexTarget(cf, cfg.FlexMin, cfg.FlexMax)
cfg.FlexTarget = t.getFlexTarget(cf)
cfg.FlexMin = t.getFlexMin(cf, cfg.FlexTarget)
cfg.FlexMax = t.getFlexMax(cf, cfg.FlexTarget)
}

t.lastMtime = mtime
Expand Down Expand Up @@ -427,37 +427,48 @@ func (t *Manager) getPriority(cf *xconfig.T) priority.T {
return priority.T(s)
}

func (t *Manager) getFlexTarget(cf *xconfig.T, min, max int) (target int) {
func (t *Manager) getFlexTarget(cf *xconfig.T) (target int) {
switch t.path.Kind {
case naming.KindSvc, naming.KindVol:
target = cf.GetInt(keyFlexTarget)
}
switch {
case target < min:
target = min
case target > max:
target = max
if i, err := cf.GetIntStrict(keyFlexTarget); err != nil {
t.log.Warnf("can't get flex_target value: %s", err)
return 1
} else {
return i
}
default:
return 0
}
return
}

func (t *Manager) getFlexMin(cf *xconfig.T) int {
func (t *Manager) getFlexMin(cf *xconfig.T, target int) int {
switch t.path.Kind {
case naming.KindSvc, naming.KindVol:
return cf.GetInt(keyFlexMin)
if i, err := cf.GetIntStrict(keyFlexMin); err != nil {
t.log.Warnf("can't get flex_min value: %s", err)
return 1
} else if i > target {
t.log.Warnf("adjusts too big flex_min value %d to flex target value %d", i, target)
return target
} else {
return i
}
default:
return 0
}
return 0
}

func (t *Manager) getFlexMax(cf *xconfig.T) int {
func (t *Manager) getFlexMax(cf *xconfig.T, target int) int {
switch t.path.Kind {
case naming.KindSvc, naming.KindVol:
if i, err := cf.GetIntStrict(keyFlexMax); err == nil {
return i
} else if scope, err := t.getScope(cf); err == nil {
return len(scope)
if i, err := cf.GetIntStrict(keyFlexMax); err != nil {
t.log.Warnf("can't get flex_max value: %s", err)
return len(clusternode.Get())
} else if i < target {
t.log.Warnf("adjusts too small flex_max value %d to flex target value %d", i, target)
return target
} else {
return 0
return i
}
default:
return 0
Expand Down
7 changes: 3 additions & 4 deletions drivers/resdiskdrbd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
package resdiskdrbd

import (
"context"
// Necessary to use go:embed
_ "embed"

"context"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -76,7 +75,7 @@ type (
Addr string
Device string
Disk string
NodeID int
NodeId int
}
)

Expand Down Expand Up @@ -489,7 +488,7 @@ func (t T) makeConfRes(allocations map[string]api.DRBDAllocation) (ConfRes, erro
Addr: fmt.Sprintf("%s %s:%d", ipVer, ip, port),
Disk: disk,
Device: device,
NodeID: nodeID,
NodeId: nodeID,
}
res.Hosts = append(res.Hosts, host)
}
Expand Down
16 changes: 16 additions & 0 deletions util/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ func New(section, option string) T {
}
}

// ParseStrings function processes a list of strings, parses them into keyword,
// filters out any invalid keyword (based on the IsZero check),
// and returns a list of valid keywords.
func ParseStrings(l []string) L {
kws := make(L, 0)
for _, s := range l {
kw := Parse(s)
if kw.IsZero() {
continue
}
kws = append(kws, Parse(s))
}
return kws
}

// Parse function construct key T from the parsed string s.
// On invalid string s the zero key is returned.
func Parse(s string) T {
if s == "" || strings.ContainsAny(s, " \t") {
return T{}
}
l := strings.SplitN(s, ".", 2)
switch len(l) {
case 1:
Expand Down Expand Up @@ -67,3 +79,7 @@ func (t T) String() string {
}
return t.Section + "." + t.Option
}

func (t T) IsZero() bool {
return (t.Option + t.Section) == ""
}
Loading

0 comments on commit d0eb85c

Please sign in to comment.