Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fix #1240:Modifies GetAll and proto to not use dot separator
Browse files Browse the repository at this point in the history
Instead of returning a map[string]*configPolicyNode Getall()
now returns a []keyNode where keyNode contains the []string key and the
*configPolicyNode.

Adds repeated string keys for Policy proto types and implements
backwards compatible logic to work with a '.' delimitted string if using
a plugin compiled against older client-libs.

Removed freeze from ctree since it's no longer used. Also removed
references to it.
  • Loading branch information
IRCody committed Sep 30, 2016
1 parent bd25750 commit 7746a24
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 243 deletions.
1 change: 0 additions & 1 deletion control/plugin/collector_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (p *mockPlugin) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
cpn.Add(r1, r2, r3, r4, r5, r6)
ns := []string{"one", "two", "potato"}
cp.Add(ns, cpn)
cp.Freeze()

return cp, nil
}
Expand Down
31 changes: 11 additions & 20 deletions control/plugin/cpolicy/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ func (c *ConfigPolicy) Add(ns []string, cpn *ConfigPolicyNode) {

// Returns a ConfigPolicyNode that is a merged version of the namespace provided.
func (c *ConfigPolicy) Get(ns []string) *ConfigPolicyNode {
// Automatically freeze on first Get
if !c.config.Frozen() {
c.config.Freeze()
}

n := c.config.Get(ns)
if n == nil {
Expand All @@ -140,28 +136,23 @@ func (c *ConfigPolicy) Get(ns []string) *ConfigPolicyNode {
return &t
default:
return t.(*ConfigPolicyNode)

}
}

func (c *ConfigPolicy) GetAll() map[string]*ConfigPolicyNode {
// Automatically freeze on first Get
if !c.config.Frozen() {
c.config.Freeze()
}
type keyNode struct {
Key []string
Node *ConfigPolicyNode
}

ret := map[string]*ConfigPolicyNode{}
for key, node := range c.config.GetAll() {
switch t := node.(type) {
func (c *ConfigPolicy) GetAll() []keyNode {

ret := make([]keyNode, 0)
for _, node := range c.config.GetAll() {
key := node.Key
switch t := node.N.(type) {
case *ConfigPolicyNode:
ret[key] = t
ret = append(ret, keyNode{Key: key, Node: t})
}
}
return ret
}

// Freezes the ConfigPolicy from future writes (adds) and triggers compression
// of tree into read-performant version.
func (c *ConfigPolicy) Freeze() {
c.config.Freeze()
}
1 change: 0 additions & 1 deletion control/plugin/cpolicy/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func TestConfigPolicy(t *testing.T) {
ns := []string{"one", "two", "potato"}

cp.Add(ns, cpn)
cp.Freeze()
Convey("retrieves store policy", func() {
gc := cp.Get(ns)
So(gc.rules["username"].Required(), ShouldEqual, false)
Expand Down
41 changes: 34 additions & 7 deletions control/plugin/rpc/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ func NewGetConfigPolicyReply(policy *cpolicy.ConfigPolicy) (*GetConfigPolicyRepl
StringPolicy: map[string]*StringPolicy{},
}

for key, node := range policy.GetAll() {
for _, rule := range node.RulesAsTable() {
for _, node := range policy.GetAll() {
key := strings.Join(node.Key, ".")

for _, rule := range node.Node.RulesAsTable() {
switch rule.Type {
case cpolicy.BoolType:
r := &BoolRule{
Expand All @@ -35,7 +37,10 @@ func NewGetConfigPolicyReply(policy *cpolicy.ConfigPolicy) (*GetConfigPolicyRepl
r.Default = rule.Default.(ctypes.ConfigValueBool).Value
}
if ret.BoolPolicy[key] == nil {
ret.BoolPolicy[key] = &BoolPolicy{Rules: map[string]*BoolRule{}}
ret.BoolPolicy[key] = &BoolPolicy{
Rules: map[string]*BoolRule{},
Key: node.Key,
}
}
ret.BoolPolicy[key].Rules[rule.Name] = r
case cpolicy.StringType:
Expand All @@ -46,7 +51,10 @@ func NewGetConfigPolicyReply(policy *cpolicy.ConfigPolicy) (*GetConfigPolicyRepl
r.Default = rule.Default.(ctypes.ConfigValueStr).Value
}
if ret.StringPolicy[key] == nil {
ret.StringPolicy[key] = &StringPolicy{Rules: map[string]*StringRule{}}
ret.StringPolicy[key] = &StringPolicy{
Rules: map[string]*StringRule{},
Key: node.Key,
}
}
ret.StringPolicy[key].Rules[rule.Name] = r
case cpolicy.IntegerType:
Expand All @@ -63,7 +71,10 @@ func NewGetConfigPolicyReply(policy *cpolicy.ConfigPolicy) (*GetConfigPolicyRepl
r.Minimum = int64(rule.Minimum.(ctypes.ConfigValueInt).Value)
}
if ret.IntegerPolicy[key] == nil {
ret.IntegerPolicy[key] = &IntegerPolicy{Rules: map[string]*IntegerRule{}}
ret.IntegerPolicy[key] = &IntegerPolicy{
Rules: map[string]*IntegerRule{},
Key: node.Key,
}
}
ret.IntegerPolicy[key].Rules[rule.Name] = r
case cpolicy.FloatType:
Expand All @@ -80,7 +91,10 @@ func NewGetConfigPolicyReply(policy *cpolicy.ConfigPolicy) (*GetConfigPolicyRepl
r.Minimum = rule.Minimum.(ctypes.ConfigValueFloat).Value
}
if ret.FloatPolicy[key] == nil {
ret.FloatPolicy[key] = &FloatPolicy{Rules: map[string]*FloatRule{}}
ret.FloatPolicy[key] = &FloatPolicy{
Rules: map[string]*FloatRule{},
Key: node.Key,
}
}
ret.FloatPolicy[key].Rules[rule.Name] = r
}
Expand Down Expand Up @@ -191,7 +205,20 @@ func ToConfigPolicy(reply *GetConfigPolicyReply) *cpolicy.ConfigPolicy {
}

for key, node := range nodes {
keys := strings.Split(key, ".")
var keys []string
// if the []string is present, use it.
// if not, fall back to dot separated key
if val, ok := reply.BoolPolicy[key]; ok && val != nil && val.Key != nil {
keys = val.Key
} else if val, ok := reply.StringPolicy[key]; ok && val != nil && val.Key != nil {
keys = val.Key
} else if val, ok := reply.FloatPolicy[key]; ok && val != nil && val.Key != nil {
keys = val.Key
} else if val, ok := reply.IntegerPolicy[key]; ok && val != nil && val.Key != nil {
keys = val.Key
} else {
keys = strings.Split(key, ".")
}
result.Add(keys, node)
}

Expand Down
Loading

0 comments on commit 7746a24

Please sign in to comment.