func (d *Daemon) policyAdd(path string, node *policy.Node) error { var ( currNode, parentNode *policy.Node err error ) if node.Name == "" { path, node.Name = policy.SplitNodePath(path) } else if strings.Contains(node.Name, ".") && node.Name != common.GlobalLabelPrefix { path, node.Name = policy.SplitNodePath(path + "." + node.Name) } currNode, parentNode, err = d.findNode(path) if err != nil { return err } log.Debugf("Policy currNode %+v, parentNode %+v", currNode, parentNode) // eg. path = io.cilium.lizards.foo.db and io.cilium.lizards doesn't exist if (currNode == nil && parentNode == nil) || // eg. path = io.cilium.lizards.foo and io.cilium.lizards.foo doesn't exist (currNode == nil && parentNode != nil) { pn := policy.NewNode("", nil) if err := d.policyAdd(path, pn); err != nil { return err } currNode, parentNode, err = d.findNode(path) if err != nil { return err } log.Debugf("Policy currNode %+v, parentNode %+v", currNode, parentNode) } // eg. path = io.cilium if currNode != nil && parentNode == nil { if currNode.Name == node.Name { node.Path() if err := currNode.Merge(node); err != nil { return err } } else { if err := currNode.AddChild(node.Name, node); err != nil { return err } } } else if currNode != nil && parentNode != nil { // eg. path = io.cilium.lizards.db exists if err := currNode.AddChild(node.Name, node); err != nil { return err } } return nil }
func loadPolicy(name string) (*policy.Node, error) { log.Debugf("Entering directory %s...", name) if name == "-" { return loadPolicyFile(name) } if fi, err := os.Stat(name); err != nil { return nil, err } else if fi.Mode().IsRegular() { return loadPolicyFile(name) } else if !fi.Mode().IsDir() { return nil, fmt.Errorf("Error: %s is not a file or a directory", name) } files, err := ioutil.ReadDir(name) if err != nil { return nil, err } var node *policy.Node // process all files first for _, f := range files { if f.IsDir() || ignoredFile(path.Base(f.Name())) { continue } if p, err := loadPolicyFile(filepath.Join(name, f.Name())); err != nil { return nil, err } else { if node != nil { if err := node.Merge(p); err != nil { return nil, fmt.Errorf("Error: %s: %s", f.Name(), err) } } else { node = p } } } // recursive search for _, f := range files { if f.IsDir() { if ignoredFile(path.Base(f.Name())) { continue } subpath := filepath.Join(name, f.Name()) if p, err := loadPolicy(subpath); err != nil { return nil, err } else { if p.Name == "" { return nil, fmt.Errorf("Policy node import from %s did not derive a name", subpath) } node.AddChild(p.Name, p) } } } log.Debugf("Leaving directory %s...", name) return node, nil }