Move config to app and connect to instance 23/87823/1
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Wed, 15 May 2019 23:47:39 +0000 (16:47 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Wed, 15 May 2019 23:47:49 +0000 (16:47 -0700)
Move config instantiation to app and connect it
to the instance to allow updates and so on.

Issue-ID: MULTICLOUD-464
Change-Id: Ic994ef78a6e0d2db5e695e33b7b8a302c74c10da
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
src/k8splugin/api/api.go
src/k8splugin/api/confighandler.go
src/k8splugin/internal/app/config.go [moved from src/k8splugin/internal/rb/config.go with 99% similarity]
src/k8splugin/internal/app/config_backend.go [moved from src/k8splugin/internal/rb/config_backend.go with 88% similarity]
src/k8splugin/internal/app/config_test.go [moved from src/k8splugin/internal/rb/config_test.go with 99% similarity]

index 5fed28a..0ddbcf8 100644 (file)
@@ -25,7 +25,7 @@ import (
 func NewRouter(defClient rb.DefinitionManager,
        profileClient rb.ProfileManager,
        instClient app.InstanceManager,
-       configClient rb.ConfigManager,
+       configClient app.ConfigManager,
        templateClient rb.ConfigTemplateManager) *mux.Router {
 
        router := mux.NewRouter()
@@ -91,7 +91,7 @@ func NewRouter(defClient rb.DefinitionManager,
 
        // Config value
        if configClient == nil {
-               configClient = rb.NewConfigClient()
+               configClient = app.NewConfigClient()
        }
        configHandler := rbConfigHandler{client: configClient}
        resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}/config", configHandler.createHandler).Methods("POST")
index 93098d6..9bd9db8 100644 (file)
@@ -18,9 +18,10 @@ package api
 
 import (
        "encoding/json"
-       "k8splugin/internal/rb"
        "net/http"
 
+       "k8splugin/internal/app"
+
        "github.com/gorilla/mux"
 )
 
@@ -29,12 +30,12 @@ import (
 type rbConfigHandler struct {
        // Interface that implements bundle Definition operations
        // We will set this variable with a mock interface for testing
-       client rb.ConfigManager
+       client app.ConfigManager
 }
 
 // createHandler handles creation of the definition entry in the database
 func (h rbConfigHandler) createHandler(w http.ResponseWriter, r *http.Request) {
-       var p rb.Config
+       var p app.Config
        vars := mux.Vars(r)
        rbName := vars["rbname"]
        rbVersion := vars["rbversion"]
@@ -73,7 +74,7 @@ func (h rbConfigHandler) createHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 // getHandler handles GET operations on a particular config
-// Returns a rb.Definition
+// Returns a app.Definition
 func (h rbConfigHandler) getHandler(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        rbName := vars["rbname"]
@@ -128,7 +129,7 @@ func (h rbConfigHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
        prName := vars["prname"]
        cfgName := vars["cfgname"]
 
-       var p rb.Config
+       var p app.Config
 
        if r.Body == nil {
                http.Error(w, "Empty body", http.StatusBadRequest)
@@ -168,7 +169,7 @@ func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request)
                return
        }
 
-       var p rb.ConfigRollback
+       var p app.ConfigRollback
        err := json.NewDecoder(r.Body).Decode(&p)
        if err != nil {
                http.Error(w, err.Error(), http.StatusUnprocessableEntity)
@@ -194,7 +195,7 @@ func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
                return
        }
 
-       var p rb.ConfigTagit
+       var p app.ConfigTagit
        err := json.NewDecoder(r.Body).Decode(&p)
        if err != nil {
                http.Error(w, err.Error(), http.StatusUnprocessableEntity)
similarity index 99%
rename from src/k8splugin/internal/rb/config.go
rename to src/k8splugin/internal/app/config.go
index 3bd8347..f7e8135 100644 (file)
  * limitations under the License.
  */
 
-package rb
+package app
 
 import (
-       pkgerrors "github.com/pkg/errors"
-       "k8splugin/internal/db"
        "strconv"
        "strings"
+
+       "k8splugin/internal/db"
+
+       pkgerrors "github.com/pkg/errors"
 )
 
 // Config contains the parameters needed for configuration
similarity index 88%
rename from src/k8splugin/internal/rb/config_backend.go
rename to src/k8splugin/internal/app/config_backend.go
index e2fa5b3..763aed0 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package rb
+package app
 
 import (
        "bytes"
@@ -25,9 +25,11 @@ import (
        "strconv"
        "strings"
        "sync"
+       "time"
 
        "k8splugin/internal/db"
        "k8splugin/internal/helm"
+       "k8splugin/internal/rb"
 
        "github.com/ghodss/yaml"
        pkgerrors "github.com/pkg/errors"
@@ -57,7 +59,8 @@ type ConfigVersionStore struct {
 
 type configResourceList struct {
        resourceTemplates []helm.KubernetesResourceTemplate
-       profile           Profile
+       createdResources  []helm.KubernetesResource
+       profile           rb.Profile
        action            string
 }
 
@@ -339,17 +342,50 @@ func scheduleResources(c chan configResourceList) {
        for {
                data := <-c
                //TODO: ADD Check to see if Application running
+               ic := NewInstanceClient()
+               resp, err := ic.Find(data.profile.RBName, data.profile.RBVersion, data.profile.ProfileName)
+               if err != nil || len(resp) == 0 {
+                       log.Println("Error finding a running instance. Retrying later...")
+                       time.Sleep(time.Second * 10)
+                       continue
+               }
                switch {
                case data.action == "POST":
                        log.Printf("[scheduleResources]: POST %v %v", data.profile, data.resourceTemplates)
+                       for _, inst := range resp {
+                               k8sClient := KubernetesClient{}
+                               err = k8sClient.init(inst.CloudRegion)
+                               if err != nil {
+                                       log.Printf("Getting CloudRegion Information: %s", err.Error())
+                                       //Move onto the next cloud region
+                                       continue
+                               }
+                               data.createdResources, err = k8sClient.createResources(data.resourceTemplates, inst.Namespace)
+                               if err != nil {
+                                       log.Printf("Error Creating resources: %s", err.Error())
+                                       continue
+                               }
+                       }
                        //TODO: Needs to add code to call Kubectl create
                case data.action == "PUT":
                        log.Printf("[scheduleResources]: PUT %v %v", data.profile, data.resourceTemplates)
                        //TODO: Needs to add code to call Kubectl apply
                case data.action == "DELETE":
                        log.Printf("[scheduleResources]: DELETE %v %v", data.profile, data.resourceTemplates)
-                       //TODO: Needs to add code to call Kubectl delete
-
+                       for _, inst := range resp {
+                               k8sClient := KubernetesClient{}
+                               err = k8sClient.init(inst.CloudRegion)
+                               if err != nil {
+                                       log.Printf("Getting CloudRegion Information: %s", err.Error())
+                                       //Move onto the next cloud region
+                                       continue
+                               }
+                               err = k8sClient.deleteResources(data.createdResources, inst.Namespace)
+                               if err != nil {
+                                       log.Printf("Error Deleting resources: %s", err.Error())
+                                       continue
+                               }
+                       }
                }
        }
 }
@@ -360,12 +396,12 @@ var resolve = func(rbName, rbVersion, profileName string, p Config) (configResou
 
        var resTemplates []helm.KubernetesResourceTemplate
 
-       profile, err := NewProfileClient().Get(rbName, rbVersion, profileName)
+       profile, err := rb.NewProfileClient().Get(rbName, rbVersion, profileName)
        if err != nil {
                return configResourceList{}, pkgerrors.Wrap(err, "Reading  Profile Data")
        }
 
-       t, err := NewConfigTemplateClient().Get(rbName, rbVersion, p.TemplateName)
+       t, err := rb.NewConfigTemplateClient().Get(rbName, rbVersion, p.TemplateName)
        if err != nil {
                return configResourceList{}, pkgerrors.Wrap(err, "Getting Template")
        }
@@ -373,7 +409,7 @@ var resolve = func(rbName, rbVersion, profileName string, p Config) (configResou
                return configResourceList{}, pkgerrors.New("Invalid template no Chart.yaml file found")
        }
 
-       def, err := NewConfigTemplateClient().Download(rbName, rbVersion, p.TemplateName)
+       def, err := rb.NewConfigTemplateClient().Download(rbName, rbVersion, p.TemplateName)
        if err != nil {
                return configResourceList{}, pkgerrors.Wrap(err, "Downloading Template")
        }
@@ -398,7 +434,7 @@ var resolve = func(rbName, rbVersion, profileName string, p Config) (configResou
        }
        defer outputfile.Close()
 
-       chartBasePath, err := ExtractTarBall(bytes.NewBuffer(def))
+       chartBasePath, err := rb.ExtractTarBall(bytes.NewBuffer(def))
        if err != nil {
                return configResourceList{}, pkgerrors.Wrap(err, "Extracting Template")
        }
similarity index 99%
rename from src/k8splugin/internal/rb/config_test.go
rename to src/k8splugin/internal/app/config_test.go
index 9bf97a5..11a300f 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package rb
+package app
 
 import (
        "k8splugin/internal/db"