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.
This commit is contained in:
James Shubin
2016-12-07 02:39:14 -05:00
parent eb545e75fb
commit 0e316b1d55
9 changed files with 75 additions and 19 deletions

43
etcd/world.go Normal file
View File

@@ -0,0 +1,43 @@
// Mgmt
// Copyright (C) 2013-2016+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> 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 <http://www.gnu.org/licenses/>.
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)
}

View File

@@ -81,7 +81,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
Comment: "comment!", 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 return g, err
} }

View File

@@ -74,7 +74,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
vertex = v // save 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 return g, nil
} }

View File

@@ -116,7 +116,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
e2.Notify = true // send a notification from v2 to v3 e2.Notify = true // send a notification from v2 to v3
g.AddEdge(v2, v3, e2) 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 return g, nil
} }

View File

@@ -19,14 +19,23 @@
package gapi package gapi
import ( import (
"github.com/purpleidea/mgmt/etcd"
"github.com/purpleidea/mgmt/pgraph" "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. // Data is the set of input values passed into the GAPI structs via Init.
type Data struct { type Data struct {
Hostname string // uuid for the host, required for GAPI Hostname string // uuid for the host, required for GAPI
EmbdEtcd *etcd.EmbdEtcd World World
Noop bool Noop bool
NoWatch bool NoWatch bool
// NOTE: we can add more fields here if needed by GAPI endpoints // NOTE: we can add more fields here if needed by GAPI endpoints

View File

@@ -324,9 +324,13 @@ func (obj *Main) Run() error {
if obj.GAPI != nil { if obj.GAPI != nil {
data := gapi.Data{ data := gapi.Data{
Hostname: hostname, Hostname: hostname,
EmbdEtcd: EmbdEtcd, // NOTE: alternate implementations can be substituted in
Noop: obj.Noop, World: &etcd.World{
NoWatch: obj.NoWatch, Hostname: hostname,
EmbdEtcd: EmbdEtcd,
},
Noop: obj.Noop,
NoWatch: obj.NoWatch,
} }
if err := obj.GAPI.Init(data); err != nil { if err := obj.GAPI.Init(data); err != nil {
obj.Exit(fmt.Errorf("Main: GAPI: Init failed: %v", err)) obj.Exit(fmt.Errorf("Main: GAPI: Init failed: %v", err))

View File

@@ -70,7 +70,7 @@ func (obj *GAPI) Graph() (*pgraph.Graph, error) {
if config == nil { if config == nil {
return nil, fmt.Errorf("Puppet: ParseConfigFromPuppet returned 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 return g, err
} }

View File

@@ -70,7 +70,7 @@ func (obj *GAPI) Graph() (*pgraph.Graph, error) {
return nil, fmt.Errorf("yamlgraph: ParseConfigFromFile returned nil") 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 return g, err
} }

View File

@@ -26,7 +26,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/purpleidea/mgmt/etcd" "github.com/purpleidea/mgmt/gapi"
"github.com/purpleidea/mgmt/global" "github.com/purpleidea/mgmt/global"
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/resources" "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. // NewGraphFromConfig transforms a GraphConfig struct into a new graph.
// FIXME: remove any possibly left over, now obsolete graph diff code from here! // 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 // hostname is the uuid for the host
var graph *pgraph.Graph // new graph to return 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 keep = append(keep, v) // append
} else if !noop { // do not export any resources if noop } 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.SetName(res.GetName()[2:]) //slice off @@
res.SetKind(kind) // cheap init res.SetKind(kind) // cheap init
resourceList = append(resourceList, res) resourceList = append(resourceList, res)
} }
} }
} }
// store in etcd // store in backend (usually etcd)
if err := etcd.EtcdSetResources(embdEtcd, hostname, resourceList); err != nil { if err := world.ResExport(resourceList); err != nil {
return nil, fmt.Errorf("Config: Could not export resources: %v", err) 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 var hostnameFilter []string // empty to get from everyone
kindFilter := []string{} kindFilter := []string{}
for _, t := range c.Collector { for _, t := range c.Collector {
@@ -165,11 +165,11 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, embdEtcd *etcd.EmbdEtc
kind := util.FirstToUpper(t.Kind) kind := util.FirstToUpper(t.Kind)
kindFilter = append(kindFilter, 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... // 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! if len(kindFilter) > 0 { // if kindFilter is empty, don't need to do lookups!
var err error var err error
resourceList, err = etcd.EtcdGetResources(embdEtcd, hostnameFilter, kindFilter) resourceList, err = world.ResCollect(hostnameFilter, kindFilter)
if err != nil { if err != nil {
return nil, fmt.Errorf("Config: Could not collect resources: %v", err) 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 { for _, t := range c.Collector {
// XXX: should we just drop these everywhere and have the kind strings be all lowercase? // XXX: should we just drop these everywhere and have the kind strings be all lowercase?
kind := util.FirstToUpper(t.Kind) 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) log.Printf("Collect: %v; Pattern: %v", kind, t.Pattern)
// XXX: expand to more complex pattern matching here... // XXX: expand to more complex pattern matching here...