From 0b59486c82a9786c85438f6352636b19b83e1021 Mon Sep 17 00:00:00 2001 From: Eric Multanen Date: Thu, 16 Apr 2020 10:44:06 -0700 Subject: Controller API support Add controller API support as baseline for adding gRPC framework. Issue-ID: MULTICLOUD-1019 Signed-off-by: Eric Multanen Change-Id: Ifd522a0eefbb8e54be45cc62003d3809283c9bfe --- src/orchestrator/pkg/module/controller.go | 75 +++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 14 deletions(-) (limited to 'src/orchestrator/pkg/module/controller.go') diff --git a/src/orchestrator/pkg/module/controller.go b/src/orchestrator/pkg/module/controller.go index 35d6f892..976b8988 100644 --- a/src/orchestrator/pkg/module/controller.go +++ b/src/orchestrator/pkg/module/controller.go @@ -27,13 +27,24 @@ import ( // Controller contains the parameters needed for Controllers // It implements the interface for managing the Controllers type Controller struct { - Name string `json:"name"` - - Host string `json:"host"` + Metadata Metadata `json:"metadata"` + Spec ControllerSpec `json:"spec"` +} - Port int64 `json:"port"` +type ControllerSpec struct { + Host string `json:"host"` + Port int `json:"port"` + Type string `json:"type"` + Priority int `json:"priority"` } +const MinControllerPriority = 1 +const MaxControllerPriority = 1000000 +const CONTROLLER_TYPE_ACTION string = "action" +const CONTROLLER_TYPE_PLACEMENT string = "placement" + +var CONTROLLER_TYPES = [...]string{CONTROLLER_TYPE_ACTION, CONTROLLER_TYPE_PLACEMENT} + // ControllerKey is the key structure that is used in the database type ControllerKey struct { ControllerName string `json:"controller-name"` @@ -52,8 +63,9 @@ func (mk ControllerKey) String() string { // ControllerManager is an interface exposes the Controller functionality type ControllerManager interface { - CreateController(ms Controller) (Controller, error) + CreateController(ms Controller, mayExist bool) (Controller, error) GetController(name string) (Controller, error) + GetControllers() ([]Controller, error) DeleteController(name string) error } @@ -74,20 +86,20 @@ func NewControllerClient() *ControllerClient { } // CreateController a new collection based on the Controller -func (mc *ControllerClient) CreateController(m Controller) (Controller, error) { +func (mc *ControllerClient) CreateController(m Controller, mayExist bool) (Controller, error) { //Construct the composite key to select the entry key := ControllerKey{ - ControllerName: m.Name, + ControllerName: m.Metadata.Name, } //Check if this Controller already exists - _, err := mc.GetController(m.Name) - if err == nil { + _, err := mc.GetController(m.Metadata.Name) + if err == nil && !mayExist { return Controller{}, pkgerrors.New("Controller already exists") } - err = db.DBconn.Create(mc.collectionName, key, mc.tagMeta, m) + err = db.DBconn.Insert(mc.collectionName, key, nil, mc.tagMeta, m) if err != nil { return Controller{}, pkgerrors.Wrap(err, "Creating DB Entry") } @@ -102,15 +114,14 @@ func (mc *ControllerClient) GetController(name string) (Controller, error) { key := ControllerKey{ ControllerName: name, } - value, err := db.DBconn.Read(mc.collectionName, key, mc.tagMeta) + value, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) if err != nil { return Controller{}, pkgerrors.Wrap(err, "Get Controller") } - //value is a byte array if value != nil { microserv := Controller{} - err = db.DBconn.Unmarshal(value, µserv) + err = db.DBconn.Unmarshal(value[0], µserv) if err != nil { return Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") } @@ -120,6 +131,42 @@ func (mc *ControllerClient) GetController(name string) (Controller, error) { return Controller{}, pkgerrors.New("Error getting Controller") } +// GetControllers returns all the Controllers that are registered +func (mc *ControllerClient) GetControllers() ([]Controller, error) { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: "", + } + + var resp []Controller + values, err := db.DBconn.Find(mc.collectionName, key, mc.tagMeta) + if err != nil { + return []Controller{}, pkgerrors.Wrap(err, "Get Controller") + } + + for _, value := range values { + microserv := Controller{} + err = db.DBconn.Unmarshal(value, µserv) + if err != nil { + return []Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") + } + + // run healthcheck + /* + err = mc.HealthCheck(microserv.Name) + if err != nil { + log.Warn("HealthCheck Failed", log.Fields{ + "Controller": microserv.Name, + }) + } + */ + resp = append(resp, microserv) + } + + return resp, nil +} + // DeleteController the Controller from database func (mc *ControllerClient) DeleteController(name string) error { @@ -127,7 +174,7 @@ func (mc *ControllerClient) DeleteController(name string) error { key := ControllerKey{ ControllerName: name, } - err := db.DBconn.Delete(name, key, mc.tagMeta) + err := db.DBconn.Remove(mc.collectionName, key) if err != nil { return pkgerrors.Wrap(err, "Delete Controller Entry;") } -- cgit 1.2.3-korg