summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/k8splugin/api/api.go79
-rw-r--r--src/k8splugin/api/defhandler.go2
-rw-r--r--src/k8splugin/api/defhandler_test.go2
-rw-r--r--src/k8splugin/api/handler.go11
-rw-r--r--src/k8splugin/api/handler_test.go8
-rw-r--r--src/k8splugin/api/profilehandler.go2
-rw-r--r--src/k8splugin/api/profilehandler_test.go2
-rw-r--r--src/k8splugin/cmd/main.go3
-rw-r--r--src/k8splugin/internal/app/client.go (renamed from src/k8splugin/krd/krd.go)2
-rw-r--r--src/k8splugin/internal/app/client_test.go (renamed from src/k8splugin/krd/krd_test.go)4
-rw-r--r--src/k8splugin/internal/app/vnfhelper.go (renamed from src/k8splugin/csar/parser.go)18
-rw-r--r--src/k8splugin/internal/app/vnfhelper_test.go (renamed from src/k8splugin/csar/parser_test.go)26
-rw-r--r--src/k8splugin/internal/db/consul.go (renamed from src/k8splugin/db/consul.go)0
-rw-r--r--src/k8splugin/internal/db/consul_test.go (renamed from src/k8splugin/db/consul_test.go)0
-rw-r--r--src/k8splugin/internal/db/mongo.go (renamed from src/k8splugin/db/mongo.go)0
-rw-r--r--src/k8splugin/internal/db/mongo_test.go (renamed from src/k8splugin/db/mongo_test.go)0
-rw-r--r--src/k8splugin/internal/db/store.go (renamed from src/k8splugin/db/store.go)0
-rw-r--r--src/k8splugin/internal/db/store_test.go (renamed from src/k8splugin/db/store_test.go)0
-rw-r--r--src/k8splugin/internal/db/testing.go (renamed from src/k8splugin/db/testing.go)0
-rw-r--r--src/k8splugin/internal/rb/archive.go (renamed from src/k8splugin/rb/archive.go)0
-rw-r--r--src/k8splugin/internal/rb/archive_test.go (renamed from src/k8splugin/rb/archive_test.go)0
-rw-r--r--src/k8splugin/internal/rb/definition.go (renamed from src/k8splugin/rb/definition.go)2
-rw-r--r--src/k8splugin/internal/rb/definition_test.go (renamed from src/k8splugin/rb/definition_test.go)2
-rw-r--r--src/k8splugin/internal/rb/profile.go (renamed from src/k8splugin/rb/profile.go)2
-rw-r--r--src/k8splugin/internal/rb/profile_test.go (renamed from src/k8splugin/rb/profile_test.go)2
-rw-r--r--src/k8splugin/internal/utils.go (renamed from src/k8splugin/krd/plugins.go)74
-rw-r--r--src/k8splugin/internal/utils_test.go (renamed from src/k8splugin/krd/plugins_test.go)4
-rw-r--r--src/k8splugin/mock_files/mock_plugins/mockplugin.go4
-rw-r--r--src/k8splugin/plugins/deployment/plugin.go10
-rw-r--r--src/k8splugin/plugins/deployment/plugin_test.go8
-rw-r--r--src/k8splugin/plugins/namespace/plugin.go8
-rw-r--r--src/k8splugin/plugins/namespace/plugin_test.go6
-rw-r--r--src/k8splugin/plugins/network/plugin.go16
-rw-r--r--src/k8splugin/plugins/network/plugin_test.go25
-rw-r--r--src/k8splugin/plugins/service/plugin.go10
-rw-r--r--src/k8splugin/plugins/service/plugin_test.go8
36 files changed, 169 insertions, 171 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go
index 593e2b0b..b566609b 100644
--- a/src/k8splugin/api/api.go
+++ b/src/k8splugin/api/api.go
@@ -14,88 +14,11 @@ limitations under the License.
package api
import (
- "k8splugin/rb"
- "os"
- "path/filepath"
- "plugin"
- "strings"
+ "k8splugin/internal/rb"
"github.com/gorilla/mux"
- pkgerrors "github.com/pkg/errors"
-
- "k8splugin/db"
- "k8splugin/krd"
)
-// CheckEnvVariables checks for required Environment variables
-func CheckEnvVariables() error {
- envList := []string{"CSAR_DIR", "KUBE_CONFIG_DIR", "PLUGINS_DIR",
- "DATABASE_TYPE", "DATABASE_IP", "OVN_CENTRAL_ADDRESS"}
- for _, env := range envList {
- if _, ok := os.LookupEnv(env); !ok {
- return pkgerrors.New("environment variable " + env + " not set")
- }
- }
-
- return nil
-}
-
-// CheckDatabaseConnection checks if the database is up and running and
-// plugin can talk to it
-func CheckDatabaseConnection() error {
- err := db.CreateDBClient(os.Getenv("DATABASE_TYPE"))
- if err != nil {
- return pkgerrors.Cause(err)
- }
-
- err = db.DBconn.HealthCheck()
- if err != nil {
- return pkgerrors.Cause(err)
- }
- return nil
-}
-
-// LoadPlugins loads all the compiled .so plugins
-func LoadPlugins() error {
- pluginsDir := os.Getenv("PLUGINS_DIR")
- err := filepath.Walk(pluginsDir,
- func(path string, info os.FileInfo, err error) error {
- if strings.Contains(path, ".so") {
- p, err := plugin.Open(path)
- if err != nil {
- return pkgerrors.Cause(err)
- }
- krd.LoadedPlugins[info.Name()[:len(info.Name())-3]] = p
- }
- return err
- })
- if err != nil {
- return err
- }
-
- return nil
-}
-
-// CheckInitialSettings is used to check initial settings required to start api
-func CheckInitialSettings() error {
- err := CheckEnvVariables()
- if err != nil {
- return pkgerrors.Cause(err)
- }
-
- err = CheckDatabaseConnection()
- if err != nil {
- return pkgerrors.Cause(err)
- }
-
- err = LoadPlugins()
- if err != nil {
- return pkgerrors.Cause(err)
- }
-
- return nil
-}
-
// NewRouter creates a router instance that serves the VNFInstance web methods
func NewRouter(kubeconfig string) *mux.Router {
router := mux.NewRouter()
diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go
index f53acdd2..21e5e768 100644
--- a/src/k8splugin/api/defhandler.go
+++ b/src/k8splugin/api/defhandler.go
@@ -19,7 +19,7 @@ package api
import (
"encoding/json"
"io/ioutil"
- "k8splugin/rb"
+ "k8splugin/internal/rb"
"net/http"
"github.com/gorilla/mux"
diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go
index e638ca05..20ef537e 100644
--- a/src/k8splugin/api/defhandler_test.go
+++ b/src/k8splugin/api/defhandler_test.go
@@ -20,7 +20,7 @@ import (
"bytes"
"encoding/json"
"io"
- "k8splugin/rb"
+ "k8splugin/internal/rb"
"net/http"
"net/http/httptest"
"reflect"
diff --git a/src/k8splugin/api/handler.go b/src/k8splugin/api/handler.go
index 4c49ba78..a6db3c18 100644
--- a/src/k8splugin/api/handler.go
+++ b/src/k8splugin/api/handler.go
@@ -25,9 +25,8 @@ import (
pkgerrors "github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
- "k8splugin/csar"
- "k8splugin/db"
- "k8splugin/krd"
+ helper "k8splugin/internal/app"
+ "k8splugin/internal/db"
)
//TODO: Separate the http handler code and backend code out
@@ -36,7 +35,7 @@ var tagData = "data"
// GetVNFClient retrieves the client used to communicate with a Kubernetes Cluster
var GetVNFClient = func(kubeConfigPath string) (kubernetes.Clientset, error) {
- client, err := krd.GetKubeClient(kubeConfigPath)
+ client, err := helper.GetKubeClient(kubeConfigPath)
if err != nil {
return client, err
}
@@ -105,7 +104,7 @@ func CreateHandler(w http.ResponseWriter, r *http.Request) {
},
nil
*/
- externalVNFID, resourceNameMap, err := csar.CreateVNF(resource.CsarID, resource.CloudRegionID, resource.Namespace, &kubeclient)
+ externalVNFID, resourceNameMap, err := helper.CreateVNF(resource.CsarID, resource.CloudRegionID, resource.Namespace, &kubeclient)
if err != nil {
werr := pkgerrors.Wrap(err, "Read Kubernetes Data information error")
http.Error(w, werr.Error(), http.StatusInternalServerError)
@@ -223,7 +222,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
- err = csar.DestroyVNF(data, namespace, &kubeclient)
+ err = helper.DestroyVNF(data, namespace, &kubeclient)
if err != nil {
werr := pkgerrors.Wrap(err, "Delete VNF error")
http.Error(w, werr.Error(), http.StatusInternalServerError)
diff --git a/src/k8splugin/api/handler_test.go b/src/k8splugin/api/handler_test.go
index d02b8515..2bac3111 100644
--- a/src/k8splugin/api/handler_test.go
+++ b/src/k8splugin/api/handler_test.go
@@ -27,8 +27,8 @@ import (
pkgerrors "github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
- "k8splugin/csar"
- "k8splugin/db"
+ helper "k8splugin/internal/app"
+ "k8splugin/internal/db"
)
type mockCSAR struct {
@@ -155,7 +155,7 @@ func TestCreateHandler(t *testing.T) {
return kubernetes.Clientset{}, testCase.mockGetVNFClientErr
}
if testCase.mockCreateVNF != nil {
- csar.CreateVNF = testCase.mockCreateVNF.CreateVNF
+ helper.CreateVNF = testCase.mockCreateVNF.CreateVNF
}
if testCase.mockStore != nil {
db.DBconn = testCase.mockStore
@@ -328,7 +328,7 @@ func TestDeleteHandler(t *testing.T) {
db.DBconn = testCase.mockStore
}
if testCase.mockDeleteVNF != nil {
- csar.DestroyVNF = testCase.mockDeleteVNF.DestroyVNF
+ helper.DestroyVNF = testCase.mockDeleteVNF.DestroyVNF
}
request, _ := http.NewRequest("DELETE", "/v1/vnf_instances/cloudregion1/testnamespace/uuid1", nil)
diff --git a/src/k8splugin/api/profilehandler.go b/src/k8splugin/api/profilehandler.go
index 1090efe5..86e0d47f 100644
--- a/src/k8splugin/api/profilehandler.go
+++ b/src/k8splugin/api/profilehandler.go
@@ -19,7 +19,7 @@ package api
import (
"encoding/json"
"io/ioutil"
- "k8splugin/rb"
+ "k8splugin/internal/rb"
"net/http"
"github.com/gorilla/mux"
diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go
index 49efd659..7594afeb 100644
--- a/src/k8splugin/api/profilehandler_test.go
+++ b/src/k8splugin/api/profilehandler_test.go
@@ -20,7 +20,7 @@ import (
"bytes"
"encoding/json"
"io"
- "k8splugin/rb"
+ "k8splugin/internal/rb"
"net/http"
"net/http/httptest"
"reflect"
diff --git a/src/k8splugin/cmd/main.go b/src/k8splugin/cmd/main.go
index ee676549..c07446a7 100644
--- a/src/k8splugin/cmd/main.go
+++ b/src/k8splugin/cmd/main.go
@@ -26,6 +26,7 @@ import (
"k8s.io/client-go/util/homedir"
"k8splugin/api"
+ utils "k8splugin/internal"
)
func main() {
@@ -37,7 +38,7 @@ func main() {
}
flag.Parse()
- err := api.CheckInitialSettings()
+ err := utils.CheckInitialSettings()
if err != nil {
log.Fatal(err)
}
diff --git a/src/k8splugin/krd/krd.go b/src/k8splugin/internal/app/client.go
index 2d06e104..3555afdd 100644
--- a/src/k8splugin/krd/krd.go
+++ b/src/k8splugin/internal/app/client.go
@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package krd
+package app
import (
"errors"
diff --git a/src/k8splugin/krd/krd_test.go b/src/k8splugin/internal/app/client_test.go
index ad52f7e4..9d892633 100644
--- a/src/k8splugin/krd/krd_test.go
+++ b/src/k8splugin/internal/app/client_test.go
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package krd
+package app
import (
"reflect"
@@ -23,7 +23,7 @@ import (
func TestGetKubeClient(t *testing.T) {
t.Run("Successfully create Kube Client", func(t *testing.T) {
- clientset, err := GetKubeClient("../mock_files/mock_configs/mock_config")
+ clientset, err := GetKubeClient("../../mock_files/mock_configs/mock_config")
if err != nil {
t.Fatalf("TestGetKubeClient returned an error (%s)", err)
}
diff --git a/src/k8splugin/csar/parser.go b/src/k8splugin/internal/app/vnfhelper.go
index c0dd6a79..3deb9f21 100644
--- a/src/k8splugin/csar/parser.go
+++ b/src/k8splugin/internal/app/vnfhelper.go
@@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package csar
+package app
import (
"encoding/hex"
@@ -25,7 +25,7 @@ import (
pkgerrors "github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
- "k8splugin/krd"
+ utils "k8splugin/internal"
)
func generateExternalVNFID() string {
@@ -35,7 +35,7 @@ func generateExternalVNFID() string {
}
func ensuresNamespace(namespace string, kubeclient kubernetes.Interface) error {
- namespacePlugin, ok := krd.LoadedPlugins["namespace"]
+ namespacePlugin, ok := utils.LoadedPlugins["namespace"]
if !ok {
return pkgerrors.New("No plugin for namespace resource found")
}
@@ -57,11 +57,11 @@ func ensuresNamespace(namespace string, kubeclient kubernetes.Interface) error {
if err != nil {
return pkgerrors.Wrap(err, "Error fetching create namespace plugin")
}
- namespaceResource := &krd.ResourceData{
+ namespaceResource := &utils.ResourceData{
Namespace: namespace,
}
- _, err = symGetNamespaceFunc.(func(*krd.ResourceData, kubernetes.Interface) (string, error))(
+ _, err = symGetNamespaceFunc.(func(*utils.ResourceData, kubernetes.Interface) (string, error))(
namespaceResource, kubeclient)
if err != nil {
return pkgerrors.Wrap(err, "Error creating "+namespace+" namespace")
@@ -102,13 +102,13 @@ var CreateVNF = func(csarID string, cloudRegionID string, namespace string, kube
}
log.Println("Processing file: " + path)
- genericKubeData := &krd.ResourceData{
+ genericKubeData := &utils.ResourceData{
YamlFilePath: path,
Namespace: namespace,
VnfId: internalVNFID,
}
- typePlugin, ok := krd.LoadedPlugins[resource]
+ typePlugin, ok := utils.LoadedPlugins[resource]
if !ok {
return "", nil, pkgerrors.New("No plugin for resource " + resource + " found")
}
@@ -118,7 +118,7 @@ var CreateVNF = func(csarID string, cloudRegionID string, namespace string, kube
return "", nil, pkgerrors.Wrap(err, "Error fetching "+resource+" plugin")
}
- internalResourceName, err := symCreateResourceFunc.(func(*krd.ResourceData, kubernetes.Interface) (string, error))(
+ internalResourceName, err := symCreateResourceFunc.(func(*utils.ResourceData, kubernetes.Interface) (string, error))(
genericKubeData, kubeclient)
if err != nil {
return "", nil, pkgerrors.Wrap(err, "Error in plugin "+resource+" plugin")
@@ -142,7 +142,7 @@ var DestroyVNF = func(data map[string][]string, namespace string, kubeclient *ku
*/
for resourceName, resourceList := range data {
- typePlugin, ok := krd.LoadedPlugins[resourceName]
+ typePlugin, ok := utils.LoadedPlugins[resourceName]
if !ok {
return pkgerrors.New("No plugin for resource " + resourceName + " found")
}
diff --git a/src/k8splugin/csar/parser_test.go b/src/k8splugin/internal/app/vnfhelper_test.go
index 93473bc6..81bea627 100644
--- a/src/k8splugin/csar/parser_test.go
+++ b/src/k8splugin/internal/app/vnfhelper_test.go
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package csar
+package app
import (
"io/ioutil"
@@ -22,20 +22,20 @@ import (
"plugin"
"testing"
+ yaml "gopkg.in/yaml.v2"
"k8s.io/client-go/kubernetes"
pkgerrors "github.com/pkg/errors"
- "gopkg.in/yaml.v2"
- "k8splugin/krd"
+ utils "k8splugin/internal"
)
func LoadMockPlugins(krdLoadedPlugins *map[string]*plugin.Plugin) error {
- if _, err := os.Stat("../mock_files/mock_plugins/mockplugin.so"); os.IsNotExist(err) {
+ if _, err := os.Stat("../../mock_files/mock_plugins/mockplugin.so"); os.IsNotExist(err) {
return pkgerrors.New("mockplugin.so does not exist. Please compile mockplugin.go to generate")
}
- mockPlugin, err := plugin.Open("../mock_files/mock_plugins/mockplugin.so")
+ mockPlugin, err := plugin.Open("../../mock_files/mock_plugins/mockplugin.so")
if err != nil {
return pkgerrors.Cause(err)
}
@@ -48,15 +48,15 @@ func LoadMockPlugins(krdLoadedPlugins *map[string]*plugin.Plugin) error {
}
func TestCreateVNF(t *testing.T) {
- oldkrdPluginData := krd.LoadedPlugins
+ oldkrdPluginData := utils.LoadedPlugins
oldReadMetadataFile := ReadMetadataFile
defer func() {
- krd.LoadedPlugins = oldkrdPluginData
+ utils.LoadedPlugins = oldkrdPluginData
ReadMetadataFile = oldReadMetadataFile
}()
- err := LoadMockPlugins(&krd.LoadedPlugins)
+ err := LoadMockPlugins(&utils.LoadedPlugins)
if err != nil {
t.Fatalf("TestCreateVNF returned an error (%s)", err)
}
@@ -65,7 +65,7 @@ func TestCreateVNF(t *testing.T) {
var seqFile MetadataFile
if _, err := os.Stat(yamlFilePath); err == nil {
- rawBytes, err := ioutil.ReadFile("../mock_files/mock_yamls/metadata.yaml")
+ rawBytes, err := ioutil.ReadFile("../../mock_files/mock_yamls/metadata.yaml")
if err != nil {
return seqFile, pkgerrors.Wrap(err, "Metadata YAML file read error")
}
@@ -97,13 +97,13 @@ func TestCreateVNF(t *testing.T) {
}
func TestDeleteVNF(t *testing.T) {
- oldkrdPluginData := krd.LoadedPlugins
+ oldkrdPluginData := utils.LoadedPlugins
defer func() {
- krd.LoadedPlugins = oldkrdPluginData
+ utils.LoadedPlugins = oldkrdPluginData
}()
- err := LoadMockPlugins(&krd.LoadedPlugins)
+ err := LoadMockPlugins(&utils.LoadedPlugins)
if err != nil {
t.Fatalf("TestCreateVNF returned an error (%s)", err)
}
@@ -125,7 +125,7 @@ func TestDeleteVNF(t *testing.T) {
func TestReadMetadataFile(t *testing.T) {
t.Run("Successfully read Metadata YAML file", func(t *testing.T) {
- _, err := ReadMetadataFile("../mock_files//mock_yamls/metadata.yaml")
+ _, err := ReadMetadataFile("../../mock_files//mock_yamls/metadata.yaml")
if err != nil {
t.Fatalf("TestReadMetadataFile returned an error (%s)", err)
}
diff --git a/src/k8splugin/db/consul.go b/src/k8splugin/internal/db/consul.go
index a61a4c10..a61a4c10 100644
--- a/src/k8splugin/db/consul.go
+++ b/src/k8splugin/internal/db/consul.go
diff --git a/src/k8splugin/db/consul_test.go b/src/k8splugin/internal/db/consul_test.go
index 754112ad..754112ad 100644
--- a/src/k8splugin/db/consul_test.go
+++ b/src/k8splugin/internal/db/consul_test.go
diff --git a/src/k8splugin/db/mongo.go b/src/k8splugin/internal/db/mongo.go
index 05976b12..05976b12 100644
--- a/src/k8splugin/db/mongo.go
+++ b/src/k8splugin/internal/db/mongo.go
diff --git a/src/k8splugin/db/mongo_test.go b/src/k8splugin/internal/db/mongo_test.go
index 1663e774..1663e774 100644
--- a/src/k8splugin/db/mongo_test.go
+++ b/src/k8splugin/internal/db/mongo_test.go
diff --git a/src/k8splugin/db/store.go b/src/k8splugin/internal/db/store.go
index a235597a..a235597a 100644
--- a/src/k8splugin/db/store.go
+++ b/src/k8splugin/internal/db/store.go
diff --git a/src/k8splugin/db/store_test.go b/src/k8splugin/internal/db/store_test.go
index eed7065f..eed7065f 100644
--- a/src/k8splugin/db/store_test.go
+++ b/src/k8splugin/internal/db/store_test.go
diff --git a/src/k8splugin/db/testing.go b/src/k8splugin/internal/db/testing.go
index 003399af..003399af 100644
--- a/src/k8splugin/db/testing.go
+++ b/src/k8splugin/internal/db/testing.go
diff --git a/src/k8splugin/rb/archive.go b/src/k8splugin/internal/rb/archive.go
index 8eb0fbed..8eb0fbed 100644
--- a/src/k8splugin/rb/archive.go
+++ b/src/k8splugin/internal/rb/archive.go
diff --git a/src/k8splugin/rb/archive_test.go b/src/k8splugin/internal/rb/archive_test.go
index a327dfd4..a327dfd4 100644
--- a/src/k8splugin/rb/archive_test.go
+++ b/src/k8splugin/internal/rb/archive_test.go
diff --git a/src/k8splugin/rb/definition.go b/src/k8splugin/internal/rb/definition.go
index 084abe7b..8a26332b 100644
--- a/src/k8splugin/rb/definition.go
+++ b/src/k8splugin/internal/rb/definition.go
@@ -19,7 +19,7 @@ package rb
import (
"bytes"
"encoding/base64"
- "k8splugin/db"
+ "k8splugin/internal/db"
"log"
uuid "github.com/hashicorp/go-uuid"
diff --git a/src/k8splugin/rb/definition_test.go b/src/k8splugin/internal/rb/definition_test.go
index f1ec18ee..46ab3c07 100644
--- a/src/k8splugin/rb/definition_test.go
+++ b/src/k8splugin/internal/rb/definition_test.go
@@ -19,7 +19,7 @@
package rb
import (
- "k8splugin/db"
+ "k8splugin/internal/db"
"reflect"
"sort"
"strings"
diff --git a/src/k8splugin/rb/profile.go b/src/k8splugin/internal/rb/profile.go
index bbd43fea..a0245af1 100644
--- a/src/k8splugin/rb/profile.go
+++ b/src/k8splugin/internal/rb/profile.go
@@ -19,7 +19,7 @@ package rb
import (
"bytes"
"encoding/base64"
- "k8splugin/db"
+ "k8splugin/internal/db"
"log"
uuid "github.com/hashicorp/go-uuid"
diff --git a/src/k8splugin/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go
index 2540d3ca..15ff8951 100644
--- a/src/k8splugin/rb/profile_test.go
+++ b/src/k8splugin/internal/rb/profile_test.go
@@ -19,7 +19,7 @@
package rb
import (
- "k8splugin/db"
+ "k8splugin/internal/db"
"reflect"
"sort"
"strings"
diff --git a/src/k8splugin/krd/plugins.go b/src/k8splugin/internal/utils.go
index 1086a2bb..4b28b688 100644
--- a/src/k8splugin/krd/plugins.go
+++ b/src/k8splugin/internal/utils.go
@@ -11,13 +11,16 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package krd
+package utils
import (
"io/ioutil"
+ "k8splugin/internal/db"
"log"
"os"
+ "path/filepath"
"plugin"
+ "strings"
pkgerrors "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
@@ -61,3 +64,72 @@ var DecodeYAML = func(path string, into runtime.Object) (runtime.Object, error)
return obj, nil
}
+
+// CheckEnvVariables checks for required Environment variables
+func CheckEnvVariables() error {
+ envList := []string{"CSAR_DIR", "KUBE_CONFIG_DIR", "PLUGINS_DIR",
+ "DATABASE_TYPE", "DATABASE_IP", "OVN_CENTRAL_ADDRESS"}
+ for _, env := range envList {
+ if _, ok := os.LookupEnv(env); !ok {
+ return pkgerrors.New("environment variable " + env + " not set")
+ }
+ }
+
+ return nil
+}
+
+// CheckDatabaseConnection checks if the database is up and running and
+// plugin can talk to it
+func CheckDatabaseConnection() error {
+ err := db.CreateDBClient(os.Getenv("DATABASE_TYPE"))
+ if err != nil {
+ return pkgerrors.Cause(err)
+ }
+
+ err = db.DBconn.HealthCheck()
+ if err != nil {
+ return pkgerrors.Cause(err)
+ }
+ return nil
+}
+
+// LoadPlugins loads all the compiled .so plugins
+func LoadPlugins() error {
+ pluginsDir := os.Getenv("PLUGINS_DIR")
+ err := filepath.Walk(pluginsDir,
+ func(path string, info os.FileInfo, err error) error {
+ if strings.Contains(path, ".so") {
+ p, err := plugin.Open(path)
+ if err != nil {
+ return pkgerrors.Cause(err)
+ }
+ LoadedPlugins[info.Name()[:len(info.Name())-3]] = p
+ }
+ return err
+ })
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// CheckInitialSettings is used to check initial settings required to start api
+func CheckInitialSettings() error {
+ err := CheckEnvVariables()
+ if err != nil {
+ return pkgerrors.Cause(err)
+ }
+
+ err = CheckDatabaseConnection()
+ if err != nil {
+ return pkgerrors.Cause(err)
+ }
+
+ err = LoadPlugins()
+ if err != nil {
+ return pkgerrors.Cause(err)
+ }
+
+ return nil
+}
diff --git a/src/k8splugin/krd/plugins_test.go b/src/k8splugin/internal/utils_test.go
index 46499adb..6b49d427 100644
--- a/src/k8splugin/krd/plugins_test.go
+++ b/src/k8splugin/internal/utils_test.go
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
-package krd
+package utils
import (
"strings"
@@ -39,7 +39,7 @@ func TestDecodeYAML(t *testing.T) {
},
{
label: "Fail to read invalid YAML format",
- input: "./plugins_test.go",
+ input: "./utils_test.go",
expectedError: "mapping values are not allowed in this contex",
},
{
diff --git a/src/k8splugin/mock_files/mock_plugins/mockplugin.go b/src/k8splugin/mock_files/mock_plugins/mockplugin.go
index c31e4fe2..80a9d594 100644
--- a/src/k8splugin/mock_files/mock_plugins/mockplugin.go
+++ b/src/k8splugin/mock_files/mock_plugins/mockplugin.go
@@ -16,13 +16,13 @@ package main
import (
"k8s.io/client-go/kubernetes"
- "k8splugin/krd"
+ utils "k8splugin/internal"
)
func main() {}
// Create object in a specific Kubernetes resource
-func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
+func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) {
return "externalUUID", nil
}
diff --git a/src/k8splugin/plugins/deployment/plugin.go b/src/k8splugin/plugins/deployment/plugin.go
index 84d01a7d..b500c86e 100644
--- a/src/k8splugin/plugins/deployment/plugin.go
+++ b/src/k8splugin/plugins/deployment/plugin.go
@@ -22,16 +22,16 @@ import (
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
- "k8splugin/krd"
+ utils "k8splugin/internal"
)
// Create deployment object in a specific Kubernetes cluster
-func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
+func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) {
namespace := data.Namespace
if namespace == "" {
namespace = "default"
}
- obj, err := krd.DecodeYAML(data.YamlFilePath, nil)
+ obj, err := utils.DecodeYAML(data.YamlFilePath, nil)
if err != nil {
return "", pkgerrors.Wrap(err, "Decode deployment object error")
}
@@ -58,7 +58,7 @@ func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
}
opts := metaV1.ListOptions{
- Limit: krd.ResourcesListLimit,
+ Limit: utils.ResourcesListLimit,
}
opts.APIVersion = "apps/v1"
opts.Kind = "Deployment"
@@ -68,7 +68,7 @@ func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
return nil, pkgerrors.Wrap(err, "Get Deployment list error")
}
- result := make([]string, 0, krd.ResourcesListLimit)
+ result := make([]string, 0, utils.ResourcesListLimit)
if list != nil {
for _, deployment := range list.Items {
log.Printf("%v", deployment.Name)
diff --git a/src/k8splugin/plugins/deployment/plugin_test.go b/src/k8splugin/plugins/deployment/plugin_test.go
index c8dcb29e..bfe5fe39 100644
--- a/src/k8splugin/plugins/deployment/plugin_test.go
+++ b/src/k8splugin/plugins/deployment/plugin_test.go
@@ -20,7 +20,7 @@ import (
"strings"
"testing"
- "k8splugin/krd"
+ utils "k8splugin/internal"
appsV1 "k8s.io/api/apps/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -33,14 +33,14 @@ func TestCreateDeployment(t *testing.T) {
internalVNFID := "1"
testCases := []struct {
label string
- input *krd.ResourceData
+ input *utils.ResourceData
clientOutput *appsV1.Deployment
expectedResult string
expectedError string
}{
{
label: "Fail to create a deployment with invalid type",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
YamlFilePath: "../../mock_files/mock_yamls/service.yaml",
},
clientOutput: &appsV1.Deployment{},
@@ -48,7 +48,7 @@ func TestCreateDeployment(t *testing.T) {
},
{
label: "Successfully create a deployment",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
VnfId: internalVNFID,
YamlFilePath: "../../mock_files/mock_yamls/deployment.yaml",
},
diff --git a/src/k8splugin/plugins/namespace/plugin.go b/src/k8splugin/plugins/namespace/plugin.go
index de2f4f8b..6f823918 100644
--- a/src/k8splugin/plugins/namespace/plugin.go
+++ b/src/k8splugin/plugins/namespace/plugin.go
@@ -23,11 +23,11 @@ import (
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8splugin/krd"
+ utils "k8splugin/internal"
)
// Create a namespace object in a specific Kubernetes cluster
-func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
+func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) {
namespace := &coreV1.Namespace{
ObjectMeta: metaV1.ObjectMeta{
Name: data.Namespace,
@@ -74,7 +74,7 @@ func Delete(name string, namespace string, client kubernetes.Interface) error {
// List of existing namespaces hosted in a specific Kubernetes cluster
func List(namespace string, client kubernetes.Interface) ([]string, error) {
opts := metaV1.ListOptions{
- Limit: krd.ResourcesListLimit,
+ Limit: utils.ResourcesListLimit,
}
opts.APIVersion = "apps/v1"
opts.Kind = "Namespace"
@@ -84,7 +84,7 @@ func List(namespace string, client kubernetes.Interface) ([]string, error) {
return nil, pkgerrors.Wrap(err, "Get Namespace list error")
}
- result := make([]string, 0, krd.ResourcesListLimit)
+ result := make([]string, 0, utils.ResourcesListLimit)
if list != nil {
for _, deployment := range list.Items {
log.Printf("%v", deployment.Name)
diff --git a/src/k8splugin/plugins/namespace/plugin_test.go b/src/k8splugin/plugins/namespace/plugin_test.go
index 8a9fc5ac..2f1475d7 100644
--- a/src/k8splugin/plugins/namespace/plugin_test.go
+++ b/src/k8splugin/plugins/namespace/plugin_test.go
@@ -20,7 +20,7 @@ import (
"strings"
"testing"
- "k8splugin/krd"
+ utils "k8splugin/internal"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,14 +31,14 @@ func TestCreateNamespace(t *testing.T) {
namespace := "test1"
testCases := []struct {
label string
- input *krd.ResourceData
+ input *utils.ResourceData
clientOutput *coreV1.Namespace
expectedResult string
expectedError string
}{
{
label: "Successfully create a namespace",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
Namespace: namespace,
},
clientOutput: &coreV1.Namespace{},
diff --git a/src/k8splugin/plugins/network/plugin.go b/src/k8splugin/plugins/network/plugin.go
index d54fc429..fb163189 100644
--- a/src/k8splugin/plugins/network/plugin.go
+++ b/src/k8splugin/plugins/network/plugin.go
@@ -14,11 +14,13 @@ limitations under the License.
package main
import (
- pkgerrors "github.com/pkg/errors"
- "k8s.io/client-go/kubernetes"
- "k8splugin/krd"
"k8splugin/plugins/network/v1"
"regexp"
+
+ utils "k8splugin/internal"
+
+ pkgerrors "github.com/pkg/errors"
+ "k8s.io/client-go/kubernetes"
)
func extractData(data string) (vnfID, cniType, networkName string) {
@@ -34,9 +36,9 @@ func extractData(data string) (vnfID, cniType, networkName string) {
}
// Create an ONAP Network object
-func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
+func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) {
network := &v1.OnapNetwork{}
- if _, err := krd.DecodeYAML(data.YamlFilePath, network); err != nil {
+ if _, err := utils.DecodeYAML(data.YamlFilePath, network); err != nil {
return "", pkgerrors.Wrap(err, "Decode network object error")
}
@@ -46,7 +48,7 @@ func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error)
}
cniType := config["cnitype"].(string)
- typePlugin, ok := krd.LoadedPlugins[cniType+"-network"]
+ typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
if !ok {
return "", pkgerrors.New("No plugin for resource " + cniType + " found")
}
@@ -72,7 +74,7 @@ func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
// Delete an existing Network
func Delete(name string, namespace string, kubeclient kubernetes.Interface) error {
_, cniType, networkName := extractData(name)
- typePlugin, ok := krd.LoadedPlugins[cniType+"-network"]
+ typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
if !ok {
return pkgerrors.New("No plugin for resource " + cniType + " found")
}
diff --git a/src/k8splugin/plugins/network/plugin_test.go b/src/k8splugin/plugins/network/plugin_test.go
index 325de31f..02391177 100644
--- a/src/k8splugin/plugins/network/plugin_test.go
+++ b/src/k8splugin/plugins/network/plugin_test.go
@@ -16,13 +16,14 @@ limitations under the License.
package main
import (
- pkgerrors "github.com/pkg/errors"
- "k8splugin/krd"
+ utils "k8splugin/internal"
"os"
"plugin"
"reflect"
"strings"
"testing"
+
+ pkgerrors "github.com/pkg/errors"
)
func LoadMockNetworkPlugins(krdLoadedPlugins *map[string]*plugin.Plugin, networkName, errMsg string) error {
@@ -53,15 +54,15 @@ func LoadMockNetworkPlugins(krdLoadedPlugins *map[string]*plugin.Plugin, network
func TestCreateNetwork(t *testing.T) {
internalVNFID := "1"
- oldkrdPluginData := krd.LoadedPlugins
+ oldkrdPluginData := utils.LoadedPlugins
defer func() {
- krd.LoadedPlugins = oldkrdPluginData
+ utils.LoadedPlugins = oldkrdPluginData
}()
testCases := []struct {
label string
- input *krd.ResourceData
+ input *utils.ResourceData
mockError string
mockOutput string
expectedResult string
@@ -69,14 +70,14 @@ func TestCreateNetwork(t *testing.T) {
}{
{
label: "Fail to decode a network object",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
YamlFilePath: "../../mock_files/mock_yamls/service.yaml",
},
expectedError: "Fail to decode network's configuration: Invalid configuration value",
},
{
label: "Fail to create a network",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
YamlFilePath: "../../mock_files/mock_yamls/ovn4nfvk8s.yaml",
},
mockError: "Internal error",
@@ -84,7 +85,7 @@ func TestCreateNetwork(t *testing.T) {
},
{
label: "Successfully create a ovn4nfv network",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
VnfId: internalVNFID,
YamlFilePath: "../../mock_files/mock_yamls/ovn4nfvk8s.yaml",
},
@@ -95,7 +96,7 @@ func TestCreateNetwork(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
- err := LoadMockNetworkPlugins(&krd.LoadedPlugins, testCase.mockOutput, testCase.mockError)
+ err := LoadMockNetworkPlugins(&utils.LoadedPlugins, testCase.mockOutput, testCase.mockError)
if err != nil {
t.Fatalf("TestCreateNetwork returned an error (%s)", err)
}
@@ -121,10 +122,10 @@ func TestCreateNetwork(t *testing.T) {
}
func TestDeleteNetwork(t *testing.T) {
- oldkrdPluginData := krd.LoadedPlugins
+ oldkrdPluginData := utils.LoadedPlugins
defer func() {
- krd.LoadedPlugins = oldkrdPluginData
+ utils.LoadedPlugins = oldkrdPluginData
}()
testCases := []struct {
@@ -154,7 +155,7 @@ func TestDeleteNetwork(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
- err := LoadMockNetworkPlugins(&krd.LoadedPlugins, testCase.mockOutput, testCase.mockError)
+ err := LoadMockNetworkPlugins(&utils.LoadedPlugins, testCase.mockOutput, testCase.mockError)
if err != nil {
t.Fatalf("TestDeleteNetwork returned an error (%s)", err)
}
diff --git a/src/k8splugin/plugins/service/plugin.go b/src/k8splugin/plugins/service/plugin.go
index 69acb348..e9b45fc8 100644
--- a/src/k8splugin/plugins/service/plugin.go
+++ b/src/k8splugin/plugins/service/plugin.go
@@ -23,16 +23,16 @@ import (
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8splugin/krd"
+ utils "k8splugin/internal"
)
// Create a service object in a specific Kubernetes cluster
-func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
+func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) {
namespace := data.Namespace
if namespace == "" {
namespace = "default"
}
- obj, err := krd.DecodeYAML(data.YamlFilePath, nil)
+ obj, err := utils.DecodeYAML(data.YamlFilePath, nil)
if err != nil {
return "", pkgerrors.Wrap(err, "Decode service object error")
}
@@ -59,7 +59,7 @@ func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
}
opts := metaV1.ListOptions{
- Limit: krd.ResourcesListLimit,
+ Limit: utils.ResourcesListLimit,
}
opts.APIVersion = "apps/v1"
opts.Kind = "Service"
@@ -69,7 +69,7 @@ func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
return nil, pkgerrors.Wrap(err, "Get Service list error")
}
- result := make([]string, 0, krd.ResourcesListLimit)
+ result := make([]string, 0, utils.ResourcesListLimit)
if list != nil {
for _, deployment := range list.Items {
log.Printf("%v", deployment.Name)
diff --git a/src/k8splugin/plugins/service/plugin_test.go b/src/k8splugin/plugins/service/plugin_test.go
index 25eeb730..b9198914 100644
--- a/src/k8splugin/plugins/service/plugin_test.go
+++ b/src/k8splugin/plugins/service/plugin_test.go
@@ -20,7 +20,7 @@ import (
"strings"
"testing"
- "k8splugin/krd"
+ utils "k8splugin/internal"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -33,14 +33,14 @@ func TestCreateService(t *testing.T) {
internalVNFID := "1"
testCases := []struct {
label string
- input *krd.ResourceData
+ input *utils.ResourceData
clientOutput *coreV1.Service
expectedResult string
expectedError string
}{
{
label: "Fail to create a service with invalid type",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
YamlFilePath: "../../mock_files/mock_yamls/deployment.yaml",
},
clientOutput: &coreV1.Service{},
@@ -48,7 +48,7 @@ func TestCreateService(t *testing.T) {
},
{
label: "Successfully create a service",
- input: &krd.ResourceData{
+ input: &utils.ResourceData{
VnfId: internalVNFID,
YamlFilePath: "../../mock_files/mock_yamls/service.yaml",
},