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()
// 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")
import (
"encoding/json"
- "k8splugin/internal/rb"
"net/http"
+ "k8splugin/internal/app"
+
"github.com/gorilla/mux"
)
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"]
}
// 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"]
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)
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)
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)
* limitations under the License.
*/
-package rb
+package app
import (
"bytes"
"strconv"
"strings"
"sync"
+ "time"
"k8splugin/internal/db"
"k8splugin/internal/helm"
+ "k8splugin/internal/rb"
"github.com/ghodss/yaml"
pkgerrors "github.com/pkg/errors"
type configResourceList struct {
resourceTemplates []helm.KubernetesResourceTemplate
- profile Profile
+ createdResources []helm.KubernetesResource
+ profile rb.Profile
action string
}
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
+ }
+ }
}
}
}
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")
}
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")
}
}
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")
}