From 0e316b1d55a38b3528f07db77a36056debd01ba3 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 7 Dec 2016 02:39:14 -0500 Subject: [PATCH] gapi: Add world interface and refactor existing code to use it This is the initial base of what will hopefully become a powerful API that machines will use to communicate. It will be the basis of the stateful data store that can be used for exported resources, fact exchange, state machine flags, locks, and much more. --- etcd/world.go | 43 ++++++++++++++++++++++++++++++++++++++++ examples/lib/libmgmt1.go | 2 +- examples/lib/libmgmt2.go | 2 +- examples/lib/libmgmt3.go | 2 +- gapi/gapi.go | 13 ++++++++++-- mgmtmain/main.go | 10 +++++++--- puppet/gapi.go | 2 +- yamlgraph/gapi.go | 2 +- yamlgraph/gconfig.go | 18 ++++++++--------- 9 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 etcd/world.go diff --git a/etcd/world.go b/etcd/world.go new file mode 100644 index 00000000..76efed2d --- /dev/null +++ b/etcd/world.go @@ -0,0 +1,43 @@ +// Mgmt +// Copyright (C) 2013-2016+ James Shubin and the project contributors +// Written by James Shubin and the project contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package etcd + +import ( + "github.com/purpleidea/mgmt/resources" +) + +// World is an etcd backed implementation of the World interface. +type World struct { + Hostname string // uuid for the consumer of these + EmbdEtcd *EmbdEtcd +} + +// ResExport exports a list of resources under our hostname namespace. +// Subsequent calls replace the previously set collection atomically. +func (obj *World) ResExport(resourceList []resources.Res) error { + return EtcdSetResources(obj.EmbdEtcd, obj.Hostname, resourceList) +} + +// ResCollect gets the collection of exported resources which match the filter. +// It does this atomically so that a call always returns a complete collection. +func (obj *World) ResCollect(hostnameFilter, kindFilter []string) ([]resources.Res, error) { + // XXX: should we be restricted to retrieving resources that were + // exported with a tag that allows or restricts our hostname? We could + // enforce that here if the underlying API supported it... Add this? + return EtcdGetResources(obj.EmbdEtcd, hostnameFilter, kindFilter) +} diff --git a/examples/lib/libmgmt1.go b/examples/lib/libmgmt1.go index 0865720c..61c07800 100644 --- a/examples/lib/libmgmt1.go +++ b/examples/lib/libmgmt1.go @@ -81,7 +81,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) { Comment: "comment!", } - g, err := gc.NewGraphFromConfig(obj.data.Hostname, obj.data.EmbdEtcd, obj.data.Noop) + g, err := gc.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop) return g, err } diff --git a/examples/lib/libmgmt2.go b/examples/lib/libmgmt2.go index 9f27cf76..33770416 100644 --- a/examples/lib/libmgmt2.go +++ b/examples/lib/libmgmt2.go @@ -74,7 +74,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) { vertex = v // save } - //g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.EmbdEtcd, obj.data.Noop) + //g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop) return g, nil } diff --git a/examples/lib/libmgmt3.go b/examples/lib/libmgmt3.go index 0e98ca93..c59725aa 100644 --- a/examples/lib/libmgmt3.go +++ b/examples/lib/libmgmt3.go @@ -116,7 +116,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) { e2.Notify = true // send a notification from v2 to v3 g.AddEdge(v2, v3, e2) - //g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.EmbdEtcd, obj.data.Noop) + //g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop) return g, nil } diff --git a/gapi/gapi.go b/gapi/gapi.go index 67d48247..96a7e22f 100644 --- a/gapi/gapi.go +++ b/gapi/gapi.go @@ -19,14 +19,23 @@ package gapi import ( - "github.com/purpleidea/mgmt/etcd" "github.com/purpleidea/mgmt/pgraph" + "github.com/purpleidea/mgmt/resources" ) +// World is an interface to the rest of the different graph state. It allows +// the GAPI to store state and exchange information throughout the cluster. It +// is the interface each machine uses to communicate with the rest of the world. +type World interface { // TODO: is there a better name for this interface? + ResExport([]resources.Res) error + // FIXME: should this method take a "filter" data struct instead of many args? + ResCollect(hostnameFilter, kindFilter []string) ([]resources.Res, error) +} + // Data is the set of input values passed into the GAPI structs via Init. type Data struct { Hostname string // uuid for the host, required for GAPI - EmbdEtcd *etcd.EmbdEtcd + World World Noop bool NoWatch bool // NOTE: we can add more fields here if needed by GAPI endpoints diff --git a/mgmtmain/main.go b/mgmtmain/main.go index c536196b..dccb737e 100644 --- a/mgmtmain/main.go +++ b/mgmtmain/main.go @@ -324,9 +324,13 @@ func (obj *Main) Run() error { if obj.GAPI != nil { data := gapi.Data{ Hostname: hostname, - EmbdEtcd: EmbdEtcd, - Noop: obj.Noop, - NoWatch: obj.NoWatch, + // NOTE: alternate implementations can be substituted in + World: &etcd.World{ + Hostname: hostname, + EmbdEtcd: EmbdEtcd, + }, + Noop: obj.Noop, + NoWatch: obj.NoWatch, } if err := obj.GAPI.Init(data); err != nil { obj.Exit(fmt.Errorf("Main: GAPI: Init failed: %v", err)) diff --git a/puppet/gapi.go b/puppet/gapi.go index 4faeb0a6..339437aa 100644 --- a/puppet/gapi.go +++ b/puppet/gapi.go @@ -70,7 +70,7 @@ func (obj *GAPI) Graph() (*pgraph.Graph, error) { if config == nil { return nil, fmt.Errorf("Puppet: ParseConfigFromPuppet returned nil!") } - g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.EmbdEtcd, obj.data.Noop) + g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop) return g, err } diff --git a/yamlgraph/gapi.go b/yamlgraph/gapi.go index 044f94b1..aab76d8e 100644 --- a/yamlgraph/gapi.go +++ b/yamlgraph/gapi.go @@ -70,7 +70,7 @@ func (obj *GAPI) Graph() (*pgraph.Graph, error) { return nil, fmt.Errorf("yamlgraph: ParseConfigFromFile returned nil") } - g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.EmbdEtcd, obj.data.Noop) + g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop) return g, err } diff --git a/yamlgraph/gconfig.go b/yamlgraph/gconfig.go index 4cae5df2..b5b36476 100644 --- a/yamlgraph/gconfig.go +++ b/yamlgraph/gconfig.go @@ -26,7 +26,7 @@ import ( "reflect" "strings" - "github.com/purpleidea/mgmt/etcd" + "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/global" "github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/resources" @@ -92,7 +92,7 @@ func (c *GraphConfig) Parse(data []byte) error { // NewGraphFromConfig transforms a GraphConfig struct into a new graph. // FIXME: remove any possibly left over, now obsolete graph diff code from here! -func (c *GraphConfig) NewGraphFromConfig(hostname string, embdEtcd *etcd.EmbdEtcd, noop bool) (*pgraph.Graph, error) { +func (c *GraphConfig) NewGraphFromConfig(hostname string, world gapi.World, noop bool) (*pgraph.Graph, error) { // hostname is the uuid for the host var graph *pgraph.Graph // new graph to return @@ -145,19 +145,19 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, embdEtcd *etcd.EmbdEtc keep = append(keep, v) // append } else if !noop { // do not export any resources if noop - // store for addition to etcd storage... + // store for addition to backend storage... res.SetName(res.GetName()[2:]) //slice off @@ res.SetKind(kind) // cheap init resourceList = append(resourceList, res) } } } - // store in etcd - if err := etcd.EtcdSetResources(embdEtcd, hostname, resourceList); err != nil { + // store in backend (usually etcd) + if err := world.ResExport(resourceList); err != nil { return nil, fmt.Errorf("Config: Could not export resources: %v", err) } - // lookup from etcd + // lookup from backend (usually etcd) var hostnameFilter []string // empty to get from everyone kindFilter := []string{} for _, t := range c.Collector { @@ -165,11 +165,11 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, embdEtcd *etcd.EmbdEtc kind := util.FirstToUpper(t.Kind) kindFilter = append(kindFilter, kind) } - // do all the graph look ups in one single step, so that if the etcd + // do all the graph look ups in one single step, so that if the backend // database changes, we don't have a partial state of affairs... if len(kindFilter) > 0 { // if kindFilter is empty, don't need to do lookups! var err error - resourceList, err = etcd.EtcdGetResources(embdEtcd, hostnameFilter, kindFilter) + resourceList, err = world.ResCollect(hostnameFilter, kindFilter) if err != nil { return nil, fmt.Errorf("Config: Could not collect resources: %v", err) } @@ -180,7 +180,7 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, embdEtcd *etcd.EmbdEtc for _, t := range c.Collector { // XXX: should we just drop these everywhere and have the kind strings be all lowercase? kind := util.FirstToUpper(t.Kind) - // use t.Kind and optionally t.Pattern to collect from etcd storage + // use t.Kind and optionally t.Pattern to collect from storage log.Printf("Collect: %v; Pattern: %v", kind, t.Pattern) // XXX: expand to more complex pattern matching here...